Vidyalelo
Java Programming · all questions

Java Generics
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

61

Questions

2/4

Page

Pick an option on a question to see the right answer and solution.

What is the primary advantage of using generics in Java?

Select an option to see the answer and solution.

Which of the following is NOT a generic type parameter in Java?

Select an option to see the answer and solution.

What is the purpose of the `get(int index)` method in a generic list in Java?

Select an option to see the answer and solution.

In Java generics, what is the purpose of the `Pair` class?

Select an option to see the answer and solution.

What does the term "type erasure" mean in Java generics?

Select an option to see the answer and solution.

In Java, which generic class is commonly used for creating key-value pairs?

Select an option to see the answer and solution.

What is the result of the following Java code?
List list = new ArrayList<>();
list.add(new Object());

Select an option to see the answer and solution.

In Java generics, what is the purpose of the `Enum` interface?

Select an option to see the answer and solution.

What does the term "bounded type parameter" mean in Java generics?

Select an option to see the answer and solution.

In Java, what is the result of the expression `List list = new ArrayList<>();`?

Select an option to see the answer and solution.

Which of these instance cannot be created?

Select an option to see the answer and solution.

What will be the output of the following Java 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));
    }
}

Select an option to see the answer and solution.

Which of these data type cannot be type parameterized?

Select an option to see the answer and solution.

What are generic methods?

Select an option to see the answer and solution.

What will be the output of the following Java 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());
    }
}

Select an option to see the answer and solution.

Which of these Exception handlers cannot be type parameterized?

Select an option to see the answer and solution.

What will be the output of the following Java 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());
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java code?
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());
    }
}

Select an option to see the answer and solution.

Which of these keywords is used to upper bound a wildcard?

Select an option to see the answer and solution.

What will be the output of the following Java 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());
    }
}

Select an option to see the answer and solution.