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.

51.

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

Which two statements are true? Deadlock will not occur if wait()/notify() is used A thread will resume execution as soon as its sleep duration expires. Synchronization can prevent two objects from being accessed by the same thread. The wait() method is overloaded to accept a duration. The notify() method is overloaded to accept a duration. 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
53.

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

Which two can be used to create a new Thread? Extend java.lang.Thread and override the run() method. Extend java.lang.Runnable and override the start() method. Implement java.lang.Thread and implement the run() method. Implement java.lang.Runnable and implement the run() method. 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
55.

Which three guarantee that a thread will leave the running state? yield() wait() notify() notifyAll() sleep(1000) aLiveThread.join() Thread.killThread()

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

Which of the following will not directly cause a thread to stop?

A. notify()
B. wait()
C. InputStream access
D. sleep()
Answer» B. wait()
57.

Which two are valid constructors for Thread? Thread(Runnable r, String name) Thread() Thread(int priority) Thread(Runnable r, ThreadGroup g) Thread(Runnable r, int priority)

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

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

Which two of the following methods are defined in class Thread? start() wait() notify() run() terminate()

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

Which three are methods of the Object class? notify(); notifyAll(); isInterrupted(); synchronized(); interrupt(); wait(long msecs); sleep(long msecs); yield();

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

class X implements Runnable { public static void main(String args[]) { /* Missing code? */ } public void run() {} } 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();
62.

Which statement is true?

A. A static method cannot be synchronized.
B. If a class has synchronized code, multiple threads can still access the nonsynchronized code.
C. Variables can be protected from concurrent access problems by marking them with the synchronized keyword.
D. When a thread sleeps, it releases its locks.
Answer» C. Variables can be protected from concurrent access problems by marking them with the synchronized keyword.