This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Generics”.
1. 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.println(gs.pop());
}
}
a) H
b) Hello
c) Runtime Error
d) Compilation Error
View Answer
Explanation: None.
Output:
$ javac Output.javac
$ java Output
Hello
2. 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());
}
}
a) 0
b) 36
c) Runtime Error
d) Compilation Error
View Answer
Explanation: None.
Output:
$ javac Output.javac $ java Output 36
3. 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());
}
}
a) Error
b) Hello
c) 36
d) Hello 36
View Answer
Explanation: None.
Output:
$ javac Output.javac $ java Output Hello 36
4. 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(36);
System.out.println(gs.pop());
}
}
a) 36
b) Hello
c) Runtime Error
d) Compilation Error
View Answer
Explanation: 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 error: incompatible types: int cannot be converted to String gs.push(36);
5. Which of these Exception handlers cannot be type parameterized?
a) catch
b) throw
c) throws
d) all of the mentioned
View Answer
Explanation: we cannot Create, Catch, or Throw Objects of Parameterized Types as generic class cannot extend the Throwable class directly or indirectly.
6. Which of the following cannot be Type parameterized?
a) Overloaded Methods
b) Generic methods
c) Class methods
d) Overriding methods
View Answer
Explanation: Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice BCA MCQs
- Apply for Computer Science Internship
- Practice Programming MCQs
- Apply for Java Internship
- Check Programming Books