Explore topic-wise MCQs in Java Programming.

This section includes 155 Mcqs, each offering curated multiple-choice questions to sharpen your Java Programming knowledge and support exam preparation. Choose a topic below to get started.

101.

What is the output of this program? class evaluate { public static void main(String args[]) { int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = 6; n = arr[arr[n] / 2]; System.out.println(arr[n] / 2); } }

A. 3
B. 0
C. 6
D. 1
Answer» E.
102.

What is the output of this program? class array_output { public static void main(String args[]) { int array_variable [] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i; System.out.print(array_variable[i] + " "); i++; } } }

A. 0 2 4 6 8
B. 1 3 5 7 9
C. 0 1 2 3 4 5 6 7 8 9
D. 1 2 3 4 5 6 7 8 9 10
Answer» B. 1 3 5 7 9
103.

What is the result of compiling and running the following code? public class Test { public static void main(String[] args) { int[] a = new int[0]; System.out.print(a.length); } }

A. 0
B. Compilation error, arrays cannot be initialized to zero size.
C. Compilation error, it is a.length() not a.length
D. None of the above
Answer» B. Compilation error, arrays cannot be initialized to zero size.
104.

What will be the output? public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } }

A. The program has a compile error because the size of the array wasn't specified when declaring the array.
B. The program has a runtime error because the array elements are not initialized.
C. The program runs fine and displays x[0] is 0.
D. The program has a runtime error because the array element x[0] is not defined.
Answer» D. The program has a runtime error because the array element x[0] is not defined.
105.

What will be the output? public class Test { public static void main(String args[]) { int a = 42; double b = 42.25; System.out.print((a%10)+" "+(b%10)); } }

A. 42 42.5
B. 2 2.5
C. 4.2 4.225
D. Compilation Error
Answer» C. 4.2 4.225
106.

public class Test { public static void main(String args[]) { System.out.print(""==""); System.out.print(" "); System.out.print("A"=="A"); System.out.print(" "); System.out.print("a==A"); } }

A. "==" A"=="A a==A
B. true true false
C. true true a==A
D. Compilation Fails
Answer» D. Compilation Fails
107.

What will be the output? if(1 + 1 + 1 + 1 + 1 == 5) { System.out.print("TRUE"); } else { System.out.print("FLASE"); }

A. TRUE
B. FALSE
C. Compiler Error
D. None of these
Answer» B. FALSE
108.

What will be the output after compiling and running following code?public class Test { public static void main(String... args) { int x =5; x *= 3 + 7; System.out.println(x); }}

A. 22
B. 50
C. 10
D. Compilation fails with an error at line 4
Answer» C. 10
109.

Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?

A. 1 < x < 100 || x < 0
B. ((x < 100) && (x > 1)) || (x < 0)
C. ((x < 100) && (x > 1)) && (x < 0)
D. (1 > x > 100) || (x < 0)
Answer» C. ((x < 100) && (x > 1)) && (x < 0)
110.

What is the output of the following program ?class Numbers{ public static void main(String args[]) { int a=20, b=10; if((a < b) && (b++ < 25)) { System.out.println("This is any language logic"); } System.out.println(b); } }

A. 12
B. 11
C. 10
D. Compilation Error
Answer» D. Compilation Error
111.

What will be the output of the above fraction of code ?int ++a = 100 ; System.out.println( ++a ) ;

A. 100
B. Displays error as ++a is not enclosed in double quotes in println statement
C. Compiler displays error as ++a is not a valid identifier
D. None of these
Answer» D. None of these
112.

What will be the value of "x" after execution ?int x = 0, y = 0 , z = 0 ; x = (++x + y-- ) * z++;

A. -2
B. -1
C. 0
D. 1
Answer» D. 1
113.

What will be the output for the below code ? public class Test { public static void main(String[] args) { byte i = 128; System.out.println(i); } }

A. 128
B. 0
C. Compilation fails with an error at line 3
D. None of these
Answer» D. None of these
114.

What is the output for the below code ? public class Test { public static void main(String[] args) { byte b = 6; b+=8; System.out.println(b); b = b+7; System.out.println(b); } }

A. 14 21
B. 14 13
C. Compilation fails with an error at line 6
D. None of these
Answer» D. None of these
115.

What is the output for the below code ? public class Test{ public static void main(String[] args) { int i = 010; int j = 07; System.out.println(i); System.out.println(j); } }

A. 8 7
B. 10 7
C. Compilation fails with an error at line 3
D. Compilation fails with an error at line 5
Answer» B. 10 7
116.

What will the output of the following program? public class Test{ public static void main(String args[]) { float f = (1 / 4) * 10; int i = Math.round(f); System.out.println(i); } }

A. 2
B. 0
C. 3
D. 2.5
Answer» C. 3
117.

The following program: public class Test{ static boolean isOK; public static void main(String args[]) { System.out.print(isOK); } }

A. Prints true
B. Prints false
C. Will not compile as boolean is not initialized
D. Will not compile as boolean can never be static
Answer» C. Will not compile as boolean is not initialized
118.

Determine output: public class Test{ int i = 34; public static void main(String args[]) { Test t1 = new Test(); Test t2 = new Test(); t1.i = 65; System.out.print(t1.i); System.out.print(t2.i); } }

A. 34 34
B. 65 34
C. 65 65
D. 34 65
Answer» C. 65 65
119.

Which of the following automatic type conversion will be possible?

A. short to int
B. byte to int
C. int to long
D. long to int
Answer» D. long to int
120.

Size of int in Java is

A. 16 bit
B. 32 bit
C. 64 bit
D. Depends on execution environment
Answer» C. 64 bit
121.

Automatic type conversion in Java takes place when

A. Two type are compatible and size of destination type is shorter than source type.
B. Two type are compatible and size of destination type is equal of source type.
C. Two type are compatible and size of destination type is larger than source type.
D. All of the above
Answer» D. All of the above
122.

The smallest integer type is _____ and its size is _____ bits.

A. short, 8
B. byte, 8
C. short, 16
D. short, 16
Answer» C. short, 16
123.

Java is a _______ language.

A. weakly typed
B. strogly typed
C. moderate typed
D. None of these
Answer» C. moderate typed
124.

In Java byte, short, int and long all of these are

A. signed
B. unsigned
C. Both of the above
D. None of these
Answer» B. unsigned
125.

In Java, each array object has a final field named _______ that stores the size of the array.

A. width
B. size
C. length
D. distance
Answer» D. distance
126.

The ith element in the array has an index _____

A. i
B. i-1
C. i+1
D. none of above
Answer» C. i+1
127.

Arrays in Java are dynamically allocated using the _________ operator

A. create
B. dynamic
C. arrayList
D. new
Answer» E.
128.

To declare a one-dimensional array, you will use this general form

A. type array-name[] = new [size];
B. type array-name[size] = new type[];
C. type array-name[] = new type[size];
D. type array-name[] = type[size];
Answer» D. type array-name[] = type[size];
129.

Which of these array declaration statements are not legal?

A. int[] i[] = { { 1, 2 }, { 1 }, {}, { 1, 2, 3 } };
B. int x[] = new int[2] {1, 2};
C. int x[][] = new int[][] { {1, 2, 3}, {4, 5, 6} };
D. int x[][] = { { 1, 2 }, new int[ 2 ] };
Answer» C. int x[][] = new int[][] { {1, 2, 3}, {4, 5, 6} };
130.

int[][] myArray = new int[4][5]; This above declaration constructs an array myArray of ______ elements, where each element is an array (row) of ______ int values.

A. 5, 4
B. 4, 5
C. 4, 4
D. 5, 5
Answer» C. 4, 4
131.

What is the output of this program? class conversion { public static void main(String args[]) { double a = 295.04; int b = 300; byte c = (byte) a; byte d = (byte) b; System.out.println(c + " " + d); } }

A. 38 43
B. 39 44
C. 295 300
D. 295.04 300
Answer» C. 295 300
132.

What is the output of this program? class char_increment { public static void main(String args[]) { char c1 = 'D'; char c2 = 84; c2++; c1++; System.out.println(c1 + " " + c2); } }

A. E U
B. U E
C. V E
D. U F
Answer» B. U E
133.

What is the error in this code? byte b = 50; b = b * 50;

A. b can not contain value 100, limited by its range.
B. * operator has converted b * 50 into int, which can not be converted to byte without casting.
C. b can not contain value 50.
D. No error in this code
Answer» C. b can not contain value 50.
134.

What is the prototype of the default constructor of this class? public class prototype { }

A. prototype( )
B. prototype(void)
C. public prototype(void)
D. public prototype( )
Answer» E.
135.

Which of the following is the advantage of BigDecimal over double?

A. Syntax
B. Memory usage
C. Garbage creation
D. Precision
Answer» E.
136.

What is the output of below code snippet? enum Levels { private TOP, public MEDIUM, protected BOTTOM; }

A. Runtime Error
B. EnumNotDefined Exception
C. It runs successfully
D. Compilation Error
Answer» E.
137.

What is the output of below code snippet? class A { } enum Enums extends A { ABC, BCD, CDE, DEF; }

A. Runtime Error
B. Compilation Error
C. It runs successfully
D. EnumNotDefined Exception
Answer» C. It runs successfully
138.

What is the output of this program?class mainclass { public static void main(String args[]) { boolean var1 = true; boolean var2 = false; if (var1) System.out.println(var1); else System.out.println(var2); } }

A. 0
B. 1
C. true
D. false
Answer» D. false
139.

What is the output of this program? class mainclass { public static void main(String args[]) { char a = 'A'; a++; System.out.print((int)a); } }

A. 66
B. 67
C. 65
D. 64
Answer» B. 67
140.

What is the output of this program? class booloperators { public static void main(String args[]) { boolean var1 = true; boolean var2 = false; System.out.println((var2 & var2)); } }

A. 0
B. 1
C. true
D. false
Answer» E.
141.

Which of the following are legal lines of Java code?int w = (int)888.8; byte x = (byte)100L; long y = (byte)100; byte z = (byte)100L;

A. 1 and 2
B. 2 and 3
C. 3 and 4
D. All statements are correct.
Answer» E.
142.

What is the output of this program? class asciicodes { public static void main(String args[]) { char var1 = 'A'; char var2 = 'a'; System.out.println((int)var1 + " " + (int)var2); } }

A. 162
B. 65 97
C. 67 95
D. 66 98
Answer» C. 67 95
143.

What will be the output of the program? public class X { public static void main(String [] args) { String names [] = new String[5]; for (int x=0; x < args.length; x++) names[x] = args[x]; System.out.println(names[2]); } } and the command line invocation is > java X a b

A. names
B. null
C. Compilation fails
D. An exception is thrown at runtime
Answer» C. Compilation fails
144.

In the given program, how many lines of output will be produced? public class Test { public static void main(String [] args) { int [] [] [] x = new int [3] [] []; int i, j; x[0] = new int[4][]; x[1] = new int[2][]; x[2] = new int[5][]; for (i = 0; i < x.length; i++) { for (j = 0; j < x[i].length; j++) { x[i][j] = new int [i + j + 1]; System.out.println("size = " + x[i][j].length); } } } }

A. 7
B. 9
C. 11
D. 13
Answer» D. 13
145.

Which three are valid declarations of a float? float f1 = -343; float f2 = 3.14; float f3 = 0x12345; float f4 = 42e7; float f5 = 2001.0D; float f6 = 2.81F;

A. 1, 2, 4
B. 2, 3, 5
C. 1, 3, 6
D. 2, 4, 6
Answer» D. 2, 4, 6
146.

Which three are valid declarations of a char? char c1 = 064770; char c2 = 'face'; char c3 = 0xbeef; char c4 = \u0022; char c5 = '\iface'; char c6 = '\uface';

A. 1, 2, 4
B. 1, 3, 6
C. 3, 5
D. 5 only
Answer» C. 3, 5
147.

What will be the output of the program? public class TestDogs { public static void main(String [] args) { Dog [][] theDogs = new Dog[3][]; System.out.println(theDogs[2][0].toString()); } } class Dog { }

A. null
B. theDogs
C. Compilation fails
D. An exception is thrown at runtime
Answer» E.
148.

What will be the output of the program? public class CommandArgsTwo { public static void main(String [] argh) { int x; x = argh.length; for (int y = 1; y <= x; y++) { System.out.print(" " + argh[y]); } } } and the command-line invocation is > java CommandArgsTwo 1 2 3

A. 0 1 2
B. 1 2 3
C. 0 0 0
D. An exception is thrown at runtime
Answer» E.
149.

What will be the output of the program ? public class Test { public static void main(String [] args) { signed int x = 10; for (int y=0; y<5; y++, x--) System.out.print(x + ", "); } }

A. 10, 9, 8, 7, 6,
B. 9, 8, 7, 6, 5,
C. Compilation fails.
D. An exception is thrown at runtime.
Answer» D. An exception is thrown at runtime.
150.

public class F0091 { public void main( String[] args ) { System.out.println( "Hello" + args[0] ); } } What will be the output of the program, if this code is executed with the command line: > java F0091 world

A. Hello
B. Hello Foo91
C. Hello world
D. The code does not run.
Answer» E.