Java Networking: Multiple Choice Question & Answers
Java Networking: Multiple Choice Question & Answers
Java Networking: Multiple Choice Question & Answers
a) java.io
b) java.util
c) java.net
d) java.network
Answer: c
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
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.
a) 10
b) 1024
c) 2048
d) 512
Answer: b
a) 8
b) 16
c) 32
d) 64
Answer: c
Answer: d
a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler
Answer: c
Explanation: InetAddress class encapsulate both IP address and DNS, we can interact with this class by
using name of an IP host.
a) throws
b) finally
c) throw
d) catch
Answer: b
Explanation: finally keyword is used to define a set of instructions that will be executed irrespective of
the exception found or not.
a) finally
b) catch
Answer: c
Explanation: try block can be followed by any of finally or catch block, try block checks for exceptions
and work is performed by finally and catch block as per the exception.
b) java.util
c) java.lang
d) java.net
Answer: c
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException
Answer: a
11) Which of these exceptions will occur if we try to access the index of an array beyond its length?
a) ArithmeticException
b) ArrayException
c) ArrayIndexException
d) ArrayIndexOutOfBoundsException
Answer: d
12) Which of these standard collection classes implements a linked list data structure?
a) AbstractList
b) LinkedList
c) HashSet
d) AbstractSet
Answer: b
a) ArrayList
b) HashSet
c) LinkedList
d) DynamicList
Answer: b
Explanation: HashSet and TreeSet implements Set interface where as LinkedList and ArrayList
implements List interface.
14) Which of these method is used to add an element to the start of a LinkedList object?
a) add()
b) first()
c) AddFirst()
d) addFirst()
Answer: d
15) Which of these method of HashSet class is used to add elements to its object?
a) add()
b) Add()
c) addFirst()
d) insert()
Answer: a
16) Which of these methods can be used to delete the last element in a LinkedList object?
a) remove()
b) delete()
c) removeLast()
d) deleteLast()
Answer: c
Explanation: removeLast() and removeFirst() methods are used to remove elements in end and
beginning of a linked list.
a) change()
b) set()
c) redo()
d) add()
Answer: c
Explanation: An element in a LinkedList object can be changed by first using get() to obtain the index or
location of that object and the passing that location to method set() along with its new value.
18) Which of these method of DatagramPacket class is used to find the destination address?
a) findAddress()
b) getAddress()
c) Address()
d) whois()
Answer: b
a) DatagramPacket
b) DatagramSocket
c) InetAddress
d) ServerSocket
Answer: c
a) Mime
b) cache
c) Datagrams
d) DatagramSocket
Answer: c
Explanation: The Datagrams are the bundle of information passed between machines.
21) Which of these class is necessary to implement datagrams?
a) DatagramPacket
b) DatagramSocket
Answer: c
22) Which of these method of DatagramPacket is used to find the port number?
a) port()
b) getPort()
c) findPort()
d) recievePort()
Answer: a
23) Which of these method of DatagramPacket is used to obtain the byte array of data contained in a
datagram?
a) getData()
b) getBytes()
c) getArray()
d) recieveBytes()
Answer: a
24) Which of these method of DatagramPacket is used to find the length of byte array?
a) getnumber()
b) length()
c) Length()
d) getLength()
Answer: d
Explanation: getLength returns the length of the valid data contained in the byte array that would be
returned from the getData () method. This typically is not equal to length of whole byte array.
25) Which of these class must be used to send a datatgram packets over a connection?
a) InetAdress
b) DatagramPacket
c) DatagramSocket
Answer: d
Explanation: By using 5 classes we can send and receive data between client and server, these are
InetAddress, Socket, ServerSocket, DatagramSocket, and DatagramPacket.
a) run()
b) start()
c) runThread()
d) startThread()
Answer: a
27) Which of these method is called when http daemon is acting like a normal web server?
a) Handle()
b) HandleGet()
c) handleGet()
d) Handleget()
Answer: c
28) Which of these methods of httpd class is used to read data from the stream?
a) getDta()
b) GetResponse()
c) getStream()
d) getRawRequest()
Answer: d
Explanation: The getRawRequest() method reads data from a stream until it gets two consecutive
newline characters.
29) Which of these method of httpd class is used to get report on each hit to HTTP server?
a) log()
b) logEntry()
c) logHttpd()
d) logResponse()
Answer: b
30) Which of these method is used to find a URL from the cache of httpd?
a) findfromCache()
b) findFromCache()
c) serveFromCache()
d) getFromCache()
Answer: c
Explanation: serveFromCatche() is a boolean method that attempts to find a particular URL in the cache.
If it is successful then the content of that cache entry are written to the client, otherwise it returns false.
31) Which of these variables stores the number of hits that are successfully served out of cache?
a) hits
b) hitstocache
c) hits_to_cache
d) hits.to.cache
Answer: d
32) Which of these class is used for operating on request from the client to the server?
a) http
b) httpDecoder
c) httpConnection
d) httpd
Answer: d
33) Which of these method of httpd class is used to write UrlCacheEntry object into local disk?
a) writeDiskCache()
b) writetoDisk()
c) writeCache()
d) writeDiskEntry()
Answer: a
Explanation: The writeDiskCache() method takes an UrlCacheEntry object and writes it persistently into
the local disk. It constructs directory names out of URL, making sure to replace the slash(/) characters
with system dependent seperatorChar.
a) port
b) cache
c) log
Answer: d
Explanation: There are 5 instance variables : port, docRoot, log, cache and stopFlag. All of them are
private.
a) port
b) cache
c) log
d) stopFlag
Answer: c
Explanation: cache is the Hashtable where all of the files are cached in RAM and is initialized when the
object is created.
Answer: a
a) URLNotFound
b) URLSourceNotFound
c) MalformedURLException
d) URLNotFoundException
Answer: c
b) getHost()
c) GetHost()
d) gethost()
Answer: b
39) Which of these methods is used to know the full URL of an URL object?
a) fullHost()
b) getHost()
c) ExternalForm()
d) toExternalForm()
Answer: d
40) Which of these class is used to access actual bits or content information of a URL?
a) URL
b) URLDecoder
c) URLConnection
Answer: d
Explanation: URL, URLDecoder and URLConnection all there are used to access information stored in a
URL.
b) URL
c) InetAddress
d) ContentHandler
Answer: c
Explanation: InetAddress class encapsulate both IP address and DNS, we can interact with this class by
using name of an IP host.
42) Which of these is wrapper around everything associated with a reply from an http server?
a) HTTP
b) HttpResponse
c) Httpserver
d) httpserver
Answer: a
Explanation: HttpResponse is wrapper around everything associated with a reply from an http server.
43) Which of these tranfer protocol must be used so that URL can be accessed by URLConnection class
object?
a) http
b) https
Answer: a
Explanation: for a URL to be accessed from remote location http protocol must be used.
44) Which of these methods is used to know when was the URL last modified?
a) LastModified()
b) getLastModified()
c) GetLastModified()
d) getlastModified()()
Answer: b
45) Which of these methods is used to know the type of content used in the URL?
a) ContentType()
b) contentType()
c) getContentType()
d) GetContentType()
Answer: c
46) Which of these class is used to access actual bits or content information of a URL?
a) URL
b) URLDecoder
c) URLConnection
Answer: d
Explanation: URL, URLDecoder and URLConnection all there are used to access information stored in a
URL.
47) Which of these data member of HttpResponse class is used to store the response from a http
server?
a) status
b) address
c) statusResponse
d) statusCode
Answer: d
Explanation: When we send a request to a http server it respond with a status code this status code is
stored in statusCode and a textual equivalent which is stored in reasonPhrase.
48) Which of these interface abstractes the output of messages from httpd?
a) LogMessage
b) LogResponse
c) Httpdserver
d) httpdResponse
Answer: a
Explanation: LogMessage is a simple interface that is used to abstract the output of messages from the
httpd.
49) Which of these class is used to create servers that listen for either local or remote client programs?
a) httpServer
b) ServerSockets
c) MimeHeader
d) HttpResponse
Answer: b
50) Which of these is a standard for communicating multimedia content over email?
a) http
b) https
c) Mime
d) httpd
Answer: c
Explanation: MIME is an internet standard for communicating multimedia content over email. The HTTP
protocol uses and extends the notion of MIME headers to pass attribute pairs between HTTP client and
server.
51) Which of these methods is used to make raw MIME formatted string?
a) parse()
b) toString()
c) getString()
d) parseString()
Answer: a
52) Which of these class is used for operating on request from the client to the server?
a) http
b) httpDecoder
c) httpConnection
d) httpd
Answer: d
53) Which of these method of MimeHeader is used to return the string equivalent of the values stores
on MimeHeader?
a) string()
b) toString()
c) convertString()
d) getString()
Answer:b
Explanation: toString() does the reverse of parse() method, it is used to return the string equivalent of
the values stores on MimeHeader.
1. import java.net.*;
2. class networking {
7. System.out.print(x);
8. }
9. }
a) 0
b) 1
c) true
d) false
Answer: c
Output:
$ javac networking.java
$ java networking
true
1. import java.net.*;
2. class networking {
6. boolean x = obj1.equals(obj2);
7. System.out.print(x);
8. }
9. }
a) 0
b) 1
c) true
d) false
Answer: d
Output:
$ javac networking.java
$ java networking
true
2. class streams {
4. try {
7. oos.writeFloat(3.5);
8. oos.flush();
9. oos.close();
10. }
11. catch(Exception e) {
13. System.exit(0);
14. }
15. try {
16. FileInputStream fis = new FileInputStream("serial");
18. ois.close();
19. System.out.println(ois.available());
20. }
22. System.out.print("deserialization");
23. System.exit(0);
24. }
25. }
26. }
a) 1
b) 2
c) 3
d) 4
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
1. import java.net.*;
2. class networking {
5. System.out.print(obj1.getHostName());
6. }
7. }
a) cisco
b) cisco.com
c) www.cisco.com
d) None of the mentioned
Answer: b
Output:
$ javac networking.java
$ java networking
cisco.com
1. import java.net.*;
2. class networking {
5. System.out.print(obj.getProtocol());
6. }
7. }
a) http
b) https
c) www
d) com
Answer: a
Explanation: obj.getProtocol() is used to know the protocol used by the host. http stands for hyper text
transfer protocol, usually 2 types of protocols are used http and https, where s in https stands for
secured.
Output:
$ javac networking.java
$ java networking
http
1. import java.net.*;
2. class networking {
5. System.out.print(obj.getPort());
6. }
7. }
a) 1
b) 0
c) -1
d) garbage value
Answer: c
Explanation: Since we have not explicitly set the port default value that is -1 is printed.
Output:
$ javac networking.java
$ java networking
-1
1. import java.net.*;
2. class networking {
5. System.out.print(obj.getHost());
6. }
7. }
a) sanfoundry
b) sanfoundry.com
c) www.sanfoundry.com
d) http://www.sanfoundry.com/javamcq
Answer: c
Output:
$ javac networking.java
$ java networking
www.sanfoundry.com
1. import java.net.*;
2. class networking {
5. System.out.print(obj.toExternalForm());
6. }
7. }
a) sanfoundry
b) sanfoundry.com
c) www.sanfoundry.com
d) http://www.sanfoundry.com/javamcq
Answer: d
Output:
$ javac networking.java
$ java networking
http://www.sanfoundry.com/javamcq
1. import java.net.*;
2. class networking {
6. System.out.print(obj1.getContentType());
7. }
8. }
Note: Host URL is written in html and simple text.
a) html
b) text
c) html/text
d) text/html
Answer: d
Output:
$ javac networking.java
$ java networking
text/html
1. import java.net.*;
2. class networking {
8. }
9. }
a) 126
b) 127
c) Compilation Error
d) Runtime Error
Answer: b
Output:
$ javac networking.java
$ java networking
127
1. import java.net.*;
2. class networking {
6. System.out.print(obj1.getLastModified);
7. }
8. }
a) july
b) 18-6-2013
Answer: d
Output:
$ javac networking.java
$ java networking
1. import java.net.*;
2. class networking {
5. System.out.print(obj.toExternalForm());
6. }
7. }
a) sanfoundry
b) sanfoundry.com
c) www.sanfoundry.com
d) http://www.sanfoundry.com/javamcq
Answer: d
Output:
$ javac networking.java
$ java networking
http://www.sanfoundry.com/javamcq
1. import java.net.*;
2. class networking {
6. System.out.print(obj1.getContentType());
7. }
8. }
a) html
b) text
c) html/text
d) text/html
Answer: d
Output:
$ javac networking.java
$ java networking
text/html
67) What is the output of this program?
1. import java.net.*;
2. class networking {
5. System.out.print(obj.toExternalForm());
6. }
7. }
a) sanfoundry
b) sanfoundry.com
c) www.sanfoundry.com
d) http://www.sanfoundry.com/javamcq
Answer: d
Output:
$ javac networking.java
$ java networking
http://www.sanfoundry.com/javamcq
68) What is the output of this program?
1. import java.net.*;
2. class networking {
7. System.out.print(len);
8. }
9. }
a) 126
b) 127
c) Compilation Error
d) Runtime Error
Answer: b
Output:
$ javac networking.java
$ java networking
127
1. import java.net.*;
2. class networking {
5. System.out.print(obj1.getHostName());
6. }
7. }
a) cisco
b) cisco.com
c) www.cisco.com
Answer: b
Output:
$ javac networking.java
$ java networking
cisco.com
1. import java.net.*;
2. class networking {
6. System.out.print(obj1.getContentType());
7. }
8. }
a) html
b) text
c) html/text
d) text/html
Answer: d
Output:
$ javac networking.java
$ java networking
text/html
1. import java.net.*;
2. class networking {
5. System.out.print(obj.toExternalForm());
6. }
7. }
a) sanfoundry
b) sanfoundry.com
c) www.sanfoundry.com
d) http://www.sanfoundry.com/javamcq
Answer: d
Output:
$ javac networking.java
$ java networking
http://www.sanfoundry.com/javamcq
1. import java.util.*;
2. class Linkedlist {
5. obj.add("A");
6. obj.add("B");
7. obj.add("C");
8. obj.addFirst("D");
9. System.out.println(obj);
10. }
11. }
a) [A, B, C]
b) [D, B, C]
c) [A, B, C, D]
d) [D, A, B, C]
Answer: d
Explanation: obj.addFirst(“D”) method is used to add ‘D’ to the start of a LinkedList object obj.
Output:
$ javac Linkedlist.java
$ java Linkedlist
[D, A, B, C]
1. import java.util.*;
2. class Linkedlist {
5. obj.add("A");
6. obj.add("B");
7. obj.add("C");
8. obj.removeFirst();
9. System.out.println(obj);
10. }
11. }
a) [A, B]
b) [B, C]
c) [A, B, C, D]
d) [A, B, C]
Answer: b
Output:
$ javac Linkedlist.java
$ java Linkedlist
[B, C]
1. import java.util.*;
2. class Output {
5. obj.add("A");
6. obj.add("B");
7. obj.add("C");
9. }
10. }
a) ABC 3
b) [A, B, C] 3
c) ABC 2
d) [A, B, C] 2
Answer: b
Explanation: HashSet obj creates an hash object which implements Set interface, obj.size() gives the
number of elements stored in the object obj which in this case is 3.
Output:
$ javac Output.java
$ java Output
[A, B, C] 3
1. import java.util.*;
2. class Output {
5. t.add("3");
6. t.add("9");
7. t.add("1");
8. t.add("4");
9. t.add("8");
10. System.out.println(t);
11. }
12. }
a) [1, 3, 5, 8, 9]
b) [3, 4, 1, 8, 9]
c) [9, 8, 4, 3, 1]
d) [1, 3, 4, 8, 9]
Answer: d
Explanation:TreeSet class uses set to store the values added by function add in ascending order using
tree for storage
Output:
$ javac Output.java
$ java Output
[1, 3, 4, 8, 9]
1. class exception_handling {
3. try {
4. int a = args.length;
5. int b = 10 / a;
6. System.out.print(a);
7. }
8. catch (ArithmeticException e) {
9. System.out.println("1");
10. }
11. }
12. }
a) 0
b) 1
c) Compilation Error
d) Runtime Error
Answer: b
Output:
$ javac exception_handling.java
$ java exception_handling
1. class exception_handling {
2. public static void main(String args[]) {
3. try {
5. System.out.print("A");
6. }
7. catch(ArithmeticException e) {
8. System.out.print("B");
9. }
10. }
11. }
a) A
b) B
c) Compilation Error
d) Runtime Error
Answer: d
Explanation: try block is throwing NullPointerException but the catch block is used to counter
Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it,
runtime error occurs.
Output:
$ javac exception_handling.java
$ java exception_handling
1. class exception_handling {
3. System.out.print("0");
5. }
7. try {
8. throwexception();
9. }
10. catch (ArithmeticException e) {
11. System.out.println("A");
12. }
13. }
14. }
a) A
b) 0
c) 0A
d) Exception
Answer: c
Output:
$ javac exception_handling.java
$ java exception_handling
0A
1.class exception_handling
2. {
3. public static void main(String args[])
4. {
5. try
6. {
7. int a = 1;
8. int b = 10 / a;
9. try
10. {
11. if (a == 1)
12. a = a / a - a;
13. if (a == 2)
14. {
16. c[8] = 9;
17. }
18. }
19. finally
20. {
21. System.out.print("A");
22. }
23. }
25. {
26. System.out.println("B");
27. }
28. }
29. }
a) A
b) B
c) AB
d) BA
Answer:a
Explanation: The inner try block does not have a catch which can tackle
ArrayIndexOutOfBoundException hence finally is executed which prints ‘A’ the outer try block does have
catch for ArrayIndexOutOfBoundException exception but no such exception occurs in it hence its catch
is never executed and only ‘A’ is printed.
Output:
$ javac exception_handling.java
$ java exception_handling
1. class exception_handling {
3. try {
4. int a = args.length;
5. int b = 10 / a;
6. System.out.print(a);
7. try {
8. if (a == 1)
9. a = a / a - a;
10. if (a == 2) {
12. c[8] = 9;
13. }
14. }
16. System.out.println("TypeA");
17. }
19. System.out.println("TypeB");
20. }
21. }
22. }
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error
Answer: c
Output:
$ javac exception_handling.java
$ java exception_handling