0% found this document useful (0 votes)
7 views3 pages

Java Array Reference and Behavior Guide

The document presents a series of Java code challenges focusing on array references, primitive vs reference types, and multi-dimensional arrays. Each challenge includes code snippets that illustrate how changes to arrays and variables affect their values and outputs. The reader is instructed to predict the output of each code snippet based on their understanding of Java's behavior with arrays and references.

Uploaded by

arnav.flics
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Java Array Reference and Behavior Guide

The document presents a series of Java code challenges focusing on array references, primitive vs reference types, and multi-dimensional arrays. Each challenge includes code snippets that illustrate how changes to arrays and variables affect their values and outputs. The reader is instructed to predict the output of each code snippet based on their understanding of Java's behavior with arrays and references.

Uploaded by

arnav.flics
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Array and Reference Challenges

1. Array Reference

int[] a = {5, 6, 7};


int[] b = a;
b[1] = 99;
[Link](a[1]);

2. Changing Multiple References

int[] x = {1,2,3};
int[] y = x;
int[] z = y;
z[2] = 50;
[Link](x[2]);

3. Primitive vs Reference

int m = 10;
int n = m;
n = 20;
[Link](m);

4. Two-Dimensional Array

int[][] arr = {{1,2}, {3,4}};


int[][] copy = arr;
copy[0][1] = 99;
[Link](arr[0][1]);

5. Array Length Confusion

int[] nums = new int[5];


[Link]([Link]);
nums = new int[10];
[Link]([Link]);

1
6. Post-Increment in Array

int[] arr = {1,2,3};


int i = 0;
arr[i++] = 10;
[Link](arr[0] + " " + arr[1]);

7. Array within Array

int[][] arr = new int[2][2];


arr[0][0] = 1;
arr[1] = arr[0];
arr[1][1] = 5;
[Link](arr[0][1]);

8. Swapping Arrays

int[] a = {1,2};
int[] b = {3,4};
int[] temp = a;
a = b;
b = temp;
[Link](a[0] + " " + b[0]);

9. For Loop and Array

int[] nums = {10, 20, 30};


for(int i = 0; i < [Link]; i++){
nums[i] += i;
}
[Link](nums[2]);

10. Reference vs New Array

int[] arr1 = {1,2,3};


int[] arr2 = new int[[Link]];
arr2 = arr1;
arr2[0] = 99;
[Link](arr1[0]);

2
11. Multi-dimensional Reference

int[][] a = {{1,2},{3,4}};
int[][] b = [Link]();
b[0][0] = 100;
[Link](a[0][0]);

12. Mixing Primitives and Arrays

int x = 5;
int[] arr = {x};
x = 10;
[Link](arr[0]);

Instructions: Read each code snippet carefully and predict the output. Think about how arrays, references,
and primitives behave in Java.

You might also like