Python Quiz-3

Q1. What will be the output of following code:

  1. a b c
  2. a b
  3. a c
  4. a

Answer: 3
Explanation: continue keyword will skip the rest of the statements after it in the loop and make the loop to run for the next iteration.

Q2. What will be the output of following code:

  1. 18
  2. 12
  3. 9
  4. Error

Answer: 2
Explanation: 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
Default arguments in Python

Q3. What are the dunder(magic) methods in Python?

  1. Methods that start with single underscore.
  2. Methods that start with double underscore and ends with double underscore.
  3. Methods that start with double underscore.
  4. Methods that ends with single underscore.

Answer: 2
Explanation: Dunder or magic methods in Python are the methods having two prefix and suffix underscores in the method name. Dunder here means “Double Under (Underscores)”. These are commonly used for operator overloading. Few examples for magic methods are: __init__, __add__, __len__, __repr__ etc.
Reference: Dunder or magic methods in Python

Q4. Which of the following option gives the value of rating2 from the code?

  1. dictionary[“Company”][“Employee”][“Performance”][“rating2”]
  2. dictionary[“Company”][“Employee”][“Performance”][1]
  3. dictionary[“Company”][0][“Performance”][“rating2”]
  4. dictionary[0][“Employee”][“Performance”][“rating2”]

Answer: 1
Explanation: dictionary -> “Company” -> “Employee” -> “Performance” -> “rating2” = 8/10

Q5. Which of the following are main component of JSON in Python?

  1. Arrays and Values
  2. DIctionary and Values
  3. Classes and Objects
  4. Keys and Values

Answer: 4
Explanation: Keys and Values are main component of JSON in Python.

Q6. Which of the following is not a correct statement regarding JSON in Python?

  1. Python has a built-in package called json, which can be used to work with JSON data.
  2. JSON is a syntax for storing and exchanging data.
  3. A Python object can be converted into JSON string using json.create()
  4. A Python object can be converted into JSON string using json.dumps()

Answer: 3
Explanation: A Python object can be converted into JSON string using json.dumps()
Python JSON

Q7. Which of the following is not an iterable Object?

  1. List and tuples
  2. Int
  3. Set
  4. Dictionary

Answer: 2
Explanation: int is a built-in data type and it is not iterable. Lists, tuples, dictionaries, and sets are all iterable objects.

Q8. What will be the output of following code:

  1. {3, 4, 5, 6}
  2. {3, 4}
  3. {1, 2, 3, 4, 5, 6}
  4. {1, 2, 5, 6}

Answer: 4
Explanation: ^ operator will return all the items which are not present in both the sets.

Leave a Reply