Test 1 Java Threads
Description: This quiz lets you test your skill on jav multi Threading | |
Number of Questions: 15 | |
Created by: Aliensbrain Bot | |
Tags: Threads Java Programming |
Which of the following thread group methods returns the number of threads in a group, along with the group, for which this thread is a parent?
When attempting to compile and run the following code, the result will be
public class MyClass extends Thread{
public MyClass(String s){ msg=s; }
String msg;
public void run(){
System.out.println(msg);
}
public static void main(String[] args) {
new MyClass(Hello);
new MyClass(World);
}
}
In the following Java program, it was found that these statements have generated an exception of Array Index out of bound Exception.
int b[ ] = {10,20,30};
b[50] = 100 ;
Choose the correct catch statement for the above code.
Which of the following methods in ThreadGroup class returns the number of threads in the group plus any group for which this thread is a parent?
What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread(); /* Line 5 */
t.run(); /* Line 6 */
}
public void run()
{
for(int i=1; i < 3; ++i)
{
System.out.print(i + ..);
}
}
}
It is given that a monitor called 'abc' has 4 threads in its waiting pool and all these waiting threads have the same priority. The name of one of the threads is 'thrd1'. How will you notify 'thrd1' so that it alone moves from its waiting state to ready state?
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}
Which of these will create and start this thread?
What will be the output of the program?
public class ThreadTest extends Thread
{
public void run()
{
System.out.println("In run");
yield();
System.out.println("Leaving run");
}
public static void main(String []argv)
{
(new ThreadTest()).start();
}
}
public class ExceptionTest
{
class TestException extends Exception {}
public void runTest() throws TestException {}
public void test() /* Point X */
{
runTest();
}
}
At Point X in line 5, which code is necessary to make the code compile?
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print(A);
}
catch (RuntimeException ex) /* Line 10 */
{
System.out.print(B);
}
catch (Exception ex1)
{
System.out.print(C);
}
finally
{
System.out.print(D);
}
System.out.print(E);
}
public static void badMethod()
{
throw new RuntimeException();
}
}
Which two can be used to create a new Thread?
- Extend java.lang.Thread and override the run() method.
- Extend java.lang.Runnable and override the start() method.
- Implement java.lang.Thread and implement the run() method.
- Implement java.lang.Runnable and implement the run() method.
- Implement java.lang.Thread and implement the start() method.
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?
What will be the output of the program?
public class Test
{
public static void main(String[] args)
{
final StringBuffer a = new StringBuffer();
final StringBuffer b = new StringBuffer();
new Thread()
{
public void run()
{
System.out.print(a.append(A));
synchronized(b)
{
System.out.print(b.append(B));
}
}
}.start();
new Thread()
{
public void run()
{
System.out.print(b.append(C));
synchronized(a)
{
System.out.print(a.append(D));
}
}
}.start();
}
}
What will be the output of the program?
public class Test107 implements Runnable
{
private int x;
private int y;
public static void main(String args[])
{
Test107 that = new Test107();
(new Thread(that)).start();
(new Thread(that)).start();
}
public synchronized void run()
{
for(int i = 0; i < 10; i++)
{
x++;
y++;
System.out.println(x = + x + , y = + y); /* Line 17 */
}
}
}
What will be the output of the program?
public class ThreadDemo
{
private int count = 1;
public synchronized void doSomething()
{
for (int i = 0; i < 10; i++)
System.out.println(count++);
}
public static void main(String[] args)
{
ThreadDemo demo = new ThreadDemo();
Thread a1 = new A(demo);
Thread a2 = new A(demo);
a1.start();
a2.start();
}
}
class A extends Thread
{
ThreadDemo demo;
public A(ThreadDemo td)
{
demo = td;
}
public void run()
{
demo.doSomething();
}
}