This set of Java Multiple Choice Questions & Answers (MCQs) on “Generic Methods”.
1. What are generic methods? Answer: c 2. Which of these type parameters is used for a generic methods to return and accept any type of object? Answer: c 3. Which of these type parameters is used for a generic methods to return and accept a number? Answer: b 4. Which of these is an correct way of defining generic method? Answer: b 5. Which of the following allows us to call generic methods as a normal method? Answer: a 6. What will be the output of the following Java program? a) H 7. What will be the output of the following Java program? a) 0 8. What will be the output of the following Java program? a) Error
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
Clarification: 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.
a) K
b) N
c) T
d) V
Clarification: 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.
a) K
b) N
c) T
d) V
Clarification: N is used for Number.
a)
b) public
c) class
d)
Clarification: 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.
a) Type Interface
b) Interface
c) Inner class
d) All of the mentioned
Clarification: Type inference, allows you to invoke a generic method as an ordinary method, without specifying a type between angle brackets.
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());
}
}
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