Instructions
Answer all questions to test your understanding of arrays and the difference between primitive and reference variables. Click "Check Answer" after each question or "Submit All Answers" at the end.
Part 1: Basic Concepts
1
True / False
A primitive variable stores the actual value directly in memory.
2
True / False
A reference variable (like an array variable) stores the actual data of the array directly.
3
Multiple Choice
What does a reference variable store?
4
Multiple Choice
Which of the following is NOT a primitive data type in Java?
Part 2: Comparing Variables
5
Code Tracing
What is the output of the following code?
ComparePrimitives.java
1int a = 10; 2int b = 10; 3 4if (a == b) { 5 System.out.println("Equal"); 6} else { 7 System.out.println("Not Equal"); 8}
6
Code Tracing
What is the output of the following code?
CompareArrays.java
1int[] arr1 = {1, 2, 3}; 2int[] arr2 = {1, 2, 3}; 3 4if (arr1 == arr2) { 5 System.out.println("Equal"); 6} else { 7 System.out.println("Not Equal"); 8}
Why are they NOT equal? Let's visualize!
Even though both arrays contain {1, 2, 3}, they are stored in different locations in memory:
Stack Memory
arr1
0x1A2B
arr2
0x5C7D
arr1 == arr2
0x1A2B ≠ 0x5C7D
FALSE!
Heap Memory
Address: 0x1A2B
[0]1
[1]2
[2]3
Address: 0x5C7D
[0]1
[1]2
[2]3
Key Insight: The
== operator compares the memory addresses (references), NOT the values inside the arrays! Since arr1 and arr2 point to different locations (0x1A2B vs 0x5C7D), they are NOT equal.
To compare array contents: Use
Arrays.equals(arr1, arr2) which returns true because the values are the same.
7
Code Tracing
What is the output of the following code?
SameReference.java
1int[] arr1 = {1, 2, 3}; 2int[] arr2 = arr1; // arr2 points to same array as arr1 3 4if (arr1 == arr2) { 5 System.out.println("Equal"); 6} else { 7 System.out.println("Not Equal"); 8}
Why ARE they equal? Let's visualize!
When we write
arr2 = arr1, we copy the reference (address), not the array. Both now point to the SAME array:Stack Memory
arr1
0x1A2B
arr2
0x1A2B
arr1 == arr2
0x1A2B = 0x1A2B
TRUE!
Heap Memory
Address: 0x1A2B (SAME!)
[0]1
[1]2
[2]3
Key Insight: Both
arr1 and arr2 hold the same address (0x1A2B). The == operator compares these addresses, and since they are identical, the result is true!
Important: Since both variables point to the same array, changing
arr2[0] will also change arr1[0]! They share the same data.
Part 3: Pass by Value vs Reference
8
Code Tracing
What is the value of
x after calling the method?
PassByValue.java
1public static void changeValue(int num) { 2 num = 100; 3} 4 5public static void main(String[] args) { 6 int x = 5; 7 changeValue(x); 8 System.out.println(x); // What is printed? 9}
9
Code Tracing
What is the value of
arr[0] after calling the method?
PassByReference.java
1public static void changeArray(int[] nums) { 2 nums[0] = 100; 3} 4 5public static void main(String[] args) { 6 int[] arr = {5, 10, 15}; 7 changeArray(arr); 8 System.out.println(arr[0]); // What is printed? 9}
10
Multiple Choice
In Question 8, the value of
x remained 5. In Question 9, the value of arr[0] changed to 100. Why is this?
Part 4: Array Manipulation
11
Code Tracing
What are the values of
a[0] and b[0] after running this code?
SharedReference.java
1int[] a = {1, 2, 3}; 2int[] b = a; 3 4b[0] = 99; 5 6System.out.println(a[0] + " " + b[0]);
12
Fill in the Blank
After running this code, what is the value printed?
DefaultValues.java
1int[] numbers = new int[5]; 2 3System.out.println(numbers[2]);
The output is:
13
True / False
The following code creates an array in memory:
Declaration.java
1int[] arr; // Only this line
14
Multiple Choice
Look at this memory diagram. What does this represent?
Stack Memory
arr
0x1A2B
→
Heap Memory (0x1A2B)
[0]
10
[1]
20
[2]
30
15
Code Tracing
What is the final output of this code?
ComplexExample.java
1int[] x = {1, 2, 3}; 2int[] y = x; 3int[] z = {1, 2, 3}; 4 5y[1] = 50; 6 7System.out.println(x[1] + " " + y[1] + " " + z[1]);
Your Results
0%
0 Correct
0 Incorrect