QUIZ ON : PROGRAMMING AND PROBLEM SOLVING THROUGH PYTHON - OPERATORS EXPRESSIONS AND PYTHON STATEMENTS

OPERATORS EXPRESSIONS AND PYTHON STATEMENTS

00:00:00

What will be the output of the following Python code?

  d = {0, 1, 2}  for x in d:      print(d.add(x))

Option A. 0 1 2
Option B. 0 1 2 0 1 2 0 1 2 …
Option C. None None None
Option D. None of the mentioned
True Answer C

Explanation :
Variable x takes the values 0, 1 and 2. set.add() returns None which is printed.

What will be the output of the following Python code?

  d = {0, 1, 2}  for x in d:      print(d.add(x))

Question No# : 01 out of 20



What will be the output of the following Python code?

  x = 123  for i in x:      print(i)

Option A. 1 2 3
Option B. 123
Option C. error
Option D. none of the mentioned
True Answer C

Explanation :
Objects of type int are not iterable.

What will be the output of the following Python code?

  x = 123  for i in x:      print(i)

Question No# : 02 out of 20



What will be the output of the following Python code snippet?

  a = [0, 1, 2, 3]  for a[0] in a:      print(a[0])

Option A. 0 1 2 3
Option B. 0 1 2 2
Option C. 3 3 3 3
Option D. error
True Answer A

Explanation :
The value of a[0] changes in each iteration. Since the first value that it takes is itself, there is no visible error in the current example.

What will be the output of the following Python code snippet?

  a = [0, 1, 2, 3]  for a[0] in a:      print(a[0])

Question No# : 03 out of 20



Which of the following expressions is an example of type conversion?

Option A. 4.0 + float(3)
Option B. 5.3 + 6.3
Option C. 5.0 + 3
Option D. 3 + 7
True Answer A

Explanation :
Type conversion is nothing but explicit conversion of operands to a specific type. Options 5.3 + 6.3 and 5.0 + 3 are examples of implicit conversion whereas option 4.0 + float(3) is an example of explicit conversion or type conversion.

Which of the following expressions is an example of type conversion?

Question No# : 04 out of 20



What will be the output of the following Python code?

for i in range(5):      if i == 5:          break      else:          print(i)  else:      print("Here")

Option A. 0 1 2 3 4 Here
Option B. 0 1 2 3 4 5 Here
Option C. 0 1 2 3 4
Option D. 1 2 3 4 5
True Answer A

Explanation :
The else part is executed if control doesn’t break out of the loop.

What will be the output of the following Python code?

for i in range(5):      if i == 5:          break      else:          print(i)  else:      print("Here")

Question No# : 05 out of 20



Which operator is also called as Conditional operator?

Option A. Ternary
Option B. Relational
Option C. Logical
Option D. Assignment
True Answer A

Explanation :

Which operator is also called as Conditional operator?

Question No# : 06 out of 20

What will be the output of the following Python expression if X=345?

print(“%06d”%X)

Option A. 345000
Option B. 000345
Option C. 000000345
Option D. 345000000
True Answer B

Explanation :
The above expression returns the output 000345. It adds the required number of zeroes before the given number in order to make the number of digits 6 (as specified in this case).

What will be the output of the following Python expression if X=345?

print(“%06d”%X)

Question No# : 07 out of 20



What will be the output of the following Python code?

  x = (i for i in range(3))  for i in x:      print(i)  for i in x:      print(i)

Option A. 0 1 2
Option B. error
Option C. 0 1 2 0 1 2
Option D. none of the mentioned
True Answer A

Explanation :
We can loop over a generator object only once.

What will be the output of the following Python code?

  x = (i for i in range(3))  for i in x:      print(i)  for i in x:      print(i)

Question No# : 08 out of 20



What will be the output of the following Python code?

  i = 1  while False:      if i%2 == 0:          break      print(i)      i += 2

Option A. 1
Option B. 1 3 5 7 …
Option C. 1 2 3 4 …
Option D. none of the mentioned
True Answer D

Explanation :
Control does not enter the loop because of False.

What will be the output of the following Python code?

  i = 1  while False:      if i%2 == 0:          break      print(i)      i += 2

Question No# : 09 out of 20



Which amongst this is not a jump statement ?

Option A. for
Option B. goto
Option C. continue
Option D. break
True Answer A

Explanation :

Which amongst this is not a jump statement ?

Question No# : 10 out of 20

What will be the output of the following Python code snippet?

not(3>4)
not(1&1)

Option A. True True
Option B. True False
Option C. False True
Option D. False False
True Answer B

Explanation :
The function not returns true if the argument amounts to false, and false if the argument amounts to true. Hence the first function returns false, and the second function returns false.

What will be the output of the following Python code snippet?

not(3>4)
not(1&1)

Question No# : 11 out of 20



What will be the output of the following Python expression if x=456?

print("%-06d"%x)

Option A. 000456
Option B. 456000
Option C. 456
Option D. error
True Answer C

Explanation :
The expression shown above results in the output 456.

What will be the output of the following Python expression if x=456?

print("%-06d"%x)

Question No# : 12 out of 20



Which operator is also called as Comparative operator?

Option A. Arithmetic
Option B. Relational
Option C. Logical
Option D. Assignment
True Answer B

Explanation :

Which operator is also called as Comparative operator?

Question No# : 13 out of 20

What will be the output of the following Python code snippet?

bool(False)

bool()

Option A. True True
Option B. False True
Option C. False False
Option D. True False
True Answer D

Explanation :
The Boolean function returns true if the argument passed to the bool function does not amount to zero. In the first example, the string ‘False’ is passed to the function bool. This does not amount to zero and hence the output is true. In the second function, an empty list is passed to the function bool. Hence the output is false.

What will be the output of the following Python code snippet?

bool(False)

bool()

Question No# : 14 out of 20



What will be the output of the following Python code snippet?

for i in '':      print (i)

Option A. None
Option B. (nothing is printed)
Option C. error
Option D. none of the mentioned
True Answer D

Explanation :
The string does not have any character to loop over.

What will be the output of the following Python code snippet?

for i in '':      print (i)

Question No# : 15 out of 20



What will be the output of the following Python code?

  x = ['ab', 'cd']  for i in x:      x.append(i.upper())  print(x)

Option A. [‘AB’, ‘CD’]
Option B. [‘ab’, ‘cd’, ‘AB’, ‘CD’]
Option C. [‘ab’, ‘cd’]
Option D. none of the mentioned
True Answer D

Explanation :
The loop does not terminate as new elements are being added to the list in each iteration.

What will be the output of the following Python code?

  x = ['ab', 'cd']  for i in x:      x.append(i.upper())  print(x)

Question No# : 16 out of 20



What will be the value of X in the following Python expression?

X = 2+9*((3*12)-8)/10

Option A. 30.0
Option B. 30.8
Option C. 28.4
Option D. 27.2
True Answer D

Explanation :
The expression shown above is evaluated as: 2+9*(36-8)/10, which simplifies to give 2+9*(2.8), which is equal to 2+25.2 = 27.2. Hence the result of this expression is 27.2.

What will be the value of X in the following Python expression?

X = 2+9*((3*12)-8)/10

Question No# : 17 out of 20



Which of the following Boolean expressions is not logically equivalent to the other three?

Option A. not(-6<0 or-6>10)
Option B. -6>=0 and -6<=10
Option C. not(-6<10 or-6==10)
Option D. not(-6>10 or-6==10)
True Answer D

Explanation :
The expression not(-6<0 or -6>10) returns the output False. The expression -6>=0 and -6<=10 returns the output False. The expression not(-6<10 or -6==10) returns the output False. The expression not(-6>10 or -6==10) returns the output True.

Which of the following Boolean expressions is not logically equivalent to the other three?

Question No# : 18 out of 20



What will be the output of the following Python code snippet?

  x = 2  for i in range(x):      x -= 2      print (x)

Option A. 0 1 2 3 4 …
Option B. 0 -2
Option C. 0
Option D. error
True Answer B

Explanation :
The loop is entered twice.

What will be the output of the following Python code snippet?

  x = 2  for i in range(x):      x -= 2      print (x)

Question No# : 19 out of 20



What will be the output of the following Python code?

  i = 1  while True:      if i%3 == 0:          break      print(i)         i + = 1

Option A. 1 2
Option B. 1 2 3
Option C. error
Option D. none of the mentioned
True Answer C

Explanation :
SyntaxError, there shouldn’t be a space between + and = in +=.

What will be the output of the following Python code?

  i = 1  while True:      if i%3 == 0:          break      print(i)         i + = 1

Question No# : 20 out of 20