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
Article: Java static methods and variables Quizzes: Beginner Intermediate Advanced |
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); } }
Article: Java static methods and variables Quizzes: Beginner Intermediate Advanced |
Suggested Articles
![]() ![]() ![]() |
This is what we need – an insight to make evoneyre think