Courseiva

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

What is the output of the following code?

print(3 * 'ab' + 'c')

⚠ Common exam trap

Python Institute often tests the order of operations and the fact that `*` binds tighter than `+` in Python, leading candidates to mistakenly think the expression is evaluated as `3 * ('ab' + 'c')` or to forget that string repetition produces a single concatenated string without separators.

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

✓

'abababc'

The expression `3 * 'ab' + 'c'` first multiplies the string `'ab'` by 3, resulting in `'ababab'` (string repetition), and then concatenates `'c'` using the `+` operator, producing `'abababc'`. In Python, the `*` operator on a string and an integer repeats the string that many times, and `+` concatenates strings.

Answer analysis

Option-by-option breakdown

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

  • ✓

    'abababc'

    Why this is correct

    String repetition binds tighter than concatenation, so `3 * 'ab'` evaluates first, yielding `'ababab'`. The `+` operator then joins that result with `'c'`, producing `'abababc'`. This satisfies the stem's single print statement, where no parentheses alter the default left-to-right precedence of `*` over `+`.

  • ✗

    'ababc'

    Why it's wrong here

    Python's string repetition binds tighter than concatenation, so 3 * 'ab' yields 'ababab' before 'c' is appended, giving 'abababc'. This option drops one repetition, as though the multiplier were 2. Repetition of a string literal is genuinely useful for padding or separators, but here the arithmetic is simply miscounted.

  • ✗

    TypeError

    Why it's wrong here

    Python repeats a string when multiplied by an integer, so 3 * 'ab' yields 'ababab' and concatenation with 'c' gives 'abababc'. A TypeError arises only from multiplying a string by another string, such as 'ab' * 'c'. The option tempts because invalid operand types do raise TypeError in other combinations.

  • ✗

    'ababab c'

    Why it's wrong here

    Python's * operator repeats a string with no inserted delimiter, so 3 * 'ab' produces 'ababab', not 'ababab ' with a trailing space. The space appears to come from imagining print separates operands, yet concatenation via + joins directly. Repetition suits building fixed-width padding, where a separator is deliberately included.

About these practice questions

One of 482 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

1 more way 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. What is the output of 'print(3 * "ab")'?

medium
  • A.ab3
  • B.3ab
  • ✓ C.ababab
  • D.Error

Why C: In Python, the multiplication operator (*) when used with a string and an integer performs string repetition. The expression 3 * "ab" repeats the string "ab" three times, concatenating the copies into a single string "ababab". The print() function then outputs this resulting string to the console.

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.