import java.awt.*;
import javax.swing.*;
public class S17Exam2Q5 extends JApplet {
int x = 5, y = 100, z = 15;
public void init( {
int x = z;
char z = 'X';
x = x - 3;
System.out.println("1: x, y, and z are "+x+" "+y+" "+z);
if (x <= y) {
x = 50;
y = y - 5;
int y = x + 3;
z = 'Q';
System.out.println("2: x, y, and z are "+x+" "+y+" "+z);
}
System.out.println("3: x, y, and z are "+x+" "+y+" "+z);
procExt(int z);
System.out.println("4: x, y, and z are "+x+" "+y+" "+z);
}
public void procExt(x) {
z = 10;
int x = 20;
y = y + 1;
System.out.println("5: x, y, and z are "+x+" "+y+" "+z);
}
}
According to the exam answer key, this should be the answer:
1: x, y, and z are 12, 100, X
2: x, y, and z are 50, 53, Q
3: x, y, and z are 50, 95, Q
5: x, y, and z are 20, 96, 10
4: x, y, and z are 50, 96, Q
The answer makes sense only for 1 and 2, it's at 3 that I get confused. Shouldn't 3 and 2 be the same since they print one after the other with nothing happening inbetween? And shouldn't it be the same for 5 and then 4?I was also a bit confused as to what was happening with the procExt subroutine The answer that I wrote is below (asterisks denote an incorrect answer)
1: x, y, and z are 12, 100, X
2: x, y, and z are 50, 53, Q
3: x, y, and z are 50, *53*, Q
5: x, y, and z are 20, *54*, 10
4: x, y, and z are *20*, *54*, *10*
3: int y (last time used) was 53. This didn't overwrite y, which is still set to 95. println used the 'int' value, but the next time y was called defaults to the (non-int numerical value) 95, which the program still saved for y.
for #4 and #5: Are you meaning to list the problem as 1,2,3,4,5 ? You have 5 & 4 backward both times, so I will refer to them in the order I see it (1,2,3,5,4)
It makes the most sense to do #5 Last, even though I listed them otherwise.
5: x is set using an int = 20, so this time reading for x will be 20, y = y + 1 will push y to 96 (and stays there for 4) and z = 10 because it is put there.
4: remember, #3 had x = 50, y = 95, z = Q. Notice proc() runs before 4. Using proc(int z) keeps the 'Q' value from #3 in z once. Because proc() was called, the y + 1 value calculates once to equal 96. And finally z = 10 because the (int z) call kept the contents of z at the value Q only once, the actual value of z is 'int' 20 at least once. This is due to int overloading previous values. The value get's popped after it is used.