PCEP Type Conversion and Built-in Functions 1 — Questions and Answers
Question 1: What is the result of evaluating int("42") in Python?
- "42" (a string)
- 42.0 (a float)
- 42 (an integer) (Correct answer)
- A TypeError is raised
Correct answer: 42 (an integer)
int() converts a valid string representation of a whole number to an integer, so int("42") returns the integer 42.
Question 2: What does float("3.14") return in Python?
- 3 (an integer)
- "3.14" (a string)
- A ValueError is raised
- 3.14 (a float) (Correct answer)
Correct answer: 3.14 (a float)
float() converts a valid string representation of a decimal number into a floating-point value, returning 3.14.
Question 3: What is the result of str(100) in Python?
- 100 (integer unchanged)
- "100" (a string) (Correct answer)
- 100.0 (a float)
- A TypeError is raised
Correct answer: "100" (a string)
str() converts any value to its string representation, so str(100) returns the string "100".
Question 4: What does bool(0) return in Python?
- False (Correct answer)
- True
- 0
- None
Correct answer: False
In Python, 0 is falsy, so bool(0) returns False.
Question 5: What is the result of int(3.9) in Python?
- 4 (rounds up)
- A ValueError is raised
- 3 (truncates toward zero) (Correct answer)
- 3.9 (unchanged)
Correct answer: 3 (truncates toward zero)
int() truncates the decimal portion toward zero, so int(3.9) returns 3, not 4.
Question 6: What happens when you execute int("hello") in Python?
- Returns 0 as a default
- Returns None
- Returns "hello" unchanged
- Raises a ValueError (Correct answer)
Correct answer: Raises a ValueError
int() raises a ValueError when given a string that cannot be interpreted as an integer.
Question 7: Which of the following is the correct output of type(3.14) in Python?
- "float"
- <class 'float'> (Correct answer)
- float()
- (float)
Correct answer: <class 'float'>
type() returns the class object itself, and its repr is <class 'float'> for floating-point values.
What is the result of evaluating int("42") in Python?