PCEP-30-02 Exam QuestionsBrowse all questions from this exam

PCEP-30-02 Exam - Question 4


What is the expected output of the following code?

Show Answer
Correct Answer: C

When the code executes, list1 and list2 both refer to the same list object in memory. Therefore, any modification to list1 will also reflect in list2. When list1[0] is assigned the value 4, the change is also reflected in list2. As a result, printing list2 will output [4, 3].

Discussion

7 comments
Sign in to comment
herrmann69Option: C
Jun 4, 2024

C is correct. list1 and list2 point to the same location in memory.

hovnivalOption: C
Dec 20, 2024

C. [4, 3] Explanation: list1 = [1, 3] This creates a list list1 with elements [1, 3]. list2 = list1 This does not create a new list. Instead, list2 is a reference to the same list object that list1 points to. Both list1 and list2 now refer to the same memory location. list1[0] = 4 This modifies the first element of the list referred to by list1 (and list2, since they refer to the same object). The list is now [4, 3]. print(list2) Since list2 refers to the same list as list1, it will also reflect the change. Therefore, the output will be [4, 3].

consultskOption: C
Sep 30, 2024

The code performs the following steps: 1. `list1 = [1, 3]`: Initializes `list1` with the elements `[1, 3]`. 2. `list2 = list1`: Assigns `list2` to refer to the same list object as `list1`. Now both `list1` and `list2` refer to the same list in memory. 3. `list1[0] = 4`: Modifies the first element of `list1` to be `4`. Since `list2` refers to the same list, this modification is reflected in `list2` as well. 4. `print(list2)`: Prints `list2`. Since `list2` refers to the same list as `list1`, after modifying `list1`, `list2` will also reflect the changes. **Expected output:** `[4, 3]`

christostz03
Aug 20, 2024

c is the correct answer

megan_maiOption: C
Aug 25, 2024

After assigning list2 = list 1 >> both refer to the same list in memory >> changing list1 value will affect list2 value >> answer is [4,3]

Vihaan_COption: C
Nov 3, 2024

list1=[1,3] list2=list1 which equals to [1,3] list1=[4,3] and list2=[4,3]

ChandizOption: C
Apr 16, 2025

list1[0] = 4 # modifies the shared list