Topics Index

Method Parameters

If you need explanation Read this topic

If you need Answer Take test on this topic

Method Parameters
What will be the output of the following program?
class Equation1
    public static void main(String s[])
        int x = 6;
        int y = 3;
        int z = 12;

        int w = method1(x, y, z);
        System.out.println("w = " + w);

    public static int method1(int y, int z, int x)
        return (x * z) / y;

A. w = 24
B. Compilation Error
C. w = 36
D. w = 6 
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 is:D
here in main method 3 variables created x=6,y=3,z=12
and a function call method1(x,y,z) (here x,y,z are actual parameters)from this  excution goes  to the function defination method1(int y,int z,int x)(y,z,x are formal parameters)
here   for formal parameters values assigned are(y=x(means y=6),z=y(means z=3),x=z(means z=12)
after that  function return a value
return (x*z)/y;
its mean its x*z evaluate 12*3=36
and after (36)/y; 36/6=6;
then result  for this expression is (x*z)/y=(12*3)/6=6;
that result is return 6;
answer is 6.
option is D

Posted by Goutham Singarapu    2015-02-09 04:05:45

ans is D execution starts with main() here x y z are initialized as integer type of values 6,3,12 respectively  value of w is returned by the function call method1(x,y,z) here x,y,z are actual parameters it goes to the called method there y z x are formal parameters  where value of y is equal to actual parameter x in the same way z=y and x=z and here y=6 x=12 z=3 method1 returns 6 it prints  w=6

Posted by Ramya Reddy Addulla    2015-02-09 12:18:53


Posted by Shubham Bansal    2015-02-09 14:59:04

Ans is we the the Concept of the Call by value where modification made to the formal arg will not effect on to the actual arg.
Execution: here we have the 3 local variables named with the x=6,y=3,z=12 and we are calling the method1() by passing the these values method1(6,3,12)
in the method we are receiving these values using the another 3 local variables named with y=6,z=3,x=12..than we have an expression so it will 1st evaluate the (x*z) Bcz () high precedence so it will give 36 than we are dividing with y which is 6 it will return the 6 so w is assigned with the 6
after returning the local variables will dead

method1() is static so we can invoke with creating the instance also..

Posted by Uday Kumar    2015-02-10 03:40:34

This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Ramya Reddy Addulla, Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is ?????????? ?????. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-02-10 04:00:58

© meritcampus 2019

All Rights Reserved.

Open In App