OCAJP – Practice Questions for Static Keyword

I have started writing about the Java Certifications and how to prepare for the various topics related to OCAJP exams in my blog. In my previous post, I have published how to handle exceptions and few sample mock questions for StringBuilder class.
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 ?

A. 15 17 22 28
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)

A. exam1 = “A”; instead of // CODE SNIPPET 1
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 ?

A. It prints O C A in that order when run
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 ?

A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
G. Compile time error.

5. What is the output ?

A. 9 10 12 22
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?

Was this post helpful?

Leave a Reply

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