914201799chapter 4, 5 and 6 Practice Questions PDF
914201799chapter 4, 5 and 6 Practice Questions PDF
914201799chapter 4, 5 and 6 Practice Questions PDF
Q1. What is the function of logical operators? Write an expression involving a logical operator to test if
marks are 55 and grade is ‘B’.
Sol. Logical operators are used for comparison of numbers. marks == 55 and grade ==’B’
Sol. 3– integer , 3 j – complex number , 13.0 – Floating point number , ‘13’ – String , “13” – String
2 + 0j – complex number , 13 – integer , [3,13,2]- List , (3,13,2) - Tuples
Q6. Given that i =4 , j=5 , k=4, what will be the result of following expres sions ?
(i) i < k (ii) i < j (iii) i <= k (iv) i == j (v) i == k (vi) j > k (vii) j >= i
(viii) j != i (ix) j <= k
a=3
b = 13
p = 3.0
c = ‘n’
d = ‘g’
e = ‘N’
f = ‘god’
g = ‘God’
h = ‘god’
j = ‘God’
k = “Godhouse”
L = [1 , 2 , 3]
M = [2 , 4 , 6 ]
N = [1 , 2 , 3]
O = (1 , 2 , 3)
P =(2 , 4 , 6)
Q = (1 , 2 , 3)
(i) a < b (ii) c < d (iii) f < h (iv) f == h (v) c ==e (vi)g == j
(vii) “God” < “Godhouse” (viii) “god” < “Godhouse” (ix) a == p (x) L == M
(xi) L == N (xii) O == P (xiii) O == Q (xiv) a == True (xv) 0 == False (xvi) 1 == True
Sol.7. (i) True (ii) False (iii) False (iv) True (v) False (vi) True
(vii) True (viii) False (ix) True (x) False (xi) True (xii) False
(xiii) True (xiv) False (xv) True (xvi) True
Q8. What is the difference between = and == operator?
Sol. == equal to operator and = assignment operator
Q9. a=1.5 , b=1.5 then why is a is b is False while a == b is True ?
Sol. a is b is False, because float values are stored at different locations, whereas a==b is True
because the values are same in both variables.
Q10. Given that variable CK is bound to string “Raman” (i.e. CK =”Raman”). What will be the output produced
by following two statements if the input given is “Raman”? Why?
DK= input(“Enter name:”)
Enter name : Raman
(a) DK == CK (b) DK is CK
Q11. If you give the following for str1 =”Hello”, why does Python report error?
str1[2] = ‘p’
Sol. Strings are immutable data types and individual character cannot be changed. That is why above
statement report error.
Q12. Write a program that reads a number of seconds and prints it in form : mins and seconds, e.g.
200 seconds are printed as 3 mins and 20 seconds.
Sol. a = int(input(“Enter number of seconds”))
mins = a/60
seconds = a%60
print(mins, “minutes”, seconds , “seconds”)
if ( x = 1)
k = 100
else
k = 10
Sol. if x = 1 :
k = 100
else :
k = 10
Q2. What will be the output of following code fragment if the input given is (i) 7 (ii) 5 ?
a = input(‘Enter a number’)
if a == 5 :
print(“Five”)
else :
print(“Not Five”)
Sol. (i) Not Five (ii) Five
Q3. What will be the output of the following code fragment if the input given is (i) 2000 (ii) 9000 (iii) 1991
year = int(input(“Enter 4-digit year”))
if year % 100 == 0 :
if year % 400 == 0 :
print(“LEAP Century year”)
else :
print(“Not century year”)
Sol. (i) LEAP Century year (ii) LEAP Century year (iii) Not Century year
Q4. What would range(3, 13) return?
Sol. 3 4 5 6 7 8 9 10 11 12
Q5. What is the output of the following code fragment?
for a in “abcde” :
print(a , ‘+’ , end= ‘ ‘)
Sol. a+b+c+d+e+
Q6. What is the output of the following code fragment?
for i in range(0 , 10) :
pass
print(i)
Sol. 9
Q7. Why does “Hello” not print even once?
for i in range(10 , 1) :
print(“Hello”)
Sol. because default step value in range function is +1.
Q8. What will be the output of following code:
while(6 + 2 > 8) :
print(“Gotcha!”)
else :
print(“Going out!”)
Sol. Going out!
Q9. What is the significance of break and continue statements?
Q10. What is the output produced by following loop?
for a in range(2,7) :
for b in range (1, a) :
print(b , end= ’ ‘)
print( )
Sol. 1
1
2
1
2
3
1
2
3
4
1
2
3
4
5
Q11. Write a for loop that displays the even numbers from 51 to 60.
Q12. Write a python script that asks the user to enter a length in centimetres. If the user enters a negative
length, the program should tell the user that the entry is invalid. Otherwise, the program should convert the
length to inches and print out the result. There are 2.54 centimetres in an inch.
Sol. len = int(input("Enter the length in Centimeters:"))
if len <=0 :
print("User entry is invalid")
else:
inch = len/2.54
print("Length in inches is:", inch)
Q13. A store charges Rs.120 per item if you buy less than 10 items. If you buy between 10 and 99 items, the
cost is Rs.100 per item. If you buy 100 or more items, the cost if Rs.70 per item. Write a program that asks
the user how many items they are buying and prints the total cost.
Q14. A year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years unless
they are also divisible by 400. Write a program that asks the user for a year and prints out whether it is a
leap year or not.
Sol.14. year = int(input(“Enter 4-digit year”))
if year % 100 == 0 :
if year % 400 == 0 :
print(“LEAP Century year”)
else :
print(“Not century year”)
Q15. Write a program to take an integer a as an input and check whether it ends with 4 or 8. If it ends with 4,
print “ends with 4”, if it ends with 8, print “ends with 8”, otherwise print “ends with neither”.
(a) count = 0
while count < 10 :
print(“Hello”)
count+=1
Sol:
(b) x = 10
y=0
while x > y :
print(x , y)
x=x–1
y= y+1
Sol: 10 0
91
82
73
64
(c) x = 45
while x < 50 :
print(x)
Sol: Infinite Loop
(a) [4 , 5 , 6]
(b) [-2 , 1, 3]
(c) [-9 , -8 , -7 , -6 , -5]
(d) [0 , 1, 2]
(a) a[0]
(b) a[-1]
(c) a[a[0]]
(d) a[a[-1]]
Q5. What is the difference between following two expressions, if lst is given as [1,3,5]
(a) lst * 3
(b) lst *= 3
Sol : (a) [1, 3, 5, 1, 3, 5, 1, 3, 5] – This expression will not change original list
(b) [1, 3, 5, 1, 3, 5, 1, 3, 5] – This expression will change original list
Q6. Given a list L1=[3 , 4.5 , 12 , 25.7 , [2 ,1, 0 , 5] , 88]
Q7. Given a list L1 = [3, 4.5 , 12 ,25.7 , [2, 1, 0, 5], 88], which function can change the list to :
Q15. Given a list of integers, L, write code to add the integers and display the sum.
Q16. Given a list of integers, L write code to calculate and display the sum of all the odd numbers in the list.
Q17. Write a program to find minimum element from a list of element along with its index in the list.
Q18. Write a program to calculate mean of a given list of numbers.
Q19. Write a program to search for an element in a given a list of numbers.
Q20. Write a program to count frequency of a given element in a list of numbers.
**************