0% found this document useful (0 votes)
22 views19 pages

Unit 03 Java OOP

The document covers key concepts in Java programming, focusing on exception handling and multithreading. It details keywords for exception handling, differences between checked and unchecked exceptions, and methods for thread creation, along with practical examples. Additionally, it addresses synchronization methods like wait() and notify(), and emphasizes the importance of these topics in exams.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views19 pages

Unit 03 Java OOP

The document covers key concepts in Java programming, focusing on exception handling and multithreading. It details keywords for exception handling, differences between checked and unchecked exceptions, and methods for thread creation, along with practical examples. Additionally, it addresses synchronization methods like wait() and notify(), and emphasizes the importance of these topics in exams.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

In this example, only the Circle class is allowed to inherit the Shape class.

Conclusion:

New Java feature​


Low frequency but possible

UNIT-03

Topic–01 – EXCEPTION HANDLING (TOP PRIORITY –


EVERY YEAR)
Asked in:

• 2023–24​
• 2024–25

Questions:

Q1. “Describe all the keywords used for exception handling in Java.”​
→ 2023–24 | Section A | 2 Marks | Theory

Answer:

Keywords Used for Exception Handling in Java


Java provides the following keywords for exception handling:

1.​ try – Used to define a block where exceptions may occur.


2.​ catch – Used to handle the exception.
3.​ finally – Executes always whether exception occurs or not.
4.​ throw – Used to explicitly throw an exception.
5.​ throws – Declares exceptions that may occur in a method.

These keywords help in handling runtime errors effectively in Java.

Q2. “Differentiate between checked and unchecked exceptions in Java. Write a Java
program to demonstrate Arithmetic Exception handlings.”​
→ 2023–24 | Section C | 7 Marks | Theory + Program

Answer:

Introduction
An exception is an unwanted event that occurs during program execution and interrupts the normal flow of the
program. Java provides exception handling mechanisms to handle such errors.

Difference Between Checked and Unchecked Exceptions


Checked Exception Unchecked Exception

Checked at compile time Checked at runtime

Must be handled using try-catch or throws Handling is optional

Subclass of Exception class Subclass of RuntimeException

Program will not compile if not handled Program compiles successfully

Example: IOException Example: ArithmeticException

Arithmetic Exception
ArithmeticException occurs when an illegal arithmetic operation is performed, such as division by zero.

Java Program for Arithmetic Exception Handling


class ExceptionDemo

public static void main(String args[])

{
int a = 10;

int b = 0;

try

int c = a / b;

[Link](c);

catch(ArithmeticException e)

[Link]("Arithmetic Exception Handled");

[Link]("Program Continues");

Output
Arithmetic Exception Handled

Program Continues

Q3. “What is the purpose of the finally block in Java exception handling?”​
→ 2024–25 | Section A | 2 Marks | Conceptual
Answer:

Purpose of finally Block in Java Exception Handling


The finally block in Java is used to execute important statements whether an exception occurs or not. It is
mainly used for resource cleanup operations such as closing files, database connections, etc.

Features

1.​ Executes always


2.​ Used after try-catch block
3.​ Helps in cleanup operations

Example

try

int a = 10/0;

catch(Exception e)

[Link]("Exception");

finally

[Link]("Finally Block Executed");

Q4. “How is throw different from throws?”​


→ 2024–25 | Section A | 2 Marks | Conceptual

Answer:

Difference Between throw and throws in Java


throw throws

Used to explicitly throw an exception Used to declare exceptions

Used inside method body Used in method declaration

Throws a single exception object Can declare multiple exceptions

Followed by exception object Followed by exception class names

Example

throw new ArithmeticException();

void show() throws IOException

Q5. “Write a Java program to handle multiple exceptions using try-catch-finally.”​


→ 2024–25 | Section C | 7 Marks | Program

Answer:

Introduction
Multiple exceptions occur when a program can generate more than one type of exception. Java handles them
using multiple catch blocks along with the finally block.

The finally block always executes whether an exception occurs or not.

Program
class MultipleException

public static void main(String args[])


{

int a[] = new int[5];

try

int x = 10 / 0;

a[10] = 50;

catch(ArithmeticException e)

[Link]("Arithmetic Exception Handled");

catch(ArrayIndexOutOfBoundsException e)

[Link]("Array Index Exception Handled");

finally

[Link]("Finally Block Executed");

[Link]("Program Continues");

}
}

Output
Arithmetic Exception Handled

Finally Block Executed

Program Continues

Explanation
1.​ try block contains statements that may generate exceptions.
2.​ catch blocks handle different exceptions separately.
3.​ finally block executes always.
4.​ Program execution continues normally after exception handling.

Conclusion:

MOST REPEATED UNIT–03 TOPIC (100% coverage)​


Asked in 2 marks + 7 marks + program​
Includes:

●​ Keywords (try, catch, throw, throws, finally)


●​ Types of exceptions
●​ Program-based handling

Topic-02 – MULTITHREADING (VERY HIGH WEIGHT)

Asked in:

• 2023–24​
• 2024–25

Questions:

Q1. “Describe various states achieved by the thread in its life cycle.”​
→ 2023–24 | Section A | 2 Marks | Theory
Answer:

Various States of Thread in Java Life Cycle


A thread in Java passes through different states during its execution. The main thread states are:

1.​ New State – Thread object is created but not started.


2.​ Runnable State – Thread is ready for execution.
3.​ Running State – Thread is currently executing.
4.​ Blocked/Waiting State – Thread waits for resources or another thread.
5.​ Terminated State – Thread execution is completed.

These states together form the life cycle of a thread in Java.

Q2. “Describe the ways to create the threads in Java with suitable code. Also explain
which method is more suitable to create threads.”​
→ 2023–24 | Section B | 7 Marks | Theory + Code

Answer:

Introduction
Thread is a lightweight subprocess used for multitasking in Java. Multithreading allows multiple tasks to
execute simultaneously.

Java mainly provides two ways to create threads:

1.​ By extending Thread class


2.​ By implementing Runnable interface

1. Creating Thread by Extending Thread Class


In this method, a class extends the Thread class and overrides the run() method.

Program
class MyThread extends Thread

public void run()

{
[Link]("Thread is Running");

public static void main(String args[])

MyThread t = new MyThread();

[Link]();

Output
Thread is Running

2. Creating Thread by Implementing Runnable Interface


In this method, a class implements the Runnable interface and defines the run() method.

Program
class Demo implements Runnable

public void run()

[Link]("Thread is Running");

public static void main(String args[])

{
Demo d = new Demo();

Thread t = new Thread(d);

[Link]();

Output

Thread is Running

Which Method is More Suitable?


The Runnable interface method is more suitable because:

1.​ Java does not support multiple inheritance with classes.


2.​ A class can implement multiple interfaces.
3.​ Better object-oriented approach.
4.​ Increases flexibility and code reusability.

Difference Between Both Methods


Extending Thread Class Implementing Runnable Interface

Inherits Thread class Implements Runnable interface

Cannot extend another class Can extend another class

Less flexible More flexible

Simpler for small programs Better for large applications


Q3. “Explain the concept of multithreading. Write a program to implement two
threads printing numbers alternatively.”​
→ 2024–25 | Section B | 7 Marks | Theory + Program

Answer:

Introduction
Multithreading is a process in which multiple threads execute simultaneously within a program. It helps in
performing multiple tasks at the same time and improves CPU utilization.

A thread is a lightweight subprocess.

Advantages of Multithreading
1.​ Improves performance
2.​ Saves CPU time
3.​ Enables multitasking
4.​ Improves responsiveness of applications

Program to Print Numbers Using Two Threads


class EvenThread extends Thread

public void run()

for(int i=2; i<=10; i=i+2)

[Link]("Even: " + i);

}
class OddThread extends Thread

public void run()

for(int i=1; i<=10; i=i+2)

[Link]("Odd: " + i);

class Test

public static void main(String args[])

EvenThread t1 = new EvenThread();

OddThread t2 = new OddThread();

[Link]();

[Link]();

Output
Odd: 1
Even: 2

Odd: 3

Even: 4

...

Explanation
1.​ Two thread classes are created:
○​ EvenThread
○​ OddThread
2.​ Both classes override the run() method.
3.​ start() method is used to execute threads simultaneously.
4.​ One thread prints even numbers while the other prints odd numbers.

Q4. “Write a Java program that creates two threads – one to print even numbers
and the other for odd numbers.”​
→ 2024–25 | Section C | 7 Marks | Program

Answer:

Program
class EvenThread extends Thread

public void run()

for(int i=2; i<=10; i=i+2)

[Link]("Even Number: " + i);

}
}

class OddThread extends Thread

public void run()

for(int i=1; i<=10; i=i+2)

[Link]("Odd Number: " + i);

class Test

public static void main(String args[])

EvenThread t1 = new EvenThread();

OddThread t2 = new OddThread();

[Link]();

[Link]();

Output
Odd Number: 1

Even Number: 2

Odd Number: 3

Even Number: 4

...

Explanation
1.​ Two classes are created by extending the Thread class.
2.​ EvenThread prints even numbers.
3.​ OddThread prints odd numbers.
4.​ start() method is used to execute both threads simultaneously.

Conclusion:

VERY IMPORTANT (appears both years)​


Mostly asked as program-based 7 marks​
Key areas:

●​ Thread creation methods


●​ Lifecycle
●​ Practical implementation

Topic-03 – THREAD METHODS & SYNCHRONIZATION


(MODERATE PRIORITY)
Asked in:

• 2023–24

Questions:

Q1. “Differentiate between with suitable examples: wait() and notify()”​


→ 2023–24 | Section C | 7 Marks (part question) | Theory
Answer:

Introduction
wait() and notify() methods are used for inter-thread communication in Java. These methods belong to the
Object class and are mainly used in synchronization.

Difference Between wait() and notify()


wait() notify()

Causes the thread to wait Wakes up a waiting thread

Releases the object lock Does not release lock immediately

Thread enters waiting state Thread becomes runnable

Used when thread waits for condition Used to signal another thread

Called inside synchronized block Called inside synchronized block

wait() Method
The wait() method pauses the execution of the current thread until another thread calls notify().

Example

synchronized(obj)

[Link]();

notify() Method
The notify() method wakes up one waiting thread.

Example

synchronized(obj)

[Link]();

Program Example
class Demo

synchronized void show()

try

wait();

[Link]("Thread Resumed");

catch(Exception e)

[Link](e);

synchronized void display()


{

notify();

[Link]("Notification Sent");

Conclusion:

Concept-based question​
Often asked as part of mixed question​
Related to synchronization

Topic–04 – BASIC EXCEPTION CONCEPTS (LOW–MODERATE


PRIORITY)
Asked in:

• 2024–25

Questions:

Q1. “What is the purpose of the finally block in Java exception handling?”​
→ 2024–25 | Section A | 2 Marks

Answer:

Purpose of finally Block in Java Exception Handling


The finally block in Java is used to execute important statements whether an exception occurs or not. It is
mainly used for cleanup operations such as closing files, releasing resources, and database connections.

Features

1.​ Executes always


2.​ Used after try-catch block
3.​ Helps in resource cleanup

Example
try

int a = 10/0;

catch(Exception e)

[Link]("Exception Handled");

finally

[Link]("Finally Block Executed");

Q2. “How is throw different from throws?”​


→ 2024–25 | Section A | 2 Marks

Answer:

Difference Between throw and throws in Java


throw throws

Used to explicitly throw an exception Used to declare exceptions

Used inside method body Used with method declaration

Throws one exception at a time Can declare multiple exceptions

Followed by exception object Followed by exception class names

Example

You might also like