CH 6, 7, 8, 9 Practice Questions
CH 6, 7, 8, 9 Practice Questions
CH 6, 7, 8 and 9
PRACTICE QUESTIONS
1. What is meant by data type in Python? List out the core data types of
pythons?
2. Write the Purpose of None data type.
3. Write the difference between sequence and mapping data type.
4. Write the difference between Mutable and Immutable data types in
python. Give an example.
5. What is type casting (coercion)? List out various types of type casting.
6. Write the difference between Implicit and explicit type conversion with an
example.
7. What is meant by operator precedence? What is operator associativity?
8. What is an expression? How many different types of expressions can you
have in Python?
*****************************************************************************
CHAPTER – 9 FLOW OF CONTROL/CONDITIONAL AND ITERATIVE
STATAEMENTS
***************************************************************************
CH 6 ,7 AND 8
(Getting Started with Python, Python Fundamentals and Data Handling
Expressions)
STATE TRUE OR FALSE
1. Python is a low level language.
2. Python is a free source language.
3. Python converts low level language to high level language.
4. Python is a compiler.
5. Python is case sensitive language.
6. Python was named after famous BBC comedy show
namely Monty Python’s Flying Circus.
7. Python is not a Cross-platform Language.
8. All identifiers in Python are in lower case.
9. An identifier is a user defined name given to a variable or a constant in a
program.
10. Python does not allows same variable to hold different data literals / data
types.
11. Operators with the same precedence are evaluated in right to left manner.
12. Interactive mode is used when a user wants to run a single line or one block
of code.
13. Script mode is used when the user is working with more than one single
code or a block of code.
14. In Python, a variable may be assigned a value of one type, and then later
assigned a value of a different type.
15. In Python, a variable must be declared before it is assigned a value.
16. Variable names can be of any length.
17. the complement operator inverts the value of each bit of
the operand
18. print(int(6>7-6-7*4) ) will print boolean value.
19. Logical operator not has highest precedence among all the logical operators.
20. “is” is a membership operator in python.
21. Following code will produce True as output:
x=10>5>1 and -3<-2<-1
print(x)
22. The value of expression 3/5*(7-2) and 3/(5*(7-2)) is same.
23. The expression 4**3**2 is evaluated as (4**3)**2
24. () have higher precedence that any other operator.
25. print() always inserts a newline after each output.
26. >>> 2*4+36/2-9
In the above expression 36/2 will be evaluated first by python.
27. When syntax error is encountered, Python displays the
name of the error and a small description about the error.
28. "In Python, data type of a variable depends on its value"
29. “Python identifiers are dynamically typed.”
30. (i) -88.0 is the output of the print(3-10**2+99/11)
(ii) range( ) is also a module function
31. Comments in Python begin with a "$" symbol.
32. In a Python program, if a break statement is given in a nested loop, it
terminates the execution of all loops in one go.
33. The math.ceil() function in Python returns the smallest integer greater than or
equal to a given number.
34. In Python, the break statement is used to terminate the entire program.
35. In Python, the math.pow () function is equivalent to the ** operator for
exponentiation.
ASSERTION & REASON
1. A:It is interpreted language.
R: Python programs are executed by an interpreter.
2. A: Python is portable and platform independent.
R:Python program can run on various operating
systems and hardware platforms.
3. A: Python is case-sensitive.
R: Python does not use indentation for blocks and
nested blocks.
4. A: Python program is executed line by line.
R: Python is compiled language.
5. A: Python is an object oriented language
R: Python is a cross platform language
6. A: Python is case-sensitive.
R: NUMBER and number are not same in Python
7. A: Python is a high-level object-oriented programming
language.
R: It can run on different platforms like
Windows, Linux, Unix, and Macintosh.
8. A: An identifier cannot have the same name as of
a keyword.
R: Python interpreter will not be able to differentiate
Between a keyword and an identifier having the
same name as of a keyword.
9. >>>print('Good'+' Morning') #Output :Goodmorning
A : Incorrect Output
R: There is a syntax error
10. A: In Python comments are interpreted and are
shown on the output screen.
R: Single line comments in python starts with #
character
11. A: The math.pow(2,4)gives the output: 16.0
R: The math.pow () method receives two float arguments, raise the first to
the second and return the result.
12. A: Python uses the concept of L-value and R-value,
that is derived from the typical mode of
evaluation on the left and right side of an
assignment statement
R: name = ‘Raj’
In the above code the value ‘Raj’ is fetched (Rvalue)
and stored in the variable named – name (L value)
13. A:>>> print(type((3 + 33)<-(-4 - 44)))
R : As output is True which is a boolean value
14. num1=input("enter a number")
print(num1+2)
A: The above code will give error message when executed.
R: input() returns a string datatype. We cannot add
string data type with a numeric datatype. So,
performing arithmetic operation on it will result in
error.
15. var1=10
var1="hello"
A: The above code is invalid. We cannot assign adata of
different data type to an existing variable.
R: Python supports implicit type casting. So it is
possible to assign a data of different data type to
an existing variable.
16. A: You will get an error if you use double quotes inside a
string that is surrounded by double quotes:
txt = "We are the so-called "Vikings" from the north."
R: To fix this problem, use the escape character \":
17. A: Variables whose values can be changed after they are
created and assigned are called immutable.
R: When an attempt is made to update the value of an
immutable variable, the old variable is destroyed and a
new variable is created by the same name in memory.
18. A:To do arithmetic python uses arithmetic
(+,*,//,**, -, / ,%)
R:Each of these operators is a binary operator
19. A: The relational operator determine the relation among different operand
R: It returns the Boolean value
20. A: not has a lower priority than non-Boolean operators
R: So not a==b is interpreted as not(a==b)
21. A: The ** operators is evaluated from right to left
R: All operators are left associative
22. A: for the given expression
S1='1'
S2= 1
S3= S1==S2 #value of S3 is False
R: Integer value cannot be compared with string value.
23. A: Following given expression will result into TypeError
S="XYZ"
v1=[2]
str3=S*v1
R: operator ‘*’ cannot be applied on string
24. A: int(‘A’) The above statement will result into error
R: ‘A’ is not recognised by Python
25. A: a=9, b=int(9.2) Both a and b have same value
R: b is converted to integer explicitly
26. A: In an expression, associativity is the solution to the order of evaluation of
those operators which clashes due to same precedence.
R: Operators in an expression may have equal precedence due to which the
order of evaluation cannot be decided just by precedence.
27. A: An example of an infinite loop is : while(1):
R: A loop that continues repeating without terminating (ending) condition is
an infinite loop.
28. A: The statements within the body of for loop are executed
till the range of values is exhausted.
R: for loop cannot be nested.
29. Analyse the following code:
for i in range(1,4):
for j in range (1,i+1):
print(j,end=’ ’)
print()
A: output is
1
12
123
R: Here, range function will generate value 1,2,3 in the outer loop and the
inner loop will run for each value of “i” used in outer loop.
30. A: Python provides two looping constructs for and while.
The for is a counting loop and while is a conditional
loop.
R: The while is a conditional loop as we check the
condition first, if it is satisfied then only we can get
inside the while. In case of for it depends upon the
counting statement of index.
31. A: for loop in Python makes the loop easy to calculate
factorial of a number
R: While loop is an indefinite iteration that is used when a loop repeats
unknown number of times and end when some condition is met.
32. A: range(0,5) will produce list as [0,1,2,3,4]
R: These are the numbers in arithmetic progression (a.p.) that begins with
lower limit 0 and goes up till Upper limit -1.
33. A: To print the sum of following series
1 + 3 + 5…….n. Ravi used the range function in for
loop as follows :range(1,n+1,2) # 3 parameters
R: In range function first parameter is start value, second parameter is stop
value & the third parameter is step value.
34. x=0
for i in range(3,9,3):
x = x * 10 + i
print(x)
A: The output of the above code will be 9.
R: The loop will run for 2 times.
35. for i in range(1, 6):
for j in range(1, i):
print("*", end=" ")
print()
A: In a nested loop, the inner loop must terminate
before the outer loop.
R: The above program will throw an error.
36. A: break statement terminates the loop.
R: The else clause of a Python loop executes when the
loop continues normally.
37. A: break statement always appears only in a nested loop.
R: If the break statement is inside the inner loop then it
will terminate the inner loop only.
38. A: break statement terminates the loop it lies within.
R: continue statement forces the next iteration of the
loop to take place, skipping any code in between.
OBJECTIVE TYPE QUESTIONS (MCQ)
1. Python uses ____ to convert its instructions into machine language.
(a) Interpreter (b) Compiler
(c) Both of the above (d) None of the above
2. Who developed the Python language?
(a) Zim Den (b)Wick van Rossum
(c)Guido Van Rossum (d)NieneStom
3. IDLE stands for __________
(a) Integrated Development Learning
(b) Integrated Development Learning Environment
(c) Intelligent Development Learning Environment
(d) None of the above
4. Python interpreter executes ……………………….statement
(Command) at a time.
(a) Two (b) Three (c) One (d) All command
5. Which of the following is not the feature of python language?
(a) Python is proprietary software.
(b )Python is not case-sensitive.
(c) Python uses brackets for blocks and nested blocks.
(d) All of the above
6. By default, the Python scripts are saved with_____ extension.
(a) .pyp (b).pys (c).py (d)None of the above
7. What is the maximum possible length of an identifier in
python?
(a) 16 (b) 32 (c) 64 (d) None of these
8. Which of the following is not considered a valid identifier in Python?
(a) Two2 (b) _main (c) hello_rsp1 (d) 2 hundred
9. Which of the following is not a component of the math module in Python?
(a) ceil() (b) mean() (c) fabs() (d) pi
10. >>> print("I" + "am" + "in" + "school") display
(a) I am in school (b)I Am In School
(c)Iaminschool (d)iaminschool
11. Which of the following is an invalid identifier to be used in Python?
(a) per%marks (b) _for (c) While (d) true
12. Which of the following statement is correctsyntactically?
(a) print(“Hello” , sep == ‘@’ , end =' ')
(b)print(“Hello” , sep = ‘@’ , end = ' ')
(c)Print(“Hello” , sep = ‘@’ , end = ' ')
(d)print(“Hello” , sep = ‘@’ , end = ' '
13. Which of the following is not a keyword in python?
(a) eval (b) assert (c) nonlocal (d) pass
14. Which of the following is not a valid declaration?
(a) S=12 (b) V="J" (c) F=32.4 (d) H=0254
15. Evaluate the following expression:
>>> not True or not False and False
16. Predict the output of the following code snippet:
for letter in "Python":
if letter =="h":
continue
print(letter,end="")
(a)P (b) Py (c) Python (d) Pyton
17. Which of the following properly expresses the precedence of operators (using
parentheses) in the following expression: 5*3 > 10 and 4+6==11
a) ((5*3) > 10) and ((4+6) == 11))
b) (5*(3 > 10)) and (4 + (6 == 11))
c) ((((5*3) > 10) and 4)+6) == 11
d) ((5*3) > (10 and (4+6))) == 11
18. All keywords in python except True,False and None are in ____?
(a) Lower case (b) Upper case
(c) Capitalized (d) None of the above
19. What is the output of the following :
print(23//9%3, 2**2**2)
(a) 7 64 (b) 2 16 (c) 7 8 (d) 2 64
20. Give the output
for i in range(1,10,3):
print(i,sep=”-”,end=”*”)
(a) 1-4-7* (b) 1-4-7-10* (c) 1*4*7* (d) 1*4*7*10
21. Find the invalid identifier from the following
(a) sub%marks (b) age (c) _subname_ (d) subject1
22. Which of the following expressions is an example of type casting?
(a) 4.0+float(6) (b) 5.3+6.3
(c) 5.0+3 (d) None of these
23. Which of the following is an invalid identifier?
(a) CS_class_XII (b) csclass12
(c) _csclass12 (d) 12CS
24. What is the output of the function shown below if the random module has
already been imported?
>>>import random
>>>random.randint(3.5,7)
(a) Error
(b) Any integer between 3.5 and 7, including 7
(c) Any integer between 3.5 and 7, excluding 7
(d) The integer closest to the mean of 3.5 and 7
25. The input() function always returns a value of ……..type.
a) Integer b) float c) string d) Complex
26. To include non-graphic characters in python, which of the following is used?
(a) Special Literals (b) Boolean Literals
(c) Escape Character Sequence (c) Special Literal – None
27. Which of the following cannot be a variable name?
(a) _init_ (b) in (c) it (d) on
28. Which is valid keyword?
(a) Int (b) WHILE (c) While (d) if
29. Predict the output of the following:
(i) >>>print(10 or 40) (ii) >>> print(22.0//5)
30. Identify the output of the following Python statements.
ans=0
for i in range (11,20,1):
if i %2==0:
ans+=4
else:
ans-=2
print(ans)
(a) 8 (b) 6 (c)10 (d) None
31. Which of the following is an invalid operator in Python?
(a) - (b) //= (c) in (d) =%
32. Which of the following operators is the correct option for
power(a,b)?
(a) a^b (b) a **b (c) a ^^b (d) a^*b
33. Which of the characters is used in python to make a single
line comment?
(a)/ (b) // (c) # (d)!
34. Which of the following is not a core data type in python?
(a)List (b) Dictionary (c) Tuple (d) Class
35. How many times does the following while loop get executed?
K=5
L=36
while K<=L:
K+=6
(a) 4 (b) 5 (c) 6. (d) 7
36. Which of the following functions generates an integer?
(a) uniform( ) (b) randint( )
(c) random( ) (d) None of the above
37. Identify the output of the following Python statements.
b=1
for a in range(1, 10, 2):
b += a + 5
print(b)
(a) 59 (b) 51 (c) 36 (d) 39
38. What will be the output of the following code?
import random (a)100$$75$$10$$
X=[100,75,10,125] (b) 75$$10$$125$$
Go=random.randint(0,3) (c) 75$$10$$
for i in range(Go): (d) 10$$125$$100
print(X[i],"$$",end=" ")
39. Which of the following has the highest precedence in python?
(a)Exponential (b) Addition (c) Parenthesis (d) Division
40. What is math.factorial (4.0)?
(a) 20 (b) 24 (c) 16 (d) 64
41. Identify the invalid variable name from the following.
Adhar@Number, none, 70outofseventy, mutable
42. Which of the following belongs to complex data type
(a) -12+2k (b) 4.0 (c) 3+4J (d) -2.05I
43. None is a special data type with a single value. It is used to
signify the absence of value in a situation
(a) TRUE (b) FALSE (c) NONE (d) NULL
44. If x=3.123, then int(x) will give ?
(a) 3.1 (b) 0 (c) 1 (d) 3
45. To execute the following code in Python, Which module need to be imported?
>>>print(_______.mean([1,2,3])
46. Find the invalid identifier from the following
(a) Marks@12 (b) string_12 (c)_bonus (d)First_Name
47. Find the invalid identifier from the following
(a) KS_Jpr (b) false (c) 3rdPlace (d) _rank
48. Find the valid identifier from the following:
(a) Tot$balance (b) TRUE (c) 4thdata (d) break
49. Which one of the following is False regarding data types in Python?
(a) In python, explicit data type conversion is possible
(b) Mutable data types are those that can be changed.
(c) Immutable data types are those that cannot be changed.
(d) None of the above
50. Which statement will give the output as : True from the following :
a) >>>not -5 b) >>>not 5 c) >>>not 0 d) >>>not(5-1)
51. Evaluate the following expression: 1+(2-3)*4**5//6
(a) -171 (b) 172 (c) -170 (d) 170
52. The correct output of the given expression is:
True and not False or False
(a) False (b) True (c) None (d) Null
53. What will the following expression be evaluated to in Python?
print(6*3 / 4**2//5-8)
(a) -10 (b) 8.0 (c) 10.0 (d) -8.0
54. Evaluate the following expressions:
>>>(not True) and False or True
55. >>> 16 // (4 + 2) * 5 + 2**3 * 4
(a) 42 (b) 46 (c) 18 (d) 32
56. Evaluate the following expression:
True and False or Not True
(a) True (b) False (c) NONE (d) NULL
57. The below given expression will evaluate to
22//5+2**2**3%5
(a)5 (b) 10 (c) 15 (d) 20
58. Which of the following is not a valid identifier name in Python?
a) First_Name b) _Area c) 2nd_num d) While
59. Evaluate the following Python expression
print(12*(3%4)//2+6)
(a)12 (b)24 (c) 10 (d) 14
60. Give the output of the following code:
>>>import math
>>>math.ceil(1.03)+math.floor(1.03)
a) 3 b) -3.0 c) 3.0 d) None of the above
61. >>>5 == True and not 0 or False
(a) True (b) False (c) NONE (d) NULL
62. Predict the output of the following:
from math import*
A=5.6
print(floor(A),ceil(A))
2 – MARKS
1. Evaluate the following expression:
False and bool(15/5*10/2+1)
2. Predict the output of the following:
M, N, O = 3, 8, 12
N, O, M = O+2, M*3, N-5.
print(N,O,M)
3. If given A=2,B=1,C=3, What will be the output of following expressions:
(i) print((A>B) and (B>C) or(C>A)) (ii) print(A**B**C)
4. What will be the output of following Python Code:
import random as rd
high=4
Guess=rd.randrange(high)+50
for C in range(Guess, 56):
print(C,end="#")
5. Write the output of the code given below:
p=10
q=20
p*=q//3
p=q**2
q+=p
print(p,q)
7. V, W, X = 20, 15, 10
W, V, X = X-2, V+3, W*2.
print(V,X,W)
8. Evaluate the following expressions:
(a) 5 // 10 * 9 % 3 ** 8 + 8 – 4
(b) 65 > 55 or not 8 < 5 and 0 != 55
9. Fill in the blanks to execute loop from 10 to 100 and 10 to 1
(i) for i in range(_______ ):
print(i)
(ii)for i in range( _______):
print(i)
10. Evaluate the following: >>> print(15.0/4+(8*3.0))
11. Predict the output of the following:
X,Y,Z = 3,5,-2
X *= Y + Z
Y -= X * 2 + Y
Z += X + Y
print(X, Y, Z)
12. Sona has written the following code to check whether number is divisible by 3.
She could not run the code successfully. Rewrite the code and underline each
correction done in the code.
x=10
for I range in (a)
if i%3=0:
print(I)
else
pass
13. Rewrite the following code in Python after removing all syntax error(s).
Underline each correction done in the code.
Value=30
for VAL in range(0,Value)
if val%4==0:
print (VAL*4)
Elseif val%5==0:
print (VAL+3)
else
print(VAL+10)
14. Mona has written a code to input a positive integer and display all its even
factors in descending order. Her code is having errors. Rewrite the correctcode
and underline the corrections made.
n=input("Enter a positive integer: ")
for i in range(n):
if i%2:
if n%i==0:
print(i,end=' ')
15. Find error in the following code(if any) and correct code by rewriting code and
underline the correction;‐
x= int("Enter value of x:")
for in range [0,10]:
if x=y
print( x + y)
else:
print( x‐y)
16. Mithilesh has written a code to input a number and evaluate its factorial and
then finally print the result in the format: “The factorial of the <number> is
<factorial value>” His code is having errors. Rewrite the correct code and
underline the corrections made.
f=0
num = input("Enter a number:")
n = num
while num> 1:
f = f * num
num -= 1
else:
print("The factorial of : ", n , "is" , f)
17. What is the output of the program given below:
import random
x = random.random()
y= random.randint(0,4)
print(int(x),”:”, y+int(x))
18. Evaluate the following expression and identify the correct answer:
(i) 18 - (1 + 2) * 5 + 2**5 * 4 (ii) 10 +(5-2) * 4 + 2**3**2
19. Rewrite the following code after removing the syntactical error(if any).Underline
each correction:
X=input("Enter a Number")
If x % 2 =0:
for i range (2*x):
print i
loop else:
print "#"
20. What possible outputs are expected to be displayed on screen at the time of
execution of the program from the following code? Also specify the maximum
value that can be assigned to each of the variables L and U.
import random (i) 40 @50 @
Arr=[10,30,40,50,70,90,100]
(ii) 10 @50 @70 @90 @
L=random.randrange(1,3)
U=random.randrange(3,6) (iii) 40 @50 @70 @90 @
for i in range(L,U+1): (iv) 40 @100 @
print(Arr[i],"@",end="")
21. Rewrite the following Python program
x=input(“Enter a number”)
if x % 2=0:
print x, “is even”
else if x<0:
print x, “should be positive”
else;
print x “is odd”
22. (i)Find the output generated by the following code:
a=5
b=10
a+=a+b
b*=a+b
print(a,b)
(a) Discuss the significance of the else clause in the context of the provided
code.
(b) When is the else block executed, and what output would be produced?
39. Predict the output of the following code:
X, Y = 2, 5
Z=1
while X < 10:
Z += 1
Y *= 2
if Y==20:
break
X=Y-X
else:
print(Z, "#", X)
print(Z,X,Y)
40. Consider the following code:
A, B, C = 1, 2, 3
for D in range(2):
C += A (i) Explain the role of the if statement and the break
B *= D statement within the loop
if D == 2: (ii) When is the else block executed in the above code?
A+= 2 (iii) If the break statement is removed from the code,
break what output would be produced?
print(A)
else: print(A, "#", B, "#", C)
*********************************************************************