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 chr() function accepts only integers as arguments and returns the corresponding Unicode character. For example, chr(97) returns ‘a’. In contrast, ord() takes a string of a single Unicode character, min() works with iterables, and any() also takes an iterable.

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 correct way to reverse and print a list in Python is to use list(reversed(l)). The reversed() function returns an iterator that yields the elements of the list in reverse order. Wrapping it with list() converts the iterator into a list. So, for l = [2, 3, 4], list(reversed(l)) will output [4, 3, 2].

3. What will be the output of the following Python function?

advertisement
print(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 float() function in Python automatically strips leading and trailing whitespace (including spaces and newline characters) from the input string before converting it to a floating-point number. So, float(‘ -12345\n’) correctly returns -12345.0 without raising an error or preserving whitespace.

4. What will be the output of the following Python function?

Free 30-Day C++ Certification Bootcamp is Live. Join Now!
print(ord(‘A’))
print(ord(65))

a)

A
65

b)

65
Error
advertisement

c)

A
Error

d)

Error
Error
View Answer
Answer: b
Explanation: Here,

  • ord(‘A’) returns the ASCII (Unicode) integer value of the character ‘A’, which is 65.
  • ord(65) will cause a TypeError because ord() expects a single character string, not an integer.

So the output will be:

65
Error
 
 

5. What will be the output of the following Python function?

print(float('-infinity'))
print(float('inf'))

a)

–inf
inf

b)

–infinity
inf

c)

Error
Error

d)

Error
Junk value 
View Answer
Answer: a
Explanation: The float() function in Python can convert special string representations of floating-point numbers such as ‘inf’ for positive infinity and ‘-infinity’ (or ‘-inf’) for negative infinity. Thus, float(‘-infinity’) returns negative infinity (-inf) and float(‘inf’) returns positive infinity (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?

print(hex(15))

a) f
b) 0xF
c) 0Xf
d) 0xf
View Answer

Answer: d
Explanation: The hex() function converts an integer to its hexadecimal string representation using lowercase letters and includes the 0x prefix. So, hex(15) returns ‘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 ord() function in Python returns the Unicode code point for a given single character. When you pass a blank space ‘ ‘ to ord(), it returns 32, which is the ASCII value for a space character, so it does not throw an error. However, calling ord() without any argument or passing an empty string “” causes an error because ord() requires exactly one character as input. Therefore, only ord(‘ ‘) among the given options does not raise an error.

9. What will be the output of the following Python function?

print(len(["hello",2, 4, 6]))

a) 4
b) 3
c) Error
d) 6
View Answer

Answer: a
Explanation: The len() function returns the number of elements in the list, regardless of their types. In this case, the list [“hello”, 2, 4, 6] contains four elements, so len() returns 4.

10. What will be the output of the following Python function?

print(oct(7))
print(oct('7'))

a)

Error
07

b)

0o7
7 

c)

0o7
Error

d)

07
0o7
View Answer
Answer: c
Explanation: The oct() function in Python converts an integer into its octal string representation. When you pass the integer 7 to oct(7), it returns the string ‘0o7’, which is the octal equivalent of the decimal number 7. However, if you try to pass a string like ‘7’ to the oct() function, it will result in a TypeError because oct() only accepts integers as valid input. Thus, the first call outputs 0o7, while the second call raises an error.
 
 

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.