Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
Java Generics Quiz Part – 1 | In Java Generics feature was introduced in Java 5 version to provide the type for method’s parameter and return type dynamically by achieving compile-time type checking for solving ClassCastException. Here we will see different Java generics test Questions.
Also See:- Java Generics Quiz Part – 2, Java Generics Quiz Part – 3
Q1) Which of the following are the benefits of Java generics?
a) Type-safety
b) Typecasting is not required
c) Compile-Time Checking
d) All of these
View Answer
Answer:- d) All of theseQ2) Why are generics used?
a) Generics make code faster
b) Generics make code more optimized 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 a runtime
View Answer
Answer:- c) Generics add stability to your code by making more of your bugs detectable at compile timeQ3) Which of these types cannot be used to initiate a generic type?
a) Integer class
b) Float class
c) Primitive Types
d) Collections
View Answer
Answer:- c) Primitive TypesQ4) Which of the following reference types cannot be generic?
a) Anonymous inner class
b) Interface
c) Inner class
d) All of the mentioned
View Answer
Answer:- a) Anonymous inner classQ5) Generics does not work with?
a) Set
b) List
c) Tree
d) Array
View Answer
Answer:- d) ArrayQ6) 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
View Answer
Answer:- a) Type InterfaceType inference, allows us to invoke a generic method as an ordinary method, without specifying a type between angle brackets.
Q7) Which of the following is an invalid Java generics syntax?
a) ArrayList<String> al1 = new ArrayList<String>();
b) List<String> al2 = new ArrayList<String>();
c) Collection<String> al3 = new ArrayList<String>();
d) ArrayList<Object> al4 = new ArrayList<String>();
View Answer
Answer:- d) ArrayList<Object> al4 = new ArrayList<String>();Polymorphism is applicable only for base type but not for parameter type. For the (d) object we will get compile-time error:- incompatible types: ArrayList<String> cannot be converted to ArrayList<Object>
Q8) Which of the following is an invalid Java generics syntax?
a) ArrayList<String> al1 = new ArrayList<String>();
b) ArrayList<int> al2 = new ArrayList<int>();
c) ArrayList<Integer> al3 = new ArrayList<Integer>();
d) ArrayList<Object> al4 = new ArrayList<Object>();
View Answer
Answer:- b) ArrayList<int> al2 = new ArrayList<int>();For the type parameter, we can provide any class/interface name but not primitive. If we are trying to provide primitive then we will get a compile-time error.
Q9) If we declare ArrayList of String type then which type of elements we can add into the ArrayList? Find the options which are invalid? (More than options are invalid)
ArrayList<String> al = new ArrayList<String>();
a) al.add(“KnowProgram”);
b) al.add(null);
c) al.add(10);
d) al.add(new StringBuilder(“Generic”));
View Answer
Answer:- (c) and (d) are invalid.If we declare ArrayList of String type then we can add only String type of elements to the ArrayList. If we try to add any other type then we will get a compile-time error. Here (a) is valid because “KnowProgram” is of string type, (b) is also valid because null is the default value for all objects, (c) is invalid because 10 is of Integer type, (d) is also invalid because “Generic” is a StringBuilder type and we can’t add them to ArrayList of String type.
Q10) Which of these is a valid declaration of a class?
a) class Test<T extends Runnable> { }
b) class Test<T implements Runnable> { }
c) class Test<T super Runnable> { }
d) All of these
View Answer
Answer:- a) class Test<T extends Runnable> { }The valid syntax to declare a bounded class with generics is:- class Test<T extends X> { }. We must use the “extends” keyword. The “implements” and “super” are invalid declarations. Here X can be either class or interface.
Q11) For the below Test class which of the following way of object creation is invalid?
class Test<T extends Number> { }
a) Test<Integer> t1 = new Test<Integer>();
b) Test<String> t2 = new Test<String>();
c) Test<Double> t3 = new Test<Double>();
d) Test<Number> t4 = new Test<Number>();
View Answer
Answer:- b) Test<String> t2 = new Test<String>();In the syntax “class Test<T extends X> { }” if the X is a class then we can pass either X type or its child class type. The bounded type of Test is “Number” therefore we can pass either the “Number” type or its child class. The “Integer” and “Double” are child classes of “Number”. But “Number” and “String” classes are siblings therefore we can’t pass String.
Q12) For the below Test class which of the following way of object creation is invalid?
class Test<T extends Runnable> { }
a) Test<Runnable> t1 = new Test<Runnable>();
b) Test<Thread> t2 = new Test<Thread>();
c) Test<Integer> t3 = new Test<Integer>();
d) None of these
View Answer
Answer:- c) Test<Integer> t3 = new Test<Integer>();In the syntax “class Test<T extends X> { }” if the X is an interface then we must pass either X type or its implementation class. Here “” therefore we can pass either Runnable or its implementation class. The (a) option is valid because we are passing Runnable. Thread class is an implementation class of Runnable class therefore (b) option is valid. But there is no relation between the Integer class and the Runnable interface therefore (c) option gives compile-time error:- type argument Integer is not within bounds of type-variable T.
Q13) For the below Test class which of the following way of object creation is invalid?
class Test<T extends Runnable> { }
class MyThread1 extends Thread { }
class MyThread2 implements Runnable {
public void run(){}
}
a) Test<Runnable> t1 = new Test<Runnable>();
b) Test<MyThread1> t2 = new Test<MyThread1>();
c) Test<MyThread2> t3 = new Test<MyThread2>();
d) None of these
View Answer
Answer:- d) None of theseThis question is similar to the previous question. We can pass either the Runnable type or its implementation class. The (a) option is valid because we are passing the Runnable interface. The “MyThread1” class extends the Thread class and the Thread class implements the Runnable interface, therefore indirectly MyThread1 class is also an implementation class of the Runnable interface. Hence option (b) is valid. The “MyThread2” class implements the Runnable interface therefore option (c) is also valid.
Q14) We can define bounded types in combination. Which of the following is a valid declaration?
a) class Test<T extends Number implements Runnable> { }
b) class Test<T extends Number super Runnable> { }
c) class Test<T extends Number & Runnable> { }
d) class Test<T extends Number | Runnable> { }
View Answer
Answer:- c) class Test<T extends Number & Runnable> { }The syntax to define bounded types in combination is:- class Test<T extends X & Y>, we can’t use implements or super keyword.
Q15) Which of the following is/are valid declarations?
a) class Test<T extends Number & Runnable> { }
b) class Test<T extends Runnable & Number> { }
c) class Test<T extends Number & Thread> { }
d) All of these
View Answer
Answer:- a) class Test<T extends Number & Runnable> { }As per OOPs, the class should be taken first before the interface name. But in option (b) we used “extends Runnable & Number”, the correct form is:- “extends Number & Runnable”. Normally, “class A extends B implements C” is valid but “class A implements C extends B” is invalid, because class should be taken first compared to the interface. Hence option (b) is invalid.
In option (c) both Number and Thread are classes. As per OOPs, any Java class can’t extend more than one class simultaneously. Hence option (c) is also invalid.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!