Topics Index

Methods Output

If you need explanation Read this topic

If you need Answer Take test on this topic

Methods Output
What will be the output of the following program.
class MethodsTest
    public static void main(String s[])
        int a = 34;
        int b = 56;
        int c = sum(a, b);
        System.out.println("c = " + c);
    public static int sum(int a, int b)
        System.out.println("a = " + a + " b = " + b);
        return a + b;

A. a = 34 b = 56
c = 90
B. a = 56 b = 34
c = 90
C. c = 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.


Posted by Muhammad Shoaib Murtaza    2015-02-24 13:41:43

here answer is A

here we are creating 3 integer variable i.e..

int a=34;
int b=56;
int c=sum(a,b);

public static void main(String s[])  //main method

public static int sum(int a,int b)     //sum method accepts two arguments and return integer type value

here two methods are used in the programe both are static and here in the method we are calling sum(a,b) method but using static keyword static method can call another static method only ..

in the third statement the control goes to the sum(a,b) method by passing two arguments at the calling part in this method the a and b values are printed
i.e...   //                   a=34 b=56

and in the return statement the addition of both values are sent to the calling part i.e..90 and asssigned to variable c
and c value is printed..c=90

Posted by Vicky Maduri    2015-02-24 13:55:36

Ans is A

In above program the concept of function is used.

--Here,two integer variables are declared with name a and b.

After that function sum () is called by passing these parameters I.e. a and b.

---In method sum() value of a and b are printed.
After that a+b I.e. 90 is returned to variable c.

---In main method(), c holds the value 90 and it is printed.

a=34 b=56

Posted by Mânïshå Mùlchåndânï    2015-02-24 14:33:08

This dose is now closed and the winners are Vicky Maduri,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Arun Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-02-25 02:12:52

© meritcampus 2019

All Rights Reserved.

Open In App