Menu
Topics Index
...
`

Bounded Types


If you need explanation Read this topic

If you need Answer Take test on this topic

Bounded Types
What will be the output?
public class SameAverage {
    public static void main(String args[])
    {
        Integer[] integerNumbers = {1, 2, 3, 4};
        Double[] doubleNumbers = {1.0, 2.0, 3.0, 4.0};
        Average<Integer> integerObject = new Average<Integer>(integerNumbers);
        Average<Double> doubleObject = new Average<Double>(doubleNumbers);
        System.out.println(integerObject.sameAverage(doubleObject));
    }
}
class Average<T extends Number>
{
    T[] numbers;
    Average(T[] numbers)
    {
        this.numbers = numbers;
    }
    double getAverage()
    {
        double sum = 0.0;
        for (int i = 0; i < numbers.length; i++) {
            sum += numbers[i].doubleValue();
        }
        return sum / numbers.length;
    }
    boolean sameAverage(Average<T> obj)
    {
        if(getAverage() == obj.getAverage())
        {
            return true;
        }
        return false;
    }
}

A. true
B. false
C. Compilation Error 
D. Runtime Error
Topic: Java Bounded Type - Bounded Type 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.

Ana is A,, getAverage methode return same(double) type value in both case,,,

Posted by Raju VarshnEy    2014-11-05 08:45:59


a

Posted by Vikas Tiyagi    2014-11-05 12:14:14


ans is C aserror: method sameAverage in class Average<T> cannot be applied to given types     System.out.println(integerObject.sameAverage(doubleObject));  required is Average<integer> but found is Average<Double>the  actual argument Average<Double> cannot be converted to Average<Integer> by method invocation conversion  where T is a type-variable    T extends Number declared in class Average

Posted by Ramya Reddy Addulla    2014-11-05 12:33:53


Ans is C...herer we have Avg class with Boundetype...here we can create only Objects with child classes of Number...here we create Array of Integer Objects and array Of Double Objects...than we create an instance with Integer Object and Double Object...by passing the Integer and Double Array...and next we are calling the samAverage method by passing the doubleObject calling with Integer Object...here compiler wil raise an error..here in method we are receving with T means it is Integer Bcz callaing with Integer Object..so that Double cant convert into int...if we use "WildCard" (?) than it will Work

Posted by Uday Kumar    2014-11-05 16:19:17


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:44


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:44


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:44


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:45


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:47


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:47


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:47


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:48


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:48


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:49


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:49


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-06 02:55:49


© meritcampus 2019

All Rights Reserved.

Open In App