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,890
Questions: 21 | Attempts: 8,894

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. 

    Which of the following statements about lambda expressions in Java SE 8 is true?

    • A.

      Lambda expressions can only be used with interfaces that have more than one abstract method.

    • B.

      Lambda expressions can access only final variables from the enclosing scope.

    • C.

      Lambda expressions can be used to create instances of functional interfaces.

    • D.

      Lambda expressions are not allowed to throw checked exceptions.

    Correct Answer
    C. Lambda expressions can be used to create instances of functional interfaces.
    Explanation
    Lambda expressions in Java SE 8 are used primarily to create instances of functional interfaces, which are interfaces with a single abstract method. This makes the code more concise and readable. Contrary to the other options:
    A is incorrect because lambda expressions can only be used with functional interfaces (interfaces with a single abstract method).
    B is incorrect because lambda expressions can access both final and effectively final variables from the enclosing scope.
    D is incorrect because lambda expressions can throw checked exceptions if their functional interface's abstract method declares them.

    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
    In Java, the correct signature for the entry point method that the JVM looks for to start execution is public static void main(String[] args). This method must be public, static, and return void. The parameter must be a String array (String[] args). The other options either lack the correct access modifier, return type, or method signature required for the Java application to run from the command line.

    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 code, what will be the output when compiled and run? public class TestClass {     public static void main(String[] args) {         int x = 5;         int y = 10;         int z = (x > y) ? x++ : y++;         System.out.println("x = " + x + ", y = " + y + ", z = " + z);     } }

    • A.

      X = 5, y = 11, z = 5

    • B.

      X = 6, y = 10, z = 5

    • C.

      X = 6, y = 11, z = 10

    • D.

      X = 5, y = 10, z = 10

    • E.

      Compilation error

    Correct Answer
    C. X = 6, y = 11, z = 10
    Explanation
    The ternary operator (x > y) ? x++ : y++ checks if x > y.
    Since x (5) is not greater than y (10), the else part (y++) is executed.
    z is assigned the current value of y, which is 10.
    After that, y is incremented to 11.
    x remains unchanged at 5, and y becomes 11.
    Thus, the output will be x = 6, y = 11, z = 10.

    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
  • Aug 20, 2024
    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.