Skip to content
Question

Q.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

(a) Statement 2
(b) Statement 3
(c) Statement 4
(d) Statement 3 and 4
CBSECBSE Class XII Board 2023MCQ· 1mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Statement 4 will cause an error because the append() method is used for lists, not for string objects in Python.

Let's break down each statement in the provided Python code to understand its function and identify any potential errors. Understanding how Python handles strings and their available methods is key here.

In Python, strings are immutable sequences of characters. This means once a string is created, its individual characters cannot be changed, nor can elements be added or removed directly using methods like append that modify sequences in place.

  • Statement 1: S="Happy"

    This statement initializes a variable S and assigns it the string value "Happy". This is a perfectly valid assignment operation.

  • Statement 2: print(S*2)

    Here, the * operator is used with a string (S) and an integer (2). In Python, when a string is multiplied by an integer, it results in the string being repeated that many times. So, S*2 would evaluate to "HappyHappy". The print() function then displays this resulting string. This is a valid operation and will execute without error.

  • Statement 3: S+="Independence"

    This statement uses the augmented assignment operator +=. For strings, += performs string concatenation. It is equivalent to S = S + "Independence". The current value of S ("Happy") is concatenated with the string "Independence", and the new resulting string ("HappyIndependence") is then assigned back to S. This is a valid operation and will execute without error.

    Note

    After Statement 3, the variable S now holds the value "HappyIndependence".

  • Statement 4: S.append("Day") …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.