Java Programming

Java Programming

Threads has 37 Questions
What is the name of the method used to start a thread execution?

init();
start();
run();
resume();
What will be the output of the program?
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 TestThreads 
{
    public static void main (String [] args) 
    {
        Thread t = new MyThread() 
        {
            public void run() 
            {
                System.out.println(" foo");
            }
        };
        t.start();
    }
}

foo
MyThread foo
MyThread bar
foo bar
Which statement is true?

A static method cannot be synchronized.
If a class has synchronized code, multiple threads can still access the nonsynchronized code.
Variables can be protected from concurrent access problems by marking them with the synchronized keyword.
When a thread sleeps, it releases its locks.
Which two are valid constructors for Thread?
  1. Thread(Runnable r, String name)
  2. Thread()
  3. Thread(int priority)
  4. Thread(Runnable r, ThreadGroup g)
  5. Thread(Runnable r, int priority)

1 and 3
2 and 4
1 and 2
2 and 5
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 ");
    }
}

Compilation fails
An exception occurs at runtime.
It prints "Thread one. Thread two."
The output cannot be determined.
Which two can be used to create a new Thread?
  1. Extend java.lang.Thread and override the run() method.
  2. Extend java.lang.Runnable and override the start() method.
  3. Implement java.lang.Thread and implement the run() method.
  4. Implement java.lang.Runnable and implement the run() method.
  5. Implement java.lang.Thread and implement the start() method.

1 and 2
2 and 3
1 and 4
3 and 4
Which three are methods of the Object class?
  1. notify();
  2. notifyAll();
  3. isInterrupted();
  4. synchronized();
  5. interrupt();
  6. wait(long msecs);
  7. sleep(long msecs);
  8. yield();

1, 2, 4
2, 4, 5
1, 2, 6
2, 3, 4
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(); 
    } 
}

Prints "Inside Thread Inside Thread"
Prints "Inside Thread Inside Runnable"
Does not compile
Throws exception at runtime
Which two statements are true?
  1. Deadlock will not occur if wait()/notify() is used
  2. A thread will resume execution as soon as its sleep duration expires.
  3. Synchronization can prevent two objects from being accessed by the same thread.
  4. The wait() method is overloaded to accept a duration.
  5. The notify() method is overloaded to accept a duration.
  6. Both wait() and notify() must be called from a synchronized context.

1 and 2
3 and 5
4 and 6
1 and 3
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() {} 
}


Thread t = new Thread(X);
Thread t = new Thread(X); t.start();
X run = new X(); Thread t = new Thread(run); t.start();
Thread t = new Thread(); x.run();
What will be the output of the program?
class s1 implements Runnable 
{ 
    int x = 0, y = 0; 
    int addX() {x++; return x;} 
    int addY() {y++; return y;} 
    public void run() { 
    for(int i = 0; i < 10; i++) 
        System.out.println(addX() + " " + addY()); 
} 
    public static void main(String args[]) 
    { 
        s1 run1 = new s1(); 
        s1 run2 = new s1(); 
        Thread t1 = new Thread(run1); 
        Thread t2 = new Thread(run2); 
        t1.start(); 
        t2.start(); 
    } 
}

Compile time Error: There is no start() method
Will print in this order: 1 1 2 2 3 3 4 4 5 5...
Will print but not exactly in an order (e.g: 1 1 2 2 1 1 3 3...)
Will print in this order: 1 2 3 4 5 6... 1 2 3 4 5 6...
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?

public class MyRunnable extends Runnable{public void run(){}}
public class MyRunnable extends Object{public void run(){}}
public class MyRunnable implements Runnable{public void run(){}}
public class MyRunnable implements Runnable{void run(){}}
Which cannot directly cause a thread to stop executing?

Calling the SetPriority() method on a Thread object.
Calling the wait() method on an object.
Calling notify() method on an object.
Calling read() method on an InputStream object.
What will be the output of the program?
public class Q126 implements Runnable 
{ 
    private int x; 
    private int y; 

    public static void main(String [] args) 
    { 
        Q126 that = new Q126(); 
        (new Thread(that)).start( ); /* Line 8 */
        (new Thread(that)).start( ); /* Line 9 */
    } 
    public synchronized void run( ) /* Line 11 */
    { 
        for (;;) /* Line 13 */
        { 
            x++; 
            y++; 
            System.out.println("x = " + x + "y = " + y); 
        } 
    } 
}

An error at line 11 causes compilation to fail
Errors at lines 8 and 9 cause compilation to fail.
The program prints pairs of values for x and y that might not always be the same on the same line (for example, "x=2, y=1")
The program prints pairs of values for x and y that are always the same on the same line (for example, "x=1, y=1". In addition, each value appears once (for example, "x=1, y=1" followed by "x=2, y=2")
Which two of the following methods are defined in class Thread?
  1. start()
  2. wait()
  3. notify()
  4. run()
  5. terminate()

1 and 4
2 and 3
3 and 4
2 and 4
What will be the output of the program?
class s1 extends Thread
{ 
    public void run() 
    { 
        for(int i = 0; i < 3; i++) 
        { 
            System.out.println("A"); 
            System.out.println("B"); 
        } 
    } 
} 
class Test120 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[]) 
        { 
        s1 t1 = new s1(); 
        Test120 t2 = new Test120(); 
        t1.start(); 
        t2.start(); 
    } 
}

Compile time Error There is no start() method
Will print in this order AB CD AB...
Will print but not be able to predict the Order
Will print in this order ABCD...ABCD...
Which three guarantee that a thread will leave the running state?
  1. yield()
  2. wait()
  3. notify()
  4. notifyAll()
  5. sleep(1000)
  6. aLiveThread.join()
  7. Thread.killThread()

1, 2 and 4
2, 5 and 6
3, 4 and 7
4, 5 and 7
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.println(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(); 
    } 
}

DeadLock
It print 12 12 12 12
Compilation Error
Cannot determine output.
Which of the following will directly stop the execution of a Thread?

wait()
notify()
notifyall()
exits synchronized code
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(); 
    } 
}

It will print the numbers 0 to 19 sequentially
It will print the numbers 1 to 20 sequentially
It will print the numbers 1 to 20, but the order cannot be determined
The code will not compile.
Which method must be defined by a class implementing the java.lang.Runnable interface?

void run()
public void run()
public void start()
void run(int priority)
What will be the output of the program?
public class WaitTest 
{
    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 ");
    }
}

It fails to compile because the IllegalMonitorStateException of wait() is not dealt with in line 11.
1 2 3
1 3
1 2
Which will contain the body of the thread?

run();
start();
stop();
main();
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?

Synchronize the run method.
Wrap a synchronize(this) around the call to f.increase().
The existing code will cause a runtime exception.
Synchronize the increase() method
Which method registers a thread in a thread scheduler?

run();
construct();
start();
register();
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(); 
    } 
}

ABBCAD
ABCBCAD
CDADACB
Output determined by the underlying platform.
Assume the following method is properly synchronized and called from a thread A on an object B:

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.
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?

Each String in the array lines will output, with a 1-second pause.
Each String in the array lines will output, with no pause in between because this method is not executed in a Thread.
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.
This code will not compile.
Which of the following will not directly cause a thread to stop?

notify()
wait()
InputStream access
sleep()
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 + "..");
        }
    }
}

This code will not compile due to line 5.
This code will not compile due to line 6.
1..2..
1..2..3..
Which class or interface defines the wait(), notify(),and notifyAll() methods?

Object
Thread
Runnable
Class
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); 
    } 
}

main() will finish before starting threads.
main() will finish in the middle of one thread.
main() will finish after one thread.
Cannot be determined.
public class MyRunnable implements Runnable 
{
    public void run() 
    {
        // some code here
    }
}
which of these will create and start this thread?

new Runnable(MyRunnable).start();
new Thread(MyRunnable).run();
new Thread(new MyRunnable()).start();
new MyRunnable().start();
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(); 
    } 
}

The code fails to compile in the main() method
The code fails to compile in the run() method
Only the text "In run" will be displayed
The text "In run" followed by "Leaving run" will be displayed
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 */
        } 
    } 
} 

Compilation error.
Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.
Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.
Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
What will be the output of the program?
public class Test 
{
    public static void main (String [] args) 
    {
        final Foo f = new Foo();
        Thread t = new Thread(new Runnable() 
        {
            public void run() 
            {
                f.doStuff();
            }
        });
        Thread g = new Thread() 
        {
            public void run() 
            {
                f.doStuff();
            }
        };
        t.start();
        g.start();
    }
}
class Foo 
{
    int x = 5;
    public void doStuff() 
    {
        if (x < 10) 
        {
            // nothing to do
            try 
            {
                wait();
                } catch(InterruptedException ex) { }
        } 
        else 
        {
            System.out.println("x is " + x++);
            if (x >= 10) 
            {
                notify();
            }
        }
    }
}

The code will not compile because of an error on notify(); of class Foo.
The code will not compile because of some other error in class Test.
An exception occurs at runtime.
It prints "x is 5 x is 6".
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 + "..");
        }
    }
}

Compilation fails.
1..2..3..
0..1..2..3..
0..1..2..