Java SE 7 OCA Certification Prep: Personal Edition Lab Manual
Mastering the Java SE 7 Oracle Certified Associate (OCA) exam (1Z0-803) requires a solid understanding of Java syntax, language mechanics, and core APIs. While reading textbooks builds theoretical knowledge, writing and debugging code establishes true operational proficiency.
This lab manual provides targeted, hands-on exercises designed to reinforce critical exam topics. By working through these practical scenarios, you will develop the muscle memory needed to identify subtle code flaws, predict execution outcomes, and pass the OCA exam with confidence. Lab 1: Java Basics and Component Cooperation
Understand the structure of a Java class, execution via the main method, and how package declarations and imports function. Exercise 1: The Out-of-Order Class
Scenario: You are given a source file that fails to compile due to structural layout errors. You must reorder the components to achieve successful compilation. Create a file named SetupTest.java. Analyze the broken code structure below:
import java.util.ArrayList; class SetupTest { public static void main(String[] args) { ArrayList Use code with caution.
Task: Correct the placement of the package statement so it precedes all other elements.
Compile the file from your command line using:javac -d . SetupTest.java
Run the compiled class using its fully qualified name:java com.certification.prep.SetupTest Key Takeaway
A Java source file must strictly follow this order: package statement first, followed by import statements, followed by class declarations. Lab 2: Working with Java Data Types and String Manipulation
Differentiate between primitive types and reference types, understand variable scope, and master String and StringBuilder behavior. Exercise 1: Object Equality vs. Reference Equality
Scenario: The OCA exam heavily tests the difference between object equality (.equals()) and reference equality (==), especially concerning the String pool.
Write a class containing the following code snippet inside the main method:
String str1 = “OCA”; String str2 = new String(“OCA”); String str3 = “OCA”; System.out.println(“Result 1: ” + (str1 == str2)); System.out.println(“Result 2: ” + (str1 == str3)); System.out.println(“Result 3: ” + str1.equals(str2)); Use code with caution.
Task: Predict the boolean output of each print statement before running the code. Compile and execute the program to verify your predictions. Exercise 2: Modifying Strings with StringBuilder
Scenario: Strings are immutable, whereas StringBuilder objects can be modified without creating entirely new instances. Implement the following code:
StringBuilder sb = new StringBuilder(“Java”); sb.append(” 7”); sb.insert(4, “ SE”); sb.reverse(); System.out.println(sb); Use code with caution.
Task: Trace the state of the StringBuilder object step-by-step to determine the final printed text. Lab 3: Using Operators and Decision Constructs
Master the nuances of arithmetic, relational, and logical operators, alongside if/else and switch statements. Exercise 1: Short-Circuit Operators and Side Effects
Scenario: Developers often make mistakes regarding whether the right-hand side of a logical operator evaluates. Write a program featuring this logic:
int x = 5; int y = 10; if ((x < 4) && (++y > 10)) { System.out.println(“Inside If”); } System.out.println(“x = ” + x + “, y = ” + y); Use code with caution.
Task: Determine if y is incremented to 11 or remains 10 based on short-circuit evaluation. Change && to & and analyze how the output changes. Exercise 2: The Switch Statement Restrictions
Scenario: Java SE 7 introduced a major feature: allowing String objects in switch statements.
Create a switch block that evaluates a String variable representing a day of the week.
Intentionally omit a break statement in one of the cases to observe how “fall-through” behavior impacts your output. Lab 4: Creating and Using Arrays and Loops
Declare, initialize, and traverse one-dimensional and multi-dimensional arrays using standard for, enhanced for, and while loops. Exercise 1: Multi-Dimensional Array Iteration
Scenario: Arrays are objects in Java, and multi-dimensional arrays are simply arrays of arrays.
Declare a two-dimensional array of integers representing a matrix: int[][] matrix = { {1, 2, 3}, {4, 5}, {6, 7, 8, 9} }; Use code with caution.
Task: Write a nested enhanced for loop (for-each) to print every integer in the matrix sequentially. Note that the sub-arrays have different lengths (ragged array). Exercise 2: Loop Control with Labels
Scenario: Break and continue statements can target specific outer loops when labels are applied.
Implement a nested loop where the outer loop is labeled OUTER: and the inner loop is labeled INNER:.
Use a condition inside the inner loop that executes break OUTER; when a specific value is found. Verify that execution exits both loops entirely. Lab 5: Inheritance, Polymorphism, and Encapsulation
Implement object-oriented principles by designing classes that utilize inheritance, override methods, and cast object references correctly. Exercise 1: Method Overriding vs. Overloading
Scenario: Methods must match signature rules exactly to count as overridden rather than overloaded.
Create a base class named Vehicle with a method public void accelerate(). Create a subclass named Car that extends Vehicle. Override accelerate() in Car to print a custom message.
Task: Instantiate a vehicle using polymorphism: Vehicle v = new Car(); and call v.accelerate();. Observe which method runs at runtime. Exercise 2: Access Modifiers and Encapsulation
Scenario: Protecting data using private fields and public getters/setters is core to Java design.
Create a class Employee with a private double salary; field.
Implement validation inside setSalary(double salary) to reject negative values.
Test your encapsulation by attempting to write a negative salary from an external Test class. Lab 6: Handling Exceptions
Understand the Java exception hierarchy, differentiate checked exceptions from unchecked exceptions, and master try-catch-finally flow. Exercise 1: Exception Propagation and Finally Execution
Scenario: The finally block runs regardless of whether an exception is thrown or caught.
Write a method that deliberately throws a NullPointerException (an unchecked exception).
Wrap the method call inside a try-catch block in your main method. Add a finally block that prints “Finally block executed”. Observe the output order when running the application.
public class ExceptionLab { public static void main(String[] args) { try { String str = null; str.length(); // Throws NullPointerException } catch (NullPointerException e) { System.out.println(“Exception caught!”); } finally { System.out.println(“Finally block executed.”); } } } Use code with caution. Final Exam Simulation Drills
Before attempting the official OCA exam, challenge yourself with these verification checklists during your lab sessions:
Can you quickly identify if a line will cause a compiler error versus a runtime exception?
Do you know which core data types can be implicitly cast to wider types without an explicit cast operator?
Are you comfortable tracing array indexes without causing an ArrayIndexOutOfBoundsException?
Consistent practice with the mechanics featured in this manual ensures you will minimize syntax mistakes and tackle the actual Java SE 7 OCA Certification exam with precision.
If you would like to deepen your practice, let me know how you want to proceed. I can provide:
The complete source code solutions for any specific exercise listed above.
A set of multiple-choice practice questions matching the format of the official OCA exam.
An extra lab module covering Java SE 7 Core API classes like ArrayList or garbage collection.
Leave a Reply