w3resource

PCEP-30-02 Exam Prep: Understanding Type Casting in Python

PCEP Certification Practice Test - Questions, Answers and Explanations

This comprehensive set of questions and explanations covers the fundamental topic of choosing operators and data types adequate to the problem, focusing on type casting. This diverse set of interactions enhances understanding and prepares for the PCEP-30-02 examination.

Question 1: What is the result of the following expression?

int(3.7)
  1. 4
  2. 3
  3. 3.7
  4. Error

Answer: b) 3

Explanation: The int() function truncates the decimal part and returns the integer part of the float, which is 3.

Question 2: What is the result of the following expression?

float(10)
  1. 10.0
  2. 10
  3. "10.0"
  4. Error

Answer: a) 10.0

Explanation: The float() function converts an integer to a floating-point number, so 10 becomes 10.0.

Question 3: What is the result of the following code?

str(100) + str(50)
  1. 150
  2. 10050
  3. "10050"
  4. Error

Answer: c) "10050"

Explanation: The str() function converts integers to strings, and concatenating two strings results in "10050".

Question 4: Which of the following expressions are valid type casts in Python? (Select all that apply)

  1. float("3.14")
  2. int("100")
  3. int("ten")
  4. str(25)

Answer: a) float("3.14"), b) int("100"), d) str(25)

Explanation: float("3.14"), int("100"), and str(25) are valid type casts. However, int("ten") will raise an error because "ten" is not a valid numeric string.

Question 5: Which of the following type casts can result in data loss? (Select all that apply)

  1. float(3)
  2. int(3.9)
  3. str(3.14)
  4. int("100")

Answer:b) int(3.9)

Explanation: Casting 3.9 to int results in data loss as the decimal part .9 is truncated, while the other type casts do not result in data loss.

Question 6: Place the following type casting operations in the correct order to convert a float to an integer and then to a string: float to int, int to str.

  • int to str
  • float to int

Answer:

  • float to int
  • int to str

Explanation: First, cast the float to an integer, then cast the integer to a string.

Question 7: The ______ function is used to convert an integer to a floating-point number in Python.

Answer: float

Explanation: The float function is used to convert an integer to a floating-point number.

Question 8: The ______ function in Python is used to convert a number to its string representation.

Answer: str

Explanation: The str function is used to convert a number to its string representation.

Question 9: Sort the following type casts in the order that would result in the value "20": str(20), float(20), int(20.5).

Answer:

  1. 1. str(20)
  2. int(20.5)
  3. float(20)

Explanation: str(20) directly results in "20", whereas int(20.5) and float(20) involve conversions that don’t directly result in "20".

Question 10: Fill in the missing code to convert the string "50" to an integer.

number = int("______")

Answer: "50"

Explanation: The correct code is number = int("50"), which converts the string "50" to the integer 50.

Question 11: Fill in the missing code to convert 5.9 to an integer.

result = ______(5.9)

Answer: int

Explanation: The correct code is result = int(5.9), which truncates the decimal part and returns 5.

Question 12: Insert the correct function to convert the integer 42 to a string.

number = 42
string_number = ______(number)

Answer: str

Explanation: The correct code is string_number = str(number), which converts the integer 42 to the string "42".

Question 13: Insert the correct function to convert the string "3.14" to a float.

pi = ______("3.14")

Answer: float

Explanation: The correct code is pi = float("3.14"), which converts the string "3.14" to the floating-point number 3.14.

Question 14: What happens if you try to cast the string "Python" to an integer?

int("Python")
  1. Returns 0
  2. Returns a large integer
  3. Raises a ValueError
  4. Converts it to ASCII values

Answer: c) Raises a ValueError

Explanation: Python raises a ValueError because "Python" is not a valid numeric string and cannot be converted to an integer.

Question 15: Which function can convert a floating-point number to an integer by truncating its decimal part?

  1. float()
  2. str()
  3. int()
  4. abs()

Answer: c) int()

Explanation: The int() function truncates the decimal part of a floating-point number, converting it to an integer.

Question 16: Which of the following casts will successfully convert a value without raising an error? (Select all that apply)

  1. float("12.34")
  2. int("12.34")
  3. str(100)
  4. int("abc")

Answer: a) float("12.34"), c) str(100)

Explanation: float("12.34") and str(100) will successfully convert the values, while int("12.34") and int("abc") will raise errors.

Question 17: Which of the following expressions will convert the integer 5 to the string "5"? (Select all that apply)

  1. str(5)
  2. int(5)
  3. float(5)
  4. "5"

Answer: a) str(5), d) "5"

Explanation: str(5) converts the integer 5 to the string "5", and "5" is already a string.

Question 18: Arrange the following steps in the correct order to convert the string "45.67" to an integer: Convert to float, Round to nearest integer, Convert to integer.

  1. Round to nearest integer
  2. Convert to float
  3. Convert to integer

Answer:

  1. Convert to float
  2. Round to nearest integer
  3. Convert to integer

Explanation: First, convert the string to a float, then round it to the nearest integer, and finally convert it to an integer.

Question 19: The ______ function is used to convert a string representation of a number to an integer.

Answer: int

Explanation: The int function is used to convert a string representation of a number to an integer.

Question 20: The ______ function is used to convert a string representation of a number to a floating-point number.

Answer: str

Explanation: The float function is used to convert a string representation of a number to a floating-point number.

Question 21: Sort the following type casts by their data loss risk, starting with the highest: int(10.99), float(10), str(10).

Answer:

  1. 1. int(10.99) (highest data loss risk)
  2. float(10)
  3. str(10) (lowest data loss risk)

Explanation: Casting 10.99 to an integer involves the highest data loss risk due to truncation. Casting 10 to a float or string does not lose data.

Question 22: Fill in the missing code to cast the float 3.14 to an integer.

number = int(______)

Answer: 3.14

Explanation: The correct code is number = int(3.14), which truncates the decimal part and returns 3.

Question 23: Fill in the missing code to cast the integer 42 to a string.

result = str(______)

Answer: 42

Explanation: The correct code is result = str(42), which converts the integer 42 to the string "42".

Question 24: Insert the correct function to convert the string "100" to an integer.

number = ______("100")

Answer: int

Explanation: The correct code is number = int("100"), which converts the string "100" to the integer 100.

Question 25: Insert the correct function to convert the integer 7 to a floating-point number.

number = ______(7)

Answer: float

Explanation: The correct code is number = float(7), which converts the integer 7 to the floating-point number 7.0.



Become a Patron!

Follow us on Facebook and Twitter for latest update.