Java MCQs on networking basics of Java Programming Language.
1. Which of these package contains classes and interfaces for networking? Answer: c 2. Which of these is a protocol for breaking and sending packets to an address across a network? Answer: a 3. How many ports of TCP/IP are reserved for specific protocols? Answer: b 4. How many bits are in a single IP address? Answer: c 5. Which of these is a full form of DNS? Answer: d 6. Which of these class is used to encapsulate IP address and DNS? Answer: c 7. What will be the output of the following Java program? a) 0 8. What will be the output of the following Java program? a) 0 9. What will be the output of the following Java program? a) Protocol: http Answer: d 10. What will be the output of the following Java program? a) cisco
a) java.io
b) java.util
c) java.net
d) java.network
Clarification: None.
a) TCP/IP
b) DNS
c) Socket
d) Proxy Server
Clarification: TCP/IP – Transfer control protocol/Internet Protocol is used to break data into small packets an send them to an address across a network.
a) 10
b) 1024
c) 2048
d) 512
Clarification: None.
a) 8
b) 16
c) 32
d) 64
Clarification: None.
a) Data Network Service
b) Data Name Service
c) Domain Network Service
d) Domain Name Service
Clarification: None.
a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler
Clarification: InetAddress class encapsulate both IP address and DNS, we can interact with this class by using name of an IP host.
import java.net.*;
class networking
{
public static void main(String[] args) throws UnknownHostException
{
InetAddress obj1 = InetAddress.getByName(".com");
InetAddress obj2 = InetAddress.getByName(".com");
boolean x = obj1.equals(obj2);
System.out.print(x);
}
}
b) 1
c) true
d) false
Clarification: None.
Output:
$ javac networking.java
$ java networking
true
import java.net.*;
public class networking
{
public static void main(String[] args) throws UnknownHostException
{
InetAddress obj1 = InetAddress.getByName("cisco.com");
InetAddress obj2 = InetAddress.getByName(".com");
boolean x = obj1.equals(obj2);
System.out.print(x);
}
}
b) 1
c) true
d) false
Clarification: InetAddress obj1 = InetAddress.getByName(“cisco.com”); creates object obj1 having DNS and IP address of cisco.com, InetAddress obj2 = InetAddress.getByName(“.com”); creates obj2 having DNS and IP address of .com, since both these address point to two different locations false is returned by obj1.equals(obj2);.
Output:
$ javac networking.java
$ java networking
false
import java.io.*;
import java.net.*;
public class URLDemo
{
public static void main(String[] args)
{
try
{
URL url=new URL("java-mcq");
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
} catch(Exception e){System.out.println(e);}
}
}
b) Host Name: www..com
c) Port Number: -1
d) All of the mentioned
Clarification: getProtocol() give protocol which is http
getUrl() give name domain name
getPort() Since we have not explicitly set the port, default value that is -1 is printed.
import java.net.*;
class networking
{
public static void main(String[] args) throws UnknownHostException
{
InetAddress obj1 = InetAddress.getByName("cisco.com");
System.out.print(obj1.getHostName());
}
}
b) cisco.com
c) www.cisco.com
d) none of the mentioned
Clarification: None.
Output:
$ javac networking.java
$ java networking
cisco.com