Java MCQ Number 01074

Java MCQ Set 1

1. Which of these clause will be executed even if no exceptions are found?
a) throws
b) finally
c) throw
d) catch

Answer

Answer: b [Reason:] finally keyword is used to define a set of instructions that will be executed irrespective of the exception found or not.

2. A single try block must be followed by which of these?
a) finally
b) catch
c) finally & catch
d) none of the mentioned

Answer

Answer: c [Reason:] 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.

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

Answer: c [Reason:] None.

4. Which of these exceptions handles the divide by zero error?
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException

Answer

Answer: a [Reason:] None.

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

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

6. 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 = args.length;
  8.                 int b = 10 / a;
  9.                 System.out.print(a);
  10.             }
  11.             catch (ArithmeticException e) 
  12.             {
  13.                     System.out.println("1");
  14.             }
  15.         }
  16.     }

a) 0
b) 1
c) Compilation Error
d) Runtime Error
Note : Execution command line : $ java exception_handling

Answer

Answer: b [Reason:] None.
Output:
$ javac exception_handling.java
$ java exception_handling
1

7. What is the output of this program?

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 throw new NullPointerException ("Hello");
  8.             }
  9.             catch(ArithmeticException e)
  10.             {
  11.         System.out.print("B");        
  12.             }
  13.         }
  14.     }

a) A
b) B
c) Compilation Error
d) Runtime Error

Answer

Answer: d [Reason:] 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

8. What is the output of this program?

  1.     class exception_handling 
  2.     {
  3.             static void throwexception() throws ArithmeticException 
  4.             {        
  5.                 System.out.print("0");
  6.                 throw new ArithmeticException ("Exception");
  7.             }
  8.             public static void main(String args[]) 
  9.             {
  10.                try 
  11.                {
  12.                 throwexception();
  13.                }
  14.                catch (ArithmeticException e) 
  15.                {
  16.                     System.out.println("A");
  17.                }
  18.             }
  19.     }

a) A
b) 0
c) 0A
d) Exception

Answer

Answer: c [Reason:] None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A

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

Answer:a [Reason:] 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

10. 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 = args.length;
  8.                 int b = 10 / a;
  9.                 System.out.print(a);
  10.                 try 
  11.                 {
  12.                      if (a == 1)
  13.                          a = a / a - a;
  14.                      if (a == 2) 
  15.                      {
  16.                          int []c = {1};
  17.                          c[8] = 9;
  18.                      }
  19.                 }
  20.                 catch (ArrayIndexOutOfBoundException e) 
  21.                 {
  22.                     System.out.println("TypeA");
  23.                 }
  24.             catch (ArithmeticException e) 
  25.             {
  26.                     System.out.println("TypeB");
  27.             }
  28.         }
  29.     }

a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error
Note: Execution command line: $ java exception_handling one two

Answer

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

Java MCQ Set 2

1. Why are generics used?
a) Generics make code more fast
b) Generics make code more optimised and readable
c) Generics add stability to your code by making more of your bugs detectable at compile time
d) Generics add stability to your code by making more of your bugs detectable at run time

Answer

Answer: c [Reason:] Generics add stability to your code by making more of your bugs detectable at compile time.

2. Which of these type parameters is used for a generic class to return and accept any type of object?
a) K
b) N
c) T
d) V

Answer

Answer: c [Reason:] T is used for type, A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable..

3. Which of these type parameters is used for a generic class to return and accept a number?
a) K
b) N
c) T
d) V

Answer

Answer: b [Reason:] N is used for Number.

4. Which of these is an correct way of defining generic class?
a) class name(T1, T2, …, Tn) { /* … */ }
b) class name { /* … */ }
c) class name[T1, T2, …, Tn] { /* … */ }
d) class name{T1, T2, …, Tn} { /* … */ }

Answer

Answer: b [Reason:] The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, …, and Tn.

5. Which of the following is incorrect statement regarding the use of generics and parameterized types in Java?
a) Generics provide type safety by shifting more type checking responsibilities to the compiler
b) Generics and parameterized types eliminate the need for down casts when using Java Collections
c) When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve type safety with just a single class definition as opposed to defining multiple classes
d) All of the mentioned

Answer

Answer: c [Reason:] None.

6. Which of the following reference types cannot be generic?
a) Anonymous inner class
b) Interface
c) Inner class
d) All of the mentioned

Answer

Answer: a [Reason:] None.

7. What is the output of this program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. public void push(E obj) 
  6.         {
  7.             stk.push(obj);
  8. }
  9. public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. }
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <String> gs = new genericstack<String>();
  20.             gs.push("Hello");
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer

Answer: b [Reason:] None.
Output:
$ javac Output.javac
$ java Output
Hello

8. What is the output of this program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. }
  9. public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. }
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) 0
b) 36
c) Runtime Error
d) Compilation Error

Answer

Answer: b [Reason:] None.
Output:
$ javac Output.javac
$ java Output
36

9. What is the output of this program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. }
  9. public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. }
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <String> gs = new genericstack<String>();
  20.             gs.push("Hello");
  21.             System.out.print(gs.pop() + " ");
  22.             genericstack <Integer> gs = new genericstack<Integer>();
  23.             gs.push(36);
  24.             System.out.println(gs.pop());
  25.         }
  26.     }

a) Error
b) Hello
c) 36
d) Hello 36

Answer

Answer: d [Reason:] None.
Output:
$ javac Output.javac
$ java Output
Hello 36

10. What is the output of this program?

  1.     import java.util.*;
  2.     public class genericstack <E> 
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. }
  9. public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. }
  14.     }
  15.     class Output 
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer

Answer: d [Reason:] genericstack’s object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.
Output:
$ javac Output.javac
$ java Output
Hello

Java MCQ Set 3

1. What are generic methods?
a) Generic methods are the methods defined in a generic class
b) Generic methods are the methods that extend generic class’s methods
c) Generic methods are methods that introduce their own type parameters
d) Generic methods are methods that take void parameters

Answer

Answer: c [Reason:] Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter’s scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.

2. Which of these type parameters is used for a generic methods to return and accept any type of object?
a) K
b) N
c) T
d) V

Answer

Answer: c [Reason:] T is used for type, A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable..

3. Which of these type parameters is used for a generic methods to return and accept a number?
a) K
b) N
c) T
d) V

Answer

Answer: b [Reason:] N is used for Number.

4. Which of these is an correct way of defining generic method?
a) name(T1, T2, …, Tn) { /* … */ }
b) public name { /* … */ }
c) class name[T1, T2, …, Tn] { /* … */ }
d) name{T1, T2, …, Tn} { /* … */ }

Answer

Answer: b [Reason:] The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method’s return type. For static generic methods, the type parameter section must appear before the method’s return type.

5. Which of the following is incorrect statement regarding the use of generics and parameterized types in Java?
a) Generics provide type safety by shifting more type checking responsibilities to the compiler
b) Generics and parameterized types eliminate the need for down casts when using Java Collections
c) When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve type safety with just a single class definition as opposed to defining multiple classes
d) All of the mentioned

Answer

Answer: c [Reason:] None.

6. Which of the following allows us to call generic methods as a normal method?
a) Type Interface
b) Interface
c) Inner class
d) All of the mentioned

Answer

Answer: a [Reason:] Type inference, allows you to invoke a generic method as an ordinary method, without specifying a type between angle brackets.

7. What is the output of this program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. public void push(E obj) 
  6.         {
  7.             stk.push(obj);
  8. }
  9. public E pop() 
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. }
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <String> gs = new genericstack<String>();
  20.             gs.push("Hello");
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer

Answer: b [Reason:] None.
Output:
$ javac Output.javac
$ java Output
Hello

8. What is the output of this program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. }
  9.         public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. }
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) 0
b) 36
c) Runtime Error
d) Compilation Error

Answer

Answer: b [Reason:] None.
Output:
$ javac Output.javac
$ java Output
36

9. What is the output of this program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5.         public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. }
  9. public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. }
  14.     }
  15.     class Output 
  16.     {
  17.         public static void main(String args[]) 
  18.         {
  19.             genericstack <String> gs = new genericstack<String>();
  20.             gs.push("Hello");
  21.             System.out.print(gs.pop() + " ");
  22.             genericstack <Integer> gs = new genericstack<Integer>();
  23.             gs.push(36);
  24.             System.out.println(gs.pop());
  25.         }
  26.     }

a) Error
b) Hello
c) 36
d) Hello 36

Answer

Answer: d [Reason:] None.
Output:
$ javac Output.javac
$ java Output
Hello 36

10. What is the output of this program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. }
  9. public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. }
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer

Answer: d [Reason:] genericstack’s object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.
Output:
$ javac Output.javac
$ java Output
Hello

Java MCQ Set 4

1. Which of these is wrapper around everything associated with a reply from an http server?
a) HTTP
b) HttpResponse
c) HttpRequest
d) httpserver

Answer

Answer: b [Reason:] HttpResponse is wrapper around everything associated with a reply from an http server.

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

Answer: a [Reason:] for a URL to be accessed from remote location http protocol must be used.

3. Which of these methods is used to know when was the URL last modified?
a) LastModified()
b) getLastModified()
c) GetLastModified()
d) getlastModified()()

Answer

Answer: b [Reason:] None.

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

Answer: c [Reason:] None.

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

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

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

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

Java MCQ Set 5

1. Which of these methods of httpd class is used to read data from the stream?
a) getDta()
b) GetResponse()
c) getStream()
d) getRawRequest()

Answer

Answer: d [Reason:] The getRawRequest() method reads data from a stream until it gets two consecutive newline characters.

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

Answer: b [Reason:] None.

3. Which of these method is used to find a URL from the cache of httpd?
a) findfromCache()
b) findFromCache()
c) serveFromCache()
d) getFromCache()

Answer

Answer: c [Reason:] 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.

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

Answer: d [Reason:] None.

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

Answer: d [Reason:] None.

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

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

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.