Java OCA OCP Practice Question 392

Question

What is the output of the following application?

package dinosaur; 
public class Park { 
        public final static void main(String... arguments) { 
           int myField = 6; 
           long myField2 = 3; 
           if(myField % 3 >= 1) 
              myField2++; //from w w w . jav a 2s  .  c o m
              myField2--; 
           System.out.print(myField2); 
        } 
} 
  • A. 2
  • B. 3
  • C. 4
  • D. The code does not compile.


A.

Note

The first step is to determine whether or not the if-then statement's expression is executed.

The expression 6 % 3 evaluates to 0, since there is no remainder, and since 0 >= 1 is false, the expression myField2++ is not called.

Notice there are no brackets {} in the if-then statement.

Despite the myField2-- line being indented, it is not part of the if-then statement.

Recall that Java does not use indentation to determine the beginning or end of a statement.

myField2-- is always executed, resulting in a value of 2 for myField2 and making Option A the correct answer.




PreviousNext

Related