Explore topic-wise MCQs in Java Programming.

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

1.

Under which conditions will a currently executing thread stop?

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

Which method must be defined by a class implementing the interface?

A. void run()
B. public void run()
C. public void start()
D. void run(int priority)
Answer» C. public void start()
3.

Which two can be used to create a new Thread?

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

Which two statements are true?

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

The following block of code creates a Thread using a Runnable 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(){}}
6.

Which class or interface defines the , ,and methods?

A. Object
B. Thread
C. Runnable
D. Class
Answer» B. Thread
7.

which of these will create and start this thread?

A. new Runnable(MyRunnable).start();
B. new Thread(MyRunnable).run();
C. new Thread(new MyRunnable()).start();
D. new MyRunnable().start();
Answer» D. new MyRunnable().start();
8.

Which of the following line of code is suitable to start a thread ?

A. Thread t = new Thread(X);
B. Thread t = new Thread(X); t.start();
C. X run = new X(); Thread t = new Thread(run); t.start();
D. Thread t = new Thread(); x.run();
Answer» D. Thread t = new Thread(); x.run();
9.

What will be the output of the program? public class SyncTest { public static void main (String [] args) { Thread t = new Thread() { Foo f = new Foo(); public void run() { f.increase(20); } }; t.start(); } } class Foo { private int data = 23; public void increase(int amt) { int x = data; data = x + amt; } } and assuming that data must be protected from corruption, what—if anything—can you add to the preceding code to ensure the integrity of data?

A. Synchronize the run method.
B. Wrap a synchronize(this) around the call to f.increase().
C. The existing code will cause a runtime exception.
D. Synchronize the increase() method
Answer» E.
10.

Thread synchronization is required because ___________

A. all threads of a process share the same address space
B. all threads of a process share the same global variables
C. all threads of a process can share the same files
D. all of the mentioned
Answer» E.
11.

Termination of the process terminates ___________

A. first thread of the process
B. first two threads of the process
C. all threads within the process
D. no thread within the process
Answer» D. no thread within the process
12.

The jacketing technique is used to ___________

A. convert a blocking system call into non blocking system call
B. create a new thread
C. communicate between threads
D. terminate a thread
Answer» B. create a new thread
13.

The time required to create a new thread in an existing process is ___________

A. greater than the time required to create a new process
B. less than the time required to create a new process
C. equal to the time required to create a new process
D. none of the mentioned
Answer» C. equal to the time required to create a new process
14.

If one thread opens a file with read privileges then ___________

A. other threads in the another process can also read from that file
B. other threads in the same process can also read from that file
C. any other thread can not read from that file
D. all of the mentioned
Answer» C. any other thread can not read from that file
15.

A process can be ___________

A. single threaded
B. multithreaded
C. both single threaded and multithreaded
D. none of the mentioned
Answer» D. none of the mentioned
16.

What will be the output after compiling and executing the following code?public class Test implements Runnable{public static void main(String[] args) throws InterruptedException{Thread a = new Thread(new Test());a.start();System.out.print("Begin");a.join();System.out.print("End");}public void run(){System.out.print("Run");}}

A. ompilation fails.
B. n exception is thrown at runtime.
C. BeginRunEnd" is printed.
D. BeginEndRun" is printed.
E. BeginEnd" is printed.
Answer» D. BeginEndRun" is printed.
17.

Predict the output:public class Test extends Thread{private int i;public void run(){i++;}public static void main(String[] args){Test a = new Test();a.run();System.out.print(a.i);a.start();System.out.print(a.i);}}

A. rints
B. rints
C. rints
D. ompiler error
E. llegalThreadStateException is thrown
Answer» D. ompiler error
18.

What is the output for the below code ?class A implements Runnable{public void run(){System.out.println(Thread.currentThread().getName());}}1. public class Test{2.public static void main(String... args){3.A a = new A();4.Thread t = new Thread(a);5.t.setName("good");6.t.start();7.}8. }

A. ood
B. ull
C. ompilation fails with an error at line 5
D. ompilation succeed but Runtime Exception
E. one of these
Answer» B. ull
19.

What is the output for the below code ?public class Test extends Thread{public static void main(String argv[]){Test t = new Test();t.run();}public void start(){for(int i = 0; i < 10; i++){System.out.println("Value of i = " + i);}}}

A. compile time error indicating that no run method is defined for the Thread class
B. run time error indicating that no run method is defined for the Thread class
C. lean compile and at run time the values 0 to 9 are printed out
D. lean compile but no output at runtime
E. one of these
Answer» E. one of these
20.

Given the code. What will be the result?public class Test implements Runnable{public static void main(String[] args) throws InterruptedException{Thread a = new Thread(new Test());a.start();System.out.print("Begin");a.join();System.out.print("End");}public void run(){System.out.print("Run");}}

A. ompilation fails.
B. n exception is thrown at runtime.
C. BeginRunEnd" is printed.
D. BeginEndRun" is printed.
E. BeginEnd" is printed.
Answer» D. BeginEndRun" is printed.
21.

What will be output of the following program code?public class Test implements Runnable{public void run(){System.out.print("go");}public static void main(String arg[]) {Thread t = new Thread(new Test());t.run();t.run();t.start();}}

A. ompilation fails.
B. n exception is thrown at runtime.
C. go" is printed
D. gogogo" is printed
E. gogo" is printed
Answer» E. gogo" is printed
22.

What will happen when you attempt to compile and run the following code?class A implements Runnable{public void run(){System.out.println("run-A");}}1. public class Test{2.public static void main(String argv[]){3.A a = new A();4.Thread t = new Thread(a);5.System.out.println(t.isAlive());6.t.start();7.System.out.println(t.isAlive());8.}9. }

A. alse run-A true
B. alse run-A false
C. rue run-A true
D. ompilation fails due to an error on line 7
E. one of these
Answer» B. alse run-A false
23.

What will be the output?class A extends Thread{public void run(){for(int i=0; i<2; i++){System.out.println(i);}}}public class Test{public static void main(String argv[]){Test t = new Test();t.check(new A(){});}public void check(A a){a.start();}}

A. 0
B. ompilation error, class A has no start method
C. 1
D. ompilation succeed but runtime exception
E. one of these
Answer» D. ompilation succeed but runtime exception
24.

Predict the output:class A implements Runnable{public void run(){try{for(int i=0;i<4;i++){Thread.sleep(100);System.out.println(Thread.currentThread().getName());}}catch(InterruptedException e){}}}public class Test{public static void main(String argv[]) throws Exception{A a = new A();Thread t = new Thread(a, "A");Thread t1 = new Thread(a, "B");t.start();t.join();t1.start();}}

A. A A A B B B B
B. B A B A B A B
C. utput order is not guaranteed
D. ompilation succeed but Runtime Exception
E. one of these
Answer» B. B A B A B A B
25.

What will happen after compiling and running following code?class A implements Runnable{public void run(){System.out.println("run-a");}}1. public class Test{2.public static void main(String... args){3.A a = new A();4.Thread t = new Thread(a);5.t.start();6.t.start();7.}8. }

A. un-a
B. un-a run-a
C. ompilation fails with an error at line 6
D. ompilation succeed but Runtime Exception
E. one of these
Answer» E. one of these
26.

What will happen when you attempt to compile and run the following code?1. public class Test extends Thread{2.public static void main(String argv[]){3.Test t = new Test();4.t.run();5.t.start();6.}7.public void run(){8.System.out.println("run-test");9.}10. }

A. un-test run-test
B. un-test
C. ompilation fails due to an error on line 4
D. ompilation fails due to an error on line 7
E. one of these
Answer» B. un-test
27.

What will be the output?class One extends Thread{ public void run(){for(int i=0; i<2; i++){System.out.print(i);} }}public class Test{ public static void main(String args[]){Test t = new Test();t.call(new One()); }public void call(One o){o.start(); }}

A. 0
B. ompilation Error
C. 1
D. one of these
Answer» D. one of these
28.

Analyze the following code:public class Test implements Runnable{public static void main(String[] args){Test t = new Test();}public Test(){Thread t = new Thread(this);t.start();}public void run(){System.out.println("test");}}

A. he program has a compilation error because t is defined in both the main() method and the constructor Test().
B. he program compiles fine, but it does not run because you cannot use the keyword this in the constructor.
C. he program compiles and runs and displays nothing.
D. he program compiles and runs and displays test.
Answer» E.
29.

Analyze the following code:public class Test implements Runnable{public static void main(String[] args){Test t = new Test();t.start();}public void run() { }}

A. he program does not compile because the start() method is not defined in the Test class.
B. he program compiles, but it does not run because the start() method is not defined.
C. he program compiles, but it does not run because the run() method is not implemented.
D. he program compiles and runs fine.
Answer» B. he program compiles, but it does not run because the start() method is not defined.
30.

Analyze the following code:public abstract class Test implements Runnable{public void doSomething() { };}

A. he program will not compile because it does not implement the run() method.
B. he program will not compile because it does not contain abstract methods.
C. he program compiles fine.
D. one of the above
Answer» D. one of the above
31.

What will be the output of the following program code?public class Test implements Runnable{public static void main(String[] args){Thread t = new Thread(this);t.start();}public void run(){System.out.println("test");}}

A. he program does not compile because this cannot be referenced in a static method.
B. he program compiles fine, but it does not print anything because t does not invoke the run() method
C. he program compiles and runs fine and displays test on the console.
D. one of the above
Answer» B. he program compiles fine, but it does not print anything because t does not invoke the run() method
32.

Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000) After calling this method, when will the thread A become a candidate to get another turn at the CPU?

A. After thread A is notified, or after two seconds
B. After the lock on B is released, or after two seconds.
C. Two seconds after thread A is notified.
D. Two seconds after lock B is released.
Answer» B. After the lock on B is released, or after two seconds.
33.

What will be the output of the program? public class ThreadTest extends Thread { public void run() { System.out.println("In run"); yield(); System.out.println("Leaving run"); } public static void main(String []argv) { (new ThreadTest()).start(); } }

A. The code fails to compile in the main() method
B. The code fails to compile in the run() method
C. Only the text "In run" will be displayed
D. The text "In run" followed by "Leaving run" will be displayed
Answer» E.
34.

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

What will be the output of the program? public class WaitTest { public static void main(String [] args) { System.out.print("1 "); synchronized(args) { System.out.print("2 "); try { args.wait(); /* Line 11 */ } catch(InterruptedException e){ } } System.out.print("3 "); } }

A. It fails to compile because the IllegalMonitorStateException of wait() is not dealt with in line 11.
B. 1 2 3
C. 1 3
D. 1 2
Answer» E.
36.

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

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

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

What will be the output of the program? public class Test { public static void main (String [] args) { final Foo f = new Foo(); Thread t = new Thread(new Runnable() { public void run() { f.doStuff(); } }); Thread g = new Thread() { public void run() { f.doStuff(); } }; t.start(); g.start(); } } class Foo { int x = 5; public void doStuff() { if (x < 10) { // nothing to do try { wait(); } catch(InterruptedException ex) { } } else { System.out.println("x is " + x++); if (x >= 10) { notify(); } } } }

A. The code will not compile because of an error on notify(); of class Foo.
B. The code will not compile because of some other error in class Test.
C. An exception occurs at runtime.
D. It prints "x is 5 x is 6".
Answer» D. It prints "x is 5 x is 6".
40.

What will be the output of the program? public class SyncTest { public static void main (String [] args) { Thread t = new Thread() { Foo f = new Foo(); public void run() { f.increase(20); } }; t.start(); } } class Foo { private int data = 23; public void increase(int amt) { int x = data; data = x + amt; } } and assuming that data must be protected from corruption, what—if anything—can you add to the preceding code to ensure the integrity of data?

A. Synchronize the run method.
B. Wrap a synchronize(this) around the call to f.increase().
C. The existing code will cause a runtime exception.
D. Synchronize the increase() method
Answer» E.
41.

class Test { public static void main(String [] args) { printAll(args); } public static void printAll(String[] lines) { for(int i = 0; i < lines.length; i++) { System.out.println(lines[i]); Thread.currentThread().sleep(1000); } } } the static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?

A. Each String in the array lines will output, with a 1-second pause.
B. Each String in the array lines will output, with no pause in between because this method is not executed in a Thread.
C. Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread.
D. This code will not compile.
Answer» E.
42.

What will be the output of the program? class Test116 { static final StringBuffer sb1 = new StringBuffer(); static final StringBuffer sb2 = new StringBuffer(); public static void main(String args[]) { new Thread() { public void run() { synchronized(sb1) { sb1.append("A"); sb2.append("B"); } } }.start(); new Thread() { public void run() { synchronized(sb1) { sb1.append("C"); sb2.append("D"); } } }.start(); /* Line 28 */ System.out.println (sb1 + " " + sb2); } }

A. main() will finish before starting threads.
B. main() will finish in the middle of one thread.
C. main() will finish after one thread.
D. Cannot be determined.
Answer» E.
43.

What will be the output of the program? class s1 extends Thread { public void run() { for(int i = 0; i < 3; i++) { System.out.println("A"); System.out.println("B"); } } } class Test120 extends Thread { public void run() { for(int i = 0; i < 3; i++) { System.out.println("C"); System.out.println("D"); } } public static void main(String args[]) { s1 t1 = new s1(); Test120 t2 = new Test120(); t1.start(); t2.start(); } }

A. Compile time Error There is no start() method
B. Will print in this order AB CD AB...
C. Will print but not be able to predict the Order
D. Will print in this order ABCD...ABCD...
Answer» D. Will print in this order ABCD...ABCD...
44.

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"
45.

What will be the output of the program? class s1 implements Runnable { int x = 0, y = 0; int addX() {x++; return x;} int addY() {y++; return y;} public void run() { for(int i = 0; i < 10; i++) System.out.println(addX() + " " + addY()); } public static void main(String args[]) { s1 run1 = new s1(); s1 run2 = new s1(); Thread t1 = new Thread(run1); Thread t2 = new Thread(run2); t1.start(); t2.start(); } }

A. Compile time Error: There is no start() method
B. Will print in this order: 1 1 2 2 3 3 4 4 5 5...
C. Will print but not exactly in an order (e.g: 1 1 2 2 1 1 3 3...)
D. Will print in this order: 1 2 3 4 5 6... 1 2 3 4 5 6...
Answer» D. Will print in this order: 1 2 3 4 5 6... 1 2 3 4 5 6...
46.

Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU?

A. After thread A is notified, or after two seconds.
B. After the lock on B is released, or after two seconds.
C. Two seconds after thread A is notified.
D. Two seconds after lock B is released.
Answer» B. After the lock on B is released, or after two seconds.
47.

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."
48.

public class MyRunnable implements Runnable { public void run() { // some code here } } which of these will create and start this thread?

A. new Runnable(MyRunnable).start();
B. new Thread(MyRunnable).run();
C. new Thread(new MyRunnable()).start();
D. new MyRunnable().start();
Answer» D. new MyRunnable().start();
49.

What will be the output of the program? public class Q126 implements Runnable { private int x; private int y; public static void main(String [] args) { Q126 that = new Q126(); (new Thread(that)).start( ); /* Line 8 */ (new Thread(that)).start( ); /* Line 9 */ } public synchronized void run( ) /* Line 11 */ { for (;;) /* Line 13 */ { x++; y++; System.out.println("x = " + x + "y = " + y); } } }

A. An error at line 11 causes compilation to fail
B. Errors at lines 8 and 9 cause compilation to fail.
C. The program prints pairs of values for x and y that might not always be the same on the same line (for example, "x=2, y=1")
D. The program prints pairs of values for x and y that are always the same on the same line (for example, "x=1, y=1". In addition, each value appears once (for example, "x=1, y=1" followed by "x=2, y=2")
Answer» E.
50.

What will be the output of the program? public class Test107 implements Runnable { private int x; private int y; public static void main(String args[]) { Test107 that = new Test107(); (new Thread(that)).start(); (new Thread(that)).start(); } public synchronized void run() { for(int i = 0; i < 10; i++) { x++; y++; System.out.println("x = " + x + ", y = " + y); /* Line 17 */ } } }

A. Compilation error.
B. Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.
C. Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.
D. Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
Answer» D. Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...