Question 16 Topic: Java Methods,Static field

Oracle Certified Foundations Associate Java Certification Questions and Discussion

Moderator: admin

Post Reply
Maria Teresa Lorenzo
Posts: 1
Joined: Fri Nov 18, 2022 2:45 pm

Question 16 Topic: Java Methods,Static field

Post by Maria Teresa Lorenzo »

Code: Select all

public class Test
    static int a;
    int b;

    public void incr(){
       int c = a++;
       System.out.println(a+" "+b+" "+c);
   public static void main(String args[])
      Test test = new Test();
      test = new Test();

What will be the output?

Select 1 best option

1. Compilation failure
2. 1 1 1
2 1 2
3. 1 1 1
3 1 3
4. 1 2 1
2 3 3
5. 1 2 1
3 3 3
Correct option is: 3
You need to remember the following points to answer this question:

1. static fields belong to the class and not to the instance of a class. Therefore, there is only 1 copy of the field 'a' irrespective of the number of objects of class Test that are created.

2. Every instance of a class gets its own copy of the instance fields. Therefore, every Test instance gets its own copy of the field 'b'.

3. The postfix increment operation increments the value of the variable after the expression is evaluated. Therefore, in the statement int c = a++; first, the existing value of a is assigned to c and then a is incremented.

Based on the above, it is easy to see the following:

1. in the first call to incr(), c is set to 0, then a is incremented to 1, then b is incremented to 1, and then c is incremented to 1.
Therefore, it prints 1 1 1.

2. Next, a is incremented to 2 in the main method.

3. Next, the second call to incr() is invoked on a different instance of Test. Therefore, in this new instance, a is 2 (because a is static) and b is 0.
Now, c gets 2, then a is incremented to 3. then b is incremented to 1, then c is incremented to 3.

Therefore, 3 1 3 is printed.

I don't understand what happens in the program from point 3. Can anybody explain me the logic behind point 3 of the question explanation above?

Thank you in advance.

Site Admin
Posts: 9705
Joined: Fri Sep 10, 2010 9:26 pm

Re: Question 16 Topic: Java Methods,Static field

Post by admin »

Any expression containing the assignment operator ( i.e. = ) has two parts - left side of the = and right side of =. The value of the right side is assigned to the variable on the left side. In case of c = a++; the right side is a++. So, if as is 1 (for example), what should be assigned to c? There are two possibilities - assign the existing value of a i.e. 1 to c or increment a and then assign the new value of a i.e. 2 to c. So, in Java (and other languages such as c/c++/c# etc.), if the right side uses postfix increment (i.e. a++), then the current value is assigned and if the right side uses prefix increment (i.e. ++a), then a is first incremented and the new value is assigned. In both the cases, the value of a itself will be 2 after the statement.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest