UNIT III OOPs
2 Marks Answers
1. What is inheritance?
Inheritance is the process by which one class acquires the
properties and methods of another class. The existing class is
called superclass and the new class is called subclass.
2. What is the purpose of keyword extends?
The extends keyword is used to inherit one class from another
class in Java.
Example:
class B extends A
3. What is the purpose of super( )?
super() is used to call the constructor of the immediate
superclass.
4. What are abstract classes?
Abstract classes are classes declared using the abstract
keyword. They cannot be instantiated and may contain abstract
methods.
abstract class Animal {
abstract void sound(); // abstract method
void sleep() { // normal method
[Link]("Sleeping");
}
}
An abstract class is a class that is used as a base (parent)
class for other classes.
5. What are abstract methods?
Abstract methods are methods declared without a body and
must be implemented by subclasses.
Syntax:
abstract void show();
6. How to prevent methods from overriding?
Methods can be prevented from overriding by using the final
keyword.
//When the same method exists in both parent and child class
with same name and parameters, it is called method
overriding.
Example:
final void display()
7. What are final variables?
Final variables are constants whose values cannot be changed
once initialized.
Example:
final int x = 10;
8. What is the purpose of final class?
A final class prevents inheritance. No other class can extend a
final class.
9. What are final methods?
Final methods are methods that cannot be overridden by
subclasses.
10. How to define a package?
A package is defined using the package keyword at the
beginning of the source file.
Syntax:
package mypack;
11. How to import a package?
A package is imported using the import keyword.
Syntax:
import [Link].*;
12. List any four API packages of Java.
1. [Link]
2. [Link]
3. [Link]
4. [Link]
// Java API Packages(for 2 marks not required)
1. [Link]
Provides basic classes for Java programs.
Automatically imported.
Examples: String, Math, System
2. [Link]
Provides utility classes.
Examples: Scanner, ArrayList, Date
3. [Link]
Used for input and output operations.
Examples: File, FileReader, FileWriter
4. [Link]
Used for creating GUI (Graphical User Interface).
Examples: Button, Frame, Label
13. What is the purpose of exception handler?
An exception handler is used to detect and handle runtime
errors so that the program does not terminate abnormally.
14. List any four types of exceptions in Java.
1. ArithmeticException
2. NullPointerException box=null;
3. ArrayIndexOutOfBoundsException
4. NumberFormatException
15. What is the purpose of try and catch block?
The try block contains code that may generate an exception,
and the catch block handles the exception.
UNIT III – 4 Marks and Above Answers
1. Explain single inheritance with suitable example.
Single inheritance is the process in which one subclass inherits
the properties and methods of one superclass.
Superclass → Parent class
Subclass → Child class
The subclass can use all accessible members of the superclass.
Example:
class A
{
int x = 10;
void show()
{
[Link]("Value of x = " + x);
}
}
class B extends A
{
int y = 20;
void display()
{
[Link]("Value of y = " + y);
}
}
class SingleInheritance
{
public static void main(String args[])
{
B obj = new B();
[Link]();
[Link]();
}
}
Output:
Value of x = 10
Value of y = 20
Advantages:
Code reusability
Reduces redundancy
Easy maintenance
2. Explain method overriding with suitable example.
Method overriding occurs when a subclass provides its own
implementation of a method already defined in the superclass.
Method name must be same
Parameters must be same
Example:
class A
{
void show()
{
[Link]("Class A Method");
}
}
class B extends A
{
void show()
{
[Link]("Class B Method");
}
}
class OverrideDemo
{
public static void main(String args[])
{
B obj = new B();
[Link]();
}
}
Output:
Class B Method
Advantages:
Runtime polymorphism
Provides specific implementation
3. Explain multi-level inheritance with suitable example.
Multilevel inheritance occurs when one class inherits another
class, and another class further inherits it.
Example:
class A
{
void displayA()
{
[Link]("Class A");
}
}
class B extends A
{
void displayB()
{
[Link]("Class B");
}
}
class C extends B
{
void displayC()
{
[Link]("Class C");
}
}
class MultiLevel
{
public static void main(String args[])
{
C obj = new C();
[Link]();
[Link]();
[Link]();
}
}
Output:
Class A
Class B
Class C
Advantages:
Supports code reusability
Creates hierarchical relationship
4. Explain the purpose of abstract and final classes.
Abstract Class
An abstract class is declared using the abstract keyword.
Cannot create objects
May contain abstract and non-abstract methods
Used to provide partial abstraction
Example:
abstract class Shape
{
abstract void draw();
}
class Circle extends Shape
{
void draw()
{
[Link]("Drawing Circle");
}
}
Final Class
A final class is declared using the final keyword.
Cannot be inherited
Used for security and preventing modification
Example:
final class Test
{
void show()
{
[Link]("Final Class");
}
}
Purpose:
Abstract class → Achieves abstraction
Final class → Prevents inheritance
5. Explain how to create and use a package in Java with
suitable example.
A package is a collection of related classes and interfaces.
Steps to Create a Package
1. Use package statement
2. Save file in folder with package name
3. Compile and use import statement
Creating Package:
package mypack;
public class Demo
{
public void show()
{
[Link]("Inside Package");
}
}
Using Package:
import [Link];
class Test
{
public static void main(String args[])
{
Demo obj = new Demo();
[Link]();
}
}
Output:
Inside Package
Advantages:
Avoids naming conflicts
Improves organization
Provides access protection
6. List and explain different API packages available in
Java.
Java API packages contain predefined classes and interfaces.
Packag
Purpose
e
[Link] Basic language support classes
[Link] Utility classes like Scanner, Date
[Link] Input and output operations
[Link] GUI components
[Link] Networking classe
1. [Link]
Provides basic classes for Java programs.
Automatically imported.
Examples: String, Math, System
2. [Link]
Provides utility classes.
Examples: Scanner, ArrayList, Date
3. [Link]
Used for input and output operations.
Examples: File, FileReader, FileWriter
4. [Link]
Used for creating GUI (Graphical User Interface).
Examples: Button, Frame, Label
Examples:
[Link]
[Link]
[Link]
[Link]
[Link]
Advantages:
Reusable classes
Reduces programming effort
Provides standard functionality
7. Explain how to create and implement interface using
suitable example.
An interface contains abstract methods. It is implemented using
the implements keyword.
Interface Example:
interface Shape
{
void draw();
}
class Circle implements Shape
{
public void draw()
{
[Link]("Drawing Circle");
}
}
class InterfaceDemo
{
public static void main(String args[])
{
Circle obj = new Circle();
[Link]();
}
}
Output:
Drawing Circle
Advantages:
Achieves full abstraction
Supports multiple inheritance
Improves flexibility
8. Explain exception handling in Java with suitable
example.
Exception handling is used to handle runtime errors and
maintain normal flow of program execution.
Java uses:
try
catch
finally
throw
throws
Example:
class ExceptionDemo
{
public static void main(String args[])
{
try
{
int a = 10 / 0;
}
catch(ArithmeticException e)
{
[Link]("Division by zero");
}
[Link]("Program continues");
}
}
Output:
Division by zero
Program continues
Advantages:
Prevents abnormal termination
Maintains program flow
9. Illustrate the use of multiple catch statements with
suitable example.
Multiple catch blocks are used to handle different types of
exceptions separately.
Example:
class MultiCatch
{
public static void main(String args[])
{
try
{
int a = 10 / 0;
int b[] = new int[5];
b[10] = 50;
}
catch(ArithmeticException e)
{
[Link]("Arithmetic Exception");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("Array Index Exception");
}
catch(Exception e)
{
[Link]("General Exception");
}
}
}
Output:
Arithmetic Exception
Advantages:
Handles different exceptions separately
Improves program reliability
10. Illustrate the use of finally with suitable example.
The finally block is always executed whether exception occurs
or not.
Example:
class FinallyDemo
{
public static void main(String args[])
{
try
{
int a = 10 / 0;
}
catch(ArithmeticException e)
{
[Link]("Exception Caught");
}
finally
{
[Link]("Finally Block Executed");
}
}
}
Output:
Exception Caught
Finally Block Executed
Uses:
Closing files
Releasing resources
Cleanup operations
11. List and explain any 5 built-in exceptions of Java.
Exception Description
Occurs during arithmetic
ArithmeticException
errors like divide by zero
Occurs when accessing null
NullPointerException
object
Occurs when invalid array
ArrayIndexOutOfBoundsException
index is used
Occurs when converting
NumberFormatException
invalid string to number
Occurs when class is not
ClassNotFoundException
found
Example:
int a = 10 / 0;
Output:
ArithmeticException
Importance:
Helps identify runtime errors
Improves debugging and reliability