Table of Contents
In this post I am going to explain write down few more practice questions for OCAJP exam. I would provide 5 sample practice questions for static keyword. The exam objective is:
Apply the static keyword to methods and fields.You will be tested in the exam about difference between static variables and instance variables. If two objects are trying to modify the static and instance variables, What will be the expected output?. The static variables and its use are not just required for the certification preparation; this is one of the important concepts in core java that has to be well understood by the programmers. Before you are trying these sample questions, you can read my article about static keyword.If you are looking for mock exams to practice OCAJP exam, you can try these practice questions for OCAJP 8 exam.
Static Keyword Mock Questions
1. What is the output ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class StaticDemo { int num1 = 6; static int num2 = 10; public static void main(String args[]) { StaticDemo s1 = new StaticDemo(); StaticDemo s2 = new StaticDemo(); s1.num1 = 15; s1.num2 = 17; s2.num1 = 22; s2.num2 = 28; System.out.println(s1.num1 + " " + s1.num2 + " " + s2.num1 + " "+ s2.num2); } } |
B. 15 17 22 17
C. 15 28 22 28
D. 22 17 22 28
E. 22 28 22 28
F. compile time error
2. Which of the following will compile when inserted in the following code? (Choose all that apply)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Demo { final String exam1 = "O"; static String exam2 = "C"; { // CODE SNIPPET 1 } static { // CODE SNIPPET 2 } } |
B. exam 2 = “J”; instead of // CODE SNIPPET 1
C. exam 1 = “P”; instead of // CODE SNIPPET 2
D. exam 2 = “8”; instead of // CODE SNIPPET 2
E. compile time error.
F. Exception at run time.
3. What is the output of the program ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class StaticDemo { static String n1= examName("O");{ n1=examName("A"); } static{ n1=examName("C"); } public static void main(String[] args) { StaticDemo sd = new StaticDemo(); } public static String examName(String s){ System.out.println(s); return s; } } |
B. It prints C A O in that order when run
C. It prints O A C in that order when run
D. It prints C A O in that order when run
E. Compile time error.
F. Exception at run time.
4. Which of the lines will fail to compile ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class StaticDemo { StaticDemo sd = new StaticDemo(); void method1() { method4(); Â // 1 StaticDemo.method2(); // 2 StaticDemo.method3(); // 3 } static void method2() { } void method3() { method1(); //4 method2(); //5 sd.method2(); //6 } static void method4() { } } |
B. 2
C. 3
D. 4
E. 5
F. 6
G. Compile time error.
5. What is the output ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class StaticDemo { int num1 = 3; static int num2 = 5; StaticDemo(int num1, int num2) { if (num2 < 30) { this.num2 = num2; } num1 = num1; } public static void main(String args[]) { StaticDemo s1 = new StaticDemo(9, 10); StaticDemo s2 = new StaticDemo(12, 22); System.out.println(s1.num1 + " " + s1.num2 + " " + s2.num1 + " "+ s2.num2); } } |
B. 9 22 12 22
C. 9 10 12 10
D. 3 22 3 22
E. Compile time error.
Answers
1. Correct option : C
You have created two objects and s1,s2 are the references. s1.num1=15 assigns value 15 to num1 variable, It doesn’t updates the value in s2 reference. s1.num2=17 assigns value 17 to num2 variable , it updates the value in s2 reference also because num2 is a static variable. s2.num1=22 assigns value 22 to num1 variable, It doesn’t updates the value in s1 reference. s2.num2=28 assigns value 28 to num2 variable , it updates the value in s1 reference also because num2 is a static variable.
2. Correct options : B,D
Options A,C are incorrect because exam1 is a final variable . You can’t re assign value to a final variable. B is correct because instance block can access static variable and can assign value to it. D is correct because static block can access static variable and can assign value to it.
3. Correct option : A
To answer this you need to remember Java program execution order. First static variable s1 will be declared and calls examName method which prints O and returns O to it . Next static block will be executed it also calls examName method which prints C and returns C to it . Object is creating . There is one instance block . It will be executed before object creation. It calls examName method which prints A and returns A to it .
4. Correct Option : C
C is correct option because you are not allowed to use class name to call instance method. Line 1, 5 are correct , you can directly call static method from instance method without class name or [reference variable](https://java2blog.com/reference-variable-java/ “reference variable”) of same type.
5. Correct Option : D
num1 = num1; It is assigning value to the same constructor parameter. It doesn’t affect the num1 instance variable in s1, s2 references . So num1 value is 3 in both the references. this.num2 = num2; It changes the static variable num2 in both s1, s2 references. References
If you are preparing for the OCAJP certification exam, the following references would be much useful.
• OCAJP 8 Exam Curriculum
• OCAJP Forums
• How to prepare for OCAJP Exam?
• What are the good books available for OCAJP 8 exam?