Q1. Abstract Class Program Question: Define an abstract class Q5.
Swing UI Program (4 Marks)
Shape with an abstract method area(). Inherit this class into a
concrete class (e.g., Circle or Rectangle) and calculate the Question: Write a Java program using Swing to create a simple
area. form (e.g., accept Employee/Student details) and display the
information on a button click.
Answer:
Answer:
// 1. Define the abstract class
import [Link].*;
abstract class Shape {
import [Link].*;
abstract void area(); // Abstract method (no body)
public class EmpForm implements ActionListener {
}
JFrame f;
// 2. Concrete class extending Shape
JTextField tName, tSal;
class Circle extends Shape {
JLabel l1, l2, lResult;
double radius;
JButton b;
Circle(double r) {
EmpForm() {
[Link] = r;
f = new JFrame("Employee Info");
}
// Creating components
// Must implement the abstract method
l1 = new JLabel("Name:"); [Link](50, 50, 100, 30);
void area() {
tName = new JTextField(); [Link](150, 50, 150,
double result = 3.14 * radius * radius; 30);
[Link]("Area of Circle: " + result); l2 = new JLabel("Salary:"); [Link](50, 100, 100, 30);
} tSal = new JTextField(); [Link](150, 100, 150, 30);
} b = new JButton("Display"); [Link](100, 150, 100,
30);
// 3. Main class to test
[Link](this); // Registering the listener
class TestShape {
lResult = new JLabel(); [Link](50, 200, 300,
public static void main(String args[]) { 30);
// We cannot create an object of Shape, so we use Circle // Adding components to frame
Shape s = new Circle(10); [Link](l1); [Link](tName); [Link](l2); [Link](tSal); [Link](b);
[Link](lResult);
[Link]();
[Link](400, 300);
}
[Link](null);
}
[Link](true);
Q2. What is AWT?
}// Logic to run when button is clicked
AWT stands for Abstract Window Toolkit.
public void actionPerformed(ActionEvent e) {
It is a platform-dependent API used to develop GUI (Graphical
User Interface) or window-based applications in Java. String n = [Link]();
AWT components are considered "heavyweight". String s = [Link]();
Q3. What is the use of an Adapter Class? [Link]("Employee: " + n + ", Salary: " + s);
An Adapter Class provides an empty implementation of all }
methods in an [Link]: If we want to use only one
method of an interface, we can extend its Adapter class public static void main(String[] args) {
instead of implementing the interface directly (which would
force us to implement all methods). new EmpForm();
Example: Extending KeyAdapter to only use keyPressed(). }
Q4. What is Polymorphism? }
Polymorphism means "many forms". It is the ability of an Q6. 'final' Keyword (4 Marks)
object to take on many forms. -Compile-time Polymorphism:
Question: Explain the uses of the final keyword in Java.
Achieved using Method Overloading (same method name,
different parameters).
Answer:The final keyword is used to restrict the user. It can be
used in three contexts: [Link] Variable: The value of a final
Run-time Polymorphism: Achieved using Method Overriding
variable cannot be changed (it becomes a constant). Example:
(same method name, same parameters in parent and child
final int MAX = 100; [Link] Method: A final method cannot be
class).
overridden by the child class. Example: final void run() { ... }
[Link] Class: A final class cannot be inherited (extended). class Student {
Example: final class A { ... }
int id;
Student(int id) {
Q7. User-Defined Exception (4 Marks)
[Link] = id; // '[Link]' is the instance variable
Question: Define a user-defined exception (e.g.,
InvalidAgeException or InvalidNameException). Write a }
program to accept input and throw this exception if the input is
}
invalid.
Example of super:
Answer:
Used to call the parent class constructor or methods.
// 1. Create the custom exception class
class A {
class InvalidAgeException extends Exception {
int i = 10;
InvalidAgeException(String message) {
}
super(message);
class B extends A {
}
void show() {
}// 2. Class to test the exception
[Link](super.i); // Prints 10 (from class A)
class TestException {
}
// Method to validate age
}
static void validate(int age) throws InvalidAgeException {
Q9. File Handling Program (4 Marks)
if (age < 18) {
Question: Write a Java program to copy the content from one
throw new InvalidAgeException("Age is not valid for
file to another (e.g., [Link] to [Link]).
voting!");
Answer:
} else {
import [Link].*;
[Link]("Welcome to vote!");
class FileCopy {
}
public static void main(String args[]) {
}
try {
public static void main(String args[]) {
// Create Reader and Writer objects
try {
FileReader fr = new FileReader("[Link]");
validate(15); // This will throw exception
FileWriter fw = new FileWriter("[Link]");
} catch (InvalidAgeException e) {
int i;
[Link]("Caught Exception: " +
[Link]());
// Read character by character until end of file (-1)
}
while ((i = [Link]()) != -1) {
}
[Link](i); // Write to the new file
}
}
Q8. 'super' vs 'this' Keywords (4 Marks)
[Link]("File copied successfully.");
Question: Explain the use of super and this keywords with
// Always close the files
examples.
[Link]();
Answer:
[Link]();
Refers to the current class object. It is used to
this access current class members
(variables/methods). } catch (Exception e) {
[Link]("Error: " + [Link]());
Refers to the immediate parent class object. It }
super }
is used to access parent class members.
}
Example of this:
Used to resolve naming conflicts between instance variables
and parameters.