Menu
Topics Index
...
`

Methods Output500


If you need explanation Read this topic

If you need Answer Take test on this topic

Methods Output500
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);
        c = 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.

option:D
it gives compile time error
because in method sum()
1.variable c is used without declared and
2.in method sum() return type is int but we not returning any value from that method due this reason it will give error

solution is create a variable c in that method  sum(); and return the c value;
or
declarion of variable c was  created like   staic variable and  
remove return type  (int) and set return type(void) for sum();method

Posted by Goutham Singarapu    2015-02-04 12:29:34


Ans is D as in "sum()" method we haved  used variable called "c" without any declaration ...

Posted by ?????????? ?????    2015-02-04 14:22:09


ans d.compilation error. initially a and b int variables assigned with 34 and 56, variable c assigned with one function call,sum(a,b).here we passed both a,b values.thn coming to the function definition of sum.here a ,b only having access bcz we passed those values.c is not declared to access in this scope or we dnt declared c variable for local access, thn nxt compiler throws    error because this method doesn't return anything, and the return type is not void .here return type is int

Posted by Maheshwari Natarajan    2015-02-04 17:44:27


ANS IS D (Compilation Error)

--THERE ARE  ERRORS IN THIS PROGRAM:

1)VARIABLE 'C' IS NOT DECLARED IN METHOD SUM()
2)VARIABLE 'C' IS NOT RETURNED FROM METHOD SUM()

--Here 'a' and 'b' variables are declared to 34 and 56 respectively.

--sum() method is called with a and b variable and target variable is 'c' which is integer.

--In sum() method,'a' and 'b' are displayed.Then,'a' and 'b' are added and assigned to 'c' varible,but 'c' is not returned and it is not declared inside method sum().

--So,There is Compilation Error.

Posted by Mânïshå Mùlchåndânï    2015-02-04 17:50:37


d

Posted by Shubham Bansal    2015-02-05 04:06:52


This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Maheshwari Natarajan, Mânïshå Mùlchåndânï,  for 'Best Comment' and Goutham Singarapu for the 'Popular Comment'. The 'lucky liker' is Asad Ahmed. 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    2015-02-05 07:27:48


© meritcampus 2019

All Rights Reserved.

Open In App