OCAJP – Practice Questions for Static Keyword

OCAJP and OCPJP Certifications

Table of Contents

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.


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?

Related Posts

  • Top 10 Oracle certifications
    22 March

    10 Best Oracle Certification Courses, Training, Classes & Tutorials Online

    Table of Contents6 Categories Of Oracle CertificationsOracle Certified Junior Associate (OJA)Oracle Certified Associate (OCA)Oracle Certified Professional (OCP)Oracle Certified Master (OCM)Oracle Certified Expert (OCE)Oracle Certified Specialist (OCS)Top 10 Oracle CertificationsOracle Java CertificationOracle Database 12c SQL Certified AssociateA Complete Oracle SQL CertificationOracle Autonomous Database Cloud Certified SpecialistOracle Database: Administration WorkshopOracle PL/SQL Is My GameOracle Cloud Infrastructure Foundations […]

  • 14 October

    Top 3 OCPJP 8 books for 1Z0-808 and 1Z0-809 Exam

    Table of ContentsIntroductionJava Programmer II Study Guide: Exam 1Z0-809Author(s): Esteban HerreraOCP: Oracle Certified Professional Java SE 8 Programmer II Study GuideAuthor(s): Jeanne Boyarsky and Scott SelikoffOracle Certified Professional Java SE 8 Programmer Exam 1Z0-809: A Comprehensive OCPJP 8 Certification GuideAuthor(s): SG Ganesh, Hari Kiran Kumar, Tushar SharmaWas this post helpful? In this article, we will […]

  • 15 September

    How to become oracle certified Java programmer

    Table of ContentsIntroductionWhat is OCPJP?Levels and Pre-RequisitesJava SE 8 Programmer Level 1Java SE 8 Programmer Level 2Syllabus for OCPJP 8Java Class DesignAdvanced Java Class DesignGenerics and CollectionsLambda Interfaces ( built in )Stream APIAssertions and ExceptionsDate/Time APII/O FundamentalsFile I/OConcurrencyJDBCLocalizationPreparing for the ExaminationLearning about the examination paths and processesFamiliarizing with the latest concepts added into Java, with […]

  • 18 August

    How to prepare for OCMJEA Java architect certification exam

    Table of ContentsWhat is the format of OCMJEA exam?What is the prerequisite for preparing OCMJEA exam?What is the syllabus of OCMJEA?How to prepare for multiple choice question level for OCMJEA exam correctly?OCM Java EE 6 Enter Architect Guide by Paul R.Allen and Joseph J.BambaraOracle Certified Master, Java EE Enterprise Architect Practical Guide by Amritendu DeHow […]

  • 14 August

    Top 4 OCPJP 7 books for 1Z0-804 and 1Z0-805 Exam

    Table of ContentsCertificationsOCPJP 7 BooksOCJP Java SE Programmer by Kathy Sierra and Bert BatesA Comprehensive OCPJP 7 Certification Guide by S G Ganesh and Tushar SharmaJava Practice Questions: Oracle Certified Professional by Esteban HerraraOCP Java SE 7 Programmer II Certification Guide by Mala GuptaOnline sourcesOther BooksHead First Java by Kathy Sierra and Bert BatesEffective Java […]

  • 11 December

    How to pass OCPJP java certification with the help of Whizlabs

    Table of ContentsWhat is OCPJP 8?Why Whizlabs OCPJP 8?Some Additional Information for OCPJP PreparationWas this post helpful? I have already shared about Whizlabs OCAJP 8 exam simulator in my previous post. I have looked further to and found it useful too, hence sharing it. OCPJP is tougher exam than OCAJP hence you will need more practice to […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.