Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the output of the following program and explain your results. public class

ID: 3575715 • Letter: W

Question


Write the output of the following program and explain your results. public class Test {private static int sum(int x, int y) {x = x + y; System.out.println("Not short circuit evaluation"); return(x);}} public static void main(String [] args) {int x = 7, y = 4; if(x 10) System.out.println("case 1"); else System.out.println("case 2");}} Write the output of the following program and explain your results. public class Test {static int sum= 15; private static int func(int i, int j) {sum++; return(j - i);} public static void main(String [] args) {System.out.printf("sum=%d func = %d ", sum, func(1, 2)}}

Explanation / Answer

First undestand static function

1)Instance method requires an object of its class to be created before it can be called while static method doesn't require object creation.

2)If you wish to call static method of another class then you have to write class name while calling static method.

3)Staic method cannot use instance variables.

4) static function ae automtically called once the progam runs..

Ans7. As progrram runs ,static function is called and it prints

Not short circuit evaluation

And in main function If condition is not satisfies so goes in else and prints

case2

So overall ouput will be

Output:

Not short circuit evaluation

case2

Ans8.

output:

sum=15 func=17

Func will be called once as it is static. So vaalue of sum will be increment by 1.
As image is missing from end .Iam taking last line as below
System.out.printf("sum=%d func =%d ",sum,func(1,2) +sum);

As per above line it will print sum =15 func =17 ,as explained above this (func(1,2) +sum) will return 17 (1+16).