Sail E0 Webinar

MCQs

Total Questions : 86 | Page 2 of 9 pages
Question 11. Predict the output:
class A implements Runnable{
public void run(){
try{
for(int i=0;i
  1.    A A A A B B B B
  2.    A B A B A B A B
  3.    Output order is not guaranteed
  4.    Compilation succeed but Runtime Exception
  5.    None of these
 Discuss Question
Answer: Option A. -> A A A A B B B B
Question 12. What will be the output?
class A extends Thread{
public void run(){
for(int i=0; i
  1.    0 0
  2.    Compilation error, class A has no start method
  3.    0 1
  4.    Compilation succeed but runtime exception
  5.    None of these
 Discuss Question
Answer: Option C. -> 0 1
Question 13. 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. }
  1.    false run-A true
  2.    false run-A false
  3.    true run-A true
  4.    Compilation fails due to an error on line 7
  5.    None of these
 Discuss Question
Answer: Option A. -> false run-A true
Question 14. Which of the following are methods of the Thread class?
1) yield()
2) sleep(long msec)
3) go()
4) stop()
  1.    1 , 2 and 4
  2.    1 and 3
  3.    3 only
  4.    None of the above
 Discuss Question
Answer: Option A. -> 1 , 2 and 4
Question 15. What notifyAll() method do?
  1.    Wakes up one threads that are waiting on this object's monitor
  2.    Wakes up all threads that are not waiting on this object's monitor
  3.    Wakes up all threads that are waiting on this object's monitor
  4.    None of the above
 Discuss Question
Answer: Option C. -> Wakes up all threads that are waiting on this object's monitor
Question 16. 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);
}
}
}
  1.    A compile time error indicating that no run method is defined for the Thread class
  2.    A run time error indicating that no run method is defined for the Thread class
  3.    Clean compile and at run time the values 0 to 9 are printed out
  4.    Clean compile but no output at runtime
  5.    None of these
 Discuss Question
Answer: Option D. -> Clean compile but no output at runtime
Question 17. Which keyword when applied on a method indicates that only one thread should execute the method at a time.
  1.    volatile
  2.    synchronized
  3.    native
  4.    static
  5.    final
 Discuss Question
Answer: Option B. -> synchronized
Question 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. }
  1.    good
  2.    null
  3.    Compilation fails with an error at line 5
  4.    Compilation succeed but Runtime Exception
  5.    None of these
 Discuss Question
Answer: Option A. -> good
Question 19. 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);
}
}
  1.    Prints
  2.    Prints
  3.    Prints
  4.    Compiler error
  5.    IllegalThreadStateException is thrown
 Discuss Question
Answer: Option C. -> Prints
Question 20. 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");
}
}
  1.    Compilation fails.
  2.    An exception is thrown at runtime.
  3.    "BeginRunEnd" is printed.
  4.    "BeginEndRun" is printed.
  5.    "BeginEnd" is printed.
 Discuss Question
Answer: Option C. -> "BeginRunEnd" is printed.

Latest Videos

Latest Test Papers