Mcq On Java Threads

11. Which of the following line of code is suitable to start a thread ?

class X implements Runnable
{
public static void main(String args[])
{
/* Missing code? */
}
public void run() {}
}

A. Thread t = new Thread(X);
B. Thread t = new Thread(X); t.start();
C. X run = new X(); Thread t = new Thread(run); t.start();
D. Thread t = new Thread(); x.run();

View Answer


12. What will be the output of the program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t);
}
}

A. Thread[5,main].
B. Thread[New Thread,5].
C. Thread[main,5,main].
D. Thread[New Thread,5,main]

View Answer


13.Number of threads in below java program is
public class ThreadExtended extends Thread {
public void run() {
System.out.println("\nThread is running now\n");
}
public static void main(String[] args) {
ThreadExtended threadE = new ThreadExtended();
threadE.start();
}
}

A. 0
B. 1
C. 2
D. 3

View Answer


14. which of these will create and start this thread?
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}

A. new Runnable(MyRunnable).start();
B. new Thread(MyRunnable).run();
C. new Thread(new MyRunnable()).start();
D. new MyRunnable().start();

View Answer


15.What is the priority of the thread in output of this program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t.getName());
}
}

A. main
B. Thread
C. New Thread
D. Thread[New Thread,5,main].

View Answer


16. What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
t.start();
System.out.print("one. ");
t.start();
System.out.print("two. ");
}
public void run()
{
System.out.print("Thread ");
}
}

A. Compilation fails
B. An exception occurs at runtime.
C. It prints "Thread one. Thread two."
D. The output cannot be determined.

View Answer


17. What is the name of the thread in output of this program?

class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.getPriority());
}
}


A. 1
B. 4
C. 0
D. 5

View Answer


18.What is the name of the thread in output of this program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.isAlive());
}
}

A. 1
B. 0
C. true
D. false

View Answer


19. The following block of code creates a Thread using a Runnable target:
Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
Which of the following classes can be used to create the target, so that the preceding code compiles correctly?

A. public class MyRunnable extends Object{public void run(){}}
B. public class MyRunnable implements Runnable{void run(){}}
C. public class MyRunnable implements Runnable{public void run(){}}
D. public class MyRunnable extends Runnable{public void run(){}}

View Answer


20. class Test
{
public static void main(String [] args)
{
printAll(args);
}
public static void printAll(String[] lines)
{
for(int i = 0; i < lines.length; i++)
{
System.out.println(lines[i]);
Thread.currentThread().sleep(1000);
}
}
}
the static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?

A. Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread.
B. Each String in the array lines will output, with no pause in between because this method is not executed in a Thread.
C. Each String in the array lines will output, with a 1-second pause.
D. This code will not compile.

View Answer







Also check :


Discussion


* You must be logged in to add comment.