Question 16 Topic: Java Methods,Static field

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

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

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++;
       b++;
       c++;
       System.out.println(a+" "+b+" "+c);
    }
   public static void main(String args[])
   {
      Test test = new Test();
      test.incr();
      a++;
      test = new Test();
      test.incr();
   }

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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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.

bladese
Posts: 1
Joined: Fri Dec 16, 2022 10:10 pm
Contact:

Re: Question 16 Topic: Java Methods,Static field

Post by bladese »

Shouldn't that be compilation error? There is no a variable initialization in question and in the source code. Regards

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Question 16 Topic: Java Methods,Static field

Post by admin »

1. Which variable do you think is not initialized in the given code? If you are talking about the static and instance variables a and b, then you need to know that static and instance fields are always automatically initialized to their default values (i.e. 0 or 0.0 for numeric/float types, false for boolean type, and null for reference types) if they are not given a value explicitly.

2. Local variables need to be initialized explicitly but only before these variables are used. If they are not used, then you may leave them uninitialized.
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 22 guests