Python Quiz-2

Q1. What will be the output of following code:

  1. 15
  2. 5
  3. 50
  4. Error

Answer: 3
Explanation: A function which is defined inside another function is known as inner function or nested function. Nested functions are able to access variables of the enclosing scope. Inner functions are used so that they can be protected from everything happening outside the function. This process is also known as Encapsulation.
Reference: Inner Functions

Q2. Which of the following function is used inside a for loop in Python?

  1. count()
  2. iterate()
  3. next()
  4. new()

Answer: 3
Explanation: You can read more about Python for loop here:
What’s inside Python ‘for’ loop?

Q3. What will be the output of following code:

  1. 1
    2
    Great!
  2. x
    y
    Great!
  3. 1
    2
    3
    Great!
  4. x
    y
    z
    Great!

Answer: 2
Explanation: If “if” condition is not True then only “elif” condition will execute.

Q4. What will be the output of following code:

  1. 5
  2. 8
  3. 6
  4. TypeError!

Answer: 3
Explanation: len() function gives the length of the tuple.

Q5. Which of the following statement will return False?

  1. bool(-1)
  2. bool(1)
  3. bool(0)
  4. bool(12E5)

Answer: 3
Explanation: Any number is True, except 0.

Q6. Which of the following brackets can be used to create lists in Python?

  1. []
  2. {}
  3. ()
  4. <>

Answer: 1
Explanation: list1 = [] will create a list in Python.

Q7. In Python, what is the correct way of casting an integer value to a string?

  1. x = str(6)
  2. x = Integer.toString(6);
  3. Both of the above.
  4. None of the above.

Answer: 1
Explanation: In Python language, you can cast an integer to a string by using ‘str’ keyword.

Q8. What will be the output of following code:

  1. 0
  2. 1
  3. 2
  4. Throws an error!

Answer: 1
Explanation: count() method returns the number of times a specified value occurs in a string. “a” occurs one time in “”TheAILearner”. But in count method start (0) and end (5) index of the string is given where it will search for “a”.

Leave a Reply