Assignment Solution 6
Assignment Solution 6
PROGRAMMING IN JAVA
Assignment 06
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
a. Running thread: 0
Running thread: 1
Running thread: 2
Main method complete.
b. Running thread: 0
Main method complete.
Running thread: 1
Running thread: 2
Correct Answer:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. Running thread: 0
Running thread: 1
Running thread: 2
Main method complete
Detailed Solution:
In this program, the run() method is called directly instead of using start(). When run() is called
like this, it does not create a new thread; it behaves like a regular method call and runs on the main
thread. As a result, the output is sequential. To create a separate thread, you must call the start()
method.For a more detailed understanding of thread methods like run() and start(), refer to the
book Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
Correct Answer:
Detailed Solution:
Multithreading in Java allows multiple threads to run concurrently within the same program, sharing the
same memory space. This feature makes Java programs more efficient, especially for tasks like
multitasking or background processing. However, proper synchronization is crucial to avoid issues like
data inconsistency. For a more detailed explanation of multithreading concepts, refer to the book Joy
with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
b. The program will throw an error when attempting to start the thread a second time.
d. The thread will run only once, and the second start() call will be ignored.
Correct Answer:
b. The program will throw an error when attempting to start the thread a second time.
Detailed Solution:
In Java, a thread can only be started once. If you try to call start() on the same thread object more than
once, the JVM will throw an IllegalThreadStateException. This is because a thread's lifecycle allows it to
transition to the "terminated" state after completing execution, and it cannot be restarted.
For more details about the thread lifecycle, refer to the book Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
a. The program will throw an error because Runnable cannot be executed directly.
b. The program will run successfully but will not create a new thread.
d. The program will throw a runtime error because Thread is not used.
Correct Answer:
b. The program will run successfully but will not create a new thread.
Detailed Solution:
In this code, the Runnable object is created, but it is executed directly using the run() method instead of
passing it to a Thread object. To create a new thread, you need to use:
Without this, the run() method behaves like a regular method call, and no new thread is created.
For more detailed guidance on creating threads using Runnable, refer to the book Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
a. The program will throw a compile-time error because Runnable is not a thread.
b. The program will execute successfully, and the run() method will run in a new thread.
c. The program will execute the run() method directly on the main thread.
d. The program will throw a runtime error because Runnable is not properly implemented.
Correct Answer:
b. The program will execute successfully, and the run() method will run in a new thread.
Detailed Solution:
The Runnable interface is implemented by the class RunnableExample. To create a thread, the Runnable
object is passed to the Thread constructor, and calling start() ensures that the run() method is executed
in a new thread.For more details on creating threads using Runnable, refer to the book Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
Which of the following states can a thread enter during its lifecycle in Java?
Correct Answer:
Detailed Solution:
QUESTION 7:
What does the thread scheduler use to decide which thread to run when multiple threads are in the
runnable state?
a. Thread priority
c. Thread name
Correct Answer:
a. Thread priority
Detailed Solution:
The thread scheduler uses thread priorities as a hint to determine the execution order of threads in the
runnable state. However, thread scheduling also depends on the operating system's scheduling policies
and may not strictly follow priority.
For more on thread scheduling, refer to the book Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}
Which of the following is true about the output?
Correct Answer:
Detailed Solution:
Thread priority is a hint to the thread scheduler but does not guarantee execution order. The actual
behavior depends on the JVM and the underlying OS.For more on thread priorities, refer to the book Joy
with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
Correct Answer:
Detailed Solution:
Thread synchronization is used to control the access of multiple threads to shared resources, ensuring
data consistency and preventing race conditions. This is typically done using synchronized methods or
blocks. For more details on thread synchronization, refer to the book Joy with Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
What is the primary difference between Byte Streams and Character Streams in Java?
b. Byte Streams are used for binary data, while Character Streams are used for text data.
Correct Answer:
b. Byte Streams are used for binary data, while Character Streams are used for text data.
Detailed Solution:
Byte Streams: Handle raw binary data like images or files (InputStream, OutputStream).
Character Streams: Handle text data and support encoding like Unicode (Reader, Writer).
Character Streams are better for text processing, especially when handling international characters.
For a detailed understanding of Java I/O streams, refer to the book Joy with Java.