EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  Java for EmSAT Achieve  >  Assignment: Arrays in Java

Assignment: Arrays in Java | Java for EmSAT Achieve PDF Download

Multiple Choice Questions (MCQs)


Q.1. Which of the following statements is true about arrays in Java?
(a) Arrays are fixed in size and cannot be changed.
(b) Arrays can only store elements of the same data type.
(c) Arrays can dynamically resize themselves.
(d) Arrays can only be used to store primitive data types.

Ans. (a)

Q.2. What is the output of the following code snippet?

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

System.out.println(numbers[3]);

(a) 1
(b) 2
(c) 3
(d) 4

Ans. (d)

Q.3. In Java, the index of the first element in an array is:
(a) 0
(b) 1
(c) -1
(d) The index can vary depending on the array size.

Ans. (a)

Q.4. Which of the following is the correct way to declare and initialize a multidimensional array in Java?
(a) int[][] matrix = new int[3][3];
(b) int[][] matrix = new int[3, 3];
(c) int[][] matrix = new int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
(d) int matrix[][] = new int[3][3];

Ans. (a)

Q.5. What is a jagged array in Java?
(a) An array that has a varying number of elements in each row.
(b) An array with elements of different data types.
(c) An array that is sorted in descending order.
(d) An array that is sorted in ascending order.

Ans. (a)

High Order Thinking Questions (HOTS)


Q.1. Explain the difference between an array and a multidimensional array in Java.

An array is a single-dimensional collection of elements of the same type, while a multidimensional array is a collection of arrays, where each array can be of different dimensions.

Q.2. Write a Java code snippet to find the maximum element in a one-dimensional array.

int[] numbers = {5, 3, 8, 2, 7};

int max = numbers[0];

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

    if (numbers[i] > max) {

        max = numbers[i];

    }

}

System.out.println("Maximum element: " + max);

Q.3. What are the advantages of using jagged arrays in Java over multidimensional arrays?

Jagged arrays allow flexibility in terms of the number of elements in each row, while multidimensional arrays require each row to have the same number of elements. This can be useful when dealing with irregular or sparse data.

Q.4. How can you copy the elements of one array to another array in Java?

Array elements can be copied to another array using a loop. Here's an example:

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

int[] destinationArray = new int[sourceArray.length];


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

    destinationArray[i] = sourceArray[i];

}

Q.5. Write a Java code snippet to transpose a given square matrix (2D array).

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

int[][] transposedMatrix = new int[matrix[0].length][matrix.length];


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

    for (int j = 0; j < matrix[0].length; j++) {

        transposedMatrix[j][i] = matrix[i][j];

    }

}

Fill in the Blanks


1. An array is a ___________ type of data structure that stores a fixed-size sequence of elements of the same type.

Ans. linear

2. In Java, arrays are zero-___________ indexed, which means the index of the first element is 0.

Ans. index

3. To access an element in a 2D array, we use ___________ notation, specifying both the row and column index.

Ans. bracket

4. The length of an array can be obtained using the ___________ field.

Ans. length

5. A jagged array is an array of arrays where each subarray can have a ___________ number of elements.

Ans. different

True or False

Indicate whether the following statements are true or false.

1. In Java, arrays are objects.

Ans. True

2. The length of an array in Java is always fixed and cannot be changed.

Ans. True

3. In a multidimensional array, each row can have a different number of columns.

Ans. True

4. The size of a jagged array cannot be changed once it is initialized.

Ans. True

5. Arrays in Java can only store elements of primitive data types.

Ans. False

Hands-On Questions


Q.1. Write a Java program to find the sum of all elements in a one-dimensional array.

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

int sum = 0;

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

    sum += numbers[i];

}

System.out.println("Sum: " + sum);

Q.2. Write a Java program to multiply two matrices (2D arrays) and store the result in another matrix.

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

int[][] matrix2 = {{5, 6}, {7, 8}};

int[][] result = new int[matrix1.length][matrix1[0].length];


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

    for (int j = 0; j < matrix1[0].length; j++) {

        result[i][j] = matrix1[i][j] * matrix2[i][j];

    }

}

Q.3. Write a Java program to sort a one-dimensional array in ascending order using the Bubble Sort algorithm.

int[] numbers = {5, 3, 8, 2, 7};

boolean sorted = false;


while (!sorted) {

    sorted = true;

    for (int i = 0; i < numbers.length - 1; i++) {

        if (numbers[i] > numbers[i + 1]) {

            int temp = numbers[i];

            numbers[i] = numbers[i + 1];

            numbers[i + 1] = temp;

            sorted = false;

        }

    }

}

Q.4. Write a Java program to find the sum of each row and each column in a 2D array.

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int rows = matrix.length;

int columns = matrix[0].length;

int[] rowSum = new int[rows];

int[] columnSum = new int[columns];


for (int i = 0; i < rows; i++) {

    for (int j = 0; j < columns; j++) {

        rowSum[i] += matrix[i][j];

        columnSum[j] += matrix[i][j];

    }

}

Q.5. Write a Java program to display a jagged array in a tabular format.

int[][] jaggedArray = {{1, 2, 3}, {4, 5}, {6, 7, 8, 9}};


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

    for (int j = 0; j < jaggedArray[i].length; j++) {

        System.out.print(jaggedArray[i][j] + "\t");

    }

    System.out.println();

}

The document Assignment: Arrays in Java | Java for EmSAT Achieve is a part of the EmSAT Achieve Course Java for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
60 videos|37 docs|12 tests

Top Courses for EmSAT Achieve

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Sample Paper

,

Assignment: Arrays in Java | Java for EmSAT Achieve

,

Semester Notes

,

study material

,

MCQs

,

Viva Questions

,

Free

,

mock tests for examination

,

Assignment: Arrays in Java | Java for EmSAT Achieve

,

Previous Year Questions with Solutions

,

video lectures

,

pdf

,

practice quizzes

,

past year papers

,

Assignment: Arrays in Java | Java for EmSAT Achieve

,

Exam

,

Extra Questions

,

Summary

,

ppt

,

Objective type Questions

,

shortcuts and tricks

,

Important questions

;