Unit Iii Java
Unit Iii Java
MCQs
Multithreaded Programming
1. Which interface must a class implement to create a thread?
a) Runnable
b) Threadable
c) Callable
d) Executable
Answer: a
2. Which method is used to start a thread?
a) run()
b) start()
c) init()
d) execute()
Answer: b
3. What is thread synchronization used for?
a) To execute threads in parallel
b) To prevent thread interference and consistency problems
c) To stop a thread
d) To create multiple threads
Answer: b
4. Which keyword is used to make a method synchronized?
a) sync
b) synchronized
c) thread-safe
d) protected
Answer: b
5. What is inter-thread communication?
a) Communication between two programs
b) Communication between threads in the same process
c) Communication between two processes
d) Communication between two classes
Answer: b
6. Which method allows threads to wait for a signal?
a) wait()
b) notify()
c) signal()
d) sleep()
Answer: a
7. What is a deadlock in multithreading?
a) A thread running indefinitely
b) A situation where two threads wait for each other indefinitely
c) A thread terminated unexpectedly
d) A thread blocked by an I/O operation
Answer: b
8. Which of the following is true about the Thread class?
a) It must be extended to create a thread
b) It provides the Runnable interface
c) It is used for synchronization
d) It can directly control thread lifecycle
Answer: a
9. What does the join() method do?
a) Combines two threads into one
b) Waits for a thread to complete
c) Stops a thread
d) Starts a thread
Answer: b
10.What is the default priority of a thread in Java?
a) 0
b) 1
c) 5
d) 10
Answer: c
I/O Streams
11.What is a stream in Java?
a) A sequence of data
b) A type of thread
c) A data structure
d) A storage medium
Answer: a
12.Which of the following is a byte stream class?
a) FileReader
b) FileWriter
c) FileInputStream
d) BufferedReader
Answer: c
13.Which class is used to read character-based input?
a) InputStream
b) FileReader
c) FileInputStream
d) BufferedInputStream
Answer: b
14.What is the purpose of the System.in object?
a) To write to the console
b) To read from the console
c) To read from a file
d) To write to a file
Answer: b
15.Which method is used to write to the console?
a) write()
b) print()
c) println()
d) Both b and c
Answer: d
16.Which class is used for file handling in Java?
a) File
b) Directory
c) Folder
d) Path
Answer: a
17.Which method checks if a file exists?
a) isFile()
b) exists()
c) checkFile()
d) None of the above
Answer: b
18.What is the purpose of the flush() method in streams?
a) To close the stream
b) To clear the stream buffer
c) To write data to the stream
d) None of the above
Answer: b
19.What does the BufferedReader class do?
a) Reads data into a buffer for faster access
b) Writes data to a buffer
c) Reads byte-based data
d) None of the above
Answer: a
20.Which exception is commonly associated with file handling?
a) NullPointerException
b) IOException
c) FileNotFoundException
d) Both b and c
Answer: d
21.What is the parent class of all I/O classes in Java?
a) Object
b) InputStream
c) OutputStream
d) None of the above
Answer: a
22.Which stream is used to read primitive data types?
a) ObjectInputStream
b) DataInputStream
c) BufferedReader
d) FileReader
Answer: b
23.What is the main difference between byte and character streams?
a) Byte streams handle binary data; character streams handle text data
b) Byte streams are faster
c) Character streams are deprecated
d) None of the above
Answer: a
24.Which class is used to write character-based data to a file?
a) FileWriter
b) FileOutputStream
c) PrintWriter
d) Both a and c
Answer: d
25.Which method is used to close a file in Java?
a) closeFile()
b) close()
c) end()
d) terminate()
Answer: b
5-Mark Questions
1. Explain the lifecycle of a thread in Java with a diagram.
2. What are the differences between Runnable interface and Thread
class?
3. Write a program to demonstrate the use of synchronized methods in
Java.
4. Describe the different types of streams in Java with examples.
5. What is inter-thread communication? Explain with an example.
10-Mark Questions
1. Write a program to demonstrate inter-thread communication using
wait() and notify(). Explain the output.
2. Explain the concept of deadlock in multithreading. Write a program
to demonstrate deadlock and suggest ways to avoid it.
3. Discuss the differences between byte streams and character streams
with examples.
4. Write a program to read data from a file and write it to another file
using streams. Explain the steps.
5. Describe the role of try, catch, finally, and throws in exception
handling with examples.