Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Generics, introduced in Java 5, revolutionized how developers approach data types. This powerful feature allows you to write flexible code that can work with a range of data types, from integers and strings to complex custom objects.
The beauty of generics lies in maintaining type safety – even though your code can handle various data types, it ensures type consistency throughout your program. This not only prevents errors at runtime but also improves code readability and maintainability.
Whether you’re a seasoned Java developer looking to refresh your knowledge or a curious beginner embarking on your programming journey, this blog post offers an engaging quiz to test your understanding of generics.
By taking the quiz, you’ll gain valuable insights into core concepts like generic classes, methods, and type parameters. So, dive into the world of generics and challenge yourself – you might be surprised by how much you’ve learned!
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
Results
Results
0 of 15 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Categories
- Not categorized 0%
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- Current
- Review / Skip
- Answered
- Correct
- Incorrect
-
Question 1 of 15
1. Question
Which of the following is not an advantage of Generics in Java ?
CorrectIncorrect -
Question 2 of 15
2. Question
class Generic {
public static void main ( String args [ ] )
{
ArrayList<Integer> list = new ArrayList<Integer> ();
list.add(100);
list.add(200);
list.add(300);
String a = list.get(2);
System.out.println(a);
}
}
CorrectIncorrect -
Question 3 of 15
3. Question
class HashMap {
public static void main ( String args [ ] )
{
Map <Integer,String> map = new HashMap<Integer,String> ();
map.add(1,”one”);
map.add(2.”two”);
map.add(3.”three”);
Set<Map.Entry<Integer,String>> set=map.entrySet();
Iterator<Map.Entry<Integer,String>> itr=set.iterator();
while(itr.hasNext()){
Map.Entry e=itr.next();
System.out.println(e.getKey()+” “+e.getValue());
}
}
CorrectIncorrect -
Question 4 of 15
4. Question
Which of the following classes use Generic Classes ?
CorrectIncorrect -
Question 5 of 15
5. Question
class Generic < G >
{
G obj;
Generic ( G obj )
{
this.obj = obj;
}
public G getObject ( ) {
return this.obj;
}
}
class Main {
public static void main ( String args [ ] )
{
Generic < Integer> obj1 = new Generic <Integer> (10);
System.out.println(obj1.getObject());
Generic <String> obj2 = new Generic <String> (“DataFlair”);
System.out.println(obj2.getObject());
}
}
CorrectIncorrect -
Question 6 of 15
6. Question
class StringSample <S> {
S obj;
StringSample( S obj )
{
this.obj = obj;
}
void print( ) {
return this.obj;
}
}
class Main {
public static void main ( String args [ ] )
{
Generic <Integer> object = new Generic <Integer> (“Ten”);
System.out.println(object.print());
}
}
CorrectIncorrect -
Question 7 of 15
7. Question
Which of the following methods are used to restrict the number of types of data for a class ?
CorrectIncorrect -
Question 8 of 15
8. Question
class Demo < O , T >
O = object1;
T = object2;
Demo ( object1 , object2 )
{
this.object1 = object1;
this.object2 = object2;
}
public void print ( object1 , object2 )
{
System.out.println( object1 + object2 );
}
}
class Main {
public static void main ( String args [ ] )
{
Demo < Integer , String > obj = new Demo < Integer , String > ( “Two” , 2 );
obj.print();
}
}
CorrectIncorrect -
Question 9 of 15
9. Question
class GenericSample {
void method ( E element )
{
System.out.println(element.getclass().getName());
}
}
public static void main ( String args [ ] )
{
method(“DataFlair”);
}
}
CorrectIncorrect -
Question 10 of 15
10. Question
Which of the following is possible in Generic classes ?
CorrectIncorrect -
Question 11 of 15
11. Question
class Error {
Error( obj ) {
this.obj = obj;
}
void display( obj )
{
System.out.println(obj);
}
}
class Main {
public static void main ( String args [ ] )
{
Error < String > object = new Error < String > (“10”);
object.display();
}
}
CorrectIncorrect -
Question 12 of 15
12. Question
class DemoClass {
public <T> void genericsMethod(T data) {
System.out.println(data);
}
}
class Main {
public static void main(String args [ ] )
DemoClass demo = new DemoClass();
demo.<String>genericsMethod(“DataFlair”);
}
}
CorrectIncorrect -
Question 13 of 15
13. Question
Which of the following is used to display the package in generic classes ?
CorrectIncorrect -
Question 14 of 15
14. Question
class Arraylist {
public static void main ( String args [ ] )
{
ArrayList <> a = new ArrayList<> ();
a.add(“String”);
a.add(“Integer”);
a.add(2);
System.out.println(a.get(2));
}
}
CorrectIncorrect -
Question 15 of 15
15. Question
class Sample < S , I >
S = obj1;
I = obj2;
Sample ( obj1 , obj2 )
{
this.obj1 = obj1;
this.obj2 = obj2;
}
public void text ( obj1 , obj2 )
{
System.out.println(obj1);
System.out.println(obj2);
}
}
class Main {
public static void main ( String args [ ] )
{
Sample < String , Integer > s = new Sample < String , Integer > ( “DataFlair” , 1 );
s.text();
}
}
CorrectIncorrect
Summary:
Have you just taken a quiz on Java generics? If so, congratulations! By tackling those questions, you’ve delved into the world of generics and their power to enhance your Java programming.
Generics aren’t just a fancy concept – they offer real benefits like improved compile-time safety, reduced type casting, and the ability to seamlessly work with a wider range of data types. This means you can write code that’s more flexible, robust, and less prone to errors. But the learning doesn’t stop here!
Consider this quiz a springboard for further exploration. Utilize online tutorials, courses, or even practice problems to solidify your understanding of generics. As you master this concept, you’ll unlock the potential to write cleaner, more efficient, and future-proof Java code.
Remember, the journey to becoming a proficient Java programmer is an ongoing adventure, and generics are a valuable tool to have in your arsenal.
