Oops Assignment 2
Oops Assignment 2
ArithmeticException
Occurs when an incorrect arithmetic operation is performed,
such as dividing a number by zero
ClassNotFoundException
Oops Assignment-2
A checked exception that occurs when a required class is not
found
ArrayIndexOutOfBoundsException
Occurs when an attempt is made to access an array element with
an index that is negative or greater than the array's size
NullPointerException
Occurs when an attempt is made to use an object reference that
has been assigned the null value
InterruptedException
A checked exception that occurs when a thread is interrupted
while waiting, sleeping, or otherwise occupied
ArrayStoreException
A runtime exception that occurs when the wrong type of object
is placed into an array
Oops Assignment-2
7. Explain the purpose of the finally block in Java exception
handling. Provide an example to illustrate its usage.
ANS
The finally block in java is used to put important codes such as
clean up code e.g. closing the file or closing the connection. The
finally block executes whether exception rise or not and whether
exception handled or not. A finally contains all the crucial
statements regardless of the exception occurs or not.
EXAMPLE:
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
System.out.println("inside try block");
System.out.println(34 / 2);
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
finally {
System.out.println( "finally : i execut always.");
}
}
}
OUTPUT:
inside try block
17
finally : i execute always.
14. Explain the role of join() and isAlive() methods in thread management.
Provide examples of their usage.
ANS
join(): It will put the current thread on wait until the thread on which it is called
is dead.
EXAMPLE:
Oops Assignment-2
isAlive():The isAlive() method returns true if the thread upon which it is called
is still running otherwise it returns false
Oops Assignment-2
15. Define enumerations in Java. Explain how they enhance type
safety and provide an example of their usage.
ANS
An enumeration (enum for short) in Java is a special data type which
contains a set of predefined constants.
Here are some ways that enums enhance type safety:
Validates values
Enums ensure that only valid values can be used, making code less
prone to errors.
Detects errors
The compiler will notify you if you mistype a case label or a value in
an assignment.
Improves readability
Enums make code more readable and understandable by representing
the logical grouping and the intended object explicitly.
Centralizes changes
Changes to enums are centralized, making the code easier to update
and maintain.
EXAMPLE:
Oops Assignment-2
16. Describe wrapper classes in Java. Explain the importance of
wrapper classes and give examples of their usage with primitive data
types.
ANS
In Java, wrapper classes are parent classes that allow users to use
primitive data types as objects. They are used to make primitive data
types more efficient and manageable.
Here are some reasons why wrapper classes are important:
Object-oriented compatibility
Wrapper classes allow primitive data types to participate in object-
oriented programming paradigms like inheritance, polymorphism, and
encapsulation.
Nullable values
Primitive data types cannot represent null values, but wrapper classes
can. This is important in certain scenarios, such as when dealing with
databases or APIs.
Working with collections
Wrapper classes allow programmers to bind the values of different
primitive data types into objects, which helps when working with
collections like HashMap and ArrayList.
Utility functions
Wrapper classes provide different utility functions that can be used
with primitive data types
Example of Usage with Primitive Data Types
Oops Assignment-2
17. Explain autoboxing and unboxing in Java. How does autoboxing
simplify code? Provide an example to illustrate.
ANS
Autoboxing is the process by which primitive data types are
automatically converted into their corresponding wrapper objects,
while unboxing involves the automatic extraction of the primitive
value from the wrapper object.
Autoboxing feature greatly simplifies code because it eliminates the
need for manual conversion between primitives and wrapper objects.
As a result, you can focus more on the logic of your program rather
than dealing with tedious type conversions.Autoboxing is particularly
useful when working with collections (like ArrayList) that require
objects but the programmer often wants to work with primitive types
(like int, double, etc.).
EXAMPLE:
Value():
In Java, value() is not a built-in method, but it could refer to retrieving
the value of a variable or an object.
EXAMPLE