Java MCQ Set 1
1. What is use of Observable class?
a) It is used to create global subclasses
b) It is used to create classes that other part of program can observe
c) It is used to create classes that can be accessed by other parts of program
d) It is used to create methods that can be accessed by other parts of program
Answer
Answer: b [Reason:] The Observable class is used to create subclasses that other part of program can observe.
2. Which of these methods is used to notify observer the change in observed object?
a) update()
b) notify()
c) check()
d) observed()
Answer
Answer: a [Reason:] None.
3. Which of these methods calls update() method?
a) notify()
b) observeObject()
c) updateObserver()
d) notifyObserver()
Answer
Answer: d [Reason:] notifyObserver() notifies all the observers of the invoking object that it has changed by calling update(). A null is passed as the second argument to update().
4. Which of these methods is called when observed object has changed?
a) setChanged()
b) update()
c) notifyObserver()
d) all of the mentioned
Answer
Answer: d [Reason:] None.
5. Which of these classes can schedule task for execution in future?
a) Thread
b) Timer
c) System
d) Observer
Answer
Answer: b [Reason:] Timer and TimerTask are the classes that support the ability to schedule tasks foe execution at some future time.
6. Which of these interfaces is implemented by TimerTask class?
a) Runnable
b) Thread
c) Observer
d) ThreadCount
Answer
Answer: a [Reason:] None.
7. Which of these package provides the ability to read and write in Zip format?
a) java.lang
b) java.io
c) java.util.zip
d) java.util.zar
Answer
Answer: c [Reason:] None.
8. What is the output of this program?
-
import java.util.*;
-
class LOCALE_CLASS
-
{
-
public static void main(String args[])
-
{
-
Locale obj = new Locale("HINDI", "INDIA") ;
-
System.out.print(obj.getCountry());
-
}
-
}
a) India
b) INDIA
c) Compilation Error
d) Nothing is displayed
Answer
Answer: b [Reason:] None.
Output:
$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS
INDIA
9. What is the output of this program?
-
import java.util.*;
-
class LOCALE_CLASS
-
{
-
public static void main(String args[])
-
{
-
Locale obj = new Locale("HINDI") ;
-
System.out.print(obj.getDisplayLanguage());
-
}
-
}
a) India
b) INDIA
c) HINDI
d) Nothing is displayed
Answer
Answer: c [Reason:] None.
Output:
$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS
HINDI
10. What is the output of this program?
-
import java.util.*;
-
class LOCALE_CLASS
-
{
-
public static void main(String args[])
-
{
-
Locale obj = new Locale("HINDI", "INDIA") ;
-
System.out.print(obj.getDisplayLanguage());
-
}
-
}
a) India
b) INDIA
c) HINDI
d) Nothing is displayed
Answer
Answer: c [Reason:] None.
Output:
$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS
HINDI
Java MCQ Set 2
1. What is the use of try & catch?
a) It allows us to manually handle the exception
b) It allows to fix errors
c) It prevents automatic terminating of the program in cases when an exception occurs
d) All of the mentioned
Answer
Answer: d [Reason:] None.
2. Which of these keywords are used for the block to be examined for exceptions?
a) try
b) catch
c) throw
d) check
Answer
Answer: a [Reason:] try is used for the block that needs to checked for exception.
3. Which of these keywords are used for the block to handle the exceptions generated by try block?
a) try
b) catch
c) throw
d) check
Answer
Answer: b [Reason:] None.
4. Which of these keywords are used for generating an exception manually?
a) try
b) catch
c) throw
d) check
Answer
Answer: c [Reason:] None.
5. Which of these statements is incorrect?
a) try block need not to be followed by catch block
b) try block can be followed by finally block instead of catch block
c) try can be followed by both catch and finally block
d) try need not to be followed by anything
Answer
Answer: d [Reason:] try must be followed by either catch or finally block.
6. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = 0;
-
int b = 5;
-
int c = b / a;
-
System.out.print("Hello");
-
}
-
catch(Exception e)
-
{
-
System.out.print("World");
-
}
-
}
-
}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
Answer
Answer: b [Reason:] None.
Output:
$ javac Output.javac
java Output
World
7. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = 0;
-
int b = 5;
-
int c = a / b;
-
System.out.print("Hello");
-
}
-
catch(Exception e)
-
{
-
System.out.print("World");
-
}
-
}
-
}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
Answer
Answer: a [Reason:] None.
Output:
$ javac Output.javac
java Output
Hello
8. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = 0;
-
int b = 5;
-
int c = b / a;
-
System.out.print("Hello");
-
}
-
}
-
}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
Answer
Answer: d [Reason:] try must be followed by either catch or finally
Output:
$ javac Output.javac
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Syntax error, insert “Finally” to complete BlockStatements
9. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = 0;
-
int b = 5;
-
int c = a / b;
-
System.out.print("Hello");
-
}
-
finally
-
{
-
System.out.print("World");
-
}
-
}
-
}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
Answer
Answer: c [Reason:] finally block is always executed after try block, no matter exception is found or not.
Output:
$ javac Output.javac
java Output
HelloWorld
10. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = 0;
-
int b = 5;
-
int c = b / a;
-
System.out.print("Hello");
-
}
-
catch(Exception e)
-
{
-
System.out.print("World");
-
}
-
finally
-
{
-
System.out.print("World");
-
}
-
}
-
}
a) Hello
b) World
c) HelloWOrld
d) WorldWorld
Answer
Answer: d [Reason:] finally block is always executed after tryblock, no matter exception is found or not. catch block is executed only when exception is found. Here divide by zero exception is found hence both catch and finally are executed.
Output:
$ javac Output.javac
java Output
WorldWorld
Java MCQ Set 3
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?
-
public class BoxDemo
-
{
-
public static <U> void addBox(U u, java.util.List<Box<U>> boxes)
-
{
-
Box<U> box = new Box<>();
-
box.set(u);
-
boxes.add(box);
-
}
-
public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
-
{
-
int counter = 0;
-
for (Box<U> box: boxes)
-
{
-
U boxContents = box.get();
-
System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
-
counter++;
-
}
-
}
-
public static void main(String[] args)
-
{
-
java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
-
BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
-
BoxDemo.outputBoxes(listOfIntegerBoxes);
-
}
-
}
a) 10
b) Box #0 [10].
c) Box contains [10].
d) Box #0 contains [10].
Answer
Answer: d [Reason:] None.
Output:
$ javac Output.javac
$ java Output
Box #0 contains [10].
8. What is the output of this program?
-
public class BoxDemo
-
{
-
public static <U> void addBox(U u,
-
java.util.List<Box<U>> boxes)
-
{
-
Box<U> box = new Box<>();
-
box.set(u);
-
boxes.add(box);
-
}
-
public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
-
{
-
int counter = 0;
-
for (Box<U> box: boxes)
-
{
-
U boxContents = box.get();
-
System.out.println("[" + boxContents.toString() + "]");
-
counter++;
-
}
-
}
-
public static void main(String[] args)
-
{
-
java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
-
BoxDemo.<Integer>addBox(Integer.valueOf(0), listOfIntegerBoxes);
-
BoxDemo.outputBoxes(listOfIntegerBoxes);
-
}
-
}
a) 0
b) 1
c) [1].
d) [0].
Answer
Answer: d [Reason:] None.
Output:
$ javac Output.javac
$ java Output
[0]
9. What is the output of this program?
-
import java.util.*;
-
public class genericstack <E>
-
{
-
Stack <E> stk = new Stack <E>();
-
public void push(E obj)
-
{
-
stk.push(obj);
-
}
-
public E pop()
-
{
-
E obj = stk.pop();
-
return obj;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
genericstack <String> gs = new genericstack<String>();
-
gs.push("Hello");
-
System.out.print(gs.pop() + " ");
-
genericstack <Integer> gs = new genericstack<Integer>();
-
gs.push(36);
-
System.out.println(gs.pop());
-
}
-
}
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?
-
public class BoxDemo
-
{
-
public static <U> void addBox(U u,
-
java.util.List<Box<U>> boxes)
-
{
-
Box<U> box = new Box<>();
-
box.set(u);
-
boxes.add(box);
-
}
-
public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
-
{
-
int counter = 0;
-
for (Box<U> box: boxes)
-
{
-
U boxContents = box.get();
-
System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
-
counter++;
-
}
-
}
-
public static void main(String[] args)
-
{
-
java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
-
BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
-
BoxDemo.outputBoxes(listOfIntegerBoxes);
-
}
-
}
a) 10
b) Box #0 [10].
c) Box contains [10].
d) Box #0 contains [10].
Answer
Answer: d [Reason:] None.
Output:
$ javac Output.javac
$ java Output
Box #0 contains [10].
Java MCQ Set 4
1. What does URL stands for?
a) Uniform Resource Locator
b) Uniform Resource Latch
c) Universal Resource Locator
d) Universal Resource Latch
Answer
Answer: a [Reason:] URL is Uniform Resource Locator.
2. Which of these exception is thrown by URL class’s constructors?
a) URLNotFound
b) URLSourceNotFound
c) MalformedURLException
d) URLNotFoundException
Answer
Answer: c [Reason:] None.
3. Which of these methods is used to know host of an URL?
a) host()
b) getHost()
c) GetHost()
d) gethost()
Answer
Answer: b [Reason:] None.
4. Which of these methods is used to know the full URL of an URL object?
a) fullHost()
b) getHost()
c) ExternalForm()
d) toExternalForm()
Answer
Answer: d [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 class is used to encapsulate IP address and DNS?
a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler
Answer
Answer: c [Reason:] InetAddress class encapsulate both IP address and DNS, we can interact with this class by using name of an IP host.
Java MCQ Set 5
1. Which of these is wildcard symbol?
a) ?
b) !
c) %
d) &
Answer
Answer: a [Reason:] In generic code, the question mark (?), called the wildcard, represents an unknown type.
2. What is use of wildcards?
a) It is used in cases when type being operated upon is not known
b) It is used to make code more readable
c) It is used to access members of super class
d) It is used for type argument of generic method
Answer
Answer: a [Reason:] The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.
3. Which of these keywords is used to upper bound a wildcard?
a) stop
b) bound
c) extends
d) implements
Answer
Answer: c [Reason:] None.
4. Which of these is an correct way making a list that is upper bounded by class Number?
a) List<? extends Number>
b) List<extends ? Number>
c) List(? extends Number)
d) List(? UpperBounds Number)
Answer
Answer: a [Reason:] None.
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 keywords are used for lower bounding a wild card?
a) extends
b) super
c) class
d) lower
Answer
Answer: b [Reason:] A lower bounded wildcard is expressed using the wildcard character (‘?’), following by the super keyword, followed by its lower bound: super A>.
7. What is the output of this program?
-
import java.util.*;
-
class Output
-
{
-
public static double sumOfList(List<? extends Number> list)
-
{
-
double s = 0.0;
-
for (Number n : list)
-
s += n.doubleValue();
-
return s;
-
}
-
public static void main(String args[])
-
{
-
List<Integer> li = Arrays.asList(1, 2, 3);
-
System.out.println(sumOfList(li));
-
}
-
}
a) 0
b) 4
c) 5.0
d) 6.0
Answer
Answer: d [Reason:] None.
Output:
$ javac Output.javac
$ java Output
6.0
8. What is the output of this program?
-
import java.util.*;
-
class Output
-
{
-
public static double sumOfList(List<? extends Number> list)
-
{
-
double s = 0.0;
-
for (Number n : list)
-
s += n.doubleValue();
-
return s;
-
}
-
public static void main(String args[])
-
{
-
List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
-
System.out.println(sumOfList(ld));
-
}
-
}
a) 5.0
b) 7.0
c) 8.0
d) 6.0
Answer
Answer: b [Reason:] None.
Output:
$ javac Output.javac
$ java Output
7.0
9. What is the output of this program?
-
import java.util.*;
-
class Output
-
{
-
public static void addNumbers(List<? super Integer> list)
-
{
-
for (int i = 1; i <= 10; i++)
-
{
-
list.add(i);
-
}
-
}
-
public static void main(String args[])
-
{
-
List<Double> ld = Arrays.asList();
-
addnumbers(10.4);
-
System.out.println("getList(2)");
-
}
-
}
a) 1
b) 2
c) 3
d) 6
Answer
Answer: a [Reason:] None.
Output:
$ javac Output.javac
$ java Output
1
10. What is the output of this program?
-
import java.util.*;
-
public class genericstack <E>
-
{
-
Stack <E> stk = new Stack <E>();
-
public void push(E obj)
-
{
-
stk.push(obj);
-
}
-
public E pop()
-
{
-
E obj = stk.pop();
-
return obj;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
genericstack <Integer> gs = new genericstack<Integer>();
-
gs.push(36);
-
System.out.println(gs.pop());
-
}
-
}
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