Courseiva
Computer Programming and Python FundamentalsmediumMultiple ChoiceObjective-mapped

PCEP Computer Programming and Python Fundamentals Practice Question

A developer wants to extract the file extension from a filename: 'report.pdf'. Which string method will return 'pdf'?

⚠ Common exam trap

Many candidates choose option C because it works for simple cases like `'report.pdf'`, but The PCEP exam often tests the edge case of filenames with multiple dots to catch those who do not use `[-1]` for the last element.

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

'report.pdf'.split('.')[-1]

The `split('.')` method divides the string at each period, returning a list `['report', 'pdf']`. Accessing index `-1` retrieves the last element, which is the file extension `'pdf'`. This is a common Python idiom for extracting file extensions.

Answer analysis

Option-by-option breakdown

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

  • 'report.pdf'.replace('.', '')

    Why it's wrong here

    Returns 'reportpdf'.

  • 'report.pdf'.split('.')[-1]

    Why this is correct

    Returns 'pdf'.

  • 'report.pdf'.split('.')[1]

    Why it's wrong here

    Also returns 'pdf' but only works with one dot.

  • 'report.pdf'.removeprefix('report.')

    Why it's wrong here

    Removes prefix, not safe.

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.