Sail E0 Webinar

MCQs

Total Questions : 86 | Page 5 of 9 pages
Question 41.


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();
}
}
  1.    Compile time Error There is no start() method
  2.    Will print in this order AB CD AB...
  3.    Will print but not be able to predict the Order
  4.    Will print in this order ABCD...ABCD...
 Discuss Question
Answer: Option C. -> Will print but not be able to predict the Order

We cannot predict the order in which threads are going to run.


Question 42.


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();
}
}
  1.    foo
  2.    MyThread foo
  3.    MyThread bar
  4.    foo bar
 Discuss Question
Answer: Option B. -> MyThread foo

Option B is correct because in the first line of main we're constructing an instance of an 

anonymous inner class extending from MyThread. So the MyThread constructor runs 

and prints "MyThread". The next statement in main invokes start() on the new thread 

instance, which causes the overridden run() method (the run() method defined in the 

anonymous inner class) to be invoked, which prints "foo"


Question 43.

Which class or interface defines the wait(), notify(),and notifyAll() methods?


  1.    Object
  2.    Thread
  3.    Runnable
  4.    Class
 Discuss Question
Answer: Option A. -> Object

The Object class defines these thread-specific methods.

Option B, C, and D are incorrect because they do not define these methods. And yes, the Java 

API does define a class called Class, though you do not need to know it for the exam.


Question 44.

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


  1.    notify()
  2.    wait()
  3.    InputStream access
  4.    sleep()
 Discuss Question
Answer: Option A. -> notify()

Option A is correct. notify() - wakes up a single thread that is waiting on this object's monitor.

Option B is wrong. wait() causes the current thread to wait until another thread invokes the 

notify() method or the notifyAll() method for this object.

Option C is wrong. Methods of the InputStream class block until input data is available, the end 

of the stream is detected, or an exception is thrown. Blocking means that a thread may stop until 

certain conditions are met.

Option D is wrong. sleep() - Causes the currently executing thread to sleep (temporarily cease 

execution) for a specified number of milliseconds. The thread does not lose ownership of any 

monitors.


Question 45.

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?


  1.    After thread A is notified, or after two seconds.
  2.    After the lock on B is released, or after two seconds.
  3.    Two seconds after thread A is notified.
  4.    Two seconds after lock B is released.
 Discuss Question
Answer: Option A. -> After thread A is notified, or after two seconds.

Option A. Either of the two events (notification or wait time expiration) will make the thread 

become a candidate for running again.

Option B is incorrect because a waiting thread will not return to runnable when the lock is 

released, unless a notification occurs.

Option C is incorrect because the thread will become a candidate immediately after notification, 

not two seconds afterwards.

Option D is also incorrect because a thread will not come out of a waiting pool just because a l

ock has been released.


Question 46.

Which will contain the body of the thread?


  1.    run();
  2.    start();
  3.    stop();
  4.    main();
 Discuss Question
Answer: Option A. -> run();

Option A is Correct. The run() method to a thread is like the main() method to an application. 

Starting the thread causes the object's run method to be called in that separately executing 

thread.

Option B is wrong. The start() method causes this thread to begin execution; the Java Virtual 

Machine calls the run method of this thread.

Option C is wrong. The stop() method is deprecated. It forces the thread to stop executing.

Option D is wrong. Is the main entry point for an application.


Question 47.

Which method registers a thread in a thread scheduler?


  1.    run();
  2.    construct();
  3.    start();
  4.    register();
 Discuss Question
Answer: Option C. -> start();

Option C is correct. The start() method causes this thread to begin execution; the Java Virtual 

Machine calls the run method of this thread.

Option A is wrong. The run() method of a thread is like the main() method to an application. 

Starting the thread causes the object's run method to be called in that separately executing 

thread.

Option B is wrong. There is no construct() method in the Thread class.

Option D is wrong. There is no register() method in the Thread class.


Question 48.


What is the output of this program?


class newthread extends Thread {
Thread t1,t2;
newthread() {
t1 = new Thread(this,"Thread_1");
t2 = new Thread(this,"Thread_2");
t1.start();
t2.start();
}
public void run() {
t2.setPriority(Thread.MAX_PRIORITY);
System.out.print(t1.equals(t2));
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
  1.    true
  2.    false
  3.    truetrue
  4.    falsefalse
 Discuss Question
Answer: Option D. -> falsefalse

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.

Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse


Question 49.


What is the output of this program?


class newthread extends Thread {
Thread t;
newthread() {
t1 = new Thread(this,"Thread_1");
t2 = new Thread(this,"Thread_2");
t1.start();
t2.start();
}
public void run() {
t2.setPriority(Thread.MAX_PRIORITY);
System.out.print(t1.equals(t2));
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
  1.    true
  2.    false
  3.    truetrue
  4.    falsefalse
 Discuss Question
Answer: Option D. -> falsefalse

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.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse


Question 50.


What is the output of this program?


class newthread implements Runnable {
Thread t;
newthread() {
t1 = new Thread(this,"Thread_1");
t2 = new Thread(this,"Thread_2");
t1.start();
t2.start();
}
public void run() {
t2.setPriority(Thread.MAX_PRIORITY);
System.out.print(t1.equals(t2));
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
  1.    true
  2.    false
  3.    truetrue
  4.    falsefalse
 Discuss Question
Answer: Option D. -> falsefalse

Threads t1 & t2 are created by class newthread that is implementing runnable interface, 

hence both the threads are provided their own run() method specifying the actions to be

 taken. When constructor of newthread class is called first the run() method of t1 executes 

than the run method of t2 printing 2 times “false” as both the threads are not equal one is

 having different priority than other, hence falsefalse is printed.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse


Latest Videos

Latest Test Papers