Java Questions & Answers – Networking Basics

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: c
Explanation: None.

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

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: None.

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

Answer: c
Explanation: None.

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

Answer: d
Explanation: None.

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

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

7. What is the output of this program?

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

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

Answer: c
Explanation: None.
Output:
$ javac networking.java
$ java networking
true

8. What is the output of this program?

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

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

Answer: d
Explanation: InetAddress obj1 = InetAddress.getByName(“cisco.com”); creates object obj1 having DNS and IP address of cisco.com, InetAddress obj2 = InetAddress.getByName(“sanfoundry.com”); creates obj2 having DNS and IP address of sanfoundry.com , since both these address point to two different locations false is returned by obj1.equals(obj2);.
Output:
$ javac networking.java
$ java networking
true

9. What is the output of this program?

  1.     import java.io.*;
  2.     class streams {
  3.         public static void main(String[] args) {
  4.             try {
  5. 	        FileOutputStream fos = new FileOutputStream("serial");
  6. 	        ObjectOutputStream oos = new ObjectOutputStream(fos);
  7. 	        oos.writeFloat(3.5);
  8. 	        oos.flush();
  9. 	        oos.close();
  10. 	    }
  11. 	    catch(Exception e) {
  12. 	        System.out.println("Serialization" + e);
  13.                 System.exit(0);
  14.             }
  15. 	    try {
  16. 	        FileInputStream fis = new FileInputStream("serial");
  17. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  18. 	        ois.close();
  19. 	        System.out.println(ois.available());		    	
  20. 	    }
  21. 	    catch (Exception e) {
  22.                 System.out.print("deserialization");
  23. 	        System.exit(0);
  24. 	    }
  25.         }
  26.     }

a) 1
b) 2
c) 3
d) 4
View Answer

Answer: d
Explanation: New input stream is linked to streal ‘serials’, an object ‘ois’ of ObjectInputStream is used to access this newly created stream, ois.close(); closes the stream hence we can’t access the stream and ois.available() returns 0.
Output:
$ javac streams.java
$ java streams
0

10. What is the output of this program?

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

a) cisco
b) cisco.com
c) www.cisco.com
d) None of the mentioned
View Answer

Answer: b
Explanation: None.
Output:
$ javac networking.java
$ java networking
cisco.com

Sanfoundry Global Education & Learning Series – Java Programming Language.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Follow Manish & Sanfoundry