Advanced quizzes – Java static members

The following Java exercise(s) are designed for advanced level developers. If the level is too hard, then I recommend to select an easier one or you might consider reading my article about this topic, which offers a theoretical explanation including more exercises. Read More: Java Static Methods And Variables

static-members-red

Quiz 1: Static methods and variables in Java

What appears in the standard output when this program is invoked?

public class MyClass {

	static int x = 5;
	int y;

	MyClass() {
		x ++ ;
		y += 2;
	}
	MyClass(int i, int i2) {
		this();
		y ++ ;
		methodA(i,i2);
	}
	static void methodA(int i, int i2){
		x = x + i + i2;
		System.out.print("-x" + x);
	}
	int methodB(int i){
		y = y + i;
		System.out.print("-x" + y);
		return x - y;
	}
	public static void main(String[] args){
		MyClass mc1 = new MyClass();
		MyClass mc2 = new MyClass(2,3);
		MyClass.methodA(3,2);
		mc1.methodB(3);
		System.out.print("-x" + mc2.methodB(1) + "-x" + mc1.y);
	}
}

Select the correct answer.







Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

1 Comment

Add a Comment
  1. This is what we need – an insight to make evoneyre think

Leave a Reply

Your email address will not be published. Required fields are marked *