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?

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



What plays a vital role in Python programming?

Option A. Statements
Option B. Control
Option C. Structure
Option D. Indentation
True Answer D

Explanation :

What plays a vital role in Python programming?

Question No# : 02 out of 20

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

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

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

Explanation :
The value of a[-1] changes in each iteration.

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

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

Question No# : 03 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# : 04 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# : 05 out of 20



What will be the output of the following Python code?

  x = "abcdef"  i = "a"  while i in x[:-1]:      print(i, end = " ")

Option A. a a a a a
Option B. a a a a a a
Option C. a a a a a a …
Option D. a
True Answer C

Explanation :
String x is not being altered and i is in x[:-1].

What will be the output of the following Python code?

  x = "abcdef"  i = "a"  while i in x[:-1]:      print(i, end = " ")

Question No# : 06 out of 20



What will be the output of the following Python code?

string = "my name is x"  for i in string.split():      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 C

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

What will be the output of the following Python code?

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

Question No# : 07 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# : 08 out of 20



Checking condition in python can be done by using the if-else statement

Option A. TRUE
Option B. FALSE
Option C.
Option D.
True Answer A

Explanation :

Checking condition in python can be done by using the if-else statement

Question No# : 09 out of 20

What will be the output of the following Python code?

for i in range(10):      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 C

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(10):      if i == 5:          break      else:          print(i)  else:      print("Here")

Question No# : 10 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# : 11 out of 20



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

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

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

Explanation :
range() is computed only at the time of entering the loop.

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

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

Question No# : 12 out of 20



What will be the output of the following Python code?

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

Option A. a a a a a a
Option B. a
Option C. no output
Option D. error
True Answer B

Explanation :
The string x is being shortened by one character in each iteration.

What will be the output of the following Python code?

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

Question No# : 13 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# : 14 out of 20



The condition in the if statement should be in the form of

Option A. Arithmetic or Relational expression
Option B. Arithmetic or Logical expression
Option C. Relational or Logical expression
Option D. Arithmetic
True Answer C

Explanation :

The condition in the if statement should be in the form of

Question No# : 15 out of 20

What will be the output of the following Python code?

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

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

Explanation :
The else part is executed when the condition in the while statement is false.

What will be the output of the following Python code?

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

Question No# : 16 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# : 17 out of 20



What will be the output of the following Python code?

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

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

Explanation :
Object of type float cannot be interpreted as an integer.

What will be the output of the following Python code?

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

Question No# : 18 out of 20



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

  a = [0, 1, 2, 3]  i = -2  for i not in a:      print(i)      i += 1

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

Explanation :
SyntaxError, not in isn’t allowed in for loops.

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

  a = [0, 1, 2, 3]  i = -2  for i not in a:      print(i)      i += 1

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