MCQ Single Best Answer Moderate

Q[StringBuffer]
m and n are two StringBuffer objects. The method call m.append(n) will:

ID: #24935 Competency focused Practice Questions ISC Class XII Computer Science 6 views
Question Info
#24935Q ID
ModerateDifficulty
Competency focused Practice Questions ISC Class XII Computer ScienceTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A mutate object m
  • B mutate object n
  • C mutate both the objects m and n
  • D append() is an accessor method so it cannot mutate any object
Correct Answer: Option A

Explanation

StringBuffer Concept Question

Correct Answer: (a) mutate object m

Explanation:

In Java, StringBuffer is a mutable class. This means its content can be changed after it is created.

When we call m.append(n), the content of object n is added to object m. However, only m is modified (mutated), while n remains unchanged.

The append() method belongs to the StringBuffer class and is a mutator method, not an accessor method. It directly modifies the original object instead of creating a new one.

Accessor Method vs Mutator Method in Java

When working with objects, methods generally fall into two categories:

Type Purpose Changes Object State?
Accessor Method Retrieves or reads data ❌ No
Mutator Method Modifies or updates data ✅ Yes

Therefore, after execution:
m → updated (changed)
n → unchanged

Share This Question

Challenge a friend or share with your study group.