ExamVeda
Login
Home
41
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());
    }
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
42
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());
    }
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
43
Which of these is wildcard symbol?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
44
What will be the output of the following Java 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)");
    }
}
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
45
Which of these is an correct way making a list that is upper bounded by class Number?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
46
What will be the output of the following Java 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);
    }
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
47
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 <Integer> gs = new genericstack<Integer>();
        gs.push(36);
        System.out.println(gs.pop());
    }
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
48
Which of these type parameters is used for a generic methods to return and accept a number?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
49
Which of the following cannot be Type parameterized?
Discuss
Answer & Solution
Answer: Option C
Solution:
Generics in Java are tied to the type of objects, meaning they work at the instance level.

Static methods belong to the class itself and are shared across all instances.

Since generics are resolved at the object level, type parameters cannot be directly used within static methods.

Why the other options are incorrect:

Option A: Overloaded Methods
Overloaded methods can have different generic type parameters.
The compiler can differentiate them based on their method signatures, making generic type parameters valid for overloaded methods.

Option B: Generic methods
Generic methods explicitly use type parameters, and this is a common and valid usage of generics in Java.

Option D: Overriding methods
Methods with generic type parameters can be overridden.
The overridden method must respect the same type parameter or use a wildcard (?).
50
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<Integer> li = Arrays.asList(1, 2, 3);
        System.out.println(sumOfList(li));
    }
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board