What is the value of z after the third statement executes below?

What is the value of z after the third statement executes below?

StaticExample a = new StaticExample(5);
StaticExample b = new StaticExample(12);
int z = a.incr( );
a)       5
b)       6
c)       12
d)       13
e)       none, the code is syntactically invalid because a and b are attempting to share an instance data

Use the following class definition:
                                          public class StaticExample
                                          {
                                                        private static int x;
                                                       
                                                        public StaticExample (int y)
                                                        {
                                                                      x = y;
                                                        }

                                                        public int incr( )
                                                        {
                                                                      x++;
                                                                      return x;
                                                        }
                                          }


Answer:  d.

Explanation:  Since instance data x is shared between a and b, it is first initialized to 5, it is then changed to 12 when b is instantiated, and then it is incremented to 13 when a.incr( ) is performed.  So, incr returns the value 13.


Enhancing Classes

Learn More Multiple Choice Question :