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

OPERATORS EXPRESSIONS AND PYTHON STATEMENTS

00:00:00

Which one of these is floor division?

Option A. /
Option B. //
Option C. %
Option D. None of the mentioned
True 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.

Which one of these is floor division?

Question No# : 01 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# : 02 out of 20



Which is the correct operator for power(x,y)?

Option A. X^y
Option B. X**y
Option C. X^^y
Option D. None of the mentioned
True Answer B

Explanation :
In python, power operator is x**y i.e. 2**3=8.

Which is the correct operator for power(x,y)?

Question No# : 03 out of 20

What will be the output of the following Python code?

  x = "abcdef"  i = "a"  while i in x:      print(i, end = " ")

Option A. no output
Option B. i i i i i i …
Option C. a a a a a a …
Option D. a b c d e f
True Answer C

Explanation :
As the value of i or x isn’t changing, the condition will always evaluate to True.

What will be the output of the following Python code?

  x = "abcdef"  i = "a"  while i in x:      print(i, end = " ")

Question No# : 04 out of 20



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

  x = 'abcd'  for i in range(len(x)):      print(x)      x = 'a'

Option A. a
Option B. abcd abcd abcd abcd
Option C. a a a a
Option D. none of the mentioned
True Answer D

Explanation :
abcd a a a is the output as x is modified only after ‘abcd’ has been printed once.

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

  x = 'abcd'  for i in range(len(x)):      print(x)      x = 'a'

Question No# : 05 out of 20



What will be the output of the following Python code?

  x = 'abcd'  for i in x:      print(i.upper())

Option A. a b c d
Option B. A B C D
Option C. a B C D
Option D. error
True Answer B

Explanation :
The instance of the string returned by upper() is being printed.

What will be the output of the following Python code?

  x = 'abcd'  for i in x:      print(i.upper())

Question No# : 06 out of 20



What will be the output of the following Python code?

  x = 'abcd'  for i in x:      print(i)      x.upper()

Option A. a B C D
Option B. a b c d
Option C. A B C D
Option D. error
True Answer B

Explanation :
Changes do not happen in-place, rather a new instance of the string is returned.

What will be the output of the following Python code?

  x = 'abcd'  for i in x:      print(i)      x.upper()

Question No# : 07 out of 20



What will be the output of the following Python code?

  d = {0: 'a', 1: 'b', 2: 'c'}  for x in d.values():      print(d[x])

Option A. 0 1 2
Option B. a b c
Option C. 0 a 1 b 2 c
Option D. none of the mentioned
True Answer D

Explanation :
Causes a KeyError.

What will be the output of the following Python code?

  d = {0: 'a', 1: 'b', 2: 'c'}  for x in d.values():      print(d[x])

Question No# : 08 out of 20



What will be the output of the following Python code?

for i in range(0):      print(i)

Option A. 0
Option B. no output
Option C. error
Option D. none of the mentioned
True Answer B

Explanation :
range(0) is empty.

What will be the output of the following Python code?

for i in range(0):      print(i)

Question No# : 09 out of 20



What will be the output of the following Python code?

string = "my name is x"  for i in string:      print (i, end=", ")

Option A. m, y, , n, a, m, e, , i, s, , x,
Option B. m, y, , n, a, m, e, , i, s, , x
Option C. my, name, is, x,
Option D. error
True Answer A

Explanation :
Variable i takes the value of one character at a time.

What will be the output of the following Python code?

string = "my name is x"  for i in string:      print (i, end=", ")

Question No# : 10 out of 20



What will be the output of the following Python code?

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

Option A. 5 6 7 8 9 10
Option B. 5 6 7 8
Option C. 5 6
Option D. error
True Answer B

Explanation :
0O11 is an octal number.

What will be the output of the following Python code?

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

Question No# : 11 out of 20



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

  x = 'abcd'  for i in range(len(x)):      i.upper()  print (x)

Option A. a b c d
Option B. 0 1 2 3
Option C. error
Option D. none of the mentioned
True Answer C

Explanation :
Objects of type int have no attribute upper().

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

  x = 'abcd'  for i in range(len(x)):      i.upper()  print (x)

Question No# : 12 out of 20



What will be the output of the following Python code?

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

Option A. 2 4 6 8 10 …
Option B. 2 4
Option C. 2 3
Option D. error
True Answer B

Explanation :
The numbers 2 and 4 are printed. The next value of i is 6 which is divisible by 3 and hence control exits the loop.

What will be the output of the following Python code?

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

Question No# : 13 out of 20



What will be the output of the following Python code?

  d = {0: 'a', 1: 'b', 2: 'c'}  for i in d:      print(i)

Option A. 0 1 2
Option B. a b c
Option C. 0 a 1 b 2 c
Option D. none of the mentioned
True Answer A

Explanation :
Loops over the keys of the dictionary.

What will be the output of the following Python code?

  d = {0: 'a', 1: 'b', 2: 'c'}  for i in d:      print(i)

Question No# : 14 out of 20



What will be the output of the following Python code?

  i = 0  while i < 5:      print(i)      i += 1      if i == 3:          break  else:      print(0)

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

Explanation :
The else part is not executed if control breaks out of the loop.

What will be the output of the following Python code?

  i = 0  while i < 5:      print(i)      i += 1      if i == 3:          break  else:      print(0)

Question No# : 15 out of 20



What will be the output of the following Python code?

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

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

Explanation :
Objects of type set have no attribute values.

What will be the output of the following Python code?

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

Question No# : 16 out of 20



Which one of the following has the same precedence level?

Option A. Addition and Subtraction
Option B. Multiplication, Division and Addition
Option C. Multiplication, Division, Addition and Subtraction
Option D. Addition and Multiplication
True Answer A

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.

Which one of the following has the same precedence level?

Question No# : 17 out of 20



Operators with the same precedence are evaluated in which manner?

Option A. Left to Right
Option B. Right to Left
Option C. Can’t say
Option D. None of the mentioned
True Answer A

Explanation :
None.

Operators with the same precedence are evaluated in which manner?

Question No# : 18 out of 20



What is the answer to this expression, 22 % 3 is?

Option A. 7
Option B. 1
Option C. 0
Option D. 5
True Answer B

Explanation :
Modulus operator gives the remainder. So, 22%3 gives the remainder, that is, 1.

What is the answer to this expression, 22 % 3 is?

Question No# : 19 out of 20



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

  x = 'abcd'  for i in range(len(x)):      i[x].upper()  print (x)

Option A. abcd
Option B. ABCD
Option C. error
Option D. none of the mentioned
True Answer C

Explanation :
Objects of type int aren’t subscriptable. However, if the statement was x[i], an error would not have been thrown.

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

  x = 'abcd'  for i in range(len(x)):      i[x].upper()  print (x)

Question No# : 20 out of 20