Is Python case sensitive when dealing with identifiers?
A) Yes
B) No
C) Machine dependent
D) None of the mentioned
Answer:
A) Yes
Explanation:
Case is always significant.
Q.2:
What is the maximum possible length of an identifier?
A) 31 characters
B) 63 characters
C) 79 characters
D) None of the mentioned
Answer:
D) None of the mentioned
Explanation:
Identifiers can be of any length.
Q.3:
Which of the following is invalid?
A) _a = 1
B) __a = 1
C) __str__ = 1
D) None of the mentioned
Answer:
D) None of the mentioned
Explanation:
All the statements will execute successfully but at the cost of reduced readability.
Q.4:
Which of the following is an invalid variable?
A) my_string_1
B) 1st_string
C) foo
D) _
Answer:
B) 1st_string
Explanation:
Variable names should not start with a number.
Q.5:
Why are local variable names beginning with an underscore discouraged?
A) they are used to indicate a private variables of a class
B) they confuse the interpreter
C) they are used to indicate global variables
D) they slow down execution
Answer:
A) they are used to indicate a private variables of a class
Explanation:
As Python has no concept of private variables, leading underscores are used to indicate variables that must not be accessed from outside the class.
Q.6:
Which of the following is not a keyword?
A) eval
B) assert
C) nonlocal
D) pass
Answer:
A) eval
Explanation:
eval can be used as a variable.
Q.7:
All keywords in Python are in _________
A) lower case
B) UPPER CASE
C) Capitalized
D) None of the mentioned
Answer:
D) None of the mentioned
Explanation:
True, False and None are capitalized while the others are in lower case.
Q.8:
Which of the following is true for variable names in Python?
A) unlimited length
B) all private members must have leading and trailing underscores
C) underscore and ampersand are the only two special characters allowed
D) none of the mentioned
Answer:
A) unlimited length
Explanation:
Variable names can be of any length.
Q.9:
Which of the following is an invalid statement?
A) abc = 1,000,000
B) a b c = 1000 2000 3000
C) a,b,c = 1000, 2000, 3000
D) a_b_c = 1,000,000
Answer:
B) a b c = 1000 2000 3000
Explanation:
Spaces are not allowed in variable names.
Q.10:
Which of the following cannot be a variable?
A) __init__
B) in
C) it
D) on
Answer:
B) in
Explanation:
in is a keyword.
Q.11:
Which is the correct operator for power(xy)?
A) X^y
B) X**y
C) X^^y
D) None of the mentioned
Answer:
B) X**y
Explanation:
In python, power operator is x**y i.e. 2**3=8.
Q.12:
Which one of these is floor division?
A) /
B) //
C) %
D) None of the mentioned
Answer:
B) //
Explanation:
When both of the operands are integer then python chops out the fraction part and gives you the round off value, to get the accurate answer use floor division.
This is floor division. For ex, 5/2 = 2.5 but both of the operands are integer so answer of this expression in python is 2. To get the 2.5 answer, use floor division.
Q.13:
What is the order of precedence in python?
i) Parentheses
ii) Exponential
iii) Multiplication
iv) Division
v) Addition
vi) Subtraction
A) i,ii,iii,iv,v,vi
B) ii,i,iii,iv,v,vi
C) ii,i,iv,iii,v,vi
D) i,ii,iii,iv,vi,v
Answer:
A) i,ii,iii,iv,v,vi
Explanation:
For order of precedence, just remember this PEMDAS (similar to BODMAS).
Q.14:
What is the answer to this expression, 22 % 3 is?
A) 7
B) 1
C) 0
D) 5
Answer:
B) 1
Explanation:
Modulus operator gives the remainder. So, 22%3 gives the remainder, that is, 1.
Q.15:
Mathematical operations can be performed on a string.
A) True
B) False
Answer:
B) False
Explanation:
You canβt perform mathematical operation on string even if the string is in the form: β1234β¦β.
Q.16:
Operators with the same precedence are evaluated in which manner ?
A) Left to Right
B) Right to Left
C) Canβt say
D) None of the mentioned
Answer:
A) Left to Right
Q.17:
What is the output of this expression, 3*1**3 ?
A) 27
B) 9
C) 3
D) 1
Answer:
C) 3
Explanation:
First this expression will solve 1**3 because exponential has higher precedence than multiplication, so 1**3 = 1 and 3*1 = 3. Final answer is 3.
Q.18:
Which one of the following has the same precedence level?
A) Addition and Subtraction
B) Multiplication, Division and Addition
C) Multiplication, Division, Addition and Subtraction
D) Addition and Multiplication
Answer:
A) Addition and Subtraction
Explanation:
βAddition and Subtractionβ are at the same precedence level.
Similarly, βMultiplication and Divisionβ are at the same precedence level.
However, Multiplication and Division operators are at a higher precedence level than Addition and Subtraction operators.
Q.19:
The expression Int(x) implies that the variable x is converted to integer.
A) True
B) False
Answer:
A) True
Q.20:
Which one of the following has the highest precedence in the expression?
A) Exponential
B) Addition
C) Multiplication
D) Parentheses
Answer:
D) Parentheses
Explanation:
Just remember: PEMDAS, that is, Parenthesis, Exponentiation, Division, Multiplication, Addition, Subtraction. Note that the precedence order of Division and Multiplication is the same.
Likewise, the order of Addition and Subtraction is also the same.
Q.21:
What is the output of print 0.1 + 0.2 == 0.3?
A) True
B) False
C) Machine dependent
D) Error
Answer:
B) False
Explanation:
Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary.
The round off errors from 0.1 and 0.2 accumulate and hence there is a difference of 5.5511e-17 between (0.1 + 0.2) and 0.3.
Q.22:
Which of the following is not a complex number?
A) k = 2 + 3j
B) k = complex(2, 3)
C) k = 2 + 3l
D) k = 2 + 3J
Answer:
C) k = 2 + 3l
Explanation:
l (or L) stands for long.
Q.23:
What is the type of inf ?
A) Boolean
B) Integer
C) Float
D) Complex
Answer:
C) Float
Explanation:
Infinity is a special case of floating point numbers.
It can be obtained by float(βinfβ).
Q.24:
What does ~4 evaluate to ?
A) -5
B) -4
C) -3
D) +3
Answer:
A) -5
Explanation:
~x is equivalent to -(x+1).
Q.25:
What does ~~~~~~5 evaluate to?
A) +5
B) -11
C) +11
D) -5
Answer:
A) +5
Explanation:
~x is equivalent to -(x+1).
~~x = β (-(x+1) + 1) = (x+1) β 1 = x
~~x is equivalent to x
Extrapolating further ~~~~~~x would be same as x in the final result.
In the question, x value is given as 5 and β~β is repeated 6 times. So, the correct answer for β~~~~~~5β is 5.
Q.26:
Which of the following is incorrect?
A) x = 0b101
B) x = 0x4f5
C) x = 19023
D) x = 03964
Answer:
D) x = 03964
Explanation:
Numbers starting with a 0 are octal numbers but 9 isnβt allowed in octal numbers.
Q.27:
What is the result of cmp(3, 1)?
A) 1
B) 0
C) True
D) False
Answer:
A) 1
Explanation:
cmp(x, y) returns 1 if x > y, 0 if x == y and -1 if x < y.
Q.28:
Which of the following is incorrect?
A) float(βinfβ)
B) float(βnanβ)
C) float(β56β+β78β)
D) float(β12+34β²)
Answer:
D) float(β12+34β²)
Explanation:
β+β cannot be converted to a float.
Q.29:
What is the result of round(0.5) β round(-0.5) ?
A) 1.0
B) 2.0
C) 0.0
D) Value depends on Python version
Answer:
D) Value depends on Python version
Explanation:
The behavior of the round() function is different in Python 2 and Python 3. In Python 2, it rounds off numbers away from 0 when the number to be rounded off is exactly halfway through.
round(0.5) is 1 and round(-0.5) is -1 whereas in Python 3, it rounds off numbers towards nearest even number when the number to be rounded off is exactly halfway through.
See the below output.
Hereβs the runtime output for Python version 2.7 interpreter.
$ python
Python 2.7.17 (default, Nov 7 2019, 10:07:09)
>>> round(0.5)
1.0
>>> round(-0.5)
-1.0
>>>
In the above output, you can see that the round() functions on 0.5 and -0.5 are moving away from 0 and hence βround(0.5) β (round(-0.5)) = 1 β (-1) = 2β
Hereβs the runtime output for Python version 3.6 interpreter.
$ python3
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
>>> round(0.5)
0
>>> round(-0.5)
0
>>> round(2.5)
2
>>> round(3.5)
4
>>>
In the above output, you can see that the round() functions on 0.5 and -0.5 are moving towards 0 and hence βround(0.5) β (round(-0.5)) = 0 β 0 = 0β. Also note that the round(2.5) is 2 (which is an even number) whereas round(3.5) is 4 (which is an even number).
Q.30:
What does 3 ^ 4 evaluate to ?
A) 81
B) 12
C) 0.75
D) 7
Answer:
D) 7
Explanation:
^ is the Binary XOR operator.
Q.31:
Which of the following statements assigns the value 25 to the variable x in Python:
A) x β 25
B) x = 25
C) int x = 25
D) x << 25
Answer:
B) x = 25
Q.32:
In Python, a variable may be assigned a value of one type, and then later assigned a value of a different type:
A) False
B) True
Answer:
B) True
Explanation:
Variables are not statically typed in Python, as they are in some other programming languages.
Q.33:
Which one of the following is the correct way of declaring and initializing a variable, x with the value 7 ?
A) x=7
B) int x
C) int x=7
D) declare x=7
Answer:
C) int x=7
Explanation:
The correct way of declaring and initializing a variable, x with the value 7 is x=7.
Q.34:
What will be the output of statement 2**2**2**2
A) 16
B) 256
C) 32768
D) 65536
Answer:
D) 65536
Explanation:
The statement is equivalent to 2^16. So, Option D is correct.
Q.35:
Which of the following statement is False ?
A) Variable names can be arbitrarily long.
B) They can contain both letters and numbers.
C) Variable name can begin with underscore.
D) Variable name can begin with number.
Answer:
D) Variable name can begin with number.
Explanation:
Variable name can not begin with the number, it can only begin with a letter or underscore.
Q.36:
What is the output of the following code: print 9//2
A) 4
B) 4.5
C) 4.0
D) Error
Answer:
A) 4
Explanation:
Floor Division operator β//β β The division of operands where the result is the quotient in which the digits after the decimal point are removed.
So in this case we get 4 as the answer. So, Option A is correct.
Q.37:
Which of the following is not a valid variable name in Python?
A) _var
B) var_name
C) var11
D) 5var
Answer:
D) 5var
Explanation:
5var is not a valid variable name in python.
Q.38:
What is the maximum length of an identifier in python?
A) 32
B) 31
C) 63
D) None of the above
Answer:
D) None of the above
Explanation:
In python, the Identifier can be of any length. So, Option D is correct.
Q.39:
Which of the following declarations is incorrect?
A) None Of the below
B) _x = 2
C) __x = 3
D) __xyz__ = 5
Answer:
A) None Of the below
Explanation:
All declarations will execute successfully but at the expense of low readability.
Q.40:
What is the result of round(0.5) β round(-0.5)?
A) 1.0
B) 2.0
C) 0
D) None Of the above
Answer:
B) 2.0
Explanation:
Python rounds off numbers away from 0 when the number to be rounded off is exactly halfway through.
round(0.5) is 1 and round (-0.5) is -1 So, 1-(-1)=2.0
Q.41:
What is the result of executing the following code?
number = 5
while number <= 5:
if number < 5:
number = number + 1
print(number)
A) The program will loop indefinitely
B) The value of number will be printed exactly 1 time
C) The while loop will never get executed
D) The value of number will be printed exactly 5 times
Answer:
A) The program will loop indefinitely
Explanation:
This code loops while number is less than or equal to 5.
number only increments if it is less than 5, and it's originally set to 5, so 'number' never changes.
Q.42:
What will the following code print ?
counter = 1
sum = 0
while counter <= 6:
sum = sum + counter
counter = counter + 2
print(sum)
A) 12
B) 9
C) 7
D) 8
Answer:
B) 9
Explanation:
This loop executes 3 times. After the first loop sum = 1 and counter = 3, after the second loop sum = 4 and counter = 5, and after the third loop sum = 9 and counter = 7.
Q.43:
What will be printed by the following code when it executes?
sum = 0
values = [1,3,5,7]
for number in values:
sum = sum + number
print(sum)
A) 4
B) 0
C) 7
D) 16
Answer:
D) 16
Explanation:
This adds up the numbers in values and prints the sum.
Q.44:
What is the last thing printed when the following code is run ?
number = 0
while number <= 10:
print("Number: ", number)
number = number + 1
A) Number: 10
B) Number: number
C Number: 0
D) Number: 11
Answer:
A) Number: 10
Explanation:
Since this while loop continues while number is less than or equal to 10, the last iteration of the loop will print "Number: 10".
Q.45:
What does the following code print ?
output = ""
x = -5
while x < 0:
x = x + 1
output = output + str(x) + " "
print(output)
A) 5 4 3 2 1
B) -4 -3 -2 -1 0
C) -5 -4 -3 -2 -1
D) This is an infinite loop, so nothing will be printed
Answer:
B) -4 -3 -2 -1 0
Explanation:
The value of x is incremented before it is printed, so the first value printed is -4.
Q.46:
What are the values of var1 and var2 that are printed when the following code executes ?
D) This is an infinite loop, so nothing will be printed
Answer:
B) var1 = 0, var2 = -2
Explanation:
This loop will execute two times, so var1 will be 0 and var2 will be -2 when the loop is exited.
Q.47:
How many asterisks will be printed when the following code executes?
for x in [0, 1, 2, 3]:
for y in [0, 1, 2, 3, 4]:
print('*')
A) 0
B) 4
C) 5
D) 20
Answer:
D) 20
Explanation:
The outer loop will iterate 4 times and the inner loop will iterate 5 times. 4 times 5 = 20.
Q.48:
The following code contains an infinite loop. Which is the best explanation for why the loop does not terminate ?
n = 10
answer = 1
while n > 0:
answer = answer + n
n = n + 1
print(answer)
A) n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive.
B) answer starts at 1 and is incremented by n each time, so it will always be positive.
C) You cannot compare n to 0 in the while loop. You must compare it to another variable.
D) In the while loop body, we must set n to False, and this code does not do that.
Answer:
A) n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive.
Explanation:
The loop will run as long as n is positive. In this case, we can see that n will never become non-positive, so it will run infinitely.
Q.49:
Which type of loop can be used to perform the following iteration: You choose a positive integer at random and then print the numbers from 1 up to and including the selected integer.
A) a for-loop or a while-loop
B) only a for-loop
C) only a while-loop
D) a for-loop And a while-loop
Answer:
A) a for-loop or a while-loop
Explanation:
Although you do not know how many iterations you loop will run before the program starts running, once you have chosen your random integer, Python knows exactly how many iterations the loop will run, so either a for-loop or a while-loop will work.
Q.50:
Which of the following statements wonβt be printed when this Python code is run?
for letter in 'Python':
if letter == 'h':
continue
print('Current Letter : ' + letter)
A) Current Letter : P
B) Current Letter : t
C) Current Letter : h
D) Current Letter : o
Answer:
C) Current Letter : h
Explanation:
Because continue sends the loop to the next iteration at h, it will not print "Current Letter: h".
Q.51:
for i in range(-3), how many times this loop will run ?
A) 0
B) 1
C) Infinite
D) Error
Answer:
A) 0
Q.52:
for i in [1,2,3]:, how many times a loop run ?
A) 0
B) 1
C) 2
D) 3
Answer:
D) 3
Q.53:
for loop in python are work on
A) range
B) iteration
C) Both of the above
D) None of the above
Answer:
C) Both of the above
Q.54:
How many times it will print the statement ?, for i in range(100): print(i)
A) 101
B) 99
C) 100
D) 0
Answer:
C) 100
Q.55:
For loop in python is
A) Entry control loop
B) Exit control loop
C) Simple loop
D) None of the above
Answer:
A) Entry control loop
Q.56:
In which of the following loop in python, we can check the condition ?
A) for loop
B) while loop
C) do while loop
D) None of the above
Answer:
B) while loop
Q.57:
It is possible to create a loop using goto statement in python ?
A) Yes
B) No
C) Sometimes
D) None of the above
Answer:
B) No
Q.58:
To break the infinite loop , which keyword we use ?
A) continue
B) break
C) exit
D) None of the above
Answer:
B) break
Q.59:
l=[],for i in l: print(l), what is the output ?
A) []
B) list
C) print()
D) None of the above
Answer:
D) None of the above
Q.60:
What is the final value of the i after this, for i in range(3): pass
A) 1
B) 2
C) 3
D) 0
Answer:
B) 2
Q.61:
What is the value of i after the for loop ?, for i in range(4):break
A) 0
B) 1
C) 3
D) 2
Answer:
A) 0
Q.62:
What we put at the last of the loop ?
A) semicolon
B) colon
C) comma
D) None of the above
Answer:
B) colon
Q.63:
which of the following is consider as a infinite loop
A) while(infinte):
B) while(1):
C) for(1):
D) None of the above
Answer:
B) while(1):
Q.64:
Which of the following is Exit control loop in python ?
A) for loop
B) while loop
C) do while loop
D) None of the above
Answer:
D) None of the above
Q.65:
Which of the following is the loop in python ?
A)for
B) while
C) do while
D) 1 and 2
Answer:
D) 1 and 2
Q.66:
Which of the following loop is not supported by the python programming language ?
A) for loop
B) while loop
C) do while loop
D) None of the above
Answer:
C) do while loop
Q.67:
Which of the following loop is work on the particular range in python ?