exam questions

Exam PCEP-30-02 All Questions

View all questions & answers for the PCEP-30-02 exam

Exam PCEP-30-02 topic 1 question 13 discussion

Actual exam question from Python Institute's PCEP-30-02
Question #: 13
Topic #: 1
[All PCEP-30-02 Questions]

Which of the following sentences is true?

  • 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
Show Suggested Answer Hide Answer
Suggested Answer: A 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
consultsk
Highly Voted 9 months, 1 week ago
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.
upvoted 7 times
...
Nandhu22
Most Recent 2 weeks, 3 days ago
Selected Answer: B
if we print the id of both str1 and str2 it will give the same id. Hence, both are referring to the same memory location, which means both are sharing the same memory. Hence, B is the correct answer
upvoted 1 times
...
janewong
2 weeks, 5 days ago
Selected Answer: A
str2 = str1[:] is to copy everything from str2, answer = A, different (but equal) strings. if str2 = str1, answer = B, there are same string but different name.
upvoted 1 times
...
shyemko
2 weeks, 6 days ago
Selected Answer: B
Different name but point to same string, B is the correct answer
upvoted 1 times
...
a9f59a8
2 months, 2 weeks ago
Selected Answer: B
str = "Peter" str2 = str[:] print(id(str)) print(id(str2)) the ID is identical , so B
upvoted 1 times
...
Billybob0604
6 months, 3 weeks ago
Selected Answer: B
The answer is B. For sure. Since strings are immutable and they have the same contents they both refer to the same memory location.
upvoted 2 times
...
Vano1
8 months, 2 weeks ago
Selected Answer: A
Since there is a slicing the str2 made a copy of str1.
upvoted 1 times
...
FedeXD
9 months, 2 weeks ago
A is correct. Slicing creates a copy of the string. Therefore, they are different strings, but if you compare them for equality you the result will be True as they have the same content.
upvoted 2 times
...
sandy589
9 months, 3 weeks ago
B is the correct answer, because their memory addresses are same if you check in your IDE. hence B. str1 and str2 are different names of the same strings.
upvoted 1 times
...
christostz03
10 months, 3 weeks ago
b is the correct answer
upvoted 1 times
...
froster02
11 months, 3 weeks ago
str1 = 'Peter' str2 = str1 print(id(str1), id(str2)) #same memory location : 123262391172144, 123262391172144 str2 = 'hell' print(str1, str2) print(id(str1), id(str2)) #on manipulation memory changes : 123262391172144, 123262391705072 hence, Str1 & Str2 are same Strings but different name.
upvoted 2 times
Vano1
8 months, 1 week ago
Please pay attention to the slice [:] at the end of the second row. It copies the content of the first str to another memory address. Therefore, the right answer is A.
upvoted 1 times
...
...
nieups
12 months ago
Selected Answer: B
B is the answer
upvoted 3 times
...
Sanela
1 year, 1 month ago
The correct answer is A, since str2 get all the elements from str1, but it's a new variable, new string. If you change str1, str2 remains unchanged
upvoted 2 times
Sanela
1 year, 1 month ago
Since there is no change made, they refer to the same string (have same id, I checked it). So I must correct my previous answer, it should be B
upvoted 2 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...