Complete the code to create an object stored in heap memory.
String str = new [1]("Hello");
The new keyword creates an object in heap memory. Here, new String("Hello") creates a String object on the heap.
Complete the code to assign a new object reference to the variable.
MyClass obj = [1] MyClass();The new keyword is used to create a new object instance in heap memory.
Fix the error in the code to properly create an array in heap memory.
int[] numbers = new int[1]5;
Arrays in Java are created with square brackets [] to specify size in heap memory.
Fill both blanks to create a new object and call its method.
MyObject obj = [1] MyObject(); obj.[2]();
new creates the object in heap memory, and run() is the method called on the object.
Fill all three blanks to create a HashMap, add a key-value pair, and retrieve a value.
HashMap<String, Integer> map = [1] HashMap<>(); map.[2]("age", 30); int age = map.[3]("age");
add instead of put for HashMap.new keyword.new creates the HashMap object in heap memory. put adds a key-value pair, and get retrieves the value by key.
