Challenge - 5 Problems
Java Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of primitive and reference variable changes
What is the output of the following Java code?
Java
public class Test { public static void main(String[] args) { int a = 5; int b = a; b = 10; System.out.println("a = " + a); System.out.println("b = " + b); } }
Attempts:
2 left
💡 Hint
Remember that primitives store values directly, so changing b does not affect a.
✗ Incorrect
In Java, primitive types like int store values directly. When b = a, b gets a copy of the value 5. Changing b to 10 does not change a, so a remains 5.
❓ Predict Output
intermediateOutput when modifying an object reference
What is the output of this Java code?
Java
public class Test { public static void main(String[] args) { int[] arr1 = {1, 2, 3}; int[] arr2 = arr1; arr2[0] = 10; System.out.println(arr1[0]); } }
Attempts:
2 left
💡 Hint
Arrays are objects, so arr1 and arr2 point to the same data.
✗ Incorrect
In Java, arrays are reference types. Both arr1 and arr2 point to the same array. Changing arr2[0] changes the first element of the array, so arr1[0] is also 10.
❓ Predict Output
advancedEffect of reassigning object references
What is the output of this Java code?
Java
public class Test { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("Hello"); StringBuilder sb2 = sb1; sb2.append(" World"); sb2 = new StringBuilder("New"); System.out.println(sb1.toString()); } }
Attempts:
2 left
💡 Hint
Appending changes the object both references point to; reassigning sb2 does not affect sb1.
✗ Incorrect
sb1 and sb2 initially point to the same StringBuilder object. Appending " World" modifies that object. Then sb2 is reassigned to a new object, but sb1 still points to the original modified object. So sb1.toString() prints "Hello World".
❓ Predict Output
advancedOutput when passing primitives and objects to methods
What is the output of this Java code?
Java
public class Test { public static void main(String[] args) { int x = 5; StringBuilder sb = new StringBuilder("Hi"); modify(x, sb); System.out.println(x + " " + sb.toString()); } static void modify(int a, StringBuilder b) { a = 10; b.append(" there"); } }
Attempts:
2 left
💡 Hint
Primitives are passed by value; objects references allow modification of the object.
✗ Incorrect
The primitive x is passed by value, so changing a inside modify does not affect x. The StringBuilder sb reference allows modification of the object, so " there" is appended. The output is "5 Hi there".
🧠 Conceptual
expertUnderstanding memory behavior of primitives and references
Which statement best describes how Java stores primitive types and reference types in memory?
Attempts:
2 left
💡 Hint
Think about where the actual data and references are stored for primitives and objects.
✗ Incorrect
In Java, primitive variables hold the actual data value directly (e.g., int holds the number). Reference variables hold the address (reference) to the object stored in the heap memory.
