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.

51.

Which two can be used to create a new Thread?

  1. Extend java.lang.Thread and override the run() method.
  2. Extend java.lang.Runnable and override the start() method.
  3. Implement java.lang.Thread and implement the run() method.
  4. Implement java.lang.Runnable and implement the run() method.
  5. Implement java.lang.Thread and implement the start() method.

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

The following block of code creates a Thread using a Runnable target:

Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
Which of the following classes can be used to create the target, so that the preceding code compiles correctly?

A. public class MyRunnable extends Runnable{public void run(){}}
B. public class MyRunnable extends Object{public void run(){}}
C. public class MyRunnable implements Runnable{public void run(){}}
D. public class MyRunnable implements Runnable{void run(){}}
Answer» D. public class MyRunnable implements Runnable{void run(){}}
53.

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() {} } 

A. AC
B. BC
C. ACD
D. ABCD
Answer» D. ABCD
54.

Which statement is true about assertions in the Java programming language?

A. Assertion expressions should not contain side effects.
B. Assertion expression values can be any primitive type.
C. Assertions should be used for enforcing preconditions on public methods.
D. An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method.
Answer» B. Assertion expression values can be any primitive type.
55.

 import java.awt.*;
class Ticker extends Component { public static void main (String [] args) { Ticker t = new Ticker(); /* Missing Statements ? */ }
}
which two of the following statements, inserted independently, could legally be inserted into missing section of this code?
  1. boolean test = (Component instanceof t);
  2. boolean test = (t instanceof Ticker);
  3. boolean test = t.instanceof(Ticker);
  4. boolean test = (t instanceof Component);

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

import java.io.*;
public class MyProgram { public static void main(String args[]) { FileOutputStream out = null; try { out = new FileOutputStream("test.txt"); out.write(122); } catch(IOException io) { System.out.println("IO Error."); } finally { out.close(); } }
}
and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?

A. This program will compile successfully.
B. This program fails to compile due to an error at line 4.
C. This program fails to compile due to an error at line 6.
D. This program fails to compile due to an error at line 18.
Answer» E.
57.

Which three statements are true?

  1. Assertion checking is typically enabled when a program is deployed.
  2. It is never appropriate to write code to handle failure of an assert statement.
  3. Assertion checking is typically enabled during program development and testing.
  4. Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis.
  5. Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.

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

class Foo { class Bar{ }
}
class Test { public static void main (String [] args) { Foo f = new Foo(); /* Line 10: Missing statement ? */ }
}
which statement, inserted at line 10, creates an instance of Bar?

A. Foo.Bar b = new Foo.Bar();
B. Foo.Bar b = f.new Bar();
C. Bar b = new f.Bar();
D. Bar b = f.new Bar();
Answer» C. Bar b = new f.Bar();
59.

System.out.print("Start ");
try { System.out.print("Hello world"); throw new FileNotFoundException();
}
System.out.print(" Catch Here "); /* Line 7 */
catch(EOFException e) { System.out.print("End of file exception");
}
catch(FileNotFoundException e) { System.out.print("File not found");
}
and given that EOFException and FileNotFoundException are both subclasses of IOException, and further assuming this block of code is placed into a class, which statement is most true concerning this code?

A. The code will not compile.
B. Code output: Start Hello world File Not Found.
C. Code output: Start Hello world End of file exception.
D. Code output: Start Hello world Catch Here File not found.
Answer» B. Code output: Start Hello world File Not Found.
60.

Which is true about an anonymous inner class?

A. It can extend exactly one class and implement exactly one interface.
B. It can extend exactly one class and can implement multiple interfaces.
C. It can extend exactly one class or implement exactly one interface.
D. It can implement multiple interfaces regardless of whether it also extends a class.
Answer» D. It can implement multiple interfaces regardless of whether it also extends a class.
61.

public class MyOuter { public static class MyInner { public static void foo() { } }
}
which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class?

A. MyOuter.MyInner m = new MyOuter.MyInner();
B. MyOuter.MyInner mi = new MyInner();
C. <p>MyOuter m = new MyOuter();</p>
D. <p>MyOuter.MyInner mi = m.new MyOuter.MyInner();</p>
E. MyInner mi = new MyOuter.MyInner();
Answer» B. MyOuter.MyInner mi = new MyInner();
62.

What will be the output of the program?

class MyThread extends Thread { MyThread() { System.out.print(" MyThread"); } public void run() { System.out.print(" bar"); } public void run(String s) { System.out.println(" baz"); }
}
public class TestThreads { public static void main (String [] args) { Thread t = new MyThread() { public void run() { System.out.println(" foo"); } }; t.start(); }
}

A. foo
B. MyThread foo
C. MyThread bar
D. foo bar
Answer» C. MyThread bar
63.

Which four can be thrown using the throw statement?

  1. Error
  2. Event
  3. Object
  4. Throwable
  5. Exception
  6. RuntimeException

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

Which two are equal?

  1. 32/4
  2. (8 >> 2) << 4
  3. 2^5
  4. 128 >>> 2
  5. 2 >> 5

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

What will be the output of the program?

class Two { byte x;
} class PassO { public static void main(String [] args) { PassO p = new PassO(); p.start(); } void start() { Two t = new Two(); System.out.print(t.x + " "); Two t2 = fix(t); System.out.println(t.x + " " + t2.x); } Two fix(Two tt) { tt.x = 42; return tt; }
}

A. null null 42
B. 0 0 42
C. 0 42 42
D. 0 0 0
Answer» D. 0 0 0
66.

What will be the output of the program?

try { int x = 0; int y = 5 / x; } catch (Exception e) { System.out.println("Exception"); } catch (ArithmeticException ae) { System.out.println(" Arithmetic Exception"); } System.out.println("finished");

A. finished
B. Exception
C. Compilation fails.
D. Arithmetic Exception
Answer» D. Arithmetic Exception
67.

What will be the output of the program?

public class Foo { Foo() { System.out.print("foo"); } class Bar
{ Bar() { System.out.print("bar"); } public void go() { System.out.print("hi"); }
} /* class Bar ends */ public static void main (String [] args) { Foo f = new Foo(); f.makeBar(); } void makeBar() { (new Bar() {}).go(); }
}/* class Foo ends */

A. Compilation fails.
B. An error occurs at runtime.
C. It prints "foobarhi"
D. It prints "barhi"
Answer» D. It prints "barhi"
68.

public class X { public static void main(String [] args) { X x = new X(); X x2 = m1(x); /* Line 6 */ X x4 = new X(); x2 = x4; /* Line 8 */ doComplexStuff(); } static X m1(X mx) { mx = new X(); return mx; }
}
After line 8 runs. how many objects are eligible for garbage collection?

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

What will be the output of the program?

public class Test { public static void aMethod() throws Exception { try /* Line 5 */ { throw new Exception(); /* Line 7 */ } finally /* Line 9 */ { System.out.print("finally "); /* Line 11 */ } } public static void main(String args[]) { try { aMethod(); } catch (Exception e) /* Line 20 */ { System.out.print("exception "); } System.out.print("finished"); /* Line 24 */ } }

A. finally
B. exception finished
C. finally exception finished
D. Compilation fails
Answer» D. Compilation fails
70.

Which is true about a method-local inner class?

A. It must be marked final.
B. It can be marked abstract.
C. It can be marked public.
D. It can be marked static.
Answer» C. It can be marked public.
71.

What will be the output of the program?

class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } }

A. Prints "Inside Thread Inside Thread"
B. Prints "Inside Thread Inside Runnable"
C. Does not compile
D. Throws exception at runtime
Answer» B. Prints "Inside Thread Inside Runnable"
72.

What will be the output of the program?

class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); t.start(); System.out.print("one. "); t.start(); System.out.print("two. "); } public void run() { System.out.print("Thread "); }
}

A. Compilation fails
B. An exception occurs at runtime.
C. It prints "Thread one. Thread two."
D. The output cannot be determined.
Answer» C. It prints "Thread one. Thread two."
73.

What will be the output of the program?

class SC2 { public static void main(String [] args) { SC2 s = new SC2(); s.start(); } void start() { int a = 3; int b = 4; System.out.print(" " + 7 + 2 + " "); System.out.print(a + b); System.out.print(" " + a + b + " "); System.out.print(foo() + a + b + " "); System.out.println(a + b + foo()); } String foo() { return "foo"; }
}

A. 9 7 7 foo 7 7foo
B. 72 34 34 foo34 34foo
C. 9 7 7 foo34 34foo
D. 72 7 34 foo34 7foo
Answer» E.
74.

What will be the output of the program?

class BoolArray { boolean [] b = new boolean[3]; int count = 0; void set(boolean [] x, int i) { x[i] = true; ++count; } public static void main(String [] args) { BoolArray ba = new BoolArray(); ba.set(ba.b, 0); ba.set(ba.b, 2); ba.test(); } void test() { if ( b[0] && b[1] | b[2] ) count++; if ( b[1] && b[(++count - 2)] ) count += 7; System.out.println("count = " + count); }
}

A. count = 0
B. count = 2
C. count = 3
D. count = 4
Answer» D. count = 4
75.

Which two statements are equivalent?

  1. 3/2
  2. 3<2
  3. 3*4
  4. 3<<2

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

public Object m() { Object o = new Float(3.14F); Object [] oa = new Object[l]; oa[0] = o; /* Line 5 */ o = null; /* Line 6 */ oa[0] = null; /* Line 7 */ return o; /* Line 8 */
}
When is the Float object, created in line 3, eligible for garbage collection?

A. just after line 5
B. just after line 6
C. just after line 7
D. just after line 8
Answer» D. just after line 8
77.

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 3
B. 8 2
C. 8 3
D. 8 5
Answer» C. 8 3
78.

import java.awt.Button;
class CompareReference { public static void main(String [] args) { float f = 42.0f; float [] f1 = new float[2]; float [] f2 = new float[2]; float [] f3 = f1; long x = 42; f1[0] = 42.0f; }
}
which three statements are true?
  1. f1 == f2
  2. f1 == f3
  3. f2 == f1[1]
  4. x == f1[0]
  5. f == f1[0]

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

What will be the output of the program?

public class MyProgram { public static void main(String args[]) { try { System.out.print("Hello world "); } finally { System.out.println("Finally executing "); } }
}

A. Nothing. The program will not compile because no exceptions are specified.
B. Nothing. The program will not compile because no catch clauses are specified.
C. Hello world.
D. Hello world Finally executing
Answer» E.
80.

What will be the output of the program?

class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); Thread x = new Thread(t); x.start(); /* Line 7 */ } public void run() { for(int i = 0; i < 3; ++i) { System.out.print(i + ".."); } }
}

A. Compilation fails.
B. 1..2..3..
C. 0..1..2..3..
D. 0..1..2..
Answer» E.
81.

What will be the output of the program?

class Happy extends Thread { final StringBuffer sb1 = new StringBuffer(); final StringBuffer sb2 = new StringBuffer(); public static void main(String args[]) { final Happy h = new Happy(); new Thread() { public void run() { synchronized(this) { h.sb1.append("A"); h.sb2.append("B"); System.out.println(h.sb1); System.out.println(h.sb2); } } }.start(); new Thread() { public void run() { synchronized(this) { h.sb1.append("D"); h.sb2.append("C"); System.out.println(h.sb2); System.out.println(h.sb1); } } }.start(); } }

A. ABBCAD
B. ABCBCAD
C. CDADACB
D. Output determined by the underlying platform.
Answer» E.
82.

What will be the output of the program?

public class ThreadDemo { private int count = 1; public synchronized void doSomething() { for (int i = 0; i < 10; i++) System.out.println(count++); } public static void main(String[] args) { ThreadDemo demo = new ThreadDemo(); Thread a1 = new A(demo); Thread a2 = new A(demo); a1.start(); a2.start(); } } class A extends Thread { ThreadDemo demo; public A(ThreadDemo td) { demo = td; } public void run() { demo.doSomething(); } }

A. It will print the numbers 0 to 19 sequentially
B. It will print the numbers 1 to 20 sequentially
C. It will print the numbers 1 to 20, but the order cannot be determined
D. The code will not compile.
Answer» C. It will print the numbers 1 to 20, but the order cannot be determined
83.

What will be the output of the program?

class s implements Runnable { int x, y; public void run() { for(int i = 0; i < 1000; i++) synchronized(this) { x = 12; y = 12; } System.out.print(x + " " + y + " "); } public static void main(String args[]) { s run = new s(); Thread t1 = new Thread(run); Thread t2 = new Thread(run); t1.start(); t2.start(); } }

A. DeadLock
B. It print 12 12 12 12
C. Compilation Error
D. Cannot determine output.
Answer» C. Compilation Error
84.

What will be the output of the program?

class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); /* Line 5 */ t.run(); /* Line 6 */ } public void run() { for(int i=1; i < 3; ++i) { System.out.print(i + ".."); } }
}

A. This code will not compile due to line 5.
B. This code will not compile due to line 6.
C. 1..2..
D. 1..2..3..
Answer» D. 1..2..3..
85.

What is the name of the method used to start a thread execution?

A. init();
B. start();
C. run();
D. resume();
Answer» C. run();
86.

What will be the output of the program?

for (int i = 0; i < 4; i += 2) { System.out.print(i + " "); } System.out.println(i); /* Line 5 */

A. 0 2 4
B. 0 2 4 5
C. 0 1 2 3 4
D. Compilation fails.
Answer» E.
87.

What will be the output of the program?

int x = 3; int y = 1; if (x = y) /* Line 3 */
{ System.out.println("x =" + x); }

A. x = 1
B. x = 3
C. Compilation fails.
D. The code runs with no output.
Answer» D. The code runs with no output.
88.

What will be the output of the program?

Float f = new Float("12"); switch (f) { case 12: System.out.println("Twelve"); case 0: System.out.println("Zero"); default: System.out.println("Default"); }

A. Zero
B. Twelve
C. Default
D. Compilation fails
Answer» E.
89.

What will be the output of the program?

int i = 0; while(1) { if(i == 4) { break; } ++i; } System.out.println("i = " + i);

A. i = 0
B. i = 3
C. i = 4
D. Compilation fails.
Answer» E.
90.

What will be the output of the program?

public class Delta { static boolean foo(char c) { System.out.print(c); return true; } public static void main( String[] argv ) { int i = 0; for (foo('A'); foo('B') && (i < 2); foo('C')) { i++; foo('D'); } } }

A. ABDCBDCB
B. ABCDABCD
C. Compilation fails.
D. An exception is thrown at runtime.
Answer» B. ABCDABCD
91.

What will be the output of the program?

public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 3; z++) { switch (z) { case y: System.out.print("0 "); /* Line 11 */ case x-1: System.out.print("1 "); /* Line 12 */ case x: System.out.print("2 "); /* Line 13 */ } } }
}

A. 0 1 2
B. 0 1 2 1 2 2
C. Compilation fails at line 11.
D. Compilation fails at line 12.
Answer» D. Compilation fails at line 12.
92.

What will be the output of the program?

public class If1 { static boolean b; public static void main(String [] args) { short hand = 42; if ( hand < 50 && !b ) /* Line 7 */ hand++; if ( hand > 50 ); /* Line 9 */ else if ( hand > 40 ) { hand += 7; hand++; } else --hand; System.out.println(hand); }
}

A. 41
B. 42
C. 50
D. 51
Answer» E.
93.

What will be the output of the program?

public class Test { public static void main(String [] args) { int I = 1; do while ( I < 1 ) System.out.print("I is " + I); while ( I > 1 ) ; }
}

A. I is 1
B. I is 1 I is 1
C. No output is produced.
D. Compilation error
Answer» D. Compilation error
94.

What will be the output of the program?

int x = l, y = 6; while (y--) { x++; } System.out.println("x = " + x +" y = " + y);

A. x = 6 y = 0
B. x = 7 y = 0
C. x = 6 y = -1
D. Compilation fails.
Answer» E.
95.

What will be the output of the program?

int I = 0; outer: while (true) { I++; inner: for (int j = 0; j < 10; j++) { I += j; if (j == 3) continue inner; break outer; } continue outer; }
System.out.println(I);

A. 1
B. 2
C. 3
D. 4
Answer» B. 2
96.

What will be the output of the program?

public class Test { public static void main(String args[]) { int i = 1, j = 0; switch(i) { case 2: j += 6; case 4: j += 1; default: j += 2; case 0: j += 4; } System.out.println("j = " + j); } }

A. j = 0
B. j = 2
C. j = 4
D. j = 6
Answer» E.
97.

What will be the output of the program?

boolean bool = true; if(bool = false) /* Line 2 */
{ System.out.println("a"); } else if(bool) /* Line 6 */
{ System.out.println("b"); } else if(!bool) /* Line 10 */
{ System.out.println("c"); /* Line 12 */
} else { System.out.println("d"); }

A. a
B. b
C. c
D. d
Answer» D. d
98.

What will be the output of the program?

public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 4; z++) { switch (z) { case x: System.out.print("0 "); default: System.out.print("def "); case x-1: System.out.print("1 "); break; case x-2: System.out.print("2 "); } } }
}

A. 0 def 1
B. 2 1 0 def 1
C. 2 1 0 def def
D. 2 1 0 def 1 def 1
Answer» E.
99.

What will be the output of the program?

int i = 0, j = 5; tp: for (;;) { i++; for (;;) { if(i > --j) { break tp; } } System.out.println("i =" + i + ", j = " + j);

A. i = 1, j = 0
B. i = 1, j = 4
C. i = 3, j = 4
D. Compilation fails.
Answer» E.
100.

What will be the output of the program?

for(int i = 0; i < 3; i++) { switch(i) { case 0: break; case 1: System.out.print("one "); case 2: System.out.print("two "); case 3: System.out.print("three "); } } System.out.println("done");

A. done
B. one two done
C. one two three done
D. one two three two three done
Answer» E.