A program can be divided into a number of small processes. Each small process can be addressed as a single thread (a lightweight process). Multithreaded programs contain two or more threads that can run concurrently. This means that a single program can perform two or more tasks simultaneously. For example, one thread is writing content on a file at the same time another thread is performing spelling check.
This quiz collection covers most basic to advanced aspect in multithreading implementation in Java.
This quiz is in Java quiz collection.
Start quizWhich two are valid constructors for Thread?
Which two of the following methods are defined in class Thread?
Under which conditions will a currently executing thread stop?
Which three guarantee that a thread will leave the running state?
Which will contain the body of the thread?
Which of the following will not directly cause a thread to stop?
Which statement is true?
Which two can be used to create a new Thread?
Which two statements are true for any concrete class implementing the java.lang.Runnable interface?
Which method registers a thread in a thread scheduler?
which of these will create and start this thread?
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}
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 ");
}
}
What will be the output of the program?
class MyThread extends Thread
{
MyThread() {}
MyThread(Runnable r) {super(r); }
public void run()
{
System.out.print("Inside Thread ");
}
}
class MyRunnable implements Runnable
{
public void run()
{
System.out.print(" Inside Runnable");
}
}
class Test
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread(new MyRunnable()).start();
}
}
What will be the output of the program?
class s implements Runnable
{
int x, y;
public void run()
{
for(int i = 0; i < 1000; i++)
synchronized(this)
{
x = 12;
y = 12;
}
System.out.print(x + " " + y + " ");
}
public static void main(String args[])
{
s run = new s();
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
t1.start();
t2.start();
}
}
What will be the output of the program?
class Happy extends Thread
{
final StringBuffer sb1 = new StringBuffer();
final StringBuffer sb2 = new StringBuffer();
public static void main(String args[])
{
final Happy h = new Happy();
new Thread()
{
public void run()
{
synchronized(this)
{
h.sb1.append("A");
h.sb2.append("B");
System.out.println(h.sb1);
System.out.println(h.sb2);
}
}
}.start();
new Thread()
{
public void run()
{
synchronized(this)
{
h.sb1.append("D");
h.sb2.append("C");
System.out.println(h.sb2);
System.out.println(h.sb1);
}
}
}.start();
}
}
What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
Thread x = new Thread(t);
x.start(); /* Line 7 */
}
public void run()
{
for(int i = 0; i < 3; ++i)
{
System.out.print(i + "..");
}
}
}
What will be the output of the program?
public class SyncTest
{
public static void main (String [] args)
{
Thread t = new Thread()
{
Foo f = new Foo();
public void run()
{
f.increase(20);
}
};
t.start();
}
}
class Foo
{
private int data = 23;
public void increase(int amt)
{
int x = data;
data = x + amt;
}
}
and assuming that data must be protected from corruption, what—if anything—can you add to the preceding code to ensure the integrity of data?
What will be the output of the program?
class Test116
{
static final StringBuffer sb1 = new StringBuffer();
static final StringBuffer sb2 = new StringBuffer();
public static void main(String args[])
{
new Thread()
{
public void run()
{
synchronized(sb1)
{
sb1.append("A");
sb2.append("B");
}
}
}.start();
new Thread()
{
public void run()
{
synchronized(sb1)
{
sb1.append("C");
sb2.append("D");
}
}
}.start(); /* Line 28 */
System.out.println (sb1 + " " + sb2);
}
}
© 2017 QuizBucket.org