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

Question 10

Q109 - Data Aggregates


What is the expected output of the following code?



  • A

    [1, 3]

  • B

    [1, 3, 4]

  • C

    [4, 3]

  • D

    [1, 4]

Reveal correct answer

Correct answer: C

Explanation

Topics: list reference of a mutable data type

Try it yourself:

Explanation:

A list is mutable.

When you assign it to a different variable,

you create a reference of the same object.

If afterwards you change one of them, the other one is changed too.


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

A. The output will not be [1, 3] because the value at index 0 of list1 is modified to 4 before printing list2. Since list2 is a reference to list1, the change in list1 affects list2 as well.

B. The output will not be [1, 3, 4] because only the value at index 0 of list1 is modified to 4. The length of list2 remains the same as list1, so the output will not include an additional element.

C. The output will be [4, 3] because when list2 is assigned to list1, they both refer to the same list object in memory. Therefore, when the value at index 0 of list1 is changed to 4, it also reflects in list2.

D. The output will not be [1, 4] because the value at index 0 of list1 is changed to 4, not 1. As list2 is a reference to list1, the change is reflected in list2 as well.

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