Java Se 8 Programmer OCA Exam Quiz!

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Alexpoiry
A
Alexpoiry
Community Contributor
Quizzes Created: 1 | Total Attempts: 8,992
| Attempts: 8,992 | Questions: 21
Please wait...
Question 1 / 21
0 %
0/100
Score 0/100
1. Which of the following are true? 

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.

Submit
Please wait...
About This Quiz
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... see moreexam. 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!
see less

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. } }

Explanation

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

Submit
3. Which of the following statements are true?

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.

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

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.

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

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.

Submit
6. 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();

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.

Submit
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. } }

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.

Submit
8. 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: }

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.

Submit
9. Which of the following are valid Java identifiers? 

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 _.

Submit
10. 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)

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;".

Submit
11. 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: } }

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.

Submit
12. Which of the following lines of code compile?

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.

Submit
13. 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) { } }

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.

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

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.

Submit
15. 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( )

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.

Submit
16. Which of the following are true statements?

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.

Submit
17. Select a statement that is true. 

Explanation

not-available-via-ai

Submit
18. 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. } }

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.

Submit
19. 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;

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.

Submit
20. 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);     } }

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.

Submit
21. Which of the following are true?

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.

Submit
View My Results

Quiz Review Timeline (Updated): Aug 20, 2024 +

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
Cancel
  • All
    All (21)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Which of the following are true? 
Determine the output of the following program. ...
Which of the following statements are true?
Which of the following are true?...
Which of the following are legal entry point methods that can be run...
Which of the following are true? ...
Given the following class, which of the following calls print out Blue...
Given the following class, which of the following is true?  ...
Which of the following are valid Java identifiers? 
Given the following class in the file /my/directory/named/A/Bird.java:...
What does the following code output?1: public class Salmon {2: int...
Which of the following lines of code compile?
Given the following classes, which of the following can independently...
Which of the following statements about lambda expressions in Java SE...
Which of the following legally fill in the blank so you can run the...
Which of the following are true statements?
Select a statement that is true. 
What is true about the following code?...
Which represents the order in which the following statements can be...
Given the following code, what will be the output when compiled and...
Which of the following are true?
Alert!

Advertisement