Java
Java
i) Explain anonymous class in detail. An anonymous class in Java is a class n) What is repaint method does? The repaint() method in Java is used to
that is defined without a name. It is a way to create a class that extends another request that the component be redrawn. It is a part of the Component class and is
class or implements an interface without declaring a separate named class. used to update the graphical user interface (GUI) of an application.
Syntax:- new [Access Modifier] [ClassName/InterfaceName]() { Functionality:
// Class body } 1. Requests the component to be redrawn.
interface Printable { Printable printable = new Printable() 2. Schedules a paint request to the event dispatch thread.
void print(); } { 3. Calls the paint() or paintComponent() method.
public class Main { public void print() {
public static void main(String[] o) What is exception? Expalin its keyword with example. An exception is
args) { System.out.println("Printing..."); } an event that occurs during the execution of a program, disrupting the normal
}; flow of instructions. Exceptions can be errors, faults, or unexpected events.
printable.print(); } } Types of Exceptions:-
j) Why Java is a platform neutral language? Java is considered a platform- 1. Checked Exceptions: These are exceptions that are checked at compile-time.
neutral language because of its unique architecture and design features, which 2. Unchecked Exceptions: These are exceptions that are not checked at
enable it to run on any device that has a Java Virtual Machine (JVM) installed. 1. compile-time.
Bytecode 2. Java Virtual Machine 3. Platform-independent APIs 3. Runtime Exceptions: These are exceptions that occur during runtime.
4. Memory management 5. No explicit pointers
p) What is collection? Explain Collection framework in details. A collection
k) Define multiple inheritance. Multiple inheritance is a feature of object- is a group of objects that are stored and manipulated together. The Java Collection
oriented programming (OOP) where a subclass can inherit properties and behavior Framework (JCF) provides a set of classes and interfaces for working with
from more than one superclass. collections. Key Components:-
Types:- 1. Multiple Interface Inheritance 2. Multilevel Inheritance 1. Interfaces: Define the contract for a collection.
public interface Printable { java 2. Classes: Implement the interfaces and provide concrete implementations.
void print(); } public class Animal { 3. Algorithms: Provide methods for common operations like sorting and
public interface Shareable { void sound() { searching.
void share(); } System.out.println("Animal import java.util.*; Map<String, Integer> map = new
public class Document implements makes a sound."); } } public class CollectionExample { HashMap<>();
Printable, Shareable { public class Mammal extends Animal public static void main(String[] map.put("Apple", 1);
public void print() { { args) { map.put("Banana", 2);
System.out.println("Printing void eat() { List<String> list = new map.put("Cherry", 3);
document..."); } System.out.println("Mammal ArrayList<>(); System.out.println("List: " + list);
public void share() { eats."); } } list.add("Apple"); System.out.println("Set: " +
System.out.println("Sharing public class Dog extends Mammal { list.add("Banana"); set);
document..."); void sound() { list.add("Cherry"); System.out.println("Map: " +
}} System.out.println("Dog map);
barks."); }} Set<String> set = new }}
l) How to handle events in applet? Explain with example. Event handling in HashSet<>();
applets involves responding to user interactions such as mouse clicks, keyboard set.add("Apple");
input, and window events. set.add("Banana");
Types:- 1. Mouse Events 2. Keyboard Events 3. Window Events 4. set.add("Cherry");
Action Events
Methods:- 1. init() 2. start() 3. stop() 4. destroy()
V) Write a Java program to check Armstrong number or not X) Write a Java program to calculate area of circle, rectangle
import java.util.Scanner; Armstrong number."); } else { Circle:- Rectangle:-
public class ArmstrongNumber { System.out.println(num + " is import java.util.Scanner; import java.util.Scanner;
public static void main(String[ ] not an Armstrong number."); } } public class CircleArea { public class RectangleArea {
args) { public static boolean isArmstrong(int public static void main(String[] public static void main(String[]
Scanner scanner = new num) { args) { args) {
Scanner(System.in); int temp = num; int sum = 0; Scanner scanner = new Scanner scanner = new
System.out.println("Enter a int digit; Scanner(System.in); Scanner(System.in);
number: "); while (temp != 0) { System.out.println("Enter the System.out.println("Enter the
int num = scanner.nextInt(); digit = temp % 10; radius of the circle:"); length of the rectangle:");
scanner.close(); sum += digit * digit * digit; double radius = double length =
if (isArmstrong(num) ) { temp /= 10; } scanner.nextDouble(); scanner.nextDouble();
System.out.println(num + " is return sum == num; } } scanner.close(); System.out.println("Enter the
an double area = Math.PI * radius * width of the rectangle:");
radius; double width =
System.out.println("The area of scanner.nextDouble();
the circle is: " + area); } } scanner.close();
W) Write Java program program to display all perfect number double area = length * width;
import java.util.Scanner; public static boolean isPerfect(int System.out.println("The area of
public class PerfectNumber { num) { the rectangle is: " + area); } }
public static void main(String[] int sum = 0;
args) { for (int i = 1; i < num; i++) { Y) Write a Java program to reverse order element in array
Scanner scanner = new if (num % i == 0) { import java.util.Scanner; public static void reverseArray(int[]
Scanner(System.in); sum += i; } } public class ArrayReverse { array)
System.out.println("Enter a return sum == num; } } public static void main(String[] { int left = 0;
limit: "); args) { int right = array.length - 1;
int limit = scanner.nextInt(); O/P:- Enter a limit: 1000 Scanner scanner = new while (left < right) {
scanner.close(); Perfect numbers up to 1000: Scanner(System.in); int temp = array[left];
System.out.println("Perfect numbers 6 System.out.println("Enter the array[left] = array[right];
up to " + limit + ":"); 28 size of the array:"); array[right] = temp;
for (int i = 1; i <= limit; i++) { 496 int size = scanner.nextInt(); left++;
if (isPerfect(i)) { int[] array = new int[size]; right--; } }
System.out.println(i); } } System.out.println("Enter the public static void printArray(int[]
} elements of the array:"); array) {
for (int i = 0; i < size; i++) { for (int i = 0; i < array.length; i+
array[i] = +) {
scanner.nextInt(); } System.out.print(array[i] + "
scanner.close(); "); }
System.out.println("Original array:"); System.out.println(); } }
printArray(array);
reverseArray(array); O/P:- Enter the size of the array:
System.out.println("Reversed 5
array:"); Enter the elements of the array:
printArray(array); 12345
}