ICSE Class 10 Computer Applications
Question 42 of 43
User-Defined Methods — Question 42
Back to all questions 42
Question Question 40
What is the output of the following program?
class AllStatic
{
static int m = 0 ;
static int n = 0 ;
public static void main(String[ ] args)
{
int m = 10;
int x = 20;
{
int n = 30 ;
System.out.println("m + n =" + m + n) ;
check(5) ;
}
x = m + n;
System.out.println("x =" + x) ;
}
public static void check int k ;
{
int m = 5 ;
n = k;
System.out.println("m is " + m) ;
System.out.println("n is " + n) ;
}
}The given code generates an error due to wrong method prototype — public static void check int k ;
The correct syntax of the method will be —
public static void check (int k)
Assuming the method prototype was correct, the following output will be generated:
m + n = 1030
m is 5
n is 5
x = 15
Explanation
Initially, m = 10 and n = 30.
The value 5 is passed by value to the function check(). The function check() declares a local variable m whose value is 5. The value of n is modified to 5. Both the values (m and n) are printed and then the control goes back to the statement following the method call.
The value of x becomes 15 (since m = 10 and n = 5). Inside the main(), the value of local variable m is 10 and n is 5.