MODULE – 3
1. Explain inheritance with an example. List the different types of inheritance in Java.
(10 Marks)
Concept (Definition – 2–3 lines):
Inheritance is an OOP mechanism in which one class (subclass) acquires the properties and methods of
another class (superclass). It promotes code reusability and establishes an IS-A relationship between
classes.
Code:
class Animal {
void eat() {
[Link]("Animal eats");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
class Test {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}
Types of inheritance in Java:
1. Single Inheritance – one subclass inherits one superclass
2. Multilevel Inheritance – inheritance occurs at multiple levels
3. Hierarchical Inheritance – multiple subclasses inherit one superclass
4. Multiple Inheritance – achieved using interfaces
5. Hybrid Inheritance – combination of inheritance types using interfaces
1
Explanation (2–3 lines):
The class Dog inherits properties of Animal using the extends keyword. This demonstrates code
reuse and the IS-A relationship in Java.
Sample Output (Exam format):
Primitive value: 10
Wrapper object: 10
Unwrapped value: 10
2. Explain the role of super keyword with suitable example. (10 Marks)
Concept (Definition – 2–3 lines):
The super keyword is a reference variable used to refer to the immediate parent class object. It is mainly
used to access parent class variables, methods, and constructors.
Code:
class A {
int x = 10;
}
class B extends A {
int x = 20;
void display() {
[Link](super.x);
}
}
class Test {
public static void main(String[] args) {
B obj = new B();
[Link]();
}
}
Explanation (2–3 lines):
The variable x in the parent class is accessed using super.x . This avoids ambiguity when both parent
and child classes have variables with the same name.
3. Develop a Java program to demonstrate polymorphism using Shape class. (10 Marks)
Code-only Question
2
Complete Code:
class Shape {
void area() {}
void draw() {}
}
class Circle extends Shape {
void area() { [Link]("Area of Circle"); }
void draw() { [Link]("Drawing Circle"); }
}
class Triangle extends Shape {
void area() { [Link]("Area of Triangle"); }
void draw() { [Link]("Drawing Triangle"); }
}
class Square extends Shape {
void area() { [Link]("Area of Square"); }
void draw() { [Link]("Drawing Square"); }
}
class Test {
public static void main(String[] args) {
Shape s;
s = new Circle(); [Link](); [Link]();
s = new Triangle(); [Link](); [Link]();
s = new Square(); [Link](); [Link]();
}
}
Explanation (2–3 lines):
A single reference variable Shape is used to refer to different subclass objects. Method calls are resolved
at runtime, demonstrating runtime polymorphism.
4. Define dynamic method dispatch with example. (10 Marks)
Concept (Definition – 2–3 lines):
Dynamic Method Dispatch is a process in which the method call is resolved at runtime based on the object
being referred to, not the reference type. It is achieved using method overriding.
Code:
class A {
void show() {
[Link]("Class A");
}
}
3
class B extends A {
void show() {
[Link]("Class B");
}
}
class Test {
public static void main(String[] args) {
A obj = new B();
[Link]();
}
}
Explanation (2–3 lines):
Although the reference type is A , the overridden method of class B is executed. This decision is made at
runtime, hence dynamic method dispatch.
5. Compare and contrast between method overloading and method overriding. (10
Marks)
Feature Method Overloading Method Overriding
Definition Same method name, different parameters Same method name and parameters
Polymorphism Compile-time Runtime
Inheritance Not required Required
Return type Can be different Must be same
Binding Early binding Late binding
Performance Faster Slightly slower
6. Explain different uses of final keyword with examples. (10 Marks)
Concept (Definition – 2–3 lines):
The final keyword in Java is used to restrict modification. It can be applied to variables, methods, and
classes to prevent change, overriding, or inheritance.
Uses of final keyword:
1. Final Variable – value cannot be changed once assigned
2. Final Method – prevents method overriding
4
3. Final Class – prevents inheritance
4. Security – avoids unauthorized modification
5. Performance – improves performance by avoiding dynamic binding
6. Consistency – ensures fixed behavior of classes and methods
Code:
class Sample {
final int MAX = 100; // final variable
final void display() { // final method
[Link]("Final method");
}
}
final class Demo { // final class
public static void main(String[] args) {
Sample s = new Sample();
[Link]();
[Link]("MAX = " + [Link]);
}
}
Explanation (2–3 lines):
A final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be
inherited. This ensures program safety, consistency, and controlled behavior.
7. Define interface. Explain with suitable example. (10 Marks)
Concept (Definition – 2–3 lines):
An interface is a collection of abstract methods that defines a contract for classes. It supports multiple
inheritance and abstraction.
Code:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
[Link]("Bark");
5
}
}
class Test {
public static void main(String[] args) {
Animal a = new Dog();
[Link]();
}
}
Explanation (2–3 lines):
The class Dog implements the interface Animal and provides implementation for the sound()
method. The main() method creates an object and invokes the interface method.
8. Program to implement Resizable interface. (10 Marks)
Code-only Question
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
class Rectangle implements Resizable {
int width, height;
public void resizeWidth(int w) { width = w; }
public void resizeHeight(int h) { height = h; }
}
class Test {
public static void main(String[] args) {
Rectangle r = new Rectangle();
[Link](10);
[Link](20);
[Link]("Width=" + [Link] + ", Height=" + [Link]);
}
}
Explanation (2–3 lines):
The class Rectangle implements the Resizable interface and provides implementations for both
methods. The main() method creates an object and resizes it, demonstrating interface usage.
6
MODULE – 4
9. Define package and explain creation and import of package. (10 Marks)
Concept (Definition – 2–3 lines):
A package is a namespace that groups related classes and interfaces. It helps in organizing code and
avoiding naming conflicts.
Code:
// File: [Link]
package mypack;
class Demo {
void show() {
[Link]("Inside package");
}
}
import [Link];
class Test {
public static void main(String[] args) {
Demo d = new Demo();
[Link]();
}
}
Explanation (2–3 lines):
The package is created using the package keyword. The class inside the package is accessed in another
program using the import statement.
10. Program to create and import balance package. (10 Marks)
Code-only Question
package balance;
public class Account {
public void displayBalance() {
[Link]("Balance: 5000");
}
}
7
import [Link];
class Test {
public static void main(String[] args) {
Account a = new Account();
[Link]();
}
}
Explanation (2–3 lines):
The Account class is placed inside the balance package and accessed in another program using the
import statement.
11. Define exception and explain exception handling mechanism. (10 Marks)
Concept (Definition – 2–3 lines):
An exception is an abnormal event that disrupts the normal flow of program execution. Java provides a
structured exception-handling mechanism.
Code:
class Test {
public static void main(String[] args) {
try {
int a = 10 / 0;
}
catch (ArithmeticException e) {
[Link](e);
}
finally {
[Link]("End of program");
}
}
}
Explanation (2–3 lines):
The exception is caught using the catch block, preventing program termination. The finally block
executes regardless of exception occurrence.
12. Custom exception for banking application. (10 Marks)
Code-only Question
8
class MinBalanceException extends Exception {
MinBalanceException(String msg) {
super(msg);
}
}
class Bank {
public static void main(String[] args) throws MinBalanceException {
int balance = 1000;
int withdraw = 2000;
if (withdraw > balance)
throw new MinBalanceException("Insufficient Balance");
}
}
Explanation (2–3 lines):
A user-defined exception is created and thrown when withdrawal exceeds balance, ensuring safe banking
operations.
MODULE – 5
13. Explain methods of creating threads in Java. (10 Marks)
Concept (Definition – 2–3 lines):
A thread is a lightweight process that allows concurrent execution of tasks. Java supports multithreading to
improve performance and resource utilization.
Code:
class MyThread extends Thread {
public void run() {
[Link]("Thread running");
}
}
class Test {
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}
Explanation (2–3 lines):
The thread is created by extending the Thread class and overriding the run() method. The start()
method invokes the thread execution.
9
14. Explain thread synchronization with example. (10 Marks)
Concept (Definition – 2–3 lines):
Synchronization controls access to shared resources when multiple threads are executing concurrently. It
prevents data inconsistency and race conditions.
Code:
class Table {
synchronized void print(int n) {
for(int i=1;i<=5;i++)
[Link](n*i);
}
}
class MyThread extends Thread {
Table t;
MyThread(Table t) {
this.t = t;
}
public void run() {
[Link](5);
}
}
class Test {
public static void main(String[] args) {
Table obj = new Table();
MyThread t1 = new MyThread(obj);
MyThread t2 = new MyThread(obj);
[Link]();
[Link]();
}
}
Explanation (2–3 lines):
The synchronized method allows only one thread to access it at a time. Both threads share the same
object, ensuring controlled execution.
15. Explain wrapper classes with example. (10 Marks)
Concept (Definition – 2–3 lines):
Wrapper classes are predefined classes in Java that convert primitive data types into objects. They are
mainly used in collections, frameworks, and when object representation is required.
10
Code:
class Test {
public static void main(String[] args) {
int a = 10; // primitive type
Integer obj = [Link](a); // wrapping
[Link]("Primitive value: " + a);
[Link]("Wrapper object: " + obj);
int b = [Link](); // unwrapping
[Link]("Unwrapped value: " + b);
}
}
Explanation (2–3 lines):
The primitive value a is converted into an Integer object using a wrapper class. The wrapped object is
later converted back to a primitive, showing the use of wrapper classes.
16. Explain autoboxing and unboxing. (10 Marks)
Concept (Definition – 2–3 lines):
Autoboxing is the automatic conversion of primitive data types into their corresponding wrapper classes.
Unboxing is the reverse process where wrapper objects are converted back into primitives.
Code:
class Test {
public static void main(String[] args) {
int x = 20; // primitive
Integer obj = x; // autoboxing
[Link]("Autoboxed value: " + obj);
int y = obj; // unboxing
[Link]("Unboxed value: " + y);
Integer sum = obj + y; // autoboxing & unboxing in expression
[Link]("Sum: " + sum);
}
}
11
Explanation (2–3 lines):
The primitive value is automatically converted into an Integer object without explicit method calls. Java
internally performs autoboxing and unboxing during assignments and expressions.
Sample Output (Exam format):
Autoboxed value: 20
Unboxed value: 20
Sum: 40
12