Courseiva
StringshardMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

Consider the following code snippet: s = 'abcdefgh'; result = s[7:3:-2]; print(result). What is the output?

⚠ Common exam trap

A common misconception is that a negative step reverses the start and stop indices, leading candidates to incorrectly assume the slice starts at the lower index and moves forward, or that the stop index is inclusive when the step is negative.

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

hf

The slice s[7:3:-2] starts at index 7 (character 'h'), goes backwards with step -2, and stops before index 3. The indices visited are 7 and 5, yielding 'h' and 'f', so the result is 'hf'. Option B is correct because the step is negative, meaning the slice moves from right to left, and the stop index is exclusive.

Answer analysis

Option-by-option breakdown

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

  • fh

    Why it's wrong here

    The slice traverses the string backward because of the negative step, so it starts at the higher index and proceeds to lower indices. Therefore, the first character emitted is 'h' (index 7), not 'f'. To produce 'fh', you would need a positive step from index 5, such as s[5:8:2], which selects 'f' then 'h' in forward order.

  • hf

    Why this is correct

    With s = 'abcdefgh', the slice s[7:3:-2] starts at index 7 (character 'h'), then subtracts 2 to reach index 5 (character 'f'), and stops before index 3 (character 'd') because the stop is exclusive. The step of -2 reverses the traversal direction and skips every other character. Hence the result is exactly 'hf'—first 'h', then 'f'.

  • h

    Why it's wrong here

    To get only 'h', the step magnitude must be large enough that the next index falls outside the stop boundary, or the stop must be chosen to exclude the next candidate. For instance, s[7:3:-4] starts at 7 and jumps to 3, which is excluded as the stop, leaving just 'h'. The given step of -2, however, lands on index 5 and includes it, so 'h' alone is incomplete.

  • hfd

    Why it's wrong here

    This result would require the slice to continue past index 3 and include character 'd' at index 3. For example, s[7:2:-2] yields indices 7, 5, and 3, producing 'hfd'. The actual slice uses a stop of 3, which is exclusive, so 'd' is not selected; a step of -1 over a similar range would also include extra characters, such as 'hgfed' for s[7:2:-1], not 'hfd'.

About these practice questions

Courseiva writes every PCAP question from scratch — 169 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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 PCAP 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 PCAP exam.