Test Series - python

Test Number 62/108

Q: Which of the following functions accepts only integers as arguments?
A. ord()
B. min()
C. chr()
D. any()
Solution: 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.
Q: 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))
Solution: 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].
Q: 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 Error
Solution: 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.
Q: 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 value
Solution: The output of the first function will be –inf and that of the second function will be inf.
Q: 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()
Solution: 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.
Q: What will be the output of the following Python function?

hex(15)
A. f
B. 0xF
C. 0Xf
D. 0xf
Solution: 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.
Q: Which of the following functions does not throw an error?
A. ord()
B. ord(‘ ‘)
C. ord(”)
D. ord(“”)
Solution: 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).
Q: What will be the output of the following Python function?

len(["hello",2, 4, 6])
A. 4
B. 3
C. Error
D. 6
Solution: The function len() returns the length of the number of elements in the iterable. Therefore the output of the function shown above is 4.
Q: What will be the output of the following Python function?

oct(7)
oct(‘7’)
A. Error 07
B. 0o7 Error
C. 0o7 Error
D. 07 0o7
Solution: 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.

You Have Score    /9