Which method must be defined by a class implementing the java.lang.Runnable interface?
Question 1 options:
void run()
public void run()
public void start()
None of the above
Question 2 (1 point)
What will be the output of the following Java code?
package com.algonquin.quiz3;
class ThreadDemo extends Thread {
public static void main(String [] args) {
ThreadDemo t = new ThreadDemo();
t.start();
System.out.print(“one. “);
t.start();
System.out.print(“two. “);
}
public void run() {
System.out.print(“Thread “);
}
}
Question 2 options:
The output cannot be determined.
It prints “Thread one. Thread two.”.
An exception occurs at runtime.
Compilation fails.
Question 3 (1 point)
Which of the following lines of code is suitable to start a thread?
class Demo implements Runnable {
public void run() {
System.out.println(“Thread is in Running state”);
}
public static void main(String args[]) {
/* Missing code? */
}
}
Question 3 options:
Thread t = new Thread();
x.run();
Thread t = new Thread(X);
t.start();
X obj = new X();
Thread tobj = new Thread(obj);
tobj.start();
Thread t = new Thread(X);
Question 4 (1 point)
What will be the output of the below Java program?
package com.algonquin.quiz3;
class MyThread extends Thread {
MyThread() {}
MyThread(Runnable r) {super(r); }
public void run() {
System.out.print(“Inside Thread “);
}
}
class RunnableDemo implements Runnable {
public void run() {
System.out.print(” Inside Runnable”);
}
}
class ThreadDemo {
public static void main(String[] args) {
new MyThread().start();
new MyThread(new RunnableDemo()).start();
}
}
Question 4 options:
Throws exception at runtime.
Prints “Inside Thread Inside Runnable”.
Prints “Inside Thread Inside Thread”.
Does not compile.
Question 5 (1 point)
What is the name of the method that is used to start the execution of a thread?
Question 5 options:
init();
resume();
start();
run();
Question 6 (1 point)
What is the output of the following program?
class ThreadTest extends Thread {
public void run() {
for(int i = 0; i < 3; i++) {
System.out.println("A");
System.out.println("B");
}
}
}
class ThreadDemo 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[]) {
ThreadTest t1 = new ThreadTest();
ThreadDemo t2 = new ThreadDemo();
t1.start();
t2.start();
}
}
Question 6 options:
Will print in this order ABCD...ABCD...
Will print A B C D but we cannot predict the Order in which this will be printed.
Will print in this order AB CD AB...
Compile time Error There is no start() method.
Question 7 (1 point)
Which of the following methods cannot directly cause a thread to stop executing?
Question 7 options:
Calling the SetPriority() method on a Thread object.
Calling the wait() method on an object.
Calling sleep() method on a Thread object.
Calling notify() method on an object.
Question 8 (1 point)
What will be the output of the below Java program?
public class ThreadWaitTest {
public static void main(String [] args) {
System.out.print("1 ");
synchronized(args) {
System.out.print("2 ");
try {
args.wait(); /* Line 11 */
} catch(InterruptedException e) { }
}
System.out.print("3 ");
}
}
Question 8 options:
1 3
1 2 3
1 2
d. It fails to compile because the IllegalMonitorStateException of wait() is not dealt with in line 11.
Question 9 (1 point)
What will be the output of the following Java program?
package com.algonquin.quiz3;
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 ThreadDemo {
public static void main (String [] args) {
Thread t = new MyThread() {
public void run() {
System.out.println(" foo");
}
};
t.start();
}
}
Question 9 options:
foo bar
MyThread bar
MyThread foo
foo
Question 10 (1 point)
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?
Question 10 options:
After thread A is notified, or after two seconds.
After the lock on B is released, or after two seconds.
Two seconds after thread A is notified.
Two seconds after lock B is released.