Array Basics
Array Traversal
Array Operations
2D Arrays
Bonus Points
100

What is the index of the first element in a Java array?

0

100

What will this code output:
int[] arr = {1, 2, 3}; 

System.out.println(arr[1]);

2

100

What is the value of arr[2] in int[] arr = {10, 20, 30, 40}?

30

100

How do you create a 2D array of integers in Java?

int[][] arr = new int[3][4];

100

This term refers to a set of instructions that a computer follows to perform a task, and it is a core concept in software development.

Programming

200

How do you declare an array of integers in Java?

int[] arr = new int[5];

200

Write a loop to print all elements of the array:

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

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

for (int i = 0; i < arr.length; i++) {

    System.out.println(arr[i]);

}


200

How would you change the value of the first element in an array?

array[0] = value;

200

Write a code snippet to access the element at the second row and third column of a 2D array.

int[][] arr = {

    {1, 2, 3},

    {4, 5, 6},

    {7, 8, 9}

};

 

int element = arr[1][2];  // Access the element at row 2, column 3 (0-based index)

System.out.println(element);  // Output will be 6

200

This control structure in Java evaluates a condition and executes one block of code if true, and another block if false.

IF statement

300

What is the size of the array int[] arr = {5, 10, 15, 20, 25}?  

5

300

Write the code to find the sum of elements in an array.

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

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

int sum = 0;

for (int i = 0; i < arr.length; i++) {

    sum += arr[i];

}

System.out.println(sum);


300

What is the result of this operation: arr[0] = arr[1] + arr[2] in int[] arr = {1, 2, 3}?

arr[0] becomes 5 (since 2 + 3 = 5).

300

How can you print all elements of a 2D array using nested loops ? 

int[][] arr = {

    {1, 2, 3},

    {4, 5, 6},

    {7, 8, 9}

};


for (int i = 0; i < arr.length; i++) {  // Outer loop for rows

    for (int j = 0; j < arr[i].length; j++) {  // Inner loop for columns

        System.out.print(arr[i][j] + " ");

    }

    System.out.println();  // Move to the next line after each row

}

300

This type of loop in Java will continue executing a block of code as long as a specified condition is true, often used when the number of iterations is not known in advance.

WHILE loop

400

What does arr[arr.length - 1] represent in an array?

last element in the array

400

Write a code to get the maximum element in an array.

    int max = arr[0];

    for (int i = 1; i < arr.length; i++) {

        if (arr[i] > max) {

            max = arr[i];

        }

    }


400

What happens when you try to access an index outside the bounds of an array?

It throws an ArrayIndexOutOfBoundsException.

400

What is the difference between a rectangular array and a jagged array?

Rectangular array: A 2D array where each row has the same number of columns. It is essentially a grid with uniform dimensions. 

Jagged array: A 2D array where each row can have a different number of columns. It’s an array of arrays, where the sub-arrays can vary in length.

400

In Java, this special method is used to initialize an object when it is created, typically setting values for the object's attributes.

Constructor

500

Explain how array indexing works in Java.

Array indexing in Java starts at 0, meaning the first element is accessed with index 0.

500

Explain how nested loops work when traversing a 2D array.

The outer loop iterates over the rows, and the inner loop iterates over the elements (columns) in each row.

  • The outer loop runs for each row.
  • The inner loop runs for each column in the current row.
500

Write a code to double each element in an array.

for (int i = 0; i < arr.length; i++) {        arr[i] *= 2;    }

500

Write a code that computes the sum of all elements in a 2D array.

 int sum = 0;

    for (int i = 0; i < arr.length; i++) {  // Iterate over rows

        for (int j = 0; j < arr[i].length; j++) {  // Iterate over columns

            sum += arr[i][j];

        }

    }

500

This concept in Java allows a method to call itself, which can be especially useful in solving problems that can be broken down into smaller, similar subproblems.

Recursion