What will be the output of the following Java program import Java net public class networking

Java MCQs on networking basics of Java Programming Language.

1. Which of these package contains classes and interfaces for networking?
a) java.io
b) java.util
c) java.net
d) java.network

Answer: c
Clarification: None.

2. Which of these is a protocol for breaking and sending packets to an address across a network?
a) TCP/IP
b) DNS
c) Socket
d) Proxy Server

Answer: a
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.

3. How many ports of TCP/IP are reserved for specific protocols?
a) 10
b) 1024
c) 2048
d) 512

Answer: b
Clarification: None.

4. How many bits are in a single IP address?
a) 8
b) 16
c) 32
d) 64

Answer: c
Clarification: None.

5. Which of these is a full form of DNS?
a) Data Network Service
b) Data Name Service
c) Domain Network Service
d) Domain Name Service

Answer: d
Clarification: None.

6. Which of these class is used to encapsulate IP address and DNS?
a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler

Answer: c
Clarification: InetAddress class encapsulate both IP address and DNS, we can interact with this class by using name of an IP host.

7. What will be the output of the following Java program?

  1.     import java.net.*;
  2.     class networking 
  3.     {
  4.         public static void main(String[] args) throws UnknownHostException 
  5.         {
  6.             InetAddress obj1 = InetAddress.getByName(".com");
  7.             InetAddress obj2 = InetAddress.getByName(".com");
  8.             boolean x = obj1.equals(obj2); 
  9.             System.out.print(x);
  10.         }
  11.     }

a) 0
b) 1
c) true
d) false

Answer: c
Clarification: None.
Output:

$ javac networking.java
$ java networking 
true

8. What will be the output of the following Java program?

  1.     import java.net.*;
  2.     public class networking 
  3.     {
  4.         public static void main(String[] args) throws UnknownHostException 
  5.         {
  6.             InetAddress obj1 = InetAddress.getByName("cisco.com");
  7.             InetAddress obj2 = InetAddress.getByName(".com");
  8.             boolean x = obj1.equals(obj2); 
  9.             System.out.print(x);
  10.         }
  11.     }

a) 0
b) 1
c) true
d) false

Answer: d
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

9. What will be the output of the following Java program?

  1. import java.io.*;  
  2. import java.net.*;  
  3. public class URLDemo 
  4. {  
  5.     public static void main(String[] args) 
  6.     {  
  7.         try 
  8.         {  
  9.             URL url=new URL("java-mcq");  
  10.             System.out.println("Protocol: "+url.getProtocol());  
  11.             System.out.println("Host Name: "+url.getHost());  
  12.             System.out.println("Port Number: "+url.getPort());   
  13.         } catch(Exception e){System.out.println(e);}  
  14.     }  
  15. }

a) Protocol: http
b) Host Name: www..com
c) Port Number: -1
d) All of the mentioned

Answer: d
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.

10. What will be the output of the following Java program?

  1.     import java.net.*;
  2.     class networking 
  3.     {
  4.         public static void main(String[] args) throws UnknownHostException 
  5.         {
  6.             InetAddress obj1 = InetAddress.getByName("cisco.com");
  7.             System.out.print(obj1.getHostName());
  8.         }
  9.     }

a) cisco
b) cisco.com
c) www.cisco.com
d) none of the mentioned

Answer: b
Clarification: None.
Output:

$ javac networking.java
$ java networking 
cisco.com

What will be the output of the program Java program?

Explanation: The output of the Java compiler is bytecode, which leads to the security and portability of the Java code. It is a highly developed set of instructions that are designed to be executed by the Java runtime system known as Java Virtual Machine (JVM).

What will be the output of the following Java program note host URL is having length of content 127?

7. What will be the output of the following Java program? Note: Host URL is having length of content 127. Explanation: None.

What is Java networking in Java?

Java networking is the concept of connecting two or more computing devices to share resources. The application layer of a Java program communicates with the network. The java.net package contains all of the Java networking classes and interfaces.

Can you use Java for networking?

Through the classes in java.net , Java programs can use TCP or UDP to communicate over the Internet. The URL , URLConnection , Socket , and ServerSocket classes all use TCP to communicate over the network. The DatagramPacket , DatagramSocket , and MulticastSocket classes are for use with UDP.