PCEP-30-02 Exam: Mastering Python's print() and input() Functions
PCEP Certification Practice Test - Questions, Answers and Explanations
These questions cover the topic of performing input/output operations using the print() and input() functions in Python, providing a comprehensive preparation for the PCEP examination.
Question 1: What will be the output of the following code?
print("Hello", "World", sep="-")
- Hello World
- Hello-World
- HelloWorld
- Hello, World
Answer: B) Hello-World
Explanation: The sep parameter in print() defines the separator between the values. Here, "Hello" and "World" are separated by a hyphen (-).
Question 2: Which of the following statements about the input() function are correct? (Choose all that apply)
- It always returns user input as a string.
- It can accept a prompt string that is displayed to the user.
- It reads a single line of text from the user.
- It can return input as an integer if the input is numeric.
Answer:
- It always returns user input as a string.
- It can accept a prompt string that is displayed to the user.
- It reads a single line of text from the user.
Explanation: The input() function returns user input as a string, can display a prompt, and reads a single line of text. It does not automatically convert input to an integer, even if the input is numeric.
Question 3: Complete the following code to prompt the user to enter their name and then print it.
name = input("Enter your name: ") print("Hello, ______")▼
Answer: name
Explanation: The user's input, stored in the variable name, is used in the print() statement to greet the user.
Question 4: What will be the output of the following code?
print("Python", end="!") print("Rocks")
- Python Rocks!
- Python Rocks
- Python! Rocks
- Python!Rocks
Answer: D) Python!Rocks
Explanation: The end="!" argument in the first print() statement changes the default newline to an exclamation mark, so the second print() continues on the same line immediately after the exclamation mark.
Question 5: Insert the appropriate function to ask the user for their age and store it in the variable age.
age = ______("Enter your age: ") print("Your age is", age)▼
Answer: input
Explanation: The input() function is used to prompt the user for their age, and the entered value is stored as a string in the variable age.
Question 6: What will happen if you use input() to get numeric input and try to add it directly to an integer?
num = input("Enter a number: ") print(num + 5)
- The number is added to 5 correctly.
- The code raises a TypeError.
- The number is concatenated with 5.
- The code raises a ValueError.
Answer: B) The code raises a TypeError.
Explanation: The input() function returns a string, so trying to add it directly to an integer will result in a TypeError because you cannot add a string and an integer.
Question 7: Which of the following are valid uses of the print() function? (Choose all that apply).
print("Hello", "World", sep=", ")
print("The result is", 10 + 5)
print(input("Enter a value: "))
print("Line 1", end="") print("Line 2")
Answer: A), B), D), and C)
Explanation: All these examples demonstrate valid uses of the print() function, including using sep and end parameters, performing calculations, and printing user input.
Question 8: Arrange the steps to write a program that asks the user for two numbers and prints their sum.
- Get the second number using input().
- Convert the numbers to integers.
- Print the sum of the two numbers.
- Get the first number using input().
Answer: 4, 1, 2, 3
Explanation: First, get the two numbers using input(), then convert them to integers, and finally, print their sum.
Question 9: Complete the following code to print multiple items on the same line separated by a comma.
print("apple", "banana", "cherry", ______=", ")▼
Answer: sep
Explanation: The sep parameter is used to define the separator between the items printed, in this case, a comma followed by a space.
Question 10: What will be the output of the following code?
print("Learning", "Python", sep="*", end="-") print("is fun!")
- Learning*Python is fun!-
- Learning*Python-is fun!
- Learning*Python-is fun!
- Learning Python is fun!
Answer: C) Learning*Python-is fun!
Explanation: The sep="*" parameter separates "Learning" and "Python" with an asterisk. The end="-" parameter appends a hyphen after "Python", and the next print() statement continues on the same line.
Question 11: Insert the correct function to read a user's input and immediately print it.
print(______())▼
Answer: input
Explanation: The input() function is used to read the user's input. The result is immediately printed by the print() function.
Question 12: What is the output of the following code?
print("Hello\nWorld")
- Hello World
- Hello\nWorld
Hello World
- Hello-World
Answer: C)
Explanation: The \n escape sequence in the string causes a newline, so "Hello" and "World" are printed on separate lines.
Question 13: Which of the following will cause a TypeError? (Choose all that apply)
- print(input() + 10)
- print("Age: " + input("Enter age: "))
- print("Total:", int(input()) + 5)
- print(5, "times", 2)
Answer: A)
Explanation: Option A will cause a TypeError because the input() function returns a string, and adding a string to an integer without conversion will raise an error. The other options are valid.
Question 14: Arrange the steps to create a program that asks for the user's first and last names and prints them on the same line.
- Print the first and last names on the same line.
- Get the first name using input().
- Get the last name using input().
Answer: 2, 3, 1
Explanation: First, get the user's first and last names using input(), then print them on the same line using print().
Question 15: Complete the following code to prompt the user to enter their favorite color and then print a message including the color.
color = input("Enter your favorite color: ") print("Your favorite color is ______")▼
Answer: color
Explanation: The variable color holds the user's input and is used in the print() function to display the message.
Question 16: What will be the output of the following code?
print("A", "B", "C", sep="-", end="!") print("D")
- A-B-C!D
- A-B-C!-D
- A-B-C-D
- A B C!D
Answer: A) A-B-C!D
Explanation: The sep="-" parameter separates "A", "B", and "C" with hyphens. The end="!" parameter appends an exclamation mark, and the second print() continues on the same line.
Question 17: Insert the correct function to convert user input to an integer.
age = int(______()) print("You are", age, "years old")▼
Answer: input
Explanation: The input() function is used to get the user's input, and int() converts the input to an integer.
Question 18: What will the following code output if the user enters 3?
num = input("Enter a number: ") print(num * 2)
- 6
- 33
- 12
- Error
Answer: B) 33
Explanation: The input() function returns a string, so multiplying it by 2 concatenates the string with itself, resulting in 33.
Question 19: Which of the following are valid ways to concatenate strings using print()? (Choose all that apply)
- print("Hello" + "World")
- print("Hello", "World")
- print("Hello" "World")
- print("Hello" + " " + "World")
Answer: A), B), C), and D)
Explanation: All these options are valid ways to concatenate and print strings in Python.
Question 20: Arrange the steps to create a program that prompts the user for a number, multiplies it by 10, and prints the result.
- Multiply the number by 10.
- Convert the user input to an integer.
- Print the result
- Prompt the user for a number using input().
Answer: 4, 2, 1, 3
Explanation: First, prompt the user for a number, convert the input to an integer, multiply it by 10, and finally print the result.
Question 21: Complete the following code to print a string and an integer on the same line.
number = 7 print("The number is", ______)▼
Answer: number
Explanation: The variable number holds the integer, and it is printed on the same line as the string "The number is".
Question 22: What will be the output of the following code?
fruit = "apple" print(fruit, "juice")
- apple juice
- applejuice
- apple, juice
- apple - juice
Answer: A) apple juice
Explanation: The print() function separates the two strings with a space by default.
Question 23: Insert the correct argument to print "End of Line" on the same line after a printed message.
print("Hello", ______="") print("End of Line")▼
Answer: end
Explanation: The end="" argument in the first print() statement ensures that the next print() statement continues on the same line.
Question 24: What is the effect of using sep="" in a print() function?
print("A", "B", "C", sep="")
- It adds a space between the items.
- It adds no space between the items.
- It adds a comma between the items.
- It raises a SyntaxError.
Answer: B) It adds no space between the items.
Explanation: The sep="" parameter removes the default space between items, so the output is ABC.
Question 25: Which of the following will correctly prompt the user for a number and print double its value? (Choose all that apply)
num = int(input("Enter a number: ")) print(num * 2)
num = input("Enter a number: ") print(int(num) * 2)
num = input("Enter a number: ") print(num + num)
-
num = float(input("Enter a number: ")) print(num * 2)
Answer: A), B), and D)
Explanation: Options A, B, and D correctly convert the input to a numeric type (integer or float) before performing the multiplication. Option C concatenates the string num with itself instead of doubling the numeric value.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics