Test Index

Python Revision Tour Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 97 of 102
Marks: +1, -0
Which of the following statement(s) would give an error after executing the following code ?
S="Happy"              # Statement 1
print(S*2) # Statement 2
S+="Independence" # Statement 3
S.append("Day") # Statement 4
print(S) # Statement 5
[CBSE 2023 Compartment]
Solution:  
Concept:
Strings are immutable and have no append() method (append is a list method).
Explanation:
Statement 2: S*2 repeats the string, prints HappyHappy. Valid.
Statement 3: S+="Independence" makes a new string and assigns it to S. Valid.
Statement 4: S.append("Day") raises AttributeError: 'str' object has no attribute 'append'.
So only Statement 4 gives an error.
Answer:
Option C (Statement 4)
© examsnet.com
Advertisements
Go to Question: