nerdexam
Python_Institute

PCEP-30-02 · Question #25

Which of the following sentences are true about the code? (Choose two.) ``python 1 nums = [1, 2, 3] 2 vals = nums ``

The correct answer is C. nums and vals are different names of the same list D. nums and vals have the same length. In Python, vals = nums does not create a new list - it creates a second name (reference) pointing to the exact same list object in memory. So C is correct: nums and vals are two names for one list. Since they're the same object, they trivially have the same length, making D…

Question

Which of the following sentences are true about the code? (Choose two.)
1 nums = [1, 2, 3]
2 vals = nums

Options

  • Anums and vals are different lists
  • Bvals is longer than nums
  • Cnums and vals are different names of the same list
  • Dnums and vals have the same length

How the community answered

(24 responses)
  • A
    17% (4)
  • B
    8% (2)
  • C
    75% (18)

Explanation

In Python, vals = nums does not create a new list - it creates a second name (reference) pointing to the exact same list object in memory. So C is correct: nums and vals are two names for one list. Since they're the same object, they trivially have the same length, making D also correct.

Why A is wrong: A would be true if you wrote vals = nums.copy() or vals = nums[:], which actually creates a second list. A plain assignment never copies.

Why B is wrong: vals can't be longer - it is nums. No elements were added.

Memory tip: Think of the variable as a sticky label on a box. vals = nums just sticks a second label on the same box - you now have two labels, one box. To get a second box (copy), you need .copy().

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice