w3resource

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="-")
  1. Hello World
  2. Hello-World
  3. HelloWorld
  4. 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)

  1. It always returns user input as a string.
  2. It can accept a prompt string that is displayed to the user.
  3. It reads a single line of text from the user.
  4. It can return input as an integer if the input is numeric.

Answer:

  1. It always returns user input as a string.
  2. It can accept a prompt string that is displayed to the user.
  3. 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")
  1. Python Rocks!
  2. Python Rocks
  3. Python! Rocks
  4. 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)
  1. The number is added to 5 correctly.
  2. The code raises a TypeError.
  3. The number is concatenated with 5.
  4. 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).

  1. print("Hello", "World", sep=", ")
  2. print("The result is", 10 + 5)
  3. print(input("Enter a value: "))
  4. 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.

  1. Get the second number using input().
  2. Convert the numbers to integers.
  3. Print the sum of the two numbers.
  4. 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!")
  1. Learning*Python is fun!-
  2. Learning*Python-is fun!
  3. Learning*Python-is fun!
  4. 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")
  1. Hello World
  2. Hello\nWorld
  3. Hello
    World
  4. 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)

  1. print(input() + 10)
  2. print("Age: " + input("Enter age: "))
  3. print("Total:", int(input()) + 5)
  4. 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.

  1. Print the first and last names on the same line.
  2. Get the first name using input().
  3. 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")
  1. A-B-C!D
  2. A-B-C!-D
  3. A-B-C-D
  4. 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)
  1. 6
  2. 33
  3. 12
  4. 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)

  1. print("Hello" + "World")
  2. print("Hello", "World")
  3. print("Hello" "World")
  4. 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.

  1. Multiply the number by 10.
  2. Convert the user input to an integer.
  3. Print the result
  4. 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")
  1. apple juice
  2. applejuice
  3. apple, juice
  4. 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="")
  1. It adds a space between the items.
  2. It adds no space between the items.
  3. It adds a comma between the items.
  4. 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)

  1. num = int(input("Enter a number: "))
    print(num * 2)
    
  2. num = input("Enter a number: ")
    print(int(num) * 2)
    
  3. num = input("Enter a number: ")
    print(num + num)
    
  4. 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.



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-print-and-input-functions.php