This set of Java Multiple Choice Questions & Answers (MCQs) on “Generics”.
1. What will be the output of the following Java code?
advertisement
-
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.println(gs.pop());
-
}
-
}
a) H 2. What will be the output of the following Java code? advertisement a) 0 3. What will be the output of the following Java code? a) Error advertisement 4. What will be the output of the following Java program? a) H advertisement 5. What will be the output of the following Java program? a) H 6. Which of these Exception handlers cannot be type parameterized? Answer: d 7. Which of the following cannot be Type parameterized? Answer: a
b) Hello
c) Runtime Error
d) Compilation Error
Clarification: None.
Output:
$ javac Output.javac
$ java Output
Hello
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());
}
}
b) 36
c) Runtime Error
d) Compilation Error
Clarification: None.
Output:
$ javac Output.javac
$ java Output
36
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());
}
}
b) Hello
c) 36
d) Hello 36
Clarification: None.
Output:
$ javac Output.javac
$ java Output
Hello 36
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());
}
}
b) Hello
c) Runtime Error
d) Compilation Error
Clarification: 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
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());
}
}
b) Hello
c) Runtime Error
d) Compilation Error
Clarification: generic stack 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
a) catch
b) throw
c) throws
d) all of the mentioned
Clarification: we cannot Create, Catch, or Throw Objects of Parameterized Types as generic class cannot extend the Throwable class directly or indirectly.
a) Overloaded Methods
b) Generic methods
c) Class methods
d) Overriding methods
Clarification: Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type.