Python Question and Answers – Built-in Functions – 3

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

Answer: c
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

Answer: d
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?

advertisement
advertisement
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

Answer: b
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?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
ord(65)
ord(‘A’)

a)

A
65
advertisement

b)

Error
65
advertisement

c)

A
Error

d)

Error
Error
View Answer
Answer: b
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 value 
View Answer
Answer: a
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

Answer: d
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

Answer: d
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

Answer: b
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

Answer: a
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
Error 

c)

0o7
Error

d)

07
0o7
View Answer
Answer: c
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.