This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1What will be the output of the following program?
public class Demo {
public static void main(String[] args) {
int[] arr = {120, 200, 016};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " "); }
}
}
Option A
In Java, numbers can be represented in different formats, such as decimal, octal, hexadecimal, and binary. By default, numbers written without any prefix are decimal numbers (base 10).
However, if a number starts with a 0, Java treats it as an octal number (base 8). Octal numbers use digits from 0 to 7 only.
If a number starts with 0x, Java treats it as an hexadecimal number (base 16). Hexadecimal numbers use digits from 0 to 9 and a to f.
In the given program, the array is declared as int[] arr = {120, 200, 016};. Here, 120 and 200 are standard decimal numbers. The third element, 016, starts with a 0, so Java interprets it as an octal number. To understand its decimal value, we convert octal 16 to decimal, 1×81+6×80 = 8+6 = 14. Therefore, the array actually contains the values {120, 200, 14}.
The for loop then iterates through this array and prints each element followed by a space. As a result, the output of the program is 120 200 14.
Q: 2Which statement about Java multidimensional arrays is correct?
Option B
In Java, a multidimensional array is actually an array whose elements are themselves arrays. This means Java does not require every row to have the same number of elements. Such arrays are often called Jagged Arrays when different rows have different lengths.
E.g.:
A regular 2D array can be created as:
int a[][]={
{11, 22, 33},
{44, 55, 66},
{77, 88, 99}
};
Here, a is an array containing three separate int[] arrays. Each row happens to contain three elements, so it looks rectangular. However, Java also allows rows of different lengths:
int a[][]={
{11, 22},
{33, 44, 55},
{66}
};
Therefore, the statement "They are arrays of arrays" correctly describes Java multidimensional arrays.
Q: 3Which of the following correctly declares a method that accepts one dimensional array of double as a parameter?
Option B
When a method needs to accept an array as a parameter, the arrays type must be specified correctly. For a one-dimensional array of double, the syntax is double[] arr, where double[] indicates an array of double values and arr is the name of the parameter.
Q: 4What happens when following code gets executed?
int[][] a = new int[3][];
a[0] = new int[2];
a[1] = new int[4];
a[2] = new int[1];
Option D
In Java, a multidimensional array is actually an array of arrays. Therefore, each row can be created separately, and different rows can have different lengths. Such an array is called a Jagged Array.
The first statement int[][] a = new int[3][]; creates an array that can hold 3 separate integer arrays. At this point, the individual rows have not yet been created. The rows are then created with different sizes.
E.g.:
class JaggedArrayDemo
{
public static void main(String args[])
{
int[][] a = new int[3][];
a[0] = new int[2];
a[1] = new int[4];
a[2] = new int[1];
System.out.println("Row 0 Length : " + a[0].length);
System.out.println("Row 1 Length : " + a[1].length);
System.out.println("Row 2 Length : " + a[2].length);
}
}
OUTPUT
Row 0 Length : 2
Row 1 Length : 4
Row 2 Length : 1
Q: 5Which syntax is incorrect to initialize a two-dimensional array in Java?
Option D
The incorrect syntax for initializing a two-dimensional array in Java is int myarr = new int[3][4];. This statement is wrong because it declares myarr as a simple integer variable, not as an array.
A two-dimensional (2-D) array must always be declared using two sets of square brackets [ ][ ] to indicate rows and columns.
Q: 6What is the output of the following given piece of code?
int[] a = {10, 20, 30, 40};
System.out.println(a[a.length-2]);
Option B
In Java, an array stores multiple values of the same type, and array indexing starts from 0. Therefore, in the given array int[] a = {10, 20, 30, 40};, the elements are stored as follows:
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Value | 10 | 20 | 30 | 40 |
The length property gives the total number of elements in the array. Since this array contains four elements so, a.length is 4.
The expression inside the square brackets is a.length-2, i.e., 4-2 is 2. Therefore, the statement becomes System.out.println(a[2]);. The element at index 2 is 30, so the output is 30.
Q: 7What is the output of following Java code?
public class Test
{
public static void main (String args[])
{
int [][] m1={{1,2,3},{4,5,6},{7,8,9}};
int [][] m2=new int[3][3];
for(int i=0;i<3;i++){
for(j=0;j<3;j++){
m2[j][i]=m1[i][j];
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(m2[i][j]+” “);
}
System.out.println();
}
}
}
Option B
The given Java program works with a 2D array (matrix) and performs a transpose operation. The original matrix m1 contains values arranged row-wise as:
1 2 3
4 5 6
7 8 9
Inside the nested loop, the statement m2[j][i] = m1[i][j] swaps the row index with the column index. This means elements of rows are converted into columns, which is the basic concept of Matrix Transpose.
After executing the loops, the first row of the new matrix becomes the first column of the original matrix, the second row becomes the second column, and so on. Hence, the resulting matrix m2 becomes:
1 4 7
2 5 8
3 6 9
Q: 8Which statement correctly compares arrays and ArrayList?
Option D
In Java, both an array and an ArrayList are used to store multiple values, but they differ mainly in how their size is managed. When an array is created, its size is fixed. It cannot automatically increase or decrease later.
class ArrayDemo
{
public static void main(String args[])
{
int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
System.out.println("Length : "+numbers.length);
}
}
OUTPUT
Length : 3
Here, the array can store exactly 3 elements. We cannot directly add a fourth element to this same array. If more space is required, a new array must be created.
An ArrayList, on the other hand, can grow or shrink dynamically when elements are added or removed. It is part of the java.util package.
import java.util.ArrayList;
class ArrayListDemo
{
public static void main(String args[])
{
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
System.out.println("Length : "+numbers.size());
System.out.println("Elements : "+numbers);
}
}
OUTPUT
Length : 4
Elements : [10, 20, 30, 40]
No fixed size was specified when the ArrayList was created. Elements can be added using add() and removed using remove().
Note:
Q: 9What is the default value of elements of an int[] created using new?
Option C
In Java, when an array is created using the new keyword, its elements are automatically assigned their default values. For an array of the int data type, the default value of every element is 0.
class ArrayDemo
{
public static void main(String args[])
{
int a[]=new int[5];
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
System.out.println(a[3]);
System.out.println(a[4]);
}
}
OUTPUT
0
0
0
0
0
Note:
The important point is that the default value depends on the data type. For example, an array of double gets 0.0, an array of boolean gets false, and an array of reference types such as String gets null.
| Array Type | Default Value |
|---|---|
| int[] | 0 |
| byte[] | 0 |
| short[] | 0 |
| long[] | 0L |
| float[] | 0.0f |
| double[] | 0.0d |
| char[] | '\u0000' |
| boolean[] | false |
| String[] | null |
Q: 10Which of the following code gives error of “outofbounds” in array?
Option D
In Java, an ArrayIndexOutOfBoundsException occurs when a program tries to access an array index that does not exist. Java arrays always start with index 0, and the last valid index is always length − 1.
So, if you create an array like int[] a = new int[3];. This array has 3 elements, and their valid indexes are a[0], a[1], and a[2]. Any attempt to access a[3], a[4], etc., will cause an ArrayIndexOutOfBoundsException.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.