PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy

Question 29

Q322 - Data Aggregates


What is the expected output of the following code?



  • A

    1

  • B

    The code is erroneous.

  • C

    0

  • D

    None

Reveal correct answer

Correct answer: C

Explanation

Topics: __len__() tuple

Try it yourself:

Explanation:

Every object has the method __len__()

Normally we use the function len() which calls the method __len__()

And sure, the length of an empty tuple is 0


Q322 (Please refer to this number, if you want to write me about this question.)

A. This choice is incorrect because the tuple 'data' is explicitly initialized as empty, so the length of the tuple will be 0, not 1.

B. This choice is incorrect as the code is not erroneous. It correctly initializes an empty tuple and retrieves its length using the __len__() method. The expected output is 0, not an error.

C. The code initializes an empty tuple named 'data' and then calls the __len__() method on it to get the length of the tuple. Since the tuple is empty, the length returned will be 0, making Choice A the correct answer.

D. This choice is incorrect because calling the __len__() method on an empty tuple will return an integer value (0) representing the length of the tuple, not None.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need