nerdexam
Python_Institute

PCEP-30-02 · Question #93

Consider the following code. ``python languages = ['English', 'Spanish', 'German'] more_languages = ['English', 'Spanish', 'German'] extra_languages = more_languages `` Which statement will print…

The correct answer is A. print(languages == more_languages) C. print(more_languages is extra_languages). A is correct because == compares values (contents), and both languages and more_languages hold identical lists - so they are equal even though they are separate objects in memory. C is correct because extra_languages = more_languages does not create a new list; it makes…

Question

Consider the following code.
languages = ['English', 'Spanish', 'German']
more_languages = ['English', 'Spanish', 'German']
extra_languages = more_languages
Which statement will print True to the monitor? (Choose two.)

Options

  • Aprint(languages == more_languages)
  • Bprint(languages is more_languages)
  • Cprint(more_languages is extra_languages)
  • Dprint(languages is extra_languages)

How the community answered

(21 responses)
  • A
    81% (17)
  • B
    14% (3)
  • D
    5% (1)

Explanation

A is correct because == compares values (contents), and both languages and more_languages hold identical lists - so they are equal even though they are separate objects in memory. C is correct because extra_languages = more_languages does not create a new list; it makes extra_languages an alias pointing to the exact same object as more_languages, so is (which checks object identity) returns True.

B is wrong because languages and more_languages were created independently with separate [...] literals, giving them different memory addresses despite holding equal values. D is wrong for the same reason - languages is a distinct object from the one more_languages and extra_languages both reference.

Memory tip: Think of == as asking "same contents?" and is as asking "same object?" - assignment (=) shares a reference, while a new [...] literal always creates a brand-new object.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice