Python Quiz-8

Q1. What will be the output of following code:

  1. [7, 9, 2, 0, 2, 1]
  2. [‘7’, ‘9’, ‘2’, ‘0’, ‘2’, ‘1’]
  3. [7, 2, 0, 2, 1]
  4. Runtime error!

Answer: 2
Explanation: This is an example of list comprehension in Python. List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.
List Comprehensions

Q2. What are the name of the operators |, &, >> and << respectively?

  1. AND, OR, RIGHT SHIFT and LEFT SHIFT
  2. AND, OR, LEFT SHIFT and RIGHT SHIFT
  3. OR, AND, LEFT SHIFT and RIGHT SHIFT
  4. OR, AND, RIGHT SHIFT and LEFT SHIFT

Answer: 4
Explanation: OR, AND, RIGHT SHIFT and LEFT SHIFT are name of the |, &, >> and << operators respectively.

Q3. What will be the output of following code:/code/

  1. [1, 2] [1, 2, 3, 4]
  2. [1, 2, 3, 4] [1, 2, 3, 4]
  3. [1, 2, 3, 4] [1, 2]
  4. [1, 2] [3, 4]

Answer: 2
Explanation: In Python language, assigning one variable to another variable creates an alias of each variable. Here variable b is an alias of variable a. It means both variable a and b points to the same object in the memory. That’s why when you are updating the value of variable b, value of variable a is also changing.

Q4. Python is an object oriented programming language.

  1. True
  2. False

Answer: 1
Explanation: Python is an object oriented programming language. Almost everything in Python is an object. A class in Python is a blueprint for creating objects.

Q5. What will be the output of following code:

  1. 1 2 (3, 4, 5)
  2. 1 2 3
  3. 1 2 [3, 4, 5]
  4. ValueError

Answer: 3
Explanation: *variable is basically extended iterable unpacking. It will assign a list of all items not assigned to a regular variable name. So from tuple “num”, 1 is assinged to a, 2 is assigned to b and rest all items will be assigend as list to c.
Extended Iterable Unpacking

Q6. Which of the following statement is the correct way of assigning an integer value to a variable in Python?

  1. x = ‘6’
  2. int x = 6;
  3. x = 6
  4. Integer x = 6;

Answer: 3
Explanation: x = ‘6’ -> will create a string data type.
int x = 6; and Integer x = 6; -> It is a way of assignment and declaration in Java language.

In Python there is no need to declare the data type using int or Integer. It will create a variable with respect to the type of data you assing to it.

Q7. What is the syntax for multiline comment in Python?

  1. Use /* at the start of the first line and use */ at the end of the last line.
  2. Use <-- at the start of the first line and use --> at the end of the last line.
  3. Python does not have syntax for multiline comment. You can either use # at the start of every line or make it as multiline string (triple quote).
  4. None of the above.

Answer: 3
Explanation: Since Python does not have syntax for multiline comments, there is a workaround. As Python ignores the string literal if it is not assigned to a variable, we can add multiline string in our code and place the multiline comment inside it.

Q8. What will be the output of following code:

  1. 1 2 3
  2. 1 2 3 4
  3. 1 2 3 5
  4. 1 2 3 4 5

Answer: 1
Explanation: while loop executes the block until a condition is satisfied. When the condition becomes false, the statement immediately after the loop is executed. The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won’t be executed.
Reference: While Loop

Leave a Reply