132k views
4 votes
What is result of executing the following code?

public class Question11 {
private static int x = 1;
public static void main(String[] args) {
int x = 5;
System.out.printf("local x in main is %d%n", x);
useLocalVariable();
useField();
useLocalVariable();
useField();
System.out.printf("%nlocal x in main is %d%n", x);
}
public static void useLocalVariable() {
int x = 25;
System.out.printf(
"%nlocal x on entering method useLocalVariable is %d%n", x);
++x;
System.out.printf(
"local x before exiting method useLocalVariable is %d%n", x);
}
public static void useField() {
System.out.printf(
"%nfield x on entering method useField is %d%n", x);
x *= 10; // modifies class Scope’s field x
System.out.printf(
"field x before exiting method useField is %d%n", x);
}
}

User BiNZGi
by
6.2k points

1 Answer

6 votes

Answer:

Step-by-step explanation:

With the main method int the question calling the useLocalVariable and useField variable various times and then also printing out the variable using the printf method. The output of executing this code would be the following...

local x in main is 5

local x on entering method useLocalVariable is 25

local x before exiting method useLocalVariable is 26

field x on entering method useField is 1

field x before exiting method useField is 10

local x on entering method useLocalVariable is 25

local x before exiting method useLocalVariable is 26

field x on entering method useField is 10

field x before exiting method useField is 100

local x in main is 5

User Twanna
by
5.9k points