Bird
0
0

What will be printed by the following code?

medium📝 Predict Output Q5 of 15
Java - Memory Management Basics
What will be printed by the following code?
String str1 = "Good";
String str2 = str1;
str2 = "Morning";
System.out.println(str1);
Anull
BMorning
CGood
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand String references

    Strings are reference types; assigning one reference to another copies the reference.
  2. Step 2: Analyze reassignment

    Changing str2 to "Morning" points it to a new String object; str1 still points to "Good".
  3. Final Answer:

    Good -> Option C
  4. Quick Check:

    Reassigning reference variable doesn't affect original object [OK]
Quick Trick: Reference reassignment doesn't change original object [OK]
Common Mistakes:
  • Thinking str1 changes when str2 is reassigned
  • Confusing reference assignment with object mutation
  • Expecting output to be 'Morning'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes