This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Generic Methods”.
1. What are generic methods?
a) Generic methods are the methods defined in a generic class
b) Generic methods are the methods that extend generic class methods
c) Generic methods are methods that introduce their own type parameters
d) Generic methods are methods that take void parameters
View Answer
Explanation: Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.
2. Which of these type parameters is used for a generic methods to return and accept any type of object?
a) K
b) N
c) T
d) V
View Answer
Explanation: T is used for type, A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.
3. Which of these type parameters is used for a generic methods to return and accept a number?
a) K
b) N
c) T
d) V
View Answer
Explanation: N is used for Number.
4. Which of these is an correct way of defining generic method?
a) <T1, T2, …, Tn> name(T1, T2, …, Tn) { /* … */ }
b) public <T1, T2, …, Tn> name<T1, T2, …, Tn> { /* … */ }
c) class <T1, T2, …, Tn> name[T1, T2, …, Tn] { /* … */ }
d) <T1, T2, …, Tn> name{T1, T2, …, Tn} { /* … */ }
View Answer
Explanation: The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method’s return type. For static generic methods, the type parameter section must appear before the method’s return type.
5. Which of the following allows us to call generic methods as a normal method?
a) Type Interface
b) Interface
c) Inner class
d) All of the mentioned
View Answer
Explanation: Type inference, allows you to invoke a generic method as an ordinary method, without specifying a type between angle brackets.
6. 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.println(gs.pop());
}
}
a) H
b) Hello
c) Runtime Error
d) Compilation Error
View Answer
Explanation: None.
Output:
$ javac Output.javac
$ java Output
Hello
7. 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());
}
}
a) 0
b) 36
c) Runtime Error
d) Compilation Error
View Answer
Explanation: None.
Output:
$ javac Output.javac $ java Output 36
8. 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());
}
}
a) Error
b) Hello
c) 36
d) Hello 36
View Answer
Explanation: None.
Output:
$ javac Output.javac $ java Output Hello 36
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 Programming MCQs
- Practice Information Technology MCQs
- Practice BCA MCQs
- Check Programming Books
- Apply for Java Internship