Explore topic-wise MCQs in Engineering.

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

1.

class HappyGarbage01 { public static void main(String args[]) { HappyGarbage01 h = new HappyGarbage01(); h.methodA(); /* Line 6 */ } Object methodA() { Object obj1 = new Object(); Object [] obj2 = new Object[1]; obj2[0] = obj1; obj1 = null; return obj2[0]; } }
Where will be the most chance of the garbage collector being invoked?

A. After line 9
B. After line 10
C. After line 11
D. Garbage collector never invoked in
E. <i class="java-code">methodA()</i>
Answer» E. <i class="java-code">methodA()</i>
2.

What will be the output of the program?

public class Foo { public static void main(String[] args) { try { return; } finally { System.out.println( "Finally" ); } } }

A. Finally
B. Compilation fails.
C. The code runs with no output.
D. An exception is thrown at runtime.
Answer» B. Compilation fails.
3.

Which one is a valid declaration of a boolean?

A. boolean b1 = 0;
B. boolean b2 = 'false';
C. boolean b3 = false;
D. boolean b4 = Boolean.false();
E. boolean b5 = no;
Answer» D. boolean b4 = Boolean.false();
4.

Which one of the following will declare an array and initialize it with five numbers?

A. Array a = new Array(5);
B. int [] a = {23,22,21,20,19};
C. int a [] = new int[5];
D. int [5] array;
Answer» C. int a [] = new int[5];
5.

Which three are valid declarations of a float?

  1. float f1 = -343;
  2. float f2 = 3.14;
  3. float f3 = 0x12345;
  4. float f4 = 42e7;
  5. float f5 = 2001.0D;
  6. 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
6.

Which is a valid declarations of a String?

A. String s1 = null;
B. String s2 = 'null';
C. String s3 = (String) 'abc';
D. String s4 = (String) ' ufeed';
Answer» B. String s2 = 'null';
7.

Which three are legal array declarations?

  1. int [] myScores [];
  2. char [] myChars;
  3. int [6] myScores;
  4. Dog myDogs [];
  5. Dog myDogs [7];

A. 1, 2, 4
B. 2, 4, 5
C. 2, 3, 4
D. All are correct.
Answer» B. 2, 4, 5
8.

Which three are valid declarations of a char?

  1. char c1 = 064770;
  2. char c2 = 'face';
  3. char c3 = 0xbeef;
  4. char c4 = u0022;
  5. char c5 = ' iface';
  6. char c6 = ' uface';

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

Which is the valid declarations within an interface definition?

A. public double methoda();
B. public final double methoda();
C. static void methoda(double d1);
D. protected void methoda(double d1);
Answer» B. public final double methoda();
10.

public interface Foo { int k = 4; /* Line 3 */
}
Which three piece of codes are equivalent to line 3?
  1. final int k = 4;
  2. public int k = 4;
  3. static int k = 4;
  4. abstract int k = 4;
  5. volatile int k = 4;
  6. protected int k = 4;

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

Which four options describe the correct default values for array elements of the types indicated?

  1. int -> 0
  2. String -> "null"
  3. Dog -> null
  4. char -> ' u0000'
  5. float -> 0.0f
  6. boolean -> true

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

Which one of these lists contains only Java programming language keywords?

A. class, if, void, long, Int, continue
B. goto, instanceof, native, finally, default, throws
C. try, virtual, throw, final, volatile, transient
D. strictfp, constant, super, implements, do
E. byte, break, assert, switch, include
Answer» C. try, virtual, throw, final, volatile, transient
13.

What will be the output of the program?

public class Test { public static void leftshift(int i, int j) { i <<= j; } public static void main(String args[]) { int i = 4, j = 2; leftshift(i, j); System.out.println(i); } }

A. 2
B. 4
C. 8
D. 16
Answer» C. 8
14.

What will be the output of the program?

class BitShift { public static void main(String [] args) { int x = 0x80000000; System.out.print(x + " and "); x = x >>> 31; System.out.println(x); }
}

A. -2147483648 and 1
B. 0x80000000 and 0x00000001
C. -2147483648 and -1
D. 1 and -2147483648
Answer» B. 0x80000000 and 0x00000001
15.

Which is a reserved word in the Java programming language?

A. method
B. native
C. subclasses
D. reference
E. array
Answer» C. subclasses
16.

Which is a valid keyword in java?

A. interface
B. string
C. Float
D. unsigned
Answer» B. string
17.

What will be the output of the program?

public class CommandArgsThree { public static void main(String [] args) { String [][] argCopy = new String[2][2]; int x; argCopy[0] = args; x = argCopy[0].length; for (int y = 0; y < x; y++) { System.out.print(" " + argCopy[0][y]); } }
}

and the command-line invocation is

> java CommandArgsThree 1 2 3

A. 0 0
B. 1 2
C. 0 0 0
D. 1 2 3
Answer» E.
18.

What will be the output of the program?

public class CommandArgs { public static void main(String [] args) { String s1 = args[1]; String s2 = args[2]; String s3 = args[3]; String s4 = args[4]; System.out.print(" args[2] = " + s2); }
}

and the command-line invocation is

> java CommandArgs 1 2 3 4

A. args[2] = 2
B. args[2] = 3
C. args[2] = null
D. An exception is thrown at runtime.
Answer» E.
19.

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.
20.

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.
21.

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
E. Compilation fails
Answer» D. 13
22.

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
23.

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.
24.

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.
25.

Which will legally declare, construct, and initialize an array?

A. int [] myList = {"1", "2", "3"};
B. int [] myList = (5, 8, 2);
C. int myList [] [] = {4,9,7,0};
D. int myList [] = {4, 3, 7};
Answer» E.
26.

What will be the output of the program?

class PassA { public static void main(String [] args) { PassA p = new PassA(); p.start(); } void start() { long [] a1 = {3,4,5}; long [] a2 = fix(a1); System.out.print(a1[0] + a1[1] + a1[2] + " "); System.out.println(a2[0] + a2[1] + a2[2]); } long [] fix(long [] a3) { a3[1] = 7; return a3; }
}

A. 12 15
B. 15 15
C. 3 4 5 3 7 5
D. 3 7 5 3 7 5
Answer» C. 3 4 5 3 7 5
27.

What will be the output of the program?

class Equals { public static void main(String [] args) { int x = 100; double y = 100.1; boolean b = (x = y); /* Line 7 */ System.out.println(b); }
}

A. true
B. false
C. Compilation fails
D. An exception is thrown at runtime
Answer» D. An exception is thrown at runtime
28.

What will be the output of the program?

class Bitwise { public static void main(String [] args) { int x = 11 & 9; int y = x ^ 3; System.out.println( y | 12 ); }
}

A. 0
B. 7
C. 8
D. 14
Answer» E.
29.

What will be the output of the program?

class SSBool { public static void main(String [] args) { boolean b1 = true; boolean b2 = false; boolean b3 = true; if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */ System.out.print("ok "); if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/ System.out.println("dokey"); }
}

A. ok
B. dokey
C. ok dokey
D. No output is produced
E. Compilation error
Answer» C. ok dokey
30.

What will be the output of the program?

public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (RuntimeException ex) /* Line 10 */ { System.out.print("B"); } catch (Exception ex1) { System.out.print("C"); } finally { System.out.print("D"); } System.out.print("E"); } public static void badMethod() { throw new RuntimeException(); } }

A. BD
B. BCD
C. BDE
D. BCDE
Answer» D. BCDE
31.

public class ExceptionTest { class TestException extends Exception {} public void runTest() throws TestException {} public void test() /* Point X */ { runTest(); } }
At Point X on line 5, which code is necessary to make the code compile?

A. No code is necessary.
B. throws Exception
C. catch ( Exception e )
D. throws RuntimeException
Answer» C. catch ( Exception e )
32.

Which two statements are true?

  1. Deadlock will not occur if wait()/notify() is used
  2. A thread will resume execution as soon as its sleep duration expires.
  3. Synchronization can prevent two objects from being accessed by the same thread.
  4. The wait() method is overloaded to accept a duration.
  5. The notify() method is overloaded to accept a duration.
  6. Both wait() and notify() must be called from a synchronized context.

A. 1 and 2
B. 3 and 5
C. 4 and 6
D. 1 and 3
Answer» D. 1 and 3
33.

What will be the output of the program?

public class X { public static void main(String [] args) { try { badMethod(); /* Line 7 */ System.out.print("A"); } catch (Exception ex) /* Line 10 */ { System.out.print("B"); /* Line 12 */ } finally /* Line 14 */ { System.out.print("C"); /* Line 16 */ } System.out.print("D"); /* Line 18 */ } public static void badMethod() { throw new RuntimeException(); } }

A. AB
B. BC
C. ABC
D. BCD
Answer» E.
34.

What will be the output of the program?

public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (Exception ex) { System.out.print("B"); } finally { System.out.print("C"); } System.out.print("D"); } public static void badMethod() { throw new Error(); /* Line 22 */ } }

A. ABCD
B. Compilation fails.
C. C is printed before exiting with an error message.
D. BC is printed before exiting with an error message.
Answer» D. BC is printed before exiting with an error message.
35.

void start() { A a = new A(); B b = new B(); a.s(b); b = null; /* Line 5 */ a = null; /* Line 6 */ System.out.println("start completed"); /* Line 7 */
} 
When is the B object, created in line 3, eligible for garbage collection?

A. after line 5
B. after line 6
C. after line 7
D. There is no way to be absolutely certain.
Answer» E.
36.

class X2 { public X2 x; public static void main(String [] args) { X2 x2 = new X2(); /* Line 6 */ X2 x3 = new X2(); /* Line 7 */ x2.x = x3; x3.x = x2; x2 = new X2(); x3 = x2; /* Line 11 */ doComplexStuff(); } } 
after line 11 runs, how many objects are eligible for garbage collection?

A. 0
B. 1
C. 2
D. 3
Answer» D. 3
37.

class Boo { Boo(String s) { } Boo() { }
}
class Bar extends Boo { Bar() { } Bar(String s) {super(s);} void zoo() { // insert code here }
}
which one create an anonymous inner class from within class Bar?

A. Boo f = new Boo(24) { };
B. Boo f = new Bar() { };
C. Bar f = new Boo(String s) { };
D. Boo f = new Boo.Bar(String s) { };
Answer» C. Bar f = new Boo(String s) { };
38.

What will be the output of the program?

class Test { public static void main(String [] args) { int x= 0; int y= 0; for (int z = 0; z < 5; z++) { if (( ++x > 2 ) && (++y > 2)) { x++; } } System.out.println(x + " " + y); }
}

A. 5 2
B. 5 3
C. 6 3
D. 6 4
Answer» D. 6 4
39.

What will be the output of the program?

class Test { public static void main(String [] args) { int x=20; String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge"; System.out.println(sup); }
}

A. small
B. tiny
C. huge
D. Compilation fails
Answer» C. huge
40.

Which of the following are legal lines of code?

  1. int w = (int)888.8;
  2. byte x = (byte)1000L;
  3. long y = (byte)100;
  4. byte z = (byte)100L;

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

Which two statements are equivalent?

  1. 16*4
  2. 16>>2
  3. 16/2^2
  4. 16>>>2

A. 1 and 2
B. 2 and 4
C. 3 and 4
D. 1 and 3
Answer» C. 3 and 4
42.

What will be the output of the program?

class PassS { public static void main(String [] args) { PassS p = new PassS(); p.start(); } void start() { String s1 = "slip"; String s2 = fix(s1); System.out.println(s1 + " " + s2); } String fix(String s1) { s1 = s1 + "stream"; System.out.print(s1 + " "); return "stream"; }
}

A. slip stream
B. slipstream stream
C. stream slip stream
D. slipstream slip stream
Answer» E.
43.

What will be the output of the program?

class Test { public static void main(String [] args) { Test p = new Test(); p.start(); } void start() { boolean b1 = false; boolean b2 = fix(b1); System.out.println(b1 + " " + b2); } boolean fix(boolean b1) { b1 = true; return b1; }
}

A. true true
B. false true
C. true false
D. false false
Answer» C. true false
44.

What will be the output of the program?

class Test { static int s; public static void main(String [] args) { Test p = new Test(); p.start(); System.out.println(s); } void start() { int x = 7; twice(x); System.out.print(x + " "); } void twice(int x) { x = x*2; s = x; }
}

A. 7 7
B. 7 14
C. 14 0
D. 14 14
Answer» C. 14 0
45.

Which constructs an anonymous inner class instance?

A. Runnable r = new Runnable() { };
B. Runnable r = new Runnable(public void run() { });
C. Runnable r = new Runnable { public void run(){}};
D. System.out.println(new Runnable() {public void run() { }});
Answer» E.
46.

What will be the output of the program?

public class HorseTest { public static void main (String [] args) { class Horse { public String name; /* Line 7 */ public Horse(String s) { name = s; } } /* class Horse ends */ Object obj = new Horse("Zippo"); /* Line 13 */ Horse h = (Horse) obj; /* Line 14 */ System.out.println(h.name); }
} /* class HorseTest ends */

A. An exception occurs at runtime at line 10.
B. It prints "Zippo".
C. Compilation fails because of an error on line 7.
D. Compilation fails because of an error on line 13.
Answer» C. Compilation fails because of an error on line 7.
47.

What will be the output of the program?

public abstract class AbstractTest { public int getNum() { return 45; } public abstract class Bar { public int getNum() { return 38; } } public static void main (String [] args) { AbstractTest t = new AbstractTest() { public int getNum() { return 22; } }; AbstractTest.Bar f = t.new Bar() { public int getNum() { return 57; } }; System.out.println(f.getNum() + " " + t.getNum()); }
}

A. 57 22
B. 45 38
C. 45 57
D. An exception occurs at runtime.
Answer» B. 45 38
48.

What allows the programmer to destroy an object x?

A. x.delete()
B. x.finalize()
C. Runtime.getRuntime().gc()
D. Only the garbage collection system can destroy an object.
Answer» E.
49.

What will be the output of the program?

public class TestObj { public static void main (String [] args) { Object o = new Object() /* Line 5 */ { public boolean equals(Object obj) { return true; } } /* Line 11 */ System.out.println(o.equals("Fred")); }
}

A. It prints "true".
B. It prints "Fred".
C. An exception occurs at runtime.
D. Compilation fails
Answer» E.
50.

What will be the output of the program?

class Exc0 extends Exception { } class Exc1 extends Exc0 { } /* Line 2 */
public class Test { public static void main(String args[]) { try { throw new Exc1(); /* Line 9 */ } catch (Exc0 e0) /* Line 11 */ { System.out.println("Ex0 caught"); } catch (Exception e) { System.out.println("exception caught"); } } }

A. <i class="java-code">Ex0</i>
B. caught
C. exception caught
D. Compilation fails because of an error at line 2.
E. Compilation fails because of an error at line 9.
Answer» B. caught