Refer to the code segment below and answer the questions in the comments labelled Question i – vi. For each answer; provide a brief explanation for your answer.
public class Scope {
private int x = 2;
private int x = 1;
public void start() {
int x = 5;
int x = 2;
//Question i
System.out.printf( "The value of x is %d\n", x );
int x = 2;
//Question ii
System.out.printf( "\nThe value of x is is %d\n", x );
} // end method begin
public void middle() {
int x = 25;
//Question iii
System.out.printf( "\nThe value of x is %d\n", x );
++x;
//Question iv
System.out.printf( "The value of x is %d\n", x );
} // end method middle
public void end(){
//Question v
System.out.printf( "\nThe value of x is %d\n", x );
x *= 10;
//Question vi
System.out.printf( "The value of x is %d\n", x );
} // end method end
} // end class Scope
Question i
It will be printed:"The value of x is 2" // last value of x before the given command System.out.printf
Question ii
It will be printed:"The value of x is 2" // last value of x before the given command System.out.printf
Question iii
It will be printed:"The value of x is 25" // the value of x in the given method
Question iv
It will be printed:"The value of x is 26" // the value of x after increment ++x
Question v
It will be printed:"The value of x is 1" // the method takes the value of x which is defined in class Scope (private x=1)
Question vi
It will be printed:"The value of x is 10" // x=1*10=10
Comments
You're the best website I've every seen.thanks a lot
Leave a comment