0% found this document useful (0 votes)
13 views

OOPs With Java Answers

Uploaded by

nimishamruthbt19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

OOPs With Java Answers

Uploaded by

nimishamruthbt19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

OOPs with JAVA 3rd Internals Questions - Answers

1. Access Protections in Packages

Java offers four access levels:

- Public: Accessible everywhere.

- Protected: Accessible within the package and subclasses in other packages.

- Default (Package-Private): Accessible only within the same package.

- Private: Accessible only within the declared class.

Example:

package mypackage;

public class Example {

protected void display() {

System.out.println("Protected method.");

package anotherpackage;

import mypackage.Example;

public class SubClass extends Example {

public static void main(String[] args) {

SubClass obj = new SubClass();

obj.display(); // Works because it's protected.

}
2. Custom Exception for Banking Application

class InsufficientBalanceException extends Exception {

InsufficientBalanceException(String message) {

super(message);

class Bank {

private double balance = 5000;

void withdraw(double amount) throws InsufficientBalanceException {

if (amount > balance) {

throw new InsufficientBalanceException("Insufficient balance!");

balance -= amount;

System.out.println("Withdrawal successful! Remaining balance: " + balance);

public static void main(String[] args) {

Bank account = new Bank();

try {

account.withdraw(6000);

} catch (InsufficientBalanceException e) {

System.out.println(e.getMessage());

}
}

3. Exception Handling in Java

Definition: Exception is an event that disrupts normal program flow.

Mechanism:

- try: Block where code is written.

- catch: Handles exceptions.

- finally: Always executes.

- throw: Used to explicitly throw an exception.

- throws: Declares an exception.

Example:

try {

int result = 10 / 0;

} catch (ArithmeticException e) {

System.out.println("Cannot divide by zero.");

} finally {

System.out.println("Cleanup code.");

4. Java Package and Import

Package Creation:

package balance;

public class Account {

public void displayBalance() {

System.out.println("Balance: $1000");
}

Import and Use:

import balance.Account;

public class Main {

public static void main(String[] args) {

Account acc = new Account();

acc.displayBalance();

5. Creating a Thread

Definition: A thread is a lightweight process.

Ways to Create Threads:

1. Extending Thread class.

2. Implementing Runnable interface.

Example:

class MyThread extends Thread {

public void run() {

System.out.println("Thread is running.");

public static void main(String[] args) {

MyThread thread = new MyThread();


thread.start();

6. Thread Synchronization

Synchronization ensures thread-safe operations.

Example:

class Counter {

private int count = 0;

synchronized void increment() {

count++;

public int getCount() {

return count;

public class Main {

public static void main(String[] args) {

Counter counter = new Counter();

Thread t1 = new Thread(() -> {

for (int i = 0; i < 1000; i++) counter.increment();

});
Thread t2 = new Thread(() -> {

for (int i = 0; i < 1000; i++) counter.increment();

});

t1.start();

t2.start();

7. Unboxing

Program:

public class UnboxingDemo {

public static void main(String[] args) {

Integer num = 100;

int n = num; // Unboxing

System.out.println("Primitive: " + n);

8. Type Wrappers in Java

- Byte, Short, Integer, Long, Float, Double, Character, Boolean.

9. Autoboxing/Unboxing

Occurs when primitives and wrapper classes interact:

public class Example {


public static void main(String[] args) {

Integer num = 50; // Autoboxing

int sum = num + 10; // Unboxing

System.out.println("Result: " + sum);

10. Base Class Constructor with super

class Base {

Base() {

System.out.println("Base class constructor");

class MyThread extends Thread {

MyThread() {

super();

public void run() {

System.out.println("Child thread is running.");

public static void main(String[] args) {

MyThread thread = new MyThread();

thread.start();

System.out.println("Main thread.");
}

You might also like