250+ TOP MCQs on Generic Methods and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) 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

Answer: c
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.

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

Answer: c
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.

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

Answer: b
Clarification: N is used for Number.

4. Which of these is an correct way of defining generic method?
a) name(T1, T2, …, Tn) { /* … */ }
b) public name { /* … */ }
c) class name[T1, T2, …, Tn] { /* … */ }
d) name{T1, T2, …, Tn} { /* … */ }

Answer: b
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.

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

Answer: a
Clarification: 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?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. 	public void push(E obj) 
  6.         {
  7.             stk.push(obj);
  8. 	}
  9. 	public E pop() 
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. 	}
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <String> gs = new genericstack<String>();
  20.             gs.push("Hello");
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer: b
Clarification: None.
Output:

$ javac Output.javac
$ java Output
Hello

7. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. 	public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. 	}
  9.         public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. 	}
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) 0
b) 36
c) Runtime Error
d) Compilation Error

Answer: b
Clarification: None.
Output:

$ javac Output.javac
$ java Output
36

8. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5.         public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. 	}
  9. 	public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. 	}
  14.     }
  15.     class Output 
  16.     {
  17.         public static void main(String args[]) 
  18.         {
  19.             genericstack <String> gs = new genericstack<String>();
  20.             gs.push("Hello");
  21.             System.out.print(gs.pop() + " ");
  22.             genericstack <Integer> gs = new genericstack<Integer>();
  23.             gs.push(36);
  24.             System.out.println(gs.pop());
  25.         }
  26.     }

a) Error
b) Hello
c) 36
d) Hello 36

Answer: d
Clarification: None.
Output:

$ javac Output.javac
$ java Output
Hello 36

Leave a Reply

Your email address will not be published. Required fields are marked *