0
0
Javaprogramming~15 mins

Heap memory in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Heap Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
What is the output of this Java code related to heap memory?

Consider the following Java code snippet. What will be printed to the console?

Java
public class HeapTest {
    public static void main(String[] args) {
        String a = new String("hello");
        String b = new String("hello");
        System.out.println(a == b);
        System.out.println(a.equals(b));
    }
}
Afalse\ntrue
Btrue\ntrue
Cfalse\nfalse
Dtrue\nfalse
Attempts:
2 left
🧠 conceptual
intermediate
1:30remaining
Which statement about Java heap memory is true?

Choose the correct statement about Java heap memory.

AHeap memory is used to store local variables inside methods.
BHeap memory is shared among all threads and stores objects and arrays.
CHeap memory stores only primitive data types.
DHeap memory is automatically cleared when a method finishes execution.
Attempts:
2 left
🔧 debug
advanced
2:00remaining
What error does this code cause related to heap memory?

Analyze the following Java code. What error will it cause when run?

Java
public class HeapOverflow {
    public static void main(String[] args) {
        int i = 0;
        while(true) {
            i++;
            int[] arr = new int[1000000];
            System.out.println("Iteration: " + i);
        }
    }
}
AOutOfMemoryError
BNo error, infinite loop runs fine
CNullPointerException
DStackOverflowError
Attempts:
2 left
📝 syntax
advanced
1:30remaining
Which option correctly declares an object stored in heap memory?

Which of the following Java code snippets correctly creates an object that will be stored in heap memory?

Aint x = new int(5);
BString s = "hello";
CString s = new String("hello");
Dint[] arr = 10;
Attempts:
2 left
🚀 application
expert
2:30remaining
How many objects are created in heap memory after running this code?

Consider this Java code snippet. How many distinct objects are created in heap memory after it runs?

Java
public class ObjectCount {
    public static void main(String[] args) {
        String s1 = "test";
        String s2 = new String("test");
        String s3 = s2.intern();
        Integer i1 = 100;
        Integer i2 = new Integer(100);
    }
}
A3
B5
C6
D4
Attempts:
2 left