Consider the following program, which includes five incomplete println statement
ID: 3629814 • Letter: C
Question
Consider the following program, which includes five incomplete println statements:public class ScopePuzzle {
public static void myMethod(int a, int b) {
int i;
for (i = 0; i < 10; i++) {
int c = 5;
for (int j = 0; j < 3; j++) {
int d = 0;
System.out.println(________); // first println
}
System.out.println(________); // second println
}
System.out.println(________); // third println
}
public static void main(String[] args) {
int x = 0;
System.out.println(________); // fourth println
int y = 1;
myMethod(x, y);
System.out.println(________); // fifth println
}
}
The program includes eight int variables: a,b,c,d, i, j, x and y. Given the rules that we have learned about variable scope:
Which of these variables could be printed by the first println statement?
Which of them could be printed by the second println statement?
Which of them could be printed by the third println statement?
Which of them could be printed by the fourth println statement?
Which of them could be printed by the fifth println statement?
Explanation / Answer
Which of these variables could be printed by the first println statement?
a,b,c,d,i,j
Which of them could be printed by the second println statement?
a,b,c,i
Which of them could be printed by the third println statement?
a,b,i
Which of them could be printed by the fourth println statement?
x
Which of them could be printed by the fifth println statement?
x,y
public class ScopePuzzle {
public static void myMethod(int a, int b) {
int i;
for (i = 0; i < 10; i++) {
int c = 5;
for (int j = 0; j < 3; j++) {
int d = 0;
System.out.println(a+b+c+d+i+j); // first println
}
System.out.println(a+b+c+i); // second println
}
System.out.println(a+b+i); // third println
}
public static void main(String[] args) {
int x = 0;
System.out.println(x); // fourth println
int y = 1;
myMethod(x, y);
System.out.println(x+y); // fifth println
}
}