Menu
Topics Index
...
`

A Generic Class With Two Type Parameters


If you need explanation Read this topic

If you need Answer Take test on this topic

A Generic Class With Two Type Parameters
What will be the output of the following program?
public class IsIn {
    static <T, V extends T> boolean isIn(T x, V[] y)
    {
        for (int i = 0; i < y.length; i++) {
            if(x.equals(y[i]))
                 return true;
        }
        return false;
    }
    public static void main(String args[])
    {
        Integer integerNumber[] = {1, 2, 5, 7};
        if(isIn(5, integerNumber))
        {
            System.out.println("5 is present in the given integer array");
        }
        else
        {
            System.out.println("5 is not present in the given integer array");
        }
        String str[] = {"M", "ER", "IT"};
        if(isIn("IT", str))
        {
            System.out.println("IT is present in the given string array");
        }
        else
        {
            System.out.println("IT is not present in the given string array");
        }

    }
}

A. 5 is not present in the given integer array
IT is not present in the given string array
B. 5 is present in the given integer array
IT is present in the given string array
C. Compilation Error
D. Runtime Error
Topic: A Generic Class With Two Type Parameters In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

b.
here main ve 2 if else conditions.
1st one check 5 with integer array.5 is present in tht array so isin() returns true.
2nd string "IT" present in string array.
so in the both the case isIn() returns true.
so if part get executed

Posted by Maheshwari Natarajan    2014-11-04 12:35:30


Ans is B...here we use the concept if generic methods with two arguments and here 2nd argment must be child of 1st argument...coming to main here we create Array of Integer objects and 1st we are passing the two argument (primitive and array of integers) ...and checking whether the element is present in the array or not ..if it is present than we are returning the true...in 1st if condition 5 is present so that it wil true and it wil print 5 is present
next we are passing array of string objects and String ...here IT is present in the given array so that here also it  wil true...than it is present wil get printed....

Posted by Uday Kumar    2014-11-04 12:50:28


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-05 01:35:17


© meritcampus 2019

All Rights Reserved.

Open In App