Sail E0 Webinar

MCQs

Total Questions : 86 | Page 6 of 9 pages
Question 51.


What is the output of this program?


class newthread extends Thread {
Thread t;
newthread() {
t = new Thread(this,"New Thread");
t.start();
}
public void run() {
System.out.println(t.isAlive());
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
  1.    0
  2.    1
  3.    true
  4.    false
 Discuss Question
Answer: Option C. -> true

 isAlive() method is used to check whether the thread being called is running or not, here 

thread is the main() method which is running till the program is terminated hence it returns

 true.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
true


Question 52.

Which method must be defined by a class implementing the java.lang.Runnable interface?


  1.    void run()
  2.    public void run()
  3.    public void start()
  4.    void run(int priority)
 Discuss Question
Answer: Option B. -> public void run()

Option B is correct because in an interface all methods are abstract by default therefore 

they must be overridden by the implementing class. The Runnable interface only contains 

1 method, the void run() method therefore it must be implemented.

Option A and D are incorrect because they are narrowing the access privileges i.e. package

(default) access is narrower than public access.

Option C is not method in the Runnable interface therefore it is incorrect.


Question 53.


What is the output of this program?


class newthread implements Runnable {
Thread t;
newthread() {
t = new Thread(this,"New Thread");
t.start();
}
public void run() {
t.setPriority(Thread.MAX_PRIORITY);
System.out.println(t);
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
  1.    Thread[New Thread,0,main]
  2.    Thread[New Thread,1,main]
  3.    Thread[New Thread,5,main]
  4.    Thread[New Thread,10,main]
 Discuss Question
Answer: Option D. -> Thread[New Thread,10,main]

Thread t has been made with default priority value 5 but in run method the priority has 

been explicitly changed to MAX_PRIORITY of class thread, that is 10 by code 't.setPriority(Thread.MAX_PRIORITY);' using the setPriority function of thread t.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,10,main]


Question 54.


What is the output of this program?


class newthread extends Thread {
Thread t;
String name;
newthread(String threadname) {
name = threadname;
t = new Thread(this,name);
t.start();
}
public void run() {
}
}
class multithreaded_programing {
public static void main(String args[]) {
newthread obj1 = new newthread("one");
newthread obj2 = new newthread("two");
try {
System.out.print(obj1.t.equals(obj2.t));
}
catch(Exception e) {
System.out.print("Main thread interrupted");
}
}
}
  1.    true
  2.    false
  3.    Main thread interrupted
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> false

Both obj1 and obj2 have threads with different name that is "one" and "two" hence obj1.t.

equals(obj2.t) returns false.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
false



Question 55.

Which of the following will directly stop the execution of a Thread?


  1.    wait()
  2.    notify()
  3.    notifyall()
  4.    exits synchronized code
 Discuss Question
Answer: Option A. -> wait()

Option A is correct. wait() causes the current thread to wait until another thread invokes the 

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

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

Option C is wrong. notifyAll() - wakes up all threads that are waiting on this object's monitor.

Option D is wrong. Typically, releasing a lock means the thread holding the lock (in other words, 

the thread currently in the synchronized method) exits the synchronized method. At that point, 

the lock is free until some other thread enters a synchronized method on that object. Does 

entering/exiting synchronized code mean that the thread execution stops? Not necessarily

 because the thread can still run code that is not synchronized. I think the word directly in the 

question gives us a clue. Exiting synchronized code does not directly stop the execution of

 a thread.

Question 56.


What is the output of this program?


class newthread extends Thread {
Thread t;
newthread() {
t = new Thread(this,"My Thread");
t.start();
}
public void run() {
try {
t.join()
System.out.println(t.getName());
}
catch(Exception e) {
System.out.print("Exception");
}
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
  1.    My Thread
  2.    Thread[My Thread,5,main]
  3.    Exception
  4.    Runtime Error
 Discuss Question
Answer: Option D. -> Runtime Error

join() method of Thread class waits for thread being called to finish or terminate, but here 

we have no condition which can terminate the thread, hence code 't.join()' leads to runtime

error and nothing will be printed on the screen.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing



Question 57.


What is the output of this program?


class newthread implements Runnable {
Thread t;
newthread() {
t = new Thread(this,"My Thread");
t.start();
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
  1.    My Thread
  2.    Thread[My Thread,5,main]
  3.    Compilation Error
  4.    Runtime Error
 Discuss Question
Answer: Option C. -> Compilation Error

Thread t has been made by using Runnable interface, hence it is necessary to use 

inherited abstract method run() method to specify instructions to be implemented on 

the thread, since no run() method is used it gives a compilation error.
Output:
$ javac multithreaded_programing.java
The type newthread must implement the inherited abstract method Runnable.run()


Question 58.


What is the output of this program?


class newthread extends Thread {
Thread t;
String name;
newthread(String threadname) {
name = threadname;
t = new Thread(this,name);
t.start();
}
public void run() {
}
}
class multithreaded_programing {
public static void main(String args[]) {
newthread obj1 = new newthread("one");
newthread obj2 = new newthread("two");
try {
Thread.sleep(1000);
System.out.print(obj1.t.isAlive());
}
catch(InterruptedException e) {
System.out.print("Main thread interrupted");
}
}
}
  1.    true
  2.    false
  3.    Main thread interrupted
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> false

Thread.sleep(1000) has caused all the threads to be suspended for some time, 

Question 59.


What is the output of this program?


class newthread implements Runnable {
Thread t;
newthread() {
t = new Thread(this,"My Thread");
t.start();
}
public void run() {
System.out.println(t);
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
  1.    My Thread
  2.    Thread[My Thread,5,main]
  3.    Compilation Error
  4.    Runtime Error
 Discuss Question
Answer: Option B. -> Thread[My Thread,5,main]

None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main]


Question 60.


What is the output of this program?


class newthread extends Thread {
newthread() {
super("My Thread");
start();
}
public void run() {
System.out.println(this);
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
  1.    My Thread
  2.    Thread[My Thread,5,main]
  3.    Compilation Error
  4.    Runtime Error
 Discuss Question
Answer: Option B. -> Thread[My Thread,5,main]

Although we have not created any object of thread class still we can make a thread 

pointing to main method, we can refer it by using this.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main]


Latest Videos

Latest Test Papers