Courseiva
Data Types, Variables, Basic I/O and OperatorshardMultiple ChoiceObjective-mapped

PCEP Practice Question: Data Types, Variables, Basic I/O and Operators

A developer is building a simple calculator that accepts two numbers and an operator string. The code:

x = float(input("First: ")) y = float(input("Second: ")) op = input("Operator (+, -, *, /): ")

if op == "+":

result = x + y elif op == "-": result = x - y elif op == "*": result = x * y elif op == "/": result = x / y

print("Result:", result)

When the user enters 10, 3, and "/", the output is "Result: 3.3333333333333335". The developer wants to display only two decimal places. Which code change will achieve this without introducing errors?

⚠ Common exam trap

Python Institute often tests the distinction between rounding a number (which returns a float) and formatting a number for display (which returns a string), and the trap here is that candidates may think `round()` solves the display problem, but it does not guarantee a fixed number of decimal places in the output.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Change print to: print("Result:", format(result, ".2f"))

The `format()` function with the format specifier `.2f` converts the floating-point result to a string rounded to two decimal places, which is exactly what the developer needs. This approach does not alter the original variable and works reliably for display purposes.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Change print to: print("Result:", format(result, ".2f"))

    Why this is correct

    format with '.2f' always shows exactly two decimal places.

  • Change print to: print("Result: " + str(result)[:4])

    Why it's wrong here

    Slicing may truncate incorrectly for larger numbers.

  • Change print to: print("Result:", result.toFixed(2))

    Why it's wrong here

    toFixed() is not a Python method; it's JavaScript.

  • Change print to: print("Result:", round(result, 2))

    Why it's wrong here

    round() may not display trailing zeros, e.g., 3.5 becomes 3.5 not 3.50.

About these practice questions

This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.