Java Network Programming.
Click here to Download this file
Multiple Choice Quwstions and Answers:-

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

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.


3) How many ports of TCP/IP are reserved for specific protocols?

 a) 10
 b) 1024
 c) 2048
 d) 512

Answer: b

4) How many bits are in a single IP address?

 a) 8
 b) 16
 c) 32
 d) 64

Answer: c

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

Answer: d

6) Which of these class is used to encapsulate IP address and DNS?

 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.



7) Which of these clause will be executed even if no exceptions are found?

 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.

8) A single try block must be followed by which of these?

 a) finally
 b) catch
 c) finally & catch
 d) None of the mentioned

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.

9) Which of these packages contain all the Java’s built in exceptions?

 a) java.io
 b) java.util
 c) java.lang
 d) java.net

Answer: c

10) Which of these exceptions handles the divide by zero error?

 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
 Explanation: ArrayIndexOutOfBoundsException is a built in exception that is caused when we try to access an index location which is beyond the length of an array.

12) Which of these standard collection classes implements a linked list data structure?

 a) AbstractList
 b) LinkedList
 c) HashSet
 d) AbstractSet

Answer: b

13) Which of these classes implements Set interface?

 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.

17) Which of these method is used to change an element in a LinkedList Object?

 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

19) Which of these is a return type of getAddress method of DatagramPacket class?

 a) DatagramPacket
 b) DatagramSocket
 c) InetAddress
 d) ServerSocket

Answer: c

20) Which of these is a bundle of information passed between machines?

 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
 c) All of the mentioned
 d) None of the mentioned

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
 d) All of the mentioned

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.


26) Which of these method is used to start a server thread?

 a) run()
 b) start()
 c) runThread()
 d) startThread()

Answer: a
 Explanation: run() method is caleed when the server thread is started.

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.

34) Which of these is an instance variable of class httpd?

 a) port
 b) cache
 c) log
 d) All of the mentioned

Answer: d
 Explanation: There are 5 instance variables : port, docRoot, log, cache and stopFlag. All of them are private.

35) Which of these is an instance variable of httpd that is a Hashtable?

 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.

36) What does URL stands for?

 a) Uniform Resource Locator
 b) Uniform Resource Latch
 c) Universal Resource Locator
 d) Universal Resource Latch

Answer: a
 Explanation: URL is Uniform Resource Locator.

37) Which of these exception is thrown by URL class’s constructors?

 a) URLNotFound
 b) URLSourceNotFound
 c) MalformedURLException
 d) URLNotFoundException

Answer: c

38) Which of these methods is used to know host of an URL?

 a) host()
 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
 d) All of the mentioned

Answer: d
 Explanation: URL, URLDecoder and URLConnection all there are used to access information stored in a URL.

41) Which of these class is used to encapsulate IP address and DNS?

 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.

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
 c) Any Protocol can be used
 d) None of the mentioned

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
 d) All of the mentioned

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.

54) 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

Answer: c

 Output:
 $ javac networking.java
 $ java networking
 true

55) 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

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

56) 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

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

57) 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

Answer: b

 Output:
 $ javac networking.java
 $ java networking
 cisco.com

58) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws MalformedURLException {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

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

59) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws MalformedURLException {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

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

60) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws MalformedURLException {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

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

61) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws MalformedURLException {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

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
 Explanation: toExternalForm() is used to know the full URL of an URL object.

 Output:
 $ javac networking.java
 $ java networking
 http://www.sanfoundry.com/javamcq

62) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws Exception {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

5.            URLConnection obj1 = obj.openConnection();

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

63) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws Exception {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

5.            URLConnection obj1 = obj.openConnection();

6.            int len = obj1.getContentLength();

7.            System.out.print(len);

8.        }

9.    }


Note: Host URL is having length of content 127.

 a) 126
 b) 127
 c) Compilation Error
 d) Runtime Error

Answer: b

 Output:
 $ javac networking.java
 $ java networking
 127

64) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws Exception {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

5.            URLConnection obj1 = obj.openConnection();

6.            System.out.print(obj1.getLastModified);

7.        }

8.    }


Note: Host URL was last modified on july 18 tuesday 2013 .

 a) july
 b) 18-6-2013
 c) Tue 18 Jun 2013
 d) Tue Jun 18 2013

Answer: d

 Output:
 $ javac networking.java
 $ java networking
 Tue Jun 18 2013

65) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws MalformedURLException {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

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
 Explanation: toExternalForm() is used to know the full URL of an URL object.

 Output:
 $ javac networking.java
 $ java networking
 http://www.sanfoundry.com/javamcq

66) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws Exception {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

5.            URLConnection obj1 = obj.openConnection();

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

67) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws MalformedURLException {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

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
 Explanation: toExternalForm() is used to know the full URL of an URL object.

 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 {

3.        public static void main(String[] args) throws Exception {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

5.            URLConnection obj1 = obj.openConnection();

6.            int len = obj1.getContentLength();

7.            System.out.print(len);

8.        }

9.    }

Note: Host URL is having length of content 127.

 a) 126
 b) 127
 c) Compilation Error
 d) Runtime Error

Answer: b

 Output:
 $ javac networking.java
 $ java networking
 127

69) 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

Answer: b

 Output:
 $ javac networking.java
 $ java networking
 cisco.com

70) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws Exception {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

5.            URLConnection obj1 = obj.openConnection();

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


71) What is the output of this program?

1.    import java.net.*;

2.    class networking {

3.        public static void main(String[] args) throws MalformedURLException {

4.            URL obj = new URL("http://www.sanfoundry.com/javamcq");

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
 Explanation: toExternalForm() is used to know the full URL of an URL object.

 Output:
 $ javac networking.java
 $ java networking
 http://www.sanfoundry.com/javamcq

72) What is the output of this program?

1.    import java.util.*;

2.    class Linkedlist {

3.        public static void main(String args[]) {

4.            LinkedList obj = new 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] 

73) What is the output of this program?

1.    import java.util.*;

2.    class Linkedlist {

3.        public static void main(String args[]) {

4.            LinkedList obj = new 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] 

74) What is the output of this program?

1.    import java.util.*;

2.    class Output {

3.        public static void main(String args[]) {

4.            HashSet obj = new HashSet();

5.            obj.add("A");

6.            obj.add("B");

7.            obj.add("C");

8.            System.out.println(obj + " " + obj.size());

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

75) What is the output of this program?

1.    import java.util.*; 

2.    class Output {

3.        public static void main(String args[]) { 

4.            TreeSet t = new TreeSet();

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] 

76) What is the output of this program?

1.    class exception_handling {

2.        public static void main(String args[]) {

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
 
Note : Execution command line : $ java exception_handling

Answer: b

 Output:
 $ javac exception_handling.java
 $ java exception_handling
 1

77) What is the output of this program?

1.    class exception_handling {

2.        public static void main(String args[]) {

3.            try {

4.                throw new NullPointerException ("Hello");

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
 Exception in thread “main” java.lang.NullPointerException: Hello

78) What is the output of this program?

1.    class exception_handling {

2.            static void throwexception() throws ArithmeticException {        

3.                System.out.print("0");

4.                throw new ArithmeticException ("Exception");

5.            }

6.            public static void main(String args[]) {

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

79) What is the output of this program?

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.                         {

15.                             int c[] = {1};

16.                             c[8] = 9;

17.                         }

18.                    }

19.                    finally 

20.                    {

21.                        System.out.print("A");

22.                    }

23.                }

24.                catch (Exception e) 

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
 A

80) What is the output of this program?

1.    class exception_handling {

2.        public static void main(String args[]) {

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) {

11.                         int c = {1};

12.                         c[8] = 9;

13.                     }

14.                }

15.                catch (ArrayIndexOutOfBoundException e) {

16.                    System.out.println("TypeA");

17.            }

18.            catch (ArithmeticException e) {

19.                    System.out.println("TypeB");

20.            }

21.        }

22.    }

 a) TypeA
 b) TypeB
 c) Compilation Error
 d) Runtime Error

 Note: Execution command line: $ java exception_handling one two

Answer: c
 Explanation: try without catch or finally

 Output:
 $ javac exception_handling.java
 $ java exception_handling
 Main.java:9: error: ‘try’ without ‘catch’, ‘finally’ or resource declarations