1D Array Exercises

Test Your Understanding: Primitive vs Reference Variables

Score: 0 / 15

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.
TRUE
FALSE
2
True / False
A reference variable (like an array variable) stores the actual data of the array directly.
TRUE
FALSE
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}
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}
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
TRUE
FALSE
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