Python Quiz-1

Q1. Which of the following is true for a lambda function in Python?

  1. A lambda function is a small anonymous function.
  2. A lambda function can take any number of arguments, but can only have one expression.
  3. Lambda functions are used for functional programming.
  4. All of the above.

Answer: 4
Explanation: All of the statements regarding the Python lambda functions are correct.

Q2. Which of the following is the recommended way to ensure that a file object is properly closed after usage?

  1. Use close() function after the use of file object.
  2. Use try/finally block
  3. Use with statement
  4. All of the above.

Answer: 3
Explanation: When you open a file using with keyword, the file is automatically closed after leaving the block, and all the resources that are tied up with the file are released.

Q3. What will be the output of following code:

  1. 0 2 3
  2. 0 16 24
  3. 48 64 72
  4. 32 96 128

Answer: 3
Explanation: sys.getsizeof(object, default) return the size of object in bytes.

Q4. Which of the following list assignment in Python will throw an exception?

  1. x = [1, 2, ‘python’]
  2. x = [1, 2, [1, 2]]
  3. x = [‘a’, ‘b’, ‘c’, ‘a’, ‘4.5’, int(3.4)]
  4. None of the above.

Answer: 4
Explanation: in Python language, list items can be of any data types and also a list can contain different data types.

Q5. What will be the output of following code:

  1. <class ‘tuple’>
  2. <class ‘str’>
  3. <class ‘list’>
  4. <class ‘int’>

Answer: 2
Explanation: To create a tuple with single item, we need to add comma(,) at the end of the item.

Q6. If str = “AILearner”, what will be the output of print(str == ‘AILearner’, str is “AILearner”)?

  1. False False
  2. True False
  3. False True
  4. True True

Answer: 4
Explanation: In Python language, “is” keyword is used to test if the two variables refers to the same object.

Q7. What will be the output of print(13//2)?

  1. 6.5
  2. 6
  3. 7
  4. Throws an error.

Answer: 2
Explanation: In Python language, // is a floor division operator. This operator will divide the two numbers and round up it to the previous lower integer.

Q8. What will be the output of following code:

  1. 1120
  2. ‘a’
  3. -1120
  4. TypeError

Answer: 4
Explanation: max() does not support comparison between instances of ‘str’ and ‘int’.

Leave a Reply