The following Java exercise(s) are designed for intermediate level programmers. 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: A static method and variable in Java
What appears in the standard output when this program is invoked?
public class MyClass { static int x = 3; int y; MyClass() { x ++ ; y ++ ; } static void methodA(int i, int i2){ x = x + i + i2; System.out.print("-x" + x); } int methodB(int i){ this.y = y + i; return x - y; } public static void main(String[] args){ MyClass mc1 = new MyClass(); MyClass.methodA(3,2); MyClass.methodA(1,3); MyClass mc2 = new MyClass(); MyClass.methodA(4,2); System.out.print("-x" + mc2.methodB(3) + "-x" + mc1.y); } }
Article: Java static methods and variables Quizzes: Beginner Intermediate Advanced |
Suggested Articles
![]() ![]() ![]() |