Java Multiple Choice Questions & Answers on Creating Threads for Freshers

1. Which of these keywords are used to implement synchronization?

a) sunchronize
b) syn
c) synch
d) synchronized
Answer: d
2. Which of these method is used to avoid polling in Java?

a) wait()
b) notify()
c) notifyAll()
d) All of the mentioned
Answer: d

Explanation: Polling is a usually implemented by looping in CPU is wastes CPU’s time, one thread being executed depends on other thread output and the other thread depends on the response on the data given to the first thread. In such situation CPU’s time is wasted, in Java this is avoided by using methods wait(), notify() and notifyAll().
3. Which of these method is used to tell the calling thread to give up monitor and go to sleep until some other thread enters the same monitor?

a) wait()
b) notify()
c) notifyAll()
d) sleep()
Answer: a

Explanation: wait() method is used to tell the calling thread to give up monitor and go to sleep until some other thread enters the same monitor. This helps in avoiding polling and minimizes CPU’s idle time.
4. Which of these method wakes up the first thread that called wait()?

a) wake()
b) notify()
c) start()
d) notifyAll()
Answer: b
5. Which of these method wakes up all the threads?

a) wakeAll()
b) notify()
c) start()
d) notifyAll()
Answer: d

Explanation: notifyAll() wakes up all the threads that called wait() on the same object. The highest priority thread will run first.
6. What is synchronization in reference to a thread?

a) Its a process of handling situations when two or more threads need access to a shared resource.
b) Its a process by which many thread are able to access same shared resource simultaneously.
c) Its a process by which a method is able to access many different threads simultaneously.
d) Its a method that allow to many threads to access any information the require.
Answer: a

Explanation: When two or more threads need to access the same shared resource, they need some way to ensure that the resource will be used by only one thread at a time, the process by which this is achieved is called synchronization
7. What is the output of this program?
  1.     class newthread extends Thread {
  2.  Thread t;
  3.  String name;
  4.  newthread(String threadname) {
  5.      name = threadname;
  6.      t = new Thread(this,name);
  7.      t.start();
  8.  }
  9.  public void run() {
  10.         }
  11.  
  12.     }
  13.     class multithreaded_programing {
  14.         public static void main(String args[]) {
  15.      newthread obj1 =   new newthread("one");
  16.      newthread obj2 =  new newthread("two");
  17.             try {
  18.                 obj1.t.wait(); 
  19.                 System.out.print(obj1.t.isAlive());
  20.             }
  21.             catch(Exception e) {
  22.      System.out.print("Main thread interrupted");
  23.             }
  24.         }
  25.     }
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
Answer: c

Explanation: obj1.t.wait() causes main thread to go out of processing in sleep state hence causes exception and “Main thread interrupted” is printed.
8. What is the output of this program?
  1.     class newthread extends Thread {
  2.  Thread t;
  3.  String name;
  4.  newthread(String threadname) {
  5.      name = threadname;
  6.      t = new Thread(this,name);
  7.      t.start();
  8.  }
  9.  public void run() {
  10.         }
  11.  
  12.     }
  13.     class multithreaded_programing {
  14.         public static void main(String args[]) {
  15.      newthread obj1 =   new newthread("one");
  16.      newthread obj2 =  new newthread("two");
  17.             try {
  18.                 Thread.sleep(1000); 
  19.                 System.out.print(obj1.t.isAlive());
  20.             }
  21.             catch(InterruptedException e) {
  22.      System.out.print("Main thread interrupted");
  23.             }
  24.         }
  25.     }
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
Answer: b

Explanation: Thread.sleep(1000) has caused all the threads to be suspended for some time, hence onj1.t.isAlive() returns false.
9. What is the output of this program?
  1.     class newthread extends Thread {
  2.  Thread t;
  3.  String name;
  4.  newthread(String threadname) {
  5.      name = threadname;
  6.      t = new Thread(this,name);
  7.      t.start();
  8.  }
  9.  public void run() {
  10.         }
  11.  
  12.     }
  13.     class multithreaded_programing {
  14.         public static void main(String args[]) {
  15.      newthread obj1 =   new newthread("one");
  16.      newthread obj2 =  new newthread("two");
  17.             try {
  18.                  System.out.print(obj1.t.equals(obj2.t));
  19.             }
  20.             catch(Exception e) {
  21.      System.out.print("Main thread interrupted");
  22.             }
  23.         }
  24.     }
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
Answer: b

Explanation: Both obj1 and obj2 have threads with different name that is “one” and “two” hence obj1.t.equals(obj2.t) returns false.
10. What is the output of this program?
  1.     class newthread extends Thread {
  2.  Thread t;
  3.  newthread() {
  4.      t1 = new Thread(this,"Thread_1");
  5.      t2 = new Thread(this,"Thread_2");
  6.      t1.start();
  7.      t2.start();
  8.  }
  9.  public void run() {
  10.      t2.setPriority(Thread.MAX_PRIORITY); 
  11.      System.out.print(t1.equals(t2));
  12.         }    
  13.     }
  14.     class multithreaded_programing {
  15.         public static void main(String args[]) {
  16.             new newthread();        
  17.         }
  18.     }
a) true
b) false
c) truetrue
d) falsefalse
Answer: d

Explanation: This program was previously done by using Runnable interface, here we have used Thread class. This shows both the method are equivalent, we can use any of them to create a thread.

Related

Multiple Choice Questions 4412265908903157997

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item