Question 272 of 510
Data Types, Variables, Basic I/O and OperatorsmediumMultiple ChoiceObjective-mapped

Quick Answer

The answer is to use `total = sum(map(int, sales))`. This approach is correct because the `sum()` function in Python requires numeric operands; passing a list of strings directly, as in `sum(sales)`, raises a `TypeError` since Python cannot add strings numerically. By applying `map(int, sales)`, you convert each string element to an integer before summation, allowing `sum()` to compute the total as an integer. On the Certified Entry-Level Python Programmer PCEP exam, this question tests your understanding of type conversion and the `sum()` function’s strict requirement for numeric input—a common trap is assuming `sum()` can implicitly handle strings. The key insight is that `sum()` works on iterables of numbers, not strings, so you must explicitly convert using `map()` or a generator expression. Memory tip: think “map to int before sum” to avoid the TypeError trap.

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

This PCEP practice question tests your understanding of data types, variables, basic i/o and operators. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

You are a data analyst at a retail company. You have a list of sales figures stored as strings in a list: sales = ['100', '200', '300']. You need to calculate the total sum. A colleague suggests using: total = sum(sales). However, this raises a TypeError because sum() requires numeric values. Which approach should you take to correctly calculate the total as an integer?

Question 1mediummultiple choice
Full question →

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

Use total = sum(int(s) for s in sales)

Option B is correct because it uses a generator expression to convert each string element in the `sales` list to an integer via `int(s)` before passing the resulting integers to `sum()`. The `sum()` function requires numeric operands; passing strings directly causes a `TypeError`. By converting to integers first, the total is correctly computed as an integer.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

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

  • Use total = sum(sales) and catch TypeError

    Why it's wrong here

    Still fails; conversion needed.

  • Use total = sum(int(s) for s in sales)

    Why this is correct

    Generator expression converts each to int and sums.

    Related concept

    Read the scenario before looking for a memorised answer.

  • Use total = sum(map(int, sales))

    Why this is correct

    map applies int to each element, sum works.

    Related concept

    Read the scenario before looking for a memorised answer.

  • Use total = 0; for s in sales: total += s

    Why it's wrong here

    This concatenates strings, not adds numbers.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the misconception that `sum()` can implicitly convert strings to numbers, or that catching an exception is a valid workaround, when in fact explicit conversion via `int()` is required.

Detailed technical explanation

How to think about this question

Under the hood, `sum()` iterates over its argument and uses the `+` operator to accumulate values. Since Python strings support concatenation with `+`, but not addition with integers, the `TypeError` arises from type mismatch. Using `map(int, sales)` applies the `int()` function to each element lazily, producing an iterator of integers that `sum()` can process efficiently without creating an intermediate list.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A practitioner preparing for the PCEP exam encounters this exact type of scenario on the job. The correct answer here is not the most general option — it is the best answer for the specific constraint described. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Real exam questions reward reading the full scenario before eliminating options, because the constraint defines which answer fits.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related PCEP practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free PCEP practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this PCEP question test?

Data Types, Variables, Basic I/O and Operators — This question tests Data Types, Variables, Basic I/O and Operators — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: Use total = sum(int(s) for s in sales) — Option B is correct because it uses a generator expression to convert each string element in the `sales` list to an integer via `int(s)` before passing the resulting integers to `sum()`. The `sum()` function requires numeric operands; passing strings directly causes a `TypeError`. By converting to integers first, the total is correctly computed as an integer.

What should I do if I get this PCEP question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, 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

1 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 data analyst uses Python to process a CSV file containing sales data. The file has columns: 'Product', 'Price', 'Quantity'. The analyst writes a script to compute total sales: sum of Price * Quantity for each row. The code reads each row as a list of strings. The analyst uses: total = 0; for row in reader: total += row['Price'] * row['Quantity']; print(total). The script raises a TypeError. What is the best fix?

hard
  • A.Convert Price and Quantity to float before multiplication.
  • B.Change the loop to for i in range(len(reader)): total += reader[i][1] * reader[i][2].
  • C.Use integer multiplication and then convert to float.
  • D.Use float(row[1]) * float(row[2]) instead of row['Price'] * row['Quantity'].

Why A: Option A is correct because the script attempts to multiply two strings (row['Price'] and row['Quantity']), which raises a TypeError. Converting both values to float before multiplication ensures numeric arithmetic. This directly addresses the type mismatch in the original code.

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.