Java OCA OCP Practice Question 2395

Question

What is the output of the following code?

class Main { /*  w  ww. j a va  2  s  .  c  om*/
    public static void main(String args[]) { 
        int ejg = 10; 
        anotherMethod(ejg); 
        System.out.println(ejg); 
        someMethod(ejg); 
        System.out.println(ejg); 
    } 
    static void someMethod(int val) { 
        ++val; 
        System.out.println(val); 
    } 
    static void anotherMethod(int val) { 
        val = 20; 
        System.out.println(val); 
    } 
} 
a  20 //from w  w w  . j  a v a2 s . c o  m
   10 
   11 
   11 

b  20 
   20 
   11 
   10 

c  20 
   10 
   11 
   10 

d  Compilation error 


c

Note

When primitive data types are passed to a method, the values of the variables in the calling method remain the same.

This behavior doesn't depend on whether the primitive values are reassigned other values or modified by addition, subtraction, or multiplication-or any other operation.




PreviousNext

Related