Topics Index

Methods Output505

If you need explanation Read this topic

If you need Answer Take test on this topic

Methods Output505
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 void 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 we have the concept of passing parameters and the returning the values...

Functions will improve the readability of the program and we can reuse the code with the functions, functions will take the different type of arguments and functions will return the only one value using the keyword return..

void one()---> this function wont take any arguments and wont return any values...void means it does not return value..this type of functions will used for the displaying purpose...
int one()--> this will return the int we write int as return type if we return float value than system will raise an error..we must return compatible type...

Coming to the program here we have the function sum() which can tak the 2 float arguments and it does not return anything Bcz here we write return type as void...

from main() we are calling the sum() by passing the values of m,n and in the function we are displaying the values next we are returning the sum of these 2 values but here we should not return value Bcz we specify return type as void so compiler will raise an error

Posted by Uday Kumar    2014-12-09 03:42:56

Compilation error bcoz  of void  in other Parameters.

Posted by Fuzail Ahmed    2014-12-09 04:46:48

ans is D,,,

parameter passing is concept calling a function by passing paramaeters of hte dame data type
void is the datatype used when a function returns nothing
it is use when we dont know what the type of the variable is
so it indeed returns null

the purpose of introduction of void in c c++ or java is there is no need
for typrxcasting as we dont know the type of variable
void *v;
v can be referenced to any data type,,,,it's an advantage indeed,,,,

Posted by Sai Veerendra    2014-12-09 05:14:42

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 to raise the recharge.

Posted by Merit Campus    2014-12-10 04:38:36

© meritcampus 2019

All Rights Reserved.

Open In App