Q.1
Given the code. What will be the result? 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"); } }
  • Compilation fails.
  • An exception is thrown at runtime.
  • "BeginRunEnd" is printed.
  • "BeginEndRun" is printed.
  • E. "BeginEnd" is printed.
Q.2
What will be output of the following program code? public class Test implements Runnable{ public void run(){ System.out.print("go"); } public static void main(String arg[]) { Thread t = new Thread(new Test()); t.run(); t.run(); t.start(); } }
  • Compilation fails.
  • An exception is thrown at runtime.
  • "go" is printed
  • "gogogo" is printed
Q.3
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"); } }
  • Compilation fails.
  • An exception is thrown at runtime.
  • "BeginRunEnd" is printed.
  • "BeginEndRun" is printed.
Q.4
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); } }
  • Prints
  • Prints
  • Prints
  • Compiler error
Q.5
What is the output for the below code ? class A implements Runnable{ public void run(){ System.out.println(Thread.currentThread().getName()); } }public class Test{public static void main(String... args){A a = new A();Thread t = new Thread(a);t.setName("good");t.start();}}
  • good
  • null
  • Compilation fails with an error at line 5
  • Compilation succeed but Runtime Exception
Q.6
Which keyword when applied on a method indicates that only one thread should execute the method at a time.
  • volatile
  • synchronized
  • native
  • static
Q.7
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 =i <i++){ System.out.println("Value of i = " + i); } } }
  • A compile time error indicating that no run method is defined for the Thread class
  • A run time error indicating that no run method is defined for the Thread class
  • Clean compile and at run time the values 0 to 9 are printed out
  • Clean compile but no output at runtime
Q.8
What notifyAll() method do?
  • Wakes up one threads that are waiting on this object's monitor
  • Wakes up all threads that are not waiting on this object's monitor
  • Wakes up all threads that are waiting on this object's monitor
  • None of the above
Q.9
Which of the following are methods of the Thread class?yield()sleep(long msec)go()stop()
  • 1 , 2 and 4
  • 1 and 3
  • 3 only
  • None of the above
Q.10
What will be the output? class A extends Thread{ public void run(){ for(int i=i
  • 0 0
  • Compilation error, class A has no start method
  • 0 1
  • Compilation succeed but runtime exception
  • E. None of these
Q.11
Predict the output: class A implements Runnable{ public void run(){ try{ for(int i=0;i
  • A A A A B B B B
  • A B A B A B A B
  • Output order is not guaranteed
  • Compilation succeed but Runtime Exception
Q.12
What will be the output? class One extends Thread{ public void run(){ for(int i=i
  • 0 0
  • Compilation Error
  • 0 1
  • None of these
Q.13
What will happen after compiling and running following code? class A implements Runnable{ public void run(){ System.out.println("run-a"); } }public class Test{public static void main(String... args){A a = new A();Thread t = new Thread(a);t.start();t.start();}}
  • run-a
  • run-a run-a
  • Compilation fails with an error at line 6
  • Compilation succeed but Runtime Exception
Q.14
What will happen when you attempt to compile and run the following code?public class Test extends Thread{public static void main(String argv[]){Test t = new Test();t.run();t.start();}public void run(){System.out.println("run-test");}}
  • run-test run-test
  • run-test
  • Compilation fails due to an error on line 4
  • Compilation fails due to an error on line 7
Q.15
Analyze the following code: public class Test implements Runnable{ public static void main(String[] args){ Test t = new Test(); t.start(); } public void run() { } }
  • The program does not compile because the start() method is not defined in the Test class.
  • The program compiles, but it does not run because the start() method is not defined.
  • The program compiles, but it does not run because the run() method is not implemented.
  • The program compiles and runs fine.
Q.16
Analyze the following code: public abstract class Test implements Runnable{ public void doSomething() { }; }
  • The program will not compile because it does not implement the run() method.
  • The program will not compile because it does not contain abstract methods.
  • The program compiles fine.
  • None of the above
Q.17
Which of the following constructor of class Thread is valid one?
  • Thread(Runnable threadOb, int priority)
  • Thread(int priority)
  • Thread(Runnable threadOb, String threadName)
  • Thread(String threadName, int priority)
Q.18
What will be the output of the following program code? public class Test implements Runnable{ public static void main(String[] args){ Thread t = new Thread(this); t.start(); } public void run(){ System.out.println("test"); } }
  • The program does not compile because this cannot be referenced in a static method.
  • The program compiles fine, but it does not print anything because t does not invoke the run() method
  • The program compiles and runs fine and displays test on the console.
  • None of the above
Q.19
When a class extends the Thread class ,it should override ............ method of Thread class to start that thread.
  • start()
  • run()
  • init()
  • go()
Q.20
In java a thread can be created by ..........
  • Extending the thread class.
  • Implementing Runnable interface.
  • Both of the above
  • None of these
0 h : 0 m : 1 s