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

Question 33

Q611 - Operators


What is the expected output of the following code?



  • A
  • B
  • C
  • D
Reveal correct answer

Correct answer: A

Explanation

Topics: list equal to operator identity operator

Try it yourself:

Explanation:

A list is a mutable data type.

The second list will be a different object, although the values are the same.

When list2 gets assigned to list1 Python creates a reference.

From now on the two lists have the same identity and the same values.

(There are similar questions Q433 and Q514.)


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

A. The 'is' operator checks if two variables point to the same object in memory. Since 'list1' and 'list2' are two separate lists with the same values, the 'is' operator will return False. The '==' operator, on the other hand, checks if the values of the two lists are the same, so it will return True for this case. After assigning 'list2' to 'list1', both variables point to the same list object in memory, so 'list1 is list2' will be True, and 'list1 == list2' will also be True.

B. The 'is' operator checks if two variables point to the same object in memory. Since 'list1' and 'list2' are two separate lists with the same values, the 'is' operator will return False. The '==' operator checks if the values of the two lists are the same, so it will return True for this case. After assigning 'list2' to 'list1', both variables point to the same list object in memory, so 'list1 is list2' will be True, but 'list1 == list2' will be False because 'list1' and 'list2' are now the same list object.

C. The 'is' operator checks if two variables point to the same object in memory. Since 'list1' and 'list2' are two separate lists with the same values, the 'is' operator will return False. Both 'list1' and 'list2' have the same values, so 'list1 == list2' will be True. After assigning 'list2' to 'list1', both variables point to the same list object in memory, so 'list1 is list2' will be True, and 'list1 == list2' will also be True.

D. The 'is' operator checks if two variables point to the same object in memory. Since 'list1' and 'list2' are two separate lists with the same values, the 'is' operator will return False. The '==' operator checks if the values of the two lists are the same, so it will return True for this case. After assigning 'list2' to 'list1', both variables point to the same list object in memory, so 'list1 is list2' will be True, but 'list1 == list2' will be False because 'list1' and 'list2' are now the same list object.

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