Topics Index

Methods Output506

If you need explanation Read this topic

If you need Answer Take test on this topic

Methods Output506
What will be the output of the following program.
class MethodsTest
    public static void main(String s[])
        float m = 34;
        float n = 56;
        float o = sum(m, n);
        System.out.println("o = " + o);
    public static float sum(float m, float n)
        System.out.println("m = " + m + " n = " + n);
        return m + n;

A. m = 34 n = 56
o = 90
B. m = 34.0 n = 56.0
o = 90.0
C. m = 34.0 n = 56.0
o = 0.0
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.

Ans is B.
By calling the sum(float,float) by passing m and n values, it will print them and returns the sum of their value to 'o'. And it prints the value of 'o'.

So the output will be :
m = 34.0 n = 56.0
o = 90.0

Posted by Gopinath Manchikanti    2014-11-17 08:41:27

Ans is we have two type casting in java Explicit and Implicit conversion in java
explicit conversion when we need to convert Higher data type to lower should perform by the user..
implicit converstion this will automatically done when lower data type is convert to higher data type..

double d=12;-->implicit conversion occur
int a=(int)12.4;
coming to the program here we create 2 local varaibles varibles and assinged with the values 34 and it implicit conversion occur and values will become 34.0 and 56.0
than we are calling the sum() which is static so that we can call these methods using the object also and this method is returng float value and in main wea are receiving with float varible "o"

in sum() class we are printing the m and n values so that it wil be 34.0 and 56.0
than we are returnging the sum of these 2 numbers than it will be90.0 than we are printing these value in the main method

Posted by Uday Kumar    2014-11-17 08:46:14

answer is B because the variables are of float datatype...

Posted by Prateek Singh    2014-11-17 09:01:03

Answer is B....
In the main method,we are declaring two variables m and n with 34 and 56...
since values of m and n are not sufficed with f,they will be considered as int by the compiler by default and type conversion from int to to float happens automatically.
sum(m,n)  method is called in the next step where values are summed in the method and returned back to the main which then assigns the result to variable 'o'.
we are printing the result 'o' which is of float type and hence it prints 90.0 (34.0+56.0) in the main

Posted by Shashanka Mogaliraju    2014-11-17 09:04:40


Posted by Praveen Pandey    2014-11-17 09:21:13


Posted by Dereje Goshu    2014-11-17 10:38:31

ans b.implicit type casting is used in this program.Typecasting-Assigning a value of one type to a variable of another this program int values are assigned to float variables.                                                                                                    but here compilr dont throw any error,bcz range of flloat is wider than int range so data loss occur.                                                                                             but if u try to assign float value to int variable then compilr throws error.                          Implicit casting is only available when you're guaranteed that there'll be no loss of data as a result of the conversion.sum() function is called and assigned to o.                                                                                                            both m,n passed as as arguments.                                                                                sum() func sop prints m and n values.and returns addition of m and n 34.0+56.0=90.0,                                  variable o assigned with 90.0. thn main sop prints o value

Posted by Maheshwari Natarajan    2014-11-17 18:02:56

This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Uday Kumar, Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-11-18 01:22:39

© meritcampus 2019

All Rights Reserved.

Open In App