hiexam
python-institute · PCEP-30-02 · Q606 · multiple_choice · topic_1

Which of the following sentences is true? //IMG//

Which of the following sentences is true? //IMG//
  • A.str1 and str2 are different (but equal) strings.
  • B.str1 and str2 are different names of the same strings.
  • C.str1 is longer than str2
  • D.str2 is longer than str1
Explanation
Selected Answer: A let's analyze what's happening in the given code: str1 = 'Peter' assigns the string 'Peter' to the variable str1. str2 = str1[:] creates a slice of str1 that includes all characters (from beginning to end). This effectively creates a copy of the string. In Python, strings are immutable, which means when you create a copy of a string using slicing, it creates a new string object with the same content. Therefore, the correct answer is: A. str1 and str2 are different (but equal) strings. This is true because: str1 and str2 contain the same characters ('Peter') They are equal in terms of content However, they are separate string objects in memory Options B, C, and D are incorrect: B is wrong because they are not the same string object, but two different objects with the same content. C and D are incorrect because both strings have the same length.

Reference: examtopics_top_comment

Practice with progress tracking

Sign in to track wrong answers, get spaced-repetition reminders, and run timed exam mode.