Python Quiz-9

Q1. Which of the following statements is true?

  1. Special method __add()__ is called when + operator is used.
  2. In Python, same operator may behave differently depending upon operands.
  3. __init__() function is called when a new object is instantiated.
  4. All of the above

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

Q2. In Python, which of the following statement will print a random number between 1 and 10 (both 1 and 10 included)?

  1. print(random.randrange(1, 10))
  2. print(random.random(1, 10))
  3. print(random.randint(1, 10))
  4. None of the above.

Answer: 3
Explanation: 1. print(random.randrange(1, 10)) -> This will print a random integer between 1 and 10 where 10 is not included.
2. print(random.random(1, 10)) -> This will throw an error because random does not take any argument but two are given.
3. print(random.randint(1, 10)) -> This will print a random number between 1 and 10 (both 1 and 10 included).

Q3. Which of the following is not true of global variable in Python?

  1. global keyword can be used to create a global variable inside a local scope.
  2. Accessible only in the block they are defined in.
  3. A variable created in the main body of the Python code is a global variable.
  4. Accessible from anywhere in the script.

Answer: 2
Explanation: Global variables are available from within any scope, either it is global or local.

Q4. If x = 2**4/2+3-1, then what will be the data type of x?

  1. int
  2. float

Answer: 2
Explanation: Division operator is used in x = 2**4/2+3-1 and division operator gives the value including the decimals which will be of float data type.

Q5. Which of the following method can be used to combine strings and numbers?

  1. concatenate()
  2. join()
  3. append()
  4. format()

Answer: 4
Explanation: The format() method format the passed arguments and places them in the string according to the placeholders {}.
Fancier Output Formatting

Q6. Suppose x = 6.3, then which of the following statement will throw an error?

  1. y = float(x)
  2. y = int(x)
  3. y = complex(x)
  4. None of the above.

Answer: 4
Explanation: int, float and complex are numeric data types. These will cast ‘x’ into it’s respective data types.

Q7. What will be the output of print(ord(‘the’))?

  1. 116 104 101
  2. 116 116 116
  3. 104 104 104
  4. TypeError

Answer: 4
Explanation: Python ord() function returns the Unicode code from a given character. This function accepts a string of unit length as an argument and returns the Unicode equivalence of the passed argument.
Reference: ord() function in Python

Q8. In Python, which of the following character is used to add at the start of a line to make it a comment?

  1. #
  2. //
  3. &

Answer: 1
Explanation: In Python language comments start with # and the rest of the line will be rendered as comment.

Leave a Reply