Define a Thread in Java MCQ-1

Java Multithreading MCQ Part-1 | Define a Thread in Java MCQ-1 | Also see:- Define Thread in Java Multithreading Interview Questions

Q1) The process of executing multiple independent sub-tasks in one main task at a time concurrently is called?

a) Multitasking
b) Multithreading
c) Both
d) None of these

View Answer Answer:- b) Multithreading

Q2) The process of executing multiple tasks at a time concurrently is called?

a) Multitasking
b) Multithreading
c) Both
d) None of these

View Answer Answer:- a) Multitasking

MCQ on Creating/Defining a Thread in Java

Q3) If we want to start a thread, which method should be called?

a) start()
b) run()
c) None of these
d) Both

View Answer Answer:- a) start()

Q4) What will be the output of the below program?

class MyThread extends Thread {
   public void run() {
      System.out.print("run() ");
   }
}

public class ThreadDemo {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      System.out.print("main() ");
      t.run();
   }
}

a) run() main()
b) main() run()
c) Sometimes (a) and Sometimes (b)
d) None of these.

View Answer Answer:- b) main() run()

The run() method of MyThread class will be executed as a normal method call. Hence output is fixed.

Q5) If we want to start a thread then which method must be called by the programmer?

a) run()
b) start()
c) both start() and run()
d) None of these

View Answer Answer:- b) start()

As a programmer we need to call the start() method. The start() method of Thread class has to perform many important jobs to start a new thread. After registering the thread, the thread will be started and the run() method will be called by the start() method. Without calling start() method thread will not be started.

Q6) Find the output of the below program?

class MyThread extends Thread {
   public void run() {
      System.out.println("No-arg run()");
   }
   public void run(int i) {
      System.out.println("int-arg run()");
   }
}

public class ThreadDemo {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start();
   }
}

a) No-arg run()
b) Int-arg run()
c) Sometimes No-arg run(), and sometimes Int-arg run()
d) Compile time error

View Answer Answer:- a) No-arg run()

The start() method of Thread class calls run() method with no argument. If a thread wants to execute another overloaded form of run() method then those methods must be called from run() method explicitly.

Q7) Find the output of the below program?

class MyThread extends Thread {
   public void run() {
      System.out.print("No-arg ");
      run(15);
   }
   public void run(int i) {
      System.out.print("int-arg ");
   }
}

public class Test {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start();
   }
}

a) No-arg int-arg
b) int-arg No-arg
c) No output
d) None of these

View Answer Answer:- b) int-arg No-arg

Q8) Find the output of the below program?

class MyThread extends Thread {
}

public class ThreadDemo {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start();
   }
}

a) Compile time error
b) Run time error
c) No output
d) None of these

View Answer Answer:- c) No output

Since the run() method is not available in child class (MyThread) therefore it will be executed from parent class (Thread). The run() method of the Thread class has an empty implementation. Therefore we won’t get any output.

Q9) What is the possible output of the below program?

class MyThread extends Thread {
   public void start() {
      System.out.print("start() Method, ");
   }
   public void run() {
      System.out.print("run() Method, ");
   }
}

public class ThreadDemo {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start();
      System.out.println("Main Thread, ");
   }
}

a) Main Thread, start() method,
b) Main Thread, run() method,
c) run() method, Main Thread,
d) start() method, Main Thread,

View Answer Answer:- d) start() method, Main Thread,

Since we have overridden the start() method in MyThread class, therefore the start() method of Thread class will not get a chance to execute. In this case the start() method of MyThread will be executed like a normal method call. New thread is not created. Complete output produced by only the main thread, therefore output will be fixed.

Q10) What is the possible output of the below program?

class MyThread extends Thread {
   public void start() {
	super.start();
      System.out.println("start() ");
   }
   public void run() {
      System.out.println("run() ");
   }
}

public class ThreadDemo {
   public static void main(String[] args) {
      MyThread t = new MyThread();
      t.start();
   }
}

a) run()
b) start()
c) start() run()
d) run() start()

View Answer Answer:- c) start() run()

In the MyThread class, the start() method is overridden but it calls super.start() method therefore the start() method of the Thread class will be also executed. Hence other than the main thread, a new thread also will be created, and it will call run() method.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *