Python Quiz-5

Q1. What will be the output of following code:

  1. 3
  2. 1
  3. 2
  4. Error

Answer: 3
Explanation: To build a class/object as an iterator, we need the methods __iter__() and __next__().
The __next__() method return the next item in the sequence.
myiter = iter(a) => Creates an iterator.
next(myiter) will execute the __next__() method of class A.
Python Iterators

Q2. The following code will throw an error.

  1. True
  2. False

Answer: 2
Explanation: *args are used as arbitrary Arguments. If we do not know how many arguments that will be passed into our function then we can add a * before the parameter name in the function definition.

Q3. What will be the output of following code:

  1. 8 5
  2. 8 6
  3. 7 6
  4. 7 5

Answer: 4
Explanation: strip() method removes any leading and trailing characters from a string.

Q4. What will be the output of following code:

  1. (‘AILearner’,’AILearner’,’AILearner’)
  2. AILearnerAILearnerAILearner
  3. (‘AILearner3’)
  4. Error

Answer: 2
Explanation: Here, * operator will multiply the content of string ‘AILearner’ 3 times.

Q5. What will be the output of following code:

  1. Python
  2. Java
  3. Java
    Python
  4. Python
    Java

Answer: 2
Explanation: Java is the correct answer.

Q6. What will be the output of following code:

  1. [1, 2, 3]
  2. [1, 3]
  3. [1, 2]
  4. Throws an error!

Answer: 1
Explanation: As the len(x) = 2, x[3:4] will add the data at the end of the list.
Extending a list in Python

Q7. Which of the following data types can have only two values (either True or False)?

  1. integer
  2. Strings
  3. Boolean
  4. Float

Answer: 3
Explanation: Booleans can only have one of two values: True of False.

Q8. What will be the output of print(2 * 3 ** 3 * 2)?

  1. 108
  2. 432
  3. 1458
  4. 36

Answer: 1
Explanation: ** is a power operator and it has higher precedence than * multiplication operator. So the equation will be solved as follows:
=> 2 * 3 ** 3 * 2
=> 2 * 27 * 2
=> 54 * 2
=> 108

Leave a Reply