This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Which Java class is primarily used to represent an IP address and perform hostname-to-IP-address resolution?
Option C
In Java networking, the InetAddress class is used to represent an IP address and to perform hostname-to-IP-address resolution. It belongs to the java.net package.
A hostname is a human-readable name such as surakuacademy.com, while an IP address is the numerical network address associated with that hostname. The InetAddress class provides methods that allow Java programs to obtain this information.
E.g.:
InetAddress address = InetAddress.getByName("surakuacademy.com");
System.out.println(address.getHostName());
System.out.println(address.getHostAddress());
Here, getByName() resolves the specified hostname and returns an InetAddress object. The getHostAddress() method can then be used to obtain the corresponding IP address.
Important InetAddress Methods:
| Method | Description |
|---|---|
| getByName() | Resolves a hostname or textual IP address and returns an InetAddress object. |
| getAllByName() | Resolves a hostname and returns all associated IP addresses. |
| getHostName() | Returns the hostname associated with the address when available. |
| getHostAddress() | Returns the IP address in textual form. |
| getLocalHost() | Returns an InetAddress representing the local host. |

Q: 2Consider the following statement:
Socket s = new Socket("example.com", 8080);
For a successful execution, what does this operation primarily attempt to establish?
Option B
In Java, the Socket class is primarily used for TCP-based network communication. The statement Socket s = new Socket("example.com", 8080); creates a Socket and attempts to establish a TCP connection to the host named example.com on port 8080.
Q: 3In Java RMI, which component is responsible for maintaining a mapping between names and remote object references so that clients can locate remote objects?
Option B
Java RMI (Remote Method Invocation) allows an object running in one Java Virtual Machine to invoke methods on an object running in another Java Virtual Machine. For a client to communicate with a remote object, the client first needs a way to locate that remote object.
The RMI Registry provides this lookup mechanism. It maintains a mapping between a name and the corresponding remote object reference. The server registers or binds a remote object with a name in the registry, and the client uses that name to look up the remote object.
| Operation | Purpose |
|---|---|
| bind() | Associates a name with a remote object reference in the registry. |
| rebind() | Associates a name with a remote object reference, replacing an existing binding if necessary. |
| lookup() | Allows a client to obtain the remote object reference associated with a name. |
| unbind() | Removes a name binding from the registry. |
Q: 4Which statement about UDP communication using Java's DatagramSocket and DatagramPacket is correct?
Option A
UDP (User Datagram Protocol) is a connectionless transport protocol. Unlike TCP, UDP does not establish a connection before sending data. It sends independent units of data called datagrams.
In Java, UDP communication is primarily handled using the DatagramSocket and DatagramPacket classes from the java.net package.
Q: 5Which statement correctly describes the purpose of URLConnection in Java?
Option D
URLConnection is a class in the java.net package that represents a communication link between a Java application and the resource identified by a URL.
It provides methods for establishing a connection and obtaining information about the resource, such as content type, content length, and input/output streams.
A URLConnection is normally obtained from a URL object by calling openConnection() method.
Q: 6What is the primary distinction between a URL and a URI in Java?
Option A
A URI (Uniform Resource Identifier) is a general identifier used to identify a resource. It can identify a resource by its name, location, or both. A URI does not necessarily provide enough information to retrieve the resource.
A URL (Uniform Resource Locator) is a specific type of URI that provides information about where a resource is located and how it can be accessed.
Q: 7Which of these package contains classes and interfaces for networking?
Option A
Java provides a dedicated package named java.net for network programming. It contains classes and interfaces that allow Java programs to communicate over a network using concepts such as IP addresses, URLs, sockets, and network connections.
The java.net package includes important classes such as Socket, ServerSocket, URL, URLConnection, InetAddress, and several networking-related interfaces.
E.g.:
import java.net.InetAddress;
class Example
{
public static void main(String args[]) throws Exception
{
InetAddress address = InetAddress.getByName("localhost");
System.out.println(address.getHostName());
System.out.println(address.getHostAddress());
}
}
OUTPUT
localhost
127.0.0.1
Q: 8In Java socket programming, which statement correctly describes the role of ServerSocket?
Option D
In Java socket programming, ServerSocket is a class used on the server side for TCP communication. Its main purpose is to listen for incoming connection requests from clients on a specified port.
When a server creates a ServerSocket, it waits for a client to request a connection. The accept() method is then used to accept an incoming connection. After a connection is accepted, the server communicates with that particular client through a Socket object.
E.g.:
ServerSocket server = new ServerSocket(5000);
Socket client = server.accept();
Here, ServerSocket listens on port 5000. When a client connects, accept() returns a Socket object representing the established connection with that client.
| Class | Role |
|---|---|
| ServerSocket | Runs on the server and listens for incoming TCP connection requests. |
| Socket | Represents an endpoint of an established TCP connection and is used for communication between client and server. |
Q: 9Which protocol does RMI use to communicate between objects distributed across a network?
Option B
In Java, RMI (Remote Method Invocation) is a technology that allows an object running in one Java virtual machine (JVM) to invoke methods on an object located in another JVM, possibly on a different machine over a network.
To enable this communication, RMI relies on a network protocol that provides reliable, ordered, and error-checked delivery of data between applications.
The protocol used for this purpose is TCP/IP (Transmission Control Protocol / Internet Protocol), which is a standard suite of communication protocols used for transmitting data over networks, including the Internet.
Q: 10A Java class intended to provide an object whose methods can be invoked remotely through RMI generally needs to implement which interface?
Option A
Java RMI (Remote Method Invocation) allows a Java program to invoke methods on an object that is running in another JVM, potentially on another computer.
For an object to be used as a remote object, its remote methods are generally declared through an interface that extends java.rmi.Remote.
The Remote interface is a marker interface, meaning that it does not define methods itself. Its purpose is to indicate that the methods of an interface are intended to be available for remote invocation.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.