Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { System.out.print(Inside Thread ); } } class MyRunnable implements Runnable {

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();     } }

a)Prints "Inside Thread Inside Thread"

b)Prints "Inside Thread Inside Runnable"

c)Does not compile

d)Throws exception at runtime

========================================

Which of the following line of code is suitable to start athread ?

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();

====================================

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();     } }


(A)Compile time Error: There is no start() method

(B)Will print in this order: 1 1 2 2 3 3 4 4 5 5...

(C)Will print but not exactly in an order (e.g: 1 1 2 2 1 1 33...)

(D)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 Runnabletarget:

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 Runnable{public voidrun(){}}

(B)public class MyRunnable extends Object{public voidrun(){}}

(C)public class MyRunnable implements Runnable{public voidrun(){}}

(D)public class MyRunnable implements Runnable{void run(){}}

Which cannot directly cause a thread to stop executing?

(A)Calling the SetPriority() method on a Thread object.

(B)Calling the wait() method on an object.

(C)Calling notify() method on an object.

(D)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);         }     } }

(A)An error at line 11 causes compilation to fail

(B)Errors at lines 8 and 9 cause compilation to fail.

(C)The program prints pairs of values for x and y that might notalways be the same on the same line (for example, "x=2, y=1")

(D)The program prints pairs of values for x and y that arealways the same on the same line (for example, "x=1, y=1". Inaddition, each value appears once (for example, "x=1, y=1" followedby "x=2, y=2")

============================================

please explain the logics and mark the correct mcq

actually i am confused with runnable interface when to implementwhen to use start() method when to use run() method when to usesynchronized method

The language is core java.

Step by Step Solution

3.39 Rating (161 Votes )

There are 3 Steps involved in it

Step: 1

Answer 1 bPrints Inside Thread Inside Runnable 2 threads will ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Java Programming, Comprehensive Version

Authors: Y. Daniel Liang

10th Edition

133761312, 978-0133761313

More Books

Students also viewed these Programming questions

Question

Which of the following compounds are isomers of spiropentane?

Answered: 1 week ago

Question

What do you think?

Answered: 1 week ago