0% found this document useful (0 votes)
5 views

Midterm Review Solution

The document provides an overview of basic concepts related to one-dimensional and two-dimensional arrays, including definitions, declarations, and examples of usage in Java. It also covers fundamental concepts of classes and objects in Java, such as class definition, object creation, constructors, and visibility modifiers. Additionally, it includes sample code snippets with expected outputs for better understanding.

Uploaded by

ahuda0648
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Midterm Review Solution

The document provides an overview of basic concepts related to one-dimensional and two-dimensional arrays, including definitions, declarations, and examples of usage in Java. It also covers fundamental concepts of classes and objects in Java, such as class definition, object creation, constructors, and visibility modifiers. Additionally, it includes sample code snippets with expected outputs for better understanding.

Uploaded by

ahuda0648
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Basic 1D Array Questions

1. What is a one-dimensional array?


A one-dimensional array is a collection of elements of the same type stored in a
contiguous memory location and accessed using a single index.

2. Write an instruction for declaring and creating a one-dimensional array of integers.

int[] myArray = new int[5]; // Creates an integer array of size 5

3. What is the result of the following code snippet?

int[] arr = {3, 5, 9, 17, 14};

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

Answer: 9

4. What is the result of the following code snippet?

int[] arr = {3, 5, 9, 17, 14};

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

Answer: 14

5. Write the instruction to find the length of a one-dimensional array named mylist.

int length = mylist.length;

6. Given the following array:

int[] numbers = {5, 10, 15, 20, 25};

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

Answer: 30

7. What is the output of the following code segments?

int[] numbers = {3, 9, 5, -5};

for (int a : numbers) {

System.out.print(" " + a);

Output:

3 9 5 -5

Basic 2D Array Questions

1. What is a two-dimensional array?


Two-dimensional array is a collection of elements of the same type used to represent a
matrix or a table. A two-dimensional array is a bi-dimensional array, where each
element is accessed using two indices (row and column).
2. Declare and create a two-dimensional integer array with 4 rows and 2 columns.

int[][] array = new int[4][2];

3. Declare and initialize a 2×3 integer matrix with the given values:

int[][] matrix = {

{2, 4, 6},

{8, 10, 12}

};

4. What is the output of the following code?

int[][] arr = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}};

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

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

Output:

90

40

5. What does this code print?

int[][] numbers = {{3, 6, 9}, {2, 4, 8}};

System.out.println(numbers.length);

System.out.println(numbers[0].length);

Output:

6. Define a ragged array and give an example.


A ragged array is a 2D array where each row can have a different number of columns.
Example:

int[][] raggedArray = {

{1, 2, 3},

{4, 5},

{6, 7, 8, 9}

};
Class and Object Basic Questions

1. What is a class in Java?


Answer: (b) A template for creating objects

2. Which keyword is used to define a class in Java?


Answer: (b) class

3. How do you declare and create an instance of a class in Python?


Answer: (c) ClassName obj = new ClassName()

Student s=new Student();

4. What does the new keyword do in Java?


Answer: (b) Creates an object

5. What is the default value of a reference variable in Java if it is not initialized?


Answer: (b) null

6. What is the purpose of the constructor in Java?


Answer: (a) To create and initialize an object

7. Which of the following statements about constructors is true?


Answer: (a) A class can have multiple constructors

8. Which of the following is the most restrictive visibility modifier in Java?


Answer: (c) private

9. What does the protected visibility modifier allow in Java?


Answer: (b) The member is accessible within the package

What will be the output of the following codes?

1.

Output:

Alice 21

2.
Output:

Item1 Price: 100

Item2 Price: 200

3.

Output: 2

4.

Output:

10

You might also like