Python Quiz-7

Q1. if x = [1, 2, 5, 3, 4], what will be the output of print(x[0:3:2])?

  1. [1, 5, 4]
  2. [1, 5]
  3. [1, 2, 5, 3, 4]
  4. List index out of range exception!

Answer: 2
Explanation: Slice in Python list here refer to list[::]. So x[0:3:2] will start at index 0, stop at index 3 at take a step of 2.

Q2. What is the difference between a method and a function in Python?

  1. Functions can be called only by its name but methods can not be called by its name only.
  2. we don’t need a class to define a function but methods are defined inside a class.
  3. Functions are independent entities in a program while methods are dependent on the class they belong to.
  4. All of the above.

Answer: 4
Explanation: All the given three options are correct.

Q3. What will be the output of following code:

  1. [20, 30, 40, 50]
  2. [10, 20, 30, 40, 10]

Answer: 2
Explanation: In Python language, + operator append the elements to the original array.

Q4. What will be the output of following code:

  1. 3 2
  2. 2 2
  3. 2 3
  4. SyntaxError

Answer: 4
Explanation: It will raise an error. In function definition, non-default arguments should be written before the default arguments.

Q5. Which of the following is true when you open a file using with keyword?

  1. File reading and writing are faster using the with statement.
  2. The file is automatically closed after leaving the block, and all the resources that are tied up with the file are released.
  3. You need not to handle exception anymore in the whole script.

Answer: 2
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.

Q6. A class can serve as base class for many derived classes.

  1. True
  2. False

Answer: 1
Explanation: yes a class can serve as base class for many derived classes. It is also call as Hierarchical inheritance.

Q7. The following code will throw an error.

  1. True
  2. False

Answer: 1
Explanation: **kwargs are used as arbitrary keyword arguments. If we do not know how many keyword arguments that will be passed into our function then we can add two asterisk ** before the parameter name in the function definition. In the given function definition x will be a dictinary of arguments. That’s why print(x[a]) will raise an error.

Q8. Which of the following statement will create a variable of string type?

  1. a = ”’AILearner”’
  2. a = “AILearner”
  3. a = ‘AILearner’
  4. All of the above.

Answer: 4
Explanation: In Python language, a string can be created by using any of the single, double or triple quotation marks. Usually three quotes are used to create a multiline string.

Leave a Reply