MCQ

Java MCQ – Generic types

This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Generic types”.
 

1. What is the purpose of Generic types in Java?

A It makes the code faster

B It makes the code more optimized and more readable

C It adds stability to your code by making your bugs detectable at runtime

D It adds stability to your code by making your bugs detectable at compile time

D
Code that uses Generic types has many advantages over non-generic code: Stronger type checks at compile time. A Java compiler applies strong type checking to generic code and issues errors if the code violates type-level security. It’s easier to fix compilation errors instead of fixing runtime errors, which can be hard to find.

 

 

2. Which of these type of parameters is used for a generic class to return and accept any type of object?

A V

B T

C N

D K

B

By convention, generic type parameter names are single upper case letters. For good reason: without this convention, it would be difficult to tell the difference between a type variable, class or interface name.

The most commonly used generic type parameter names are:

  • E – is supposed to be an element (widely used by Java Collections Framework)
  • K – is supposed to be a key (in a Map < K, V >)
  • N – is supposed to be a number
  • T – is supposed to be a type
  • V – is supposed to be a value (as a return value or a mapped value)
  • S, U, V etc. – 2nd, 3rd, 4th types

 

 

3. Which of these type parameters is used for a generic class to return and accept a number?

A V

B T

C N

D K

C

 

[st_adsense]  

4. Which of the following types of reference cannot be generic?

A Inner classes

B Interface

C Inner class anonymous

D All the answers are true

C
This is not possible. Since an anonymous class is supposed to be used only once, what is the point of being generic that you can never use or inherit. You cannot instantiate an anonymous class more than once.

 

 

5. Which of the following statements is incorrect about the use of generic and parameterized types in Java?

A When designing your own class of collections, generic and parameterized types allow you to achieve type security with a single class definition rather than defining multiple classes

B Generic and parameterized types removes the need for top-down conversions when using Java collections

C Generic type in Java ensures type safety by transferring type checking responsibilities to the compiler

D All the answers are true

A

 

 

6. Which of the following methods is correct for defining a generic class?

A class MyClass2 <T1, T2, ....., Tn> { /* … */ }

B class MyClass4 {T1, T2, …, Tn} { /* … */ }

C class MyClass3 [T1, T2, …, Tn] { /* … */ }

D class MyClass1(T1, T2, …, Tn) { /* … */ }

A

 

 

7. What is the output of this program?
import java.util.*;

class GenericStack <E>
{
      Stack <E> stack = new Stack <E>();
      
      public E pop()
      {
          E obj = stack.pop();
          return obj;
      }
      public void push(E obj) 
      {
         stack.push(obj);
      }
}
public class Test
{
     public static void main(String args[])
     {
         GenericStack <String> gs = new GenericStack <String>();
         gs.push("Hello");
         System.out.println(gs.pop());
     }
}

A Hello

B Compilation error

C obj478962

D 44

A
Output:

	
$ javac Test.javac
$ java Test
Hello

 

[st_adsense]  

8. What is the output of this program?
import java.util.*;

class GenericStack <E> 
{
     Stack <E> stack = new Stack <E>();
     public E pop()
     {
          E obj = stack.pop();
          return obj;
     }
     public void push(E obj)
     {
         stack.push(obj);
     }
}

public class Test
{
     public static void main(String args[])
     {
         GenericStack <Integer> gs = new GenericStack <Integer>();
         gs.push(44);
         System.out.println(gs.pop());
     }
}

A Compilation error

B Runtime error

C obj478962

D 44

D
Output:

	
$ javac Test.javac
$ java Test
44

 

 

9. What is the output of this program?
import java.util.*;


class GenericStack <E>
{
    Stack <E> stack = new Stack <E>();
    public void push(E obj)
    {
         stack.push(obj);
    }
    public E pop()
    {
         E obj = stack.pop();
         return obj;
    }
}

public class Test
{
    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(44);
         System.out.println(gs.pop());
     }
}

A Hello 44

B Compilation error

C Hello

D 44

B
The gs object is defined to contain a parameter of type string but we are sending a parameter of type integer, which results in a compilation error. The output is as follows:

$ javac Test.javac
$ java Test
error: incompatible types: int cannot be converted to String

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More [st_adsense]

Leave a Reply

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