Courseiva
Question 291 of 498
Functions, Tuples, Dictionaries and ExceptionsmediumMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A large e-commerce platform uses a Python function to calculate the average rating from a tuple of customer ratings. The function is called thousands of times per second with the same ratings tuple (which is static across many calls). The function currently computes sum(ratings) / len(ratings) each time, causing a performance bottleneck. The development team wants to optimize the function without changing its signature (it still takes the tuple as argument). They also want to avoid using global variables or external libraries. Which approach best optimizes the function?

⚠ Common exam trap

The PCEP exam often tests the misconception that Python automatically caches results of built-in functions like sum() on repeated calls, when in fact no such optimization exists and the developer must implement caching manually.

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 a local variable with a simple cache (dictionary) to store sums and lengths for previously seen tuples

It implements memoization: a local dictionary caches the sum and length for each tuple key, avoiding repeated computation of sum() and len() for the same static tuple. This reduces time complexity from O(n) per call to O(1) after the first call, without using globals or external libraries, and without changing the function signature.

Answer analysis

Option-by-option breakdown

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

  • Store the sum and length in global variables

    Why it's wrong here

    Using global variables is error-prone and violates encapsulation.

  • Use the tuple as is; Python internally optimizes repeated sum() calls

    Why it's wrong here

    Python does not automatically cache such results.

  • Use a local variable with a simple cache (dictionary) to store sums and lengths for previously seen tuples

    Why this is correct

    Caching avoids redundant computation and keeps the function self-contained.

  • Convert the tuple to a list and use list operations

    Why it's wrong here

    Conversion adds overhead and does not prevent repeated sum/len calls.

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

Last reviewed: Jul 4, 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.