PCEP sorted() Practice Question
Which code sorts a list of strings by their length in descending order?
lst = ['aa', 'b', 'ccc']
⚠ Common exam trap
A common trap is assuming `reverse=True` alone sorts by length; it actually sorts alphabetically. Another trap is thinking a lambda is required when the built-in `len` works directly as the key.
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
✓
sorted(lst, key=len, reverse=True)
Option A is correct. `sorted(lst, key=len, reverse=True)` sorts the list by string length in descending order. Although option C (`sorted(lst, key=lambda s: len(s), reverse=True)`) would also produce the same result, this single-answer question expects the most straightforward and Pythonic solution, which is using the built-in `len` function directly without an unnecessary lambda. Option B sorts alphabetically in reverse order, not by length. Option D sorts by length in ascending order.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
sorted(lst, key=len, reverse=True)
Why this is correct
Correct: Uses `key=len` to sort by length and `reverse=True` for descending order.
- ✗
sorted(lst, reverse=True)
Why it's wrong here
Incorrect: Sorts alphabetically in reverse order, not by length.
- ✗
sorted(lst, key=lambda s: len(s), reverse=True)
Why it's wrong here
Incorrect in this context: While it produces the same result as A, using a lambda for `len` is redundant; the simplest correct answer is A.
- ✗
sorted(lst, key=lambda s: len(s))
Why it's wrong here
Incorrect: Sorts by length in ascending order because `reverse` defaults to False.
Quick reference
Cloud Service Model Comparison
| Model | You Manage | Provider Manages | Examples |
|---|---|---|---|
| IaaS | OS, runtime, apps, data | Hardware, hypervisor, networking | EC2, Azure VMs, GCP Compute Engine |
| PaaS | Apps and data | OS, runtime, middleware, hardware | Elastic Beanstalk, Azure App Service |
| SaaS | Data and settings only | Everything else | Microsoft 365, Salesforce, Workday |
| FaaS / Serverless | Function code only | Infra, scaling, runtime | Lambda, Azure Functions, Cloud Run |
| CaaS | Containers and apps | Kubernetes, OS, hardware | EKS, AKS, GKE |
Go deeper
Related to this question
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 →
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. Which TWO of the following are valid ways to iterate over a list in reverse order? (Choose two.)
easy- ✓ A.for item in reversed(lst):
- ✓ B.for i in range(len(lst)-1, -1, -1):
- C.for i in range(len(lst)):
- D.for item in lst[-1::-1]:
- E.for item in lst:
Why A: `reversed(lst)` returns an iterator that yields elements in reverse order without modifying the original list. Option B is correct because `range(len(lst)-1, -1, -1)` generates indices from the last to the first, allowing index-based access. These are the two valid methods according to the PCEP certification.
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.