This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Built-in Functions – 3”.
1. Which of the following functions accepts only integers as arguments?
a) ord()
b) min()
c) chr()
d) any()
View Answer
Explanation: The function chr() accepts only integers as arguments. The function ord() accepts only strings. The functions min() and max() can accept floating point as well as integer arguments.
2. Suppose there is a list such that: l=[2,3,4]. If we want to print this list in reverse order, which of the following methods should be used?
a) reverse(l)
b) list(reverse[(l)])
c) reversed(l)
d) list(reversed(l))
View Answer
Explanation: The built-in function reversed() can be used to reverse the elements of a list. This function accepts only an iterable as an argument. To print the output in the form of a list, we use: list(reversed(l)). The output will be: [4,3,2].
3. What will be the output of the following Python function?
float(' -12345\n')
(Note that the number of blank spaces before the number is 5)
a) -12345.0 (5 blank spaces before the number)
b) -12345.0
c) Error
d) -12345.000000000…. (infinite decimal places)
View Answer
Explanation: The function float() will remove all the blank spaces and convert the integer to a floating point number. Hence the output will be: -12345.0.
4. What will be the output of the following Python function?
ord(65) ord(‘A’)
a)
A 65
b)
Error 65
c)
A Error
d)
Error ErrorView Answer
Explanation: The built-in function ord() is used to return the ASCII value of the alphabet passed to it as an argument. Hence the first function results in an error and the output of the second function is 65.
5. What will be the output of the following Python function?
float(‘-infinity’) float(‘inf’)
a)
–inf inf
b)
–infinity inf
c)
Error Error
d)
Error Junk valueView Answer
Explanation: The output of the first function will be –inf and that of the second function will be inf.
6. Which of the following functions will not result in an error when no arguments are passed to it?
a) min()
b) divmod()
c) all()
d) float()
View Answer
Explanation: The built-in functions min(), max(), divmod(), ord(), any(), all() etc throw an error when no arguments are passed to them. However there are some built-in functions like float(), complex() etc which do not throw an error when no arguments are passed to them. The output of float() is 0.0.
7. What will be the output of the following Python function?
hex(15)
a) f
b) 0xF
c) 0Xf
d) 0xf
View Answer
Explanation: The function hex() is used to convert the given argument into its hexadecimal representation, in lower case. Hence the output of the function hex(15) is 0xf.
8. Which of the following functions does not throw an error?
a) ord()
b) ord(‘ ‘)
c) ord(”)
d) ord(“”)
View Answer
Explanation: The function ord() accepts a character. Hence ord(), ord(”) and ord(“”) throw errors. However the function ord(‘ ‘) does not throw an error because in this case, we are actually passing a blank space as an argument. The output of ord(‘ ‘) is 32 (ASCII value corresponding to blank space).
9. What will be the output of the following Python function?
len(["hello",2, 4, 6])
a) 4
b) 3
c) Error
d) 6
View Answer
Explanation: The function len() returns the length of the number of elements in the iterable. Therefore the output of the function shown above is 4.
10. What will be the output of the following Python function?
oct(7) oct(‘7’)
a)
Error 07
b)
0o7 7
c)
0o7 Error
d)
07 0o7View Answer
Explanation: The function oct() is used to convert its argument into octal form. This function does not accept strings. Hence the second function results in an error while the output of the first function is 0o7.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Programming Internship
- Check Python Books
- Apply for Python Internship
- Check Information Technology Books
- Practice Programming MCQs