w3resource

PCEP-30-02 Exam: Python I/O Operations - int() and float() functions

PCEP Certification Practice Test - Questions, Answers and Explanations

This comprehensive set of questions and explanations covers the topic of performing Input/Output console operations with a focus on the int() and float() functions. These exercises will help reinforce your understanding and preparation for the PCEP-30-02 examination.

Question 1: What will be the output of the following code?

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

Answer: a) 3

Explanation: The int() function truncates the decimal part of the floating-point number, resulting in 3.

Question 2: What will be the result of the following code?

print(float(7))
  1. 7
  2. 7.0
  3. 7.00
  4. Error

Answer: b) 7.0

Explanation: The float() function converts an integer to a floating-point number, appending .0 to the integer.

Question 3: Which of the following conversions is valid?

value = int("123")
  1. Converts string "123" to integer 123
  2. Converts string "123" to float 123.0
  3. Converts integer 123 to string "123"
  4. Converts float 123.0 to integer 123

Answer: a) Converts string "123" to integer 123

Explanation: The int() function can convert a string representing an integer to an actual integer value.

Question 4: What will be the result of the following code?

print(int("12.34"))
  1. 12
  2. 12.34
  3. Error
  4. 1234

Answer: c) Error

Explanation: The int() function cannot directly convert a string containing a decimal point to an integer, resulting in a ValueError.

Question 5: Which of the following statements will correctly convert the string "456" to a number? (Select all that apply)

  1. int("456")
  2. float("456")
  3. int(456)
  4. float(456)

Answer: a) int("456"), b) float("456")

Explanation: The int() function converts the string "456" to the integer 456, and the float() function converts the string "456" to the floating-point number 456.0.

Question 6: Which of the following statements will raise an error? (Select all that apply)

  1. int("abc")
  2. float("1.23.45")
  3. int("123")
  4. float("123.45")

Answer: a) int("abc"), b) float("1.23.45")

Explanation: The int() function will raise a ValueError when attempting to convert a non-numeric string, and the float() function will raise a ValueError when attempting to convert a string with an invalid float format.

Question 7: Arrange the following steps in the correct order to convert a string representing a floating-point number into an integer:

  1. Convert the float to an integer.
  2. Convert the string to a float.

Answer:

  1. Convert the string to a float.
  2. Convert the float to an integer.

Explanation: First, you need to convert the string to a float using float(), then convert the float to an integer using int().

Question 8: The ______ function is used to convert a floating-point number to an integer by removing the decimal part.

Answer: int

Explanation: The int() function truncates the decimal part and converts the floating-point number to an integer.

Question 9: The ______ function converts a string representing a number to a floating-point number.

Answer: float()

Explanation: The float() function is used to convert a string that represents a number to a floating-point number.

Question 10: Sort the following code snippets to output the integer 5:

  1. number = int(number)
  2. number = "5.9"
  3. number = float(number)

Answer:

  1. number = "5.9"
  2. number = float(number)
  3. number = int(number)

Explanation: First, assign the string "5.9" to number, then convert it to a float using float(), and finally convert the float to an integer using int().

Question 11: Fill in the missing code to convert the string "8.25" to an integer.

number = int(float("______"))

Answer: "8.25"

Explanation: The correct code is number = int(float("8.25")), which first converts the string "8.25" to a float, then to an integer, resulting in 8.

Question 12: Fill in the missing code to convert the integer 10 to a floating-point number.

number = float(______)

Answer: 10

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

Question 13: Insert the correct function to convert the string "3.14159" to a floating-point number.

number = ______("3.14159")

Answer: float

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

Question 14: Insert the correct function to convert the integer 42 to a floating-point number.

number = ______(42)

Answer: float

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

Question 15: What will be the result of the following code?

print(int(9.99))
  1. 9
  2. 10
  3. 9.99
  4. Error

Answer: a) 9

Explanation: The int() function truncates the decimal part of the floating-point number, resulting in 9.

Question 16: What will be the output of the following code?

print(float("2.718"))
  1. 2
  2. 2.7
  3. 2.718
  4. Error

Answer: c) 2.718

Explanation: The float() function converts the string "2.718" to the floating-point number 2.718.

Question 17: Which of the following will convert the integer 123 to a floating-point number? (Select all that apply)

  1. float(123)
  2. int(123.0)
  3. float("123")
  4. float("123.0")

Answer: a) float(123), c) float("123"), d) float("123.0")

Explanation: The float() function can convert both an integer and a string representing an integer or float to a floating-point number.

Question 18: Which of the following statements will correctly convert the string "5" to an integer? (Select all that apply)

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

Answer: a) int("5"), b) int(5.0), c) int(float("5"))

Explanation: The int() function can convert a string representing an integer, a floating-point number, or a float-converted string to an integer. Option d) will raise a ValueError.

Question 19: Arrange the following steps to convert the string "10.5" to an integer:

  1. Convert the float to an integer.
  2. Convert the string to a float.

Answer:

  1. Convert the string to a float.
  2. Convert the float to an integer.

Explanation: First, convert the string "10.5" to a float using float(), then convert it to an integer using int().

Question 20: The ______ function converts a string or an integer to a floating-point number.

Answer: float()

Explanation: The float() function is used to convert a string representing a number or an integer to a floating-point number.

Question 21: The ______ function is used to truncate a floating-point number to an integer.

Answer: int()

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

Question 22: Sort the following code snippets to output the floating-point number 12.34:

  1. value = float(value)
  2. value = "12.34"

Answer:

  1. value = "12.34"
  2. value = float(value)

Explanation: First, assign the string "12.34" to value, then convert it to a float using float().

Question 23: Fill in the missing code to convert the string "7.89" to an integer after converting it to a float.

number = int(float("______"))

Answer: "7.89"

Explanation: The correct code is number = int(float("7.89")), which first converts the string "7.89" to a float, then to an integer, resulting in 7.

Question 24: Fill in the missing code to convert the string "123" to an integer.

number = int("______")

Answer: "123"

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

Question 25: Insert the correct function to convert the string "3.5" to an integer after converting it to a float.

number = int(______("3.5"))

Answer: float

Explanation: The correct code is number = int(float("3.5")), which first converts the string "3.5" to a float, then to an integer, resulting in 3.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python/certificate/perform-input-output-console-operations-int-and-float-functions.php