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

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

A program calculates the total price including tax: total = price * 1.08. The variable price is assigned as price = 100.0. After execution, total is 108.0. The developer then changes price to 100. What will total be?

⚠ Common exam trap

Python Institute often tests the misconception that changing an integer literal to a float literal changes the result type, when in fact the result type is determined by the wider operand (the float 1.08) regardless of whether price is 100 or 100.0.

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

108.0

In Python, multiplying an integer (100) by a float (1.08) results in a float (108.0). The expression `price * 1.08` performs implicit type coercion to float, so even when `price` is changed from 100.0 to 100, the result remains 108.0, not 108.

Answer analysis

Option-by-option breakdown

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

  • The program will raise a TypeError

    Why it's wrong here

    Incorrect: Python allows mixed type arithmetic.

  • It depends on the Python version

    Why it's wrong here

    Incorrect: behavior is consistent across Python 3.

  • 108.0

    Why this is correct

    Correct: float * int yields float.

  • 108

    Why it's wrong here

    Incorrect: total will be a float, not int.

About these practice questions

One of 498 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

2 more ways this is tested on PCEP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A junior developer is writing a script to calculate the total cost of items in a shopping cart. The script uses variables item_price (float) and quantity (int). The code is: item_price = 2.5 quantity = 3 total = item_price * quantity print("Total: " + total) When run, this code raises a TypeError. The developer is confused because the multiplication seems correct. What is the most likely issue and the correct fix?

easy
  • A.The variable total is being overwritten by the print statement; use a different variable name.
  • B.The multiplication should be written as int(item_price) * quantity to ensure integer result.
  • C.The variable names are too descriptive; rename them to a and b to avoid confusion.
  • D.The print function cannot concatenate a string with a float; convert total to string using str(total).

Why D: The `print` function in Python expects all arguments to be strings when using the `+` operator for concatenation. The variable `total` is a float (result of multiplying a float by an int), and Python does not implicitly convert it to a string. The error is a `TypeError: can only concatenate str (not 'float') to str`. The fix is to explicitly convert `total` to a string using `str(total)` before concatenation.

Variation 2. A developer wants to output a variable price with two decimal places using formatting. Which line of code will produce 'Price: $12.50' for price = 12.5?

medium
  • A.print('Price: $' + price)
  • B.print(f'Price: ${price:.2f}')
  • C.print('Price: $' + str(round(price, 2)))
  • D.print('Price: $%s' % price)

Why B: It uses an f-string with the format specifier `:.2f`, which formats the float `12.5` as a string with exactly two decimal places, producing '12.50'. The f-string then interpolates this into the full string 'Price: $12.50'.

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.