OCA Java SE 8 Method - OCA Mock Question Method 14








Question

What is the result of the following program?

     1: public class Main { 
     2:   public static long square(int x) { 
     3:     long y = x * (long) x; 
     4:     x = -1; 
     5:     return y; 
     6:   } 
     7:   public static void main(String[] args) { 
     8:     int value = 9; 
     9:     long result = square(value); 
     10:     System.out.println(value); 
     11:   } 
     12:} 
  1. -1
  2. 2
  3. 4
  4. Compiler error on line 9.
  5. Compiler error on a different line.




Answer



B.

Note

Java is pass-by-value and the variable value never gets reassigned.

      
public class Main {
  public static long square(int x) {
    long y = x * (long) x;
    x = -1;
    return y;
  }

  public static void main(String[] args) {
    int value = 2;
    long result = square(value);
    System.out.println(value);
  }
}