Menu
Topics Index
...
`

Methods Output513


If you need explanation Read this topic

If you need Answer Take test on this topic

Methods Output513
What will be the output of the following program.
class MethodsTest
{
    public static void main(String s[])
    {
        float m = 34;
        float n = 56;
        double x = 21.0;
        double y = 98.0;
        boolean b = m < n;
        int o = (int) operate(m, n, x, y, b);
        System.out.println("o = " + o);
    }
    
    public static double operate(double d1, double d2, double d3, double d4, boolean d5)
    {
        System.out.println("d1 = " + d1 + " d2 = " + d2);
        System.out.println("d3 = " + d3 + " d4 = " + d4);
        return d5 ? d1 + d2 : d3 - d4;
    }
}

A. d1 = 34.0 d2 = 56.0
d3 = 21.0 d4 = 98.0
o = -77
B. d1 = 21.0 d2 = 98.0
d3 = 34.0 d4 = 56.0
o = 90
C. d1 = 34.0 d2 = 56.0
d3 = 21.0 d4 = 98.0
o = 90
D. Compilation Error
Topic: Java Methods - Parameter Passing And Scope

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.

c because d5 value is true so it executes d1+d2

Posted by Aparna Kanjarla    2014-11-13 12:15:51


ans c bcz
in 5th line b=34<56=>true
operate(34,56,21,98,true) is called
then d1=34.0 d2=56.0 d3=21.0 d4=98.0 gets printed
then d5=true
so 1st condition is satisfid d1+d2 is returned=90.0
then value is type casted to integer
so o=90 which is printed

Posted by Pooja Singh    2014-11-13 12:20:18


Ans is C..here we have implicit type conversion involved...when we assign the lowest data type to highest type than system wil automatically convert it into the requred type....Ex float a=4...here 4 is int which wil automatically converterd into float...

in the main we are creating two float variables and assigned with 34 and 56 here implicit conversion takes place...

next we create 2 double varibles and assinged with the 21.0 and 98.0...every fraction value is by default double so no need to Convert...

here boolean b assigned with the true Bcz M<N(34.0<56.0)

here we are passing the above 5 values to the function operate...here we are printing the d1 and d1 which is 34.0 and 56.0...next we are printing d3 and d4 means 21.0 and 98.0

while returning here condition is true Bcz b is true so thata qst expression will execute than it wil be 90.0...

in main classs while receving we are type casting these 90.0 into int so that it wil 90...this is called Explicit convesrion.....

Posted by Uday Kumar    2014-11-13 12:43:26


c

Posted by Shubham Bansal    2014-11-13 14:14:42


This dose is now closed and the winners are Pooja Singh,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar 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-14 03:09:07


By Default Every fractional part is treated as double so that we need to tell the compiler that treat it as float Rohit Adlakha

Posted by Uday Kumar    2014-11-14 13:15:56


© meritcampus 2019

All Rights Reserved.

Open In App