Chapter 5: File and Stream
1. Which Java package provides classes for file and stream handling?
A. [Link]
B. [Link]
C. [Link]
D. [Link]
Answer: C
Explanation: All file and stream handling classes like FileInputStream, FileReader, File,
BufferedReader, etc., are found in the [Link] package.
2. A FileInputStream is primarily used for:
A. Reading characters
B. Writing binary data
C. Reading binary data
D. Writing characters
Answer: C
Explanation: FileInputStream reads data in bytes, making it suitable for reading binary files
(images, audio, executables).
3. Which stream reads one character and returns it as an integer?
A. FileWriter
B. FileReader
C. FileOutputStream
D. PrintWriter
Answer: B
Explanation: FileReader is a character-based stream, and its read() method returns the Unicode
integer representation of a character.
4. What does the read() method of FileInputStream return when end-of-file is reached?
A. 0
B. Null
C. False
D. -1
Answer: D
Explanation: All input streams return -1 to indicate the end of the stream (EOF).
5. Buffered streams improve performance by:
A. Compressing data
B. Reading/writing in chunks
C. Converting data to ASCII
D. Encrypting data
Answer: B
Explanation: Buffered streams store data in memory buffers, reducing the number of disk
accesses.
6. Which class is used to obtain information about files and directories?
A. FileInfo
B. FileDescriptor
C. File
D. FileReader
Answer: C
Explanation: The File class provides methods such as exists(), isFile(), length(), getPath(),
isDirectory(), etc.
7. Which method checks whether a file exists?
A. isFile()
B. exists()
C. check()
D. valid()
Answer: B
Explanation: The exists() method returns true if the file or directory physically exists on the disk.
8. The method [Link]() returns:
A. False always
B. True only for files
C. True if the path is a directory
D. True if file is hidden
Answer: C
Explanation: isDirectory() checks if the File object represents a directory, not a normal file.
9. Which File constructor accepts a path and a file name?
A. File(String path)
B. File(File dir, String fname)
C. File(String dirpath, String fname)
D. File(String directory)
Answer: C
Explanation: The constructor File(String dirpath, String fname) combines the directory path and
file name.
10. Which method returns the absolute path of a file?
A. absolute()
B. getAbsolutePath()
C. getPath()
D. fullPath()
Answer: B
Explanation: getAbsolutePath() returns the full path starting from the root directory.
11. FileOutputStream is mainly used for:
A. Writing binary data
B. Reading text
C. Writing characters
D. Reading Unicode data
Answer: A
Explanation: FileOutputStream writes byte-based (binary) data to files.
12. Which class is used to write character data to a file?
A. FileInputStream
B. FileWriter
C. FileOutputStream
D. DataOutputStream
Answer: B
Explanation: FileWriter is a character-based output stream for writing text files.
13. DataInputStream is used for:
A. Reading objects
B. Reading text only
C. Reading primitive data types in binary form
D. Reading only integers
Answer: C
Explanation: DataInputStream supports methods like readInt(), readFloat(), readBoolean(),
which read data in binary format.
14. Which class is used for writing primitive data types in binary form?
A. FileWriter
B. DataOutputStream
C. PrintWriter
D. FileReader
Answer: B
Explanation: DataOutputStream provides writeInt(), writeFloat(), writeUTF(), etc., to write
primitives in binary form.
15. What does ObjectInputStream do?
A. Reads primitive types only
B. Reads objects from a stream
C. Writes formatted text
D. Converts byte stream to character stream
Answer: B
Explanation: ObjectInputStream deserializes objects previously written using
ObjectOutputStream.
16. The [Link] constant is used for:
A. File compression
B. Platform-independent path names
C. Creating new directories
D. Buffering file data
Answer: B
Explanation: [Link] provides / or \ depending on the operating system, ensuring portable
file paths.
17. Which method creates a directory?
A. createDir()
B. newDir()
C. mkdir()
D. dir()
Answer: C
Explanation: mkdir() creates a single directory. mkdirs() creates all necessary parent directories.
18. Using the list() method in File class returns:
A. A list of File objects
B. A File array
C. A String array of file names
D. A Map of files
Answer: C
Explanation: list() returns a String[] containing the names of files and subdirectories.
19. Which stream is used to convert byte streams into character streams?
A. ObjectInputStream
B. PrintWriter
C. InputStreamReader
D. DataInputStream
Answer: C
Explanation: InputStreamReader acts as a bridge between byte streams and character streams.
20. Which stream allows characters to be pushed back into the stream?
A. PushbackReader
B. PrintWriter
C. FileReader
D. FilterReader
Answer: A
Explanation: PushbackReader allows unread operations by pushing characters back into the
stream.
Chapter 6: multithreading’s
1. Multitasking allows a system to:
A. Run only one task at a time
B. Execute multiple tasks simultaneously
C. Shut down unused processes
D. Increase thread priority only
Answer: B
Explanation: Multitasking refers to performing multiple tasks at the same time to utilize CPU
efficiently, as described in the document.
2. A process is defined as:
A. A lightweight execution unit
B. A part of a thread
C. An executing instance of a program
D. A terminated thread
Answer: C
Explanation: The document defines a process as a program loaded into memory with required
resources.
3. A thread must always:
A. Exist independently
B. Be part of a process
C. Run before the main method
D. Have its own memory space
Answer: B
Explanation: Threads cannot exist alone; they must be inside a process.
4. Threads are considered lightweight because:
A. They use more memory
B. They run slower
C. They share memory with their process
D. They cannot communicate
Answer: C
Explanation: Threads share memory and resources, making them lightweight compared to
processes.
5. The state in which a thread is created but not yet started is:
A. Running
B. New
C. Waiting
D. Terminated
Answer: B
Explanation: A new thread enters the New state until start() is called.
6. After calling start(), a thread moves to:
A. Blocked
B. New
C. Ready/Runnable
D. Dead
Answer: C
Explanation: The document states that calling start() places the thread in the Runnable state.
7. Which method causes a thread to pause for a specific time?
A. yield()
B. stop()
C. sleep()
D. interrupt()
Answer: C
Explanation: sleep() pauses the current thread for a specified duration.
8. Which method allows a thread to give up CPU temporarily?
A. stop()
B. yield()
C. wait()
D. end()
Answer: B
Explanation: yield() lets the current thread pause and allows threads with same priority to run.
9. Which method allows one thread to wait for another to finish?
A. interrupt()
B. notify()
C. join()
D. sleep()
Answer: C
Explanation: join() blocks the current thread until another thread completes.
10. Which of the following is true about starting a thread?
A. You can call start() multiple times
B. A thread can start only once
C. run() must be called directly
D. Threads restart automatically after ending
Answer: B
Explanation: Calling start() twice causes an IllegalThreadStateException.
11. A daemon thread:
A. Blocks JVM from shutting down
B. Must finish before JVM exits
C. Runs in background, JVM does not wait for it
D. Always has priority 10
Answer: C
Explanation: JVM stops all daemon threads once user (non-daemon) threads finish.
12. To create a thread using Runnable:
A. Extend Thread class only
B. Implement run() method and pass object to Thread class
C. Call run() directly
D. Use static run()
Answer: B
Explanation: Runnable requires implementing run() and passing it to a Thread object.
13. Which is an advantage of using Runnable?
A. Cannot extend other classes
B. Full control of thread
C. Allows class to extend another parent class
D. Always faster
Answer: C
Explanation: Runnable allows extending another class since Java supports single inheritance.
14. Thread priority values in Java range from:
A. 0–5
B. 1–10
C. 10–100
D. 0–100
Answer: B
Explanation: Thread priority values range from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY).
15. In preemptive scheduling:
A. Only lowest priority threads run
B. Thread runs for fixed time slice
C. Highest priority thread runs until waiting or finished
D. Random thread is selected
Answer: C
Explanation: Preemptive scheduling gives full CPU time to highest priority threads until they
block.
16. What is thread starvation?
A. Thread cannot start
B. Thread runs too fast
C. Low-priority threads never get CPU time
D. Deadlock between threads
Answer: C
Explanation: Starvation occurs when higher priority threads constantly prevent lower ones from
executing.
17. Thread pools are preferred because:
A. They create one thread per task
B. They reduce overhead of creating new threads
C. They slow down execution
D. They prevent concurrency
Answer: B
Explanation: Thread pools reuse threads and avoid creating new ones for every task.
18. To avoid race conditions, one must:
A. Use multiple threads always
B. Avoid using shared resources
C. Use synchronization mechanisms
D. Use high-priority threads
Answer: C
Explanation: Synchronization ensures only one thread accesses critical section at a time.
19. The synchronized keyword ensures:
A. Threads run in order
B. Only one thread at a time executes the method/block
C. Thread always gets CPU first
D. Priority becomes maximum
Answer: B
Explanation: synchronized ensures mutual exclusion by allowing only one thread to hold the
object's lock.
20. A deadlock occurs when:
A. Threads run too fast
B. Threads wait for events that will never occur
C. Threads have same priority
D. Thread pool runs out of threads
Answer: B
Explanation: Deadlock happens when two or more threads wait on each other indefinitely,
causing a permanent halt.
Chapter 7: Network programming
1. Networking refers to:
A. Sharing memory between threads
B. Connecting multiple devices
C. Opening local files
D. CPU scheduling
Answer: B
Explanation: Networking means connecting multiple remote or local devices so they can
communicate.
2. Which protocol is connection-oriented?
A. UDP
B. ICMP
C. TCP
D. HTTP
Answer: C
Explanation: TCP establishes a connection and waits for acknowledgments, making it reliable
and connection-oriented.
3. Which protocol is faster but unreliable?
A. TCP
B. UDP
C. SMTP
D. FTP
Answer: B
Explanation: UDP does not guarantee delivery or ordering; it is faster because it avoids
connection setup.
4. A protocol is:
A. A physical device
B. A set of rules for communication
C. A TCP port
D. A data container
Answer: B
Explanation: A protocol defines rules for exchanging data over a network.
5. An IP address contains:
A. 2 numbers
B. 8 hexadecimal digits
C. 4 decimal numbers between 0–255
D. Only letters
Answer: C
Explanation: IPv4 addresses have four numbers each between 0 and 255.
6. A port is used to:
A. Identify the hardware device
B. Store application files
C. Forward data to the correct application
D. Boost network speed
Answer: C
Explanation: Ports help the computer direct incoming data to the correct program.
7. A socket is best described as:
A. A physical cable
B. An interface between an application and the network
C. A protocol
D. A type of packet
Answer: B
Explanation: A socket provides a communication endpoint for sending/receiving data.
8. In TCP, the server uses which class to listen for connections?
A. Socket
B. DatagramSocket
C. ServerSocket
D. URL
Answer: C
Explanation: ServerSocket listens for incoming TCP connection requests.
9. The accept() method of ServerSocket:
A. Sends data to the client
B. Blocks until a client connects
C. Closes the server
D. Restarts the JVM
Answer: B
Explanation: The accept() method pauses execution until a client connects.
10. After accept() returns, the server typically:
A. Reboots
B. Creates a thread to handle the client
C. Terminates
D. Switches to UDP
Answer: B
Explanation: Servers usually create a new thread per client for simultaneous connections.
11. Creating a TCP client requires first:
A. Creating a ServerSocket
B. Creating a Socket object
C. Creating a DatagramSocket
D. Closing the server
Answer: B
Explanation: A TCP client begins by creating a Socket to connect to the server.
12. Which Java package provides TCP/UDP support?
A. [Link]
B. [Link]
C. [Link]
D. [Link]
Answer: C
Explanation: The [Link] package contains classes for networking.
13. UDP communication involves:
A. Guaranteed delivery
B. Ordered messages
C. Independent packets that may be lost or unordered
D. Continuous connection
Answer: C
Explanation: UDP uses datagrams that may arrive out of order or not at all.
14. Which class is used to store data for UDP sending?
A. ServerSocket
B. Socket
C. DatagramPacket
D. URLConnection
Answer: C
Explanation: DatagramPacket contains the message data for UDP.
15. Which class is used to send or receive UDP messages?
A. DatagramSocket
B. Socket
C. ServerSocket
D. URL
Answer: A
Explanation: DatagramSocket sends and receives UDP packets.
16. Why is UDP used if it is unreliable?
A. It guarantees delivery
B. It uses encryption
C. It is faster with lower overhead
D. It uses TCP internally
Answer: C
Explanation: UDP is chosen when speed is more important than reliability, such as in streaming.
17. Which class represents an IP address in Java?
A. URL
B. InetAddress
C. Port
D. Socket
Answer: B
Explanation: InetAddress represents IPv4 and IPv6 addresses.
18. To access content from a URL, Java uses:
A. DatagramSocket
B. URLConnection
C. ServerSocket
D. Port
Answer: B
Explanation: URLConnection allows programs to read data from a URL.
19. URL parsing can extract:
A. CPU speed
B. Thread priority
C. Protocol, host, port, path
D. RAM size
Answer: C
Explanation: URL methods return components such as protocol, host, port, file, etc.
20. In UDP, the receive() method:
A. Never blocks
B. Always fails
C. Blocks until a packet arrives or timeout occurs
D. Guarantees packet order
Answer: C
Explanation: The receive() method waits for a packet unless the socket has a timeout.
Thank You