Computer Applications
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) ;
}
}
Java
User Defined Methods
7 Likes
Answer
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
Working
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.
Answered By
6 Likes
Related Questions
How does the compiler interpret more than one definitions having same name ? What steps does it follow to distinguish these ?
Discuss how the best match is found when a call to an overloaded method is encountered. Give example(s) to support your answer.
Design a class to overload a function area( ) as follows:
- double area (double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula:
area = √(s(s-a)(s-b)(s-c))
where s = (a+b+c) / 2 - double area (int a, int b, int height) with three integer arguments, returns the area of a trapezium using the formula:
area = (1/2)height(a + b) - double area (double diagonal1, double diagonal2) with two double arguments, returns the area of a rhombus using the formula:
area = 1/2(diagonal1 x diagonal2)
- double area (double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula:
What is the output of the following code ?
void func(String s) { String s = s1 + "xyz" ; System.out.println("s1 =" + s1) ; System.out.println("s =" + s) ; }