Welcome to my personal repository where I'm preparing for OCA_1Z0-808 and OCP_1Z0_815!
This is where I’m gathering all the resources, notes, and exercises while preparing for the Oracle Certified Associate (OCA) and Oracle Certified Professional (OCP) exams. Just a little bit of patience, a lot of practice, and maybe some coffee along the way.
- OCA_1Z0-808: Kicking off with the basics to become a Java pro.
- OCP_1Z0_815: Leveling up and getting ready to be a certified professional!
Well, mainly for me, but feel free to follow along if you're also preparing for the same certifications! It’s all about sharing the journey of getting certified and staying motivated.
- Clone the repo (if you want to follow along).
- Get comfortable with some coffee.
- Go through the materials, solve exercises, take practice tests, and learn a lot!
- Get certified, celebrate, and feel proud of the hard work!
On my way to becoming a Java certified professional! No stress, just progress.
Answer: Java has several key features:
- Platform Independent: Java programs can run on different operating systems without modification due to the JVM (Java Virtual Machine).
- Object-Oriented: Java follows OOP principles, such as inheritance, encapsulation, and polymorphism.
- Robust: Java provides strong memory management and exception handling features.
- Multithreaded: Supports concurrent execution of two or more parts of a program.
- Secure: Java has security features like bytecode verification, runtime security checks, and the security manager.
- High Performance: Although Java is an interpreted language, it achieves high performance with Just-In-Time (JIT) compilation.
- Dynamic and Extensible: Supports dynamic loading of classes and libraries.
Answer:
- JDK (Java Development Kit): Includes the JRE along with development tools like compilers and debuggers. It is used for developing Java applications.
- JRE (Java Runtime Environment): Includes JVM and libraries required for running Java applications but does not contain development tools.
- JVM (Java Virtual Machine): A runtime environment that executes Java bytecode and enables platform independence.
Answer:
Feature | Java | C++ |
---|---|---|
Platform Independence | Yes (via JVM) | No (compiled to machine code) |
Memory Management | Automatic (Garbage Collector) | Manual (pointers, new/delete) |
Multiple Inheritance | Not supported (achieved via interfaces) | Supported |
Pointers | Not available | Available |
Exception Handling | Built-in | Manual |
Security | High (sandboxing, bytecode verification) | Lower (direct memory access) |
Answer: JVM is an engine that provides a runtime environment for executing Java bytecode. It converts bytecode into machine code and ensures Java’s platform independence. It consists of:
- Class Loader: Loads class files into memory.
- Runtime Memory Areas: Heap, Stack, Method Area, etc.
- Execution Engine: Interprets bytecode or compiles it using JIT.
- Garbage Collector: Manages memory automatically by reclaiming unused objects.
Answer: Bytecode is an intermediate representation of Java source code compiled by the Java compiler. It is platform-independent and can be executed on any system with a JVM. Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
When compiled using javac HelloWorld.java
, it generates HelloWorld.class
, which contains bytecode.
Answer:
==
is used for reference comparison (i.e., whether two references point to the same memory location).equals()
is used for content comparison (i.e., whether two objects are logically equal). Example:
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2); // false (different memory locations)
System.out.println(s1.equals(s2)); // true (same content)
Answer: A constructor is a special method used to initialize objects. It has the same name as the class and does not have a return type. Example:
class Example {
int x;
// Constructor
Example(int value) {
x = value;
}
void display() {
System.out.println("Value: " + x);
}
}
public class Main {
public static void main(String[] args) {
Example obj = new Example(10);
obj.display();
}
}
Answer:
- Default Constructor: No parameters, initializes default values.
- Parameterized Constructor: Accepts arguments to initialize an object.
- Copy Constructor: Creates a new object by copying values from another object.
Answer: Method overloading allows multiple methods with the same name but different parameter lists within a class. Example:
class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
Answer: Method overriding allows a subclass to provide a specific implementation of a method already defined in its parent class. Example:
class Parent {
void show() {
System.out.println("Parent class method");
}
}
class Child extends Parent {
@Override
void show() {
System.out.println("Child class method");
}
}
Answer: An interface in Java is a reference type that defines abstract methods that must be implemented by a class. Interfaces support multiple inheritance. Example:
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark");
}
}
Answer: An abstract class in Java is a class that cannot be instantiated and may contain abstract methods (methods without implementation). Example:
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car starting...");
}
}
Answer:
Feature | Abstract Class | Interface |
---|---|---|
Methods | Can have both abstract and concrete methods | Only abstract methods (Java 7), can have default/static methods (Java 8) |
Variables | Can have instance variables | Only public, static, and final variables |
Multiple Inheritance | Not supported | Supported |
Constructor | Can have a constructor | Cannot have a constructor |
Answer:
The final
keyword is used to declare constants, prevent method overriding, and prevent inheritance.
- Final variable: Cannot be reassigned.
- Final method: Cannot be overridden.
- Final class: Cannot be inherited. Example:
final class Constants {
static final double PI = 3.14159;
}
Answer: A static variable/method belongs to the class rather than an instance of the class. Example:
class Example {
static int count = 0;
static void show() {
System.out.println("Static method");
}
}
Answer:
The super
keyword is used to refer to the immediate parent class in Java. It can be used to call parent class constructors, methods, or access parent class variables.
Example:
class Parent {
void display() {
System.out.println("Parent class");
}
}
class Child extends Parent {
void display() {
super.display(); // Calls parent class method
System.out.println("Child class");
}
}
Answer:
The this
keyword is a reference variable that refers to the current object. It is used to differentiate between instance variables and parameters with the same name.
Example:
class Example {
int x;
Example(int x) {
this.x = x; // Refers to the instance variable
}
}
Answer:
An immutable class is a class whose objects cannot be modified after creation. The String
class is a prime example.
Example:
final class ImmutableClass {
private final int value;
ImmutableClass(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Answer: Garbage collection in Java is the process of automatically reclaiming memory occupied by unused objects to prevent memory leaks. It is performed by the JVM’s Garbage Collector (GC).
Answer: Java provides four types of access modifiers:
- Private: Accessible only within the class.
- Default (no modifier): Accessible within the same package.
- Protected: Accessible within the same package and subclasses.
- Public: Accessible from anywhere.
Answer:
final
: Used for variables (makes them constants), methods (prevents overriding), and classes (prevents inheritance).finally
: A block used in exception handling that always executes, regardless of whether an exception occurs or not.finalize()
: A method that is called by the garbage collector before an object is removed from memory. Example:
class Example {
protected void finalize() {
System.out.println("Finalize method called");
}
}
Answer:
- Checked Exceptions: Exceptions that are checked at compile time (e.g.,
IOException
,SQLException
). Must be handled usingtry-catch
orthrows
. - Unchecked Exceptions: Exceptions that occur at runtime (e.g.,
NullPointerException
,ArithmeticException
). Example:
try {
FileReader file = new FileReader("test.txt");
} catch (IOException e) {
System.out.println("File not found!");
}
Answer:
throw
: Used to explicitly throw an exception.throws
: Declares exceptions that a method might throw. Example:
void checkAge(int age) throws ArithmeticException {
if (age < 18) {
throw new ArithmeticException("Not allowed");
}
}
Answer:
- Extending the
Thread
class:
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
- Implementing the
Runnable
interface:
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread running");
}
}
Answer:
sleep()
: A method inThread
that pauses execution for a specified time but does not release the lock.wait()
: A method inObject
that pauses execution untilnotify()
ornotifyAll()
is called, and releases the lock. Example:
synchronized(obj) {
obj.wait(); // Releases lock and waits
obj.notify(); // Wakes up waiting thread
}
Answer:
synchronized
: Ensures that only one thread can access a block of code or method at a time. It provides locking to avoid race conditions.volatile
: Ensures visibility of changes to a variable across threads but does not provide locking. Example:
class SharedResource {
private volatile boolean flag = true;
void updateFlag() {
flag = false; // Other threads immediately see the change
}
}
Answer: Daemon threads are background threads that run in the JVM and terminate when all user threads are completed. They are used for tasks like garbage collection. Example:
class MyDaemonThread extends Thread {
public void run() {
while (true) {
System.out.println("Daemon running");
}
}
}
public class Main {
public static void main(String[] args) {
MyDaemonThread daemonThread = new MyDaemonThread();
daemonThread.setDaemon(true);
daemonThread.start();
}
}
Answer:
Feature | ArrayList |
LinkedList |
---|---|---|
Implementation | Uses a dynamic array | Uses a doubly linked list |
Access time | Fast (O(1) for get() ) |
Slow (O(n) for get() ) |
Insertion/Deletion | Slow (shifting elements required) | Fast (modifying pointers) |
Memory Usage | Less (stores elements in contiguous memory) | More (stores extra references) |
Example: |
List<String> list = new ArrayList<>(); // or new LinkedList<>();
Answer: Lambda expressions provide a concise way to implement functional interfaces in Java. Example:
@FunctionalInterface
interface MyFunction {
void display();
}
public class Main {
public static void main(String[] args) {
MyFunction func = () -> System.out.println("Hello from Lambda");
func.display();
}
}
Answer: A functional interface is an interface that contains exactly one abstract method. It can have multiple default or static methods. Example:
@FunctionalInterface
interface MyInterface {
void execute();
default void show() {
System.out.println("Default method");
}
}