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 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: 3Which 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: 4What 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: 5Which 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.
Q: 6What is the output of following code?
public class ex {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
printn(a);
}
public static void printn(int[] a){
for(int n : a) {
System.out.print(n+” “);
}
}
}
Option D
In this Java code, an integer array a is created with the elements {1, 2, 3, 4, 5}. The method printn(a) is called, which receives this array and uses a for-each loop to iterate through each element. The for-each loop processes elements in the same order in which they appear in the array. Therefore, the values are printed sequentially: 1 2 3 4 5.
You have reached the end of this topic. Continue learning with the next topic below.
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.