Java Se 8 Programmer OCA Exam Quiz!

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Alexpoiry
A
Alexpoiry
Community Contributor
Quizzes Created: 1 | Total Attempts: 8,556
Questions: 21 | Attempts: 8,564

SettingsSettingsSettings
Java Se 8 Programmer OCA Exam Quiz! - Quiz

The OCAJP 8 exam is associate-level programmer certification offered by Oracle. Here is a 'Java SE 8 Programmer OCA Exam quiz' that will help you prepare for this exam. So, if you're planning to appear in the exam, then try this quiz. This exam is easier than OCPJP 8 (next level), and it validates Java language fundamentals. It is one of the most sought-after certifications for aspiring software developers. The quiz below is well designed and will help you revise your concepts. All the best!


Questions and Answers
  • 1. 

    Which of the following are valid Java identifiers? 

    • A.

      A$B

    • B.

      _helloWorld

    • C.

      . true

    • D.

      . java.lang

    • E.

      Public

    • F.

      1980_s

    Correct Answer(s)
    A. A$B
    B. _helloWorld
    E. Public
    Explanation
    A, B, E. Option A is valid because you can use the dollar sign in identifiers. Option B is valid because you can use an underscore in identifiers. Option C is not a valid identifier because true is a Java reserved word. Option D is not valid because the dot (.) is not allowed in identifiers. Option E is valid because Java is case sensitive, so Public is not a reserved word and therefore a valid identifier. Option F is not valid because the first character is not a letter, $, or _.

    Rate this question:

  • 2. 

    Determine the output of the following program. 1. public class WaterBottle { 2. private String brand; 3. private boolean empty; 4. public static void main(String[] args) { 5. WaterBottle wb = new WaterBottle(); 6. System.out.print("Empty = " + wb.empty); 7. System.out.print(", Brand = " + wb.brand); 8. } }

    • A.

      Line 6 generates a compiler error.

    • B.

      Line 7 generates a compiler error.

    • C.

      There is no output.

    • D.

      Empty = false, Brand = null

    • E.

      Empty = false, Brand =

    Correct Answer
    D. Empty = false, Brand = null
    Explanation
    D. Boolean fields initialize to false and references initialize to null, so empty is false and brand is null. Brand = null is output.

    Rate this question:

  • 3. 

    Which of the following are true? 4: short numPets = 5; 5: int numGrains = 5.6; 6: String name = "Scruffy"; 7: numPets.length(); 8: numGrains.length(); 9: name.length();

    • A.

      Line 4 generates a compiler error.

    • B.

      Line 5 generates a compiler error.

    • C.

      Line 6 generates a compiler error.

    • D.

      Line 7 generates a compiler error.

    • E.

      Line 8 generates a compiler error.

    • F.

      Line 9 generates a compiler error.

    • G.

      The code compiles as is.

    Correct Answer(s)
    B. Line 5 generates a compiler error.
    D. Line 7 generates a compiler error.
    E. Line 8 generates a compiler error.
    Explanation
    Line 5 generates a compiler error because the variable "numGrains" is declared as an int, which can only store whole numbers, but it is assigned a decimal value of 5.6.

    Line 7 generates a compiler error because the variable "numPets" is declared as a short, which is a primitive data type and does not have any methods. Therefore, calling the "length()" method on it will result in a compiler error.

    Line 8 generates a compiler error because the variable "numGrains" is declared as an int, which is a primitive data type and does not have any methods. Therefore, calling the "length()" method on it will result in a compiler error.

    Rate this question:

  • 4. 

    Given the following class, which of the following is true?  1: public class Snake { 2: 3: public void shed(boolean time) { 4: 5: if (time) { 6: 7: } 8: System.out.println(result); 9: 10: }
    1. 11: }

    • A.

      If String result = "done"; is inserted on line 2, the code will compile.

    • B.

      If String result = "done"; is inserted on line 4, the code will compile.

    • C.

      If String result = "done"; is inserted on line 6, the code will compile.

    • D.

      If String result = "done"; is inserted on line 9, the code will compile.

    • E.

      None of the above changes will make the code compile.

    Correct Answer(s)
    A. If String result = "done"; is inserted on line 2, the code will compile.
    B. If String result = "done"; is inserted on line 4, the code will compile.
    Explanation
    If String result = "done"; is inserted on line 2, the code will compile because the variable "result" is declared and initialized before it is used in the println statement on line 8. Similarly, if String result = "done"; is inserted on line 4, the code will compile because the variable "result" is declared and initialized before it is used in the if statement on line 5.

    Rate this question:

  • 5. 

    Given the following classes, which of the following can independently replace INSERT IMPORTS HERE to make the code compile?
    1. package aquarium;
    2. public class Tank { }
    3. package aquarium.jellies;
    4. public class Jelly { }
    5. package visitor;
    6. INSERT IMPORTS HERE
    7. public class AquariumVisitor {
    8. public void admire(Jelly jelly) { } }

    • A.

      Import aquarium.*;

    • B.

      Import aquarium.*.Jelly;

    • C.

      Import aquarium.jellies.Jelly;

    • D.

      Import aquarium.jellies.*;

    • E.

      Import aquarium.jellies.Jelly.*;

    • F.

      None of these can make the code compile.

    Correct Answer(s)
    C. Import aquarium.jellies.Jelly;
    D. Import aquarium.jellies.*;
    Explanation
    The code requires the class "Jelly" to be imported in order to compile. The correct answer options "import aquarium.jellies.Jelly;" and "import aquarium.jellies.*;" both fulfill this requirement. "import aquarium.jellies.Jelly;" specifically imports the "Jelly" class, while "import aquarium.jellies.*;" imports all classes in the "jellies" package, including the "Jelly" class.

    Rate this question:

  • 6. 

    Given the following classes, what is the maximum number of imports that can be removed and have the code still compile?
    1. package aquarium; public class Water { }
    2. package aquarium;
    3. import java.lang.*;
    4. import java.lang.System;
    5. import aquarium.Water;
    6. import aquarium.*;
    7. public class Tank {
    8. public void print(Water water) {
    9. System.out.println(water); } }

    • A.

      0

    • B.

      1

    • C.

      2

    • D.

      3

    • E.

      4

    • F.

      Does not compile.

    Correct Answer
    E. 4
    Explanation
    The code can still compile if 4 imports are removed. The imports for "java.lang", "java.lang.System", "aquarium.Water", and "aquarium.*" are unnecessary because they are not used in the code. Removing these imports will not affect the compilation of the code.

    Rate this question:

  • 7. 

    Given the following class, which of the following calls print out Blue Jay?
    1. public class BirdDisplay {
    2. public static void main(String[] name) {
    3. System.out.println(name[1]);
    4. } }

    • A.

      Java BirdDisplay Sparrow Blue Jay

    • B.

      Java BirdDisplay Sparrow "Blue Jay"

    • C.

      Java BirdDisplay Blue Jay Sparrow

    • D.

      Java BirdDisplay "Blue Jay" Sparrow

    • E.

      Java BirdDisplay.class Sparrow "Blue Jay"

    • F.

      Java BirdDisplay.class "Blue Jay" Sparrow

    • G.

      Does not compile.

    Correct Answer
    B. Java BirdDisplay Sparrow "Blue Jay"
    Explanation
    B. Option B is correct because arrays start counting from zero and strings with spaces must be in quotes. Option A is incorrect because it outputs Blue. C is incorrect because it outputs Jay. Option D is incorrect because it outputs Sparrow. Options E and F are incorrect because they output Error: Could not find or load main class Bird-Display.class.

    Rate this question:

  • 8. 

    Which of the following legally fill in the blank so you can run the main() method from the command line?
    1. public static void main( )

    • A.

      String[] _names

    • B.

      String[] 123

    • C.

      String abc[]

    • D.

      String _Names[]

    • E.

      String... $n

    • F.

      String names

    • G.

      None of the above.

    Correct Answer(s)
    A. String[] _names
    C. String abc[]
    D. String _Names[]
    E. String... $n
    Explanation
    The correct answer is String[] _names, String abc[], String _Names[], String... $n. These options are all valid ways to declare a parameter for the main() method in Java. The main() method is the entry point for a Java program, and it must have a parameter of type String[] in order to accept command-line arguments. The other options listed are not valid because they do not have the correct syntax for declaring an array parameter.

    Rate this question:

  • 9. 

    Which of the following are legal entry point methods that can be run from the command line?

    • A.

      Private static void main(String[] args)

    • B.

      Public static final main(String[] args)

    • C.

      Public void main(String[] args)

    • D.

      Public static void main(String[] args)

    • E.

      Public static main(String[] args)

    • F.

      None of the above.

    Correct Answer
    D. Public static void main(String[] args)
    Explanation
    The correct answer is "public static void main(String[] args)". This is the correct signature for the entry point method in Java. It must be declared as public, static, and void, and it must accept a String array as a parameter. The other options provided do not have the correct signature or access modifiers, making them invalid entry point methods.

    Rate this question:

  • 10. 

    Which of the following statements are true?

    • A.

      An instance variable of type int defaults to null.

    • B.

      An instance variable of type String defaults to null.

    • C.

      An instance variable of type double defaults to 0.0.

    • D.

      An instance variable of type int defaults to 0.0.

    • E.

      None of the above.

    Correct Answer(s)
    B. An instance variable of type String defaults to null.
    C. An instance variable of type double defaults to 0.0.
    Explanation
    An instance variable of type String defaults to null because when an instance variable is not assigned a value, its default value is null. Similarly, an instance variable of type double defaults to 0.0 because when an instance variable of type double is not assigned a value, its default value is 0.0.

    Rate this question:

  • 11. 

    What is true about the following code?
    1. public class Bear {
    2. protected void finalize() {
    3. System.out.println("Roar!");
    4. }
    5. public static void main(String[] args) {
    6. Bear bear = new Bear();
    7. bear = null;
    8. System.gc();
    9. } }

    • A.

      Finalize() is guaranteed to be called.

    • B.

      Finalize() might or might not be called

    • C.

      Finalize() is guaranteed not to be called.

    • D.

      Garbage collection is guaranteed to run.

    • E.

      Garbage collection might or might not run.

    • F.

      Garbage collection is guaranteed not to run.

    • G.

      The code does not compile.

    Correct Answer(s)
    B. Finalize() might or might not be called
    E. Garbage collection might or might not run.
    Explanation
    The code includes a finalize() method, which is a method that is called by the garbage collector before an object is destroyed. However, the execution of the finalize() method is not guaranteed. It might or might not be called depending on various factors such as the availability of system resources. Similarly, the execution of garbage collection is also not guaranteed. It might or might not run depending on the JVM's decision and the availability of system resources. Therefore, both finalize() and garbage collection might or might not be called/run in this code.

    Rate this question:

  • 12. 

    Select a statement that is true. 

    • A.

      A local variable of type float defaults to 0.

    • B.

      A local variable of type Object defaults to null.

    • C.

      A local variable of type boolean defaults to false.

    • D.

      A local variable of type float defaults to 0.0.

    • E.

      None of the above.

    Correct Answer
    E. None of the above.
  • 13. 

    Which of the following are true? 

    • A.

      An instance variable of type boolean defaults to false.

    • B.

      An instance variable of type boolean defaults to true.

    • C.

      An instance variable of type boolean defaults to null.

    • D.

      An instance variable of type int defaults to 0.

    • E.

      An instance variable of type int defaults to 0.0.

    • F.

      An instance variable of type int defaults to null.

    • G.

      None of the above.

    Correct Answer(s)
    A. An instance variable of type boolean defaults to false.
    D. An instance variable of type int defaults to 0.
    Explanation
    An instance variable of type boolean defaults to false because boolean is a primitive data type in Java, and all primitive data types have default values. For boolean, the default value is false. An instance variable of type int defaults to 0 because int is also a primitive data type, and its default value is 0.

    Rate this question:

  • 14. 

    Given the following class in the file /my/directory/named/A/Bird.java:
    1. INSERT CODE HERE
    2. public class Bird { }
    3. Which of the following replaces INSERT CODE HERE if we compile from /my/directory? (Choose all that apply)

    • A.

      Package my.directory.named.a;

    • B.

      Package my.directory.named.A;

    • C.

      Package named.a;

    • D.

      Package named.A;

    • E.

      Package a;

    • F.

      Package A;

    • G.

      Does not compile.

    Correct Answer
    D. Package named.A;
    Explanation
    The correct answer is "package named.A;". This is because the class Bird is declared in the package named.A, so the correct package statement should be "package named.A;".

    Rate this question:

  • 15. 

    Which of the following lines of code compile?

    • A.

      Int i1 = 1_234;

    • B.

      Double d1 = 1_234_.0;

    • C.

      Double d2 = 1_234._0;

    • D.

      Double d3 = 1_234.0_;

    • E.

      Double d4 = 1_234.0;

    • F.

      None of the above.

    Correct Answer(s)
    A. Int i1 = 1_234;
    E. Double d4 = 1_234.0;
    Explanation
    A, E. Underscores are allowed as long as they are directly between two other digits. This means options A and E are correct. Options B and C are incorrect because the underscore is adjacent to the decimal point. Option D is incorrect because the underscore is the last character.

    Rate this question:

  • 16. 

    What does the following code output?
    1. 1: public class Salmon {
    2. 2: int count;
    3. 3: public void Salmon() {
    4. 4: count = 4;
    5. 5: }
    6. 6: public static void main(String[] args) {
    7. 7: Salmon s = new Salmon();
    8. 8: System.out.println(s.count);
    9. 9: } }

    • A.

      0

    • B.

      4

    • C.

      Compilation fails on line 3.

    • D.

      Compilation fails on line 4.

    • E.

      Compilation fails on line 7.

    • F.

      Compilation fails on line 8.

    Correct Answer
    A. 0
    Explanation
    The code outputs 0. The reason for this is that the constructor of the Salmon class is defined as a method instead of a constructor. As a result, when the object is created on line 7, the count variable is not initialized and its default value of 0 is printed on line 8.

    Rate this question:

  • 17. 

    Given the following class, which of the following lines of code can replace INSERT CODE HERE to make the code compile?
    1. public class Price {
    2. public void admission() {
    3. INSERT CODE HERE
    4. System.out.println(amount);
    5. } }

    • A.

      Int amount = 9L;

    • B.

      Int amount = 0b101;

    • C.

      Int amount = 0xE;

    • D.

      Double amount = 0xE;

    • E.

      Double amount = 1_2_.0_0;

    • F.

      Int amount = 1_2_;

    • G.

      None of the above.

    Correct Answer(s)
    B. Int amount = 0b101;
    C. Int amount = 0xE;
    D. Double amount = 0xE;
    Explanation
    The given code snippet is a class with a method called "admission". The goal is to replace the "INSERT CODE HERE" line with code that will make the code compile without any errors.

    Looking at the options, we can see that the lines of code that can replace "INSERT CODE HERE" are "int amount = 0b101;", "int amount = 0xE;", and "double amount = 0xE;". These lines of code initialize the "amount" variable with different values. Since the variable "amount" is declared inside the "admission" method, it needs to be initialized with a value before it can be used in the following line that prints its value.

    Therefore, these lines of code will make the code compile and print the value of the "amount" variable.

    Rate this question:

  • 18. 

    Which of the following are true statements?

    • A.

      Java allows operator overloading.

    • B.

      Java code compiled on Windows can run on Linux.

    • C.

      Java has pointers to specific locations in memory.

    • D.

      Java is a procedural language.

    • E.

      Java is an object-oriented language.

    • F.

      Java is a functional programming language.

    Correct Answer(s)
    B. Java code compiled on Windows can run on Linux.
    E. Java is an object-oriented language.
    Explanation
    Java code compiled on Windows can run on Linux because Java programs are compiled into bytecode, which is platform-independent and can be executed on any system that has a Java Virtual Machine (JVM) installed. Java is an object-oriented language because it supports the concepts of encapsulation, inheritance, and polymorphism. However, Java does not allow operator overloading, does not have pointers to specific locations in memory, and is not a procedural or functional programming language.

    Rate this question:

  • 19. 

    Which of the following are true?
    1. public class Bunny {
    2. public static void main(String[] args) {
    3. Bunny bun = new Bunny();
    4. } }

    • A.

      Bunny is a class.

    • B.

      Bun is a class.

    • C.

      Main is a class.

    • D.

      Bunny is a reference to an object.

    • E.

      Bun is a reference to an object.

    • F.

      Main is a reference to an object.

    • G.

      None of the above.

    Correct Answer(s)
    A. Bunny is a class.
    E. Bun is a reference to an object.
    Explanation
    Bunny is a class because it is declared with the keyword "class" and followed by the class name. bun is a reference to an object because it is declared as an instance of the Bunny class using the new keyword.

    Rate this question:

  • 20. 

    Which of the following are true?

    • A.

      Javac compiles a .class file into a .java file.

    • B.

      Javac compiles a .java file into a .bytecode file.

    • C.

      Javac compiles a .java file into a .class file.

    • D.

      Java takes the name of the class as a parameter.

    • E.

      Java takes the name of the .bytecode file as a parameter.

    • F.

      Java takes the name of the .class file as a parameter.

    Correct Answer(s)
    C. Javac compiles a .java file into a .class file.
    D. Java takes the name of the class as a parameter.
    Explanation
    The javac command is used to compile Java source code files (.java) into bytecode files (.class). Therefore, the statement "javac compiles a .java file into a .class file" is true. Additionally, when running a Java program, the name of the class containing the main method is passed as a parameter to the java command. Hence, the statement "Java takes the name of the class as a parameter" is also true.

    Rate this question:

  • 21. 

    Which represents the order in which the following statements can be assembled into a program that will compile successfully? 
    1. A: class Rabbit {}
    2. B: import java.util.*;
    3. C: package animals;

    • A.

      A, B, C

    • B.

      B, C, A

    • C.

      C, B, A

    • D.

      B, A

    • E.

      C, A

    • F.

      A, C

    • G.

      A, B

    Correct Answer(s)
    C. C, B, A
    D. B, A
    E. C, A
    Explanation
    The correct order in which the statements can be assembled into a program that will compile successfully is C, B, A. Firstly, the package declaration (C) should come before any import statements (B). Then, the class declaration (A) should come after the import statements. The second occurrence of B and A in the given answer is redundant and can be ignored.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jul 03, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jan 21, 2016
    Quiz Created by
    Alexpoiry
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.