Question

Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args)...

Consider the following codes:

public class TestThread extends Thread {

    public static void main(String[] args) {

        TestThread thread = new TestThread();

    }

    @Override

    public void run() {

        printMyName();

    }

    private void printMyName() {

        System.out.println("Thread is running");

    }

}

Test Stem / Question

Choices

1: What method should you invoke to start the thread TestThread?

A: start()

B: run()

C: No. Thread will run automatically when executed.

D: TestThread is not a thread.

2: What will happen when you started the thread?

A: “Thread is running” will be printed out.

B: Will have an compiler error, you cannot access a static method in the thread.

C: Exception will be thrown.

D: Thread will be started but the “Thread is running” will not be printed since the run method is not called.

3: How many and what threads is/are running when you successfully run the above code?

A: 2, the main thread and the TestThread.

B: 1, only the main thread

C: 1, only the TestThread

D: No thread is running

4: What method is internally called by when we call the method start()?

A: run()

B: printMyName()

C: main()

D: None

5: Now try to use the method getName() and print it out. What does the console say?

A: Thread-0

B: The name of the class - TestThread

C: None since we did not define a name for the thread.

D: Syntax error, not such method exists.

public class TestThread implements Runnable{

        public static void main(String[] args){

                TestThread t = new TestThread();

        }

        public void run() {

            System.out.println("Running");

        }

}

6: How will you be able to run this thread?

A: by calling the run() method.

B: By calling the start() method.

C: This will not run since this is a runnable. You need to extend the Thread class.

D: This will not run since there is no @override annotation in the run method.

7: What will happen when you called the t.start() twice?

A: IllegalStateThreadException will occur.

B: The “Running” text will be printed twice

C: Nothing.

D: MulitThreadingException will occur

8: What will happen when you called t.run() and t.start()?

A: The “Running” text will be printed twice

B: IllegalStateThreadException will occur.

C: Nothing.

D: MulitThreadingException will occur

public class TestExample extends Thread{

      public static void main(String argv[]){

            TestExample t = new TestExample();

            t.run();

      }

      public void start(){

            for(int i = 0; i < 5; i++){

                  System.out.println("Count : " + i);

            }

      }

}

9: What happens when you compile?

A: Nothing but it compiled.

B: Syntax error. run() method is not defined.

C: Compiler error.

D: A run time exception that says no run method is defined in this class.

public class TestExample implements Runnable{

      public static void main(String[] args) throws InterruptedException{

            Thread a = new Thread(new TestExample());

            a.start();

       

            System.out.println("Starting...");

            a.join();

            System.out.println("Ending...");

      }

   

      public void run(){

            System.out.println("Running...");

      }

}

10: What happens when you compile?

A: Starting… Running… Ending… is printed

B: Starting… Ending… Running… is printed

C: Starting… Ending… is printed

D: Runtime exception occurred.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

start() method must be invoked to start the thread TestThread

public class TestThread extends Thread {
public TestThread()
{
start();
}
public static void main(String[] args) {
TestThread thread = new TestThread();
}
@Override
public void run() {
printMyName();
}
private void printMyName() {
System.out.println("Thread is running");
}
}

2)

a) Thread is running will be printed on the screen

3)

When thread is started 2 threads will be running i..e, Main thread & TestThread

4)

run() is internally called when the start() method is called

5)

Thread-0 will be printed on the screen

public class TestThread extends Thread {
public TestThread()
{
start();
}
public static void main(String[] args) {
TestThread thread = new TestThread();
}
@Override
public void run() {
printMyName();
}
private void printMyName() {
System.out.println(getName()+"Thread is running");
}
}

Output

Thread-0Thread is running

6)

b is correct option it can be accomplished with the below example

public class TestThread implements Runnable{
public TestThread()
{
new Thread(this).start();
}
public static void main(String[] args){
TestThread t = new TestThread();
}
public void run() {
System.out.println("Running");
}
}

Output

Running

7)

When starting thread twice IllegalThreadStateException will be thrown

8)

a is correct answer

9)

a is correct answer since it doesnot raise any compile time error

10)

a is correct answer since join() method waits for the current thread to finish its execution

Add a comment
Know the answer?
Add Answer to:
Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT