AOOP MID 2 Question Bank Answers
AOOP MID 2 Question Bank Answers
• Abstract Class: An abstract class in Java is a class that cannot be instantiated and is meant to be
extended by subclasses. It can contain both abstract methods (without implementation) and
concrete methods (with implementation). It is declared using the abstract keyword.
Example: public abstract class Shape { public abstract void draw(); }
• Interface: An interface in Java is a blueprint of a class that contains only abstract methods (prior to
Java 8) and constants. It is used to achieve abstraction and multiple inheritance. Classes implement
interfaces using the implements keyword.
Example: public interface Drawable { void draw(); }
• String Class: The String class in Java represents a sequence of characters. It is immutable, meaning
its value cannot be changed after creation. It belongs to the java.lang package and provides
methods like length(), substring(), and concat().
Example: String str = "Hello";
• Final Class: A final class in Java cannot be extended (i.e., it cannot have subclasses). It is declared
using the final keyword to prevent inheritance.
Example: public final class MyClass { }
Multilevel Inheritance: In multilevel inheritance, a class inherits from a parent class, and another class
inherits from that child class, forming a chain of inheritance. For example, Class A → Class B → Class C.
Java Program:
class Grandparent {
void displayGrandparent() {
void displayParent() {
System.out.println("I am the Parent class");
void displayChild() {
child.displayGrandparent();
child.displayParent();
child.displayChild();
Output:
4. Discuss the Various Levels of Protection Available for Packages & Their Implications
Java provides four levels of access modifiers for packages and their members:
2. Protected: Accessible within the same package and also in subclasses (even in different packages).
Implication: Useful for inheritance but limits access to non-subclasses outside the package.
Example: protected void myMethod() { }
3. Default (Package-Private): No modifier specified; accessible only within the same package.
Implication: Restricts access to classes outside the package, enhancing encapsulation.
Example: class MyClass { }
Hierarchical Inheritance: In hierarchical inheritance, multiple classes inherit from a single parent class,
forming a tree-like structure. For example, Class A (parent) → Class B, Class C (children).
Example:
class Animal {
void eat() {
void bark() {
System.out.println("Dog barks.");
void meow() {
System.out.println("Cat meows.");
dog.eat();
dog.bark();
cat.eat();
cat.meow();
Output:
Dog barks.
Cat meows.
6. List Out Any Three Differences Between Abstract Class and Interface
Methods Can have both abstract and concrete methods Only abstract methods (prior to Java 8)
Variables Can have instance variables Only constants (public static final)
2. Runnable: Thread is ready to run after calling start(); it waits for CPU allocation.
4. Blocked/Waiting: Thread is not running due to waiting for a resource, sleep, or wait().
Used in method body (e.g., throw new Used in method signature (e.g., throws
Usage
IOException()) IOException)
Number of
Throws one exception at a time Can declare multiple exceptions
Exceptions
Exception handling in Java manages runtime errors to prevent program crashes. It ensures the program can
handle unexpected situations gracefully.
Key Concepts:
• Try-Catch: try block contains code that might throw an exception; catch block handles it.
• Throw/Throws: throw creates an exception; throws declares exceptions a method might throw.
Syntax:
try {
} catch (ExceptionType e) {
} finally {
Example:
try {
} catch (ArithmeticException e) {
} finally {
Output:
Error: / by zero
11. Write a Program in Java to Develop User-Defined Exception for 'Divide by Zero' Error
Java Program:
super(message);
if (b == 0) {
}
public static void main(String[] args) {
try {
divide(10, 0);
} catch (DivideByZeroException e) {
Output:
Thread priorities in Java determine the order in which threads are scheduled for execution. They range
from 1 (lowest, Thread.MIN_PRIORITY) to 10 (highest, Thread.MAX_PRIORITY), with 5 being the default
(Thread.NORM_PRIORITY). Higher-priority threads get preference, but the actual scheduling depends on
the JVM and OS. Priorities can be set using setPriority(int priority) and retrieved using getPriority().
13. Write a Program That Executes Two Threads: One Thread Prints Even Numbers and Another Prints
Odd Numbers from 1 to 200
Java Program:
even.start();
odd.start();
Odd: 1
Even: 2
Odd: 3
Even: 4
...
Odd: 199
Even: 200
14. Write a Program That Executes Two Threads: One Displays "Thread1" Every 1000 ms, Another
Displays "Thread2" Every 2000 ms
Java Program:
try {
System.out.println("Thread1");
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread1 interrupted");
try {
System.out.println("Thread2");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread2 interrupted");
t1.start();
t2.start();
Output:
Thread1
Thread1
Thread2
Thread1
Thread1
Thread2
Thread1
Thread2
Thread2
Thread2
Multithreading: Multithreading in Java allows multiple threads to run concurrently within a single program,
sharing the same memory space. Each thread executes a separate task.
Advantages:
3. Responsiveness: Enhances user experience (e.g., UI remains responsive while background tasks
run).
Java Program:
import java.io.FileInputStream;
import java.io.IOException;
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
} catch (IOException e) {
Note: This program assumes an input.txt file exists. It reads bytes from the file and prints them as
characters.
Java Program:
import java.io.FileOutputStream;
import java.io.IOException;
fos.write(data.getBytes());
} catch (IOException e) {
Output: Creates output.txt with the content "Hello, this is a test file!".
18. Write a Program in Java to Demonstrate Use of List. Create LinkedList and Add Months (in String
Form), Display
Java Program:
import java.util.LinkedList;
months.add("January");
months.add("February");
months.add("March");
months.add("April");
months.add("May");
months.add("June");
months.add("July");
months.add("August");
months.add("September");
months.add("October");
months.add("November");
months.add("December");
System.out.println(month);
Output:
January
February
March
April
May
June
July
August
September
October
November
December
19. Write a Program in Java to Demonstrate Use of List. Create ArrayList and Add Weekdays (in String
Form)
Java Program:
import java.util.ArrayList;
weekdays.add("Monday");
weekdays.add("Tuesday");
weekdays.add("Wednesday");
weekdays.add("Thursday");
weekdays.add("Friday");
System.out.println(day);
}
}
Output:
Monday
Tuesday
Wednesday
Thursday
Friday
20. Write a Program in Java to Create a New HashSet, Add Colors (in String Form), and Iterate Using for-
each Loop to Display
Java Program:
import java.util.HashSet;
colors.add("Red");
colors.add("Blue");
colors.add("Green");
colors.add("Yellow");
colors.add("Purple");
System.out.println(color);
}
Output (order may vary due to HashSet):
Red
Blue
Green
Yellow
Purple
21. Write a Java Program to Create a New HashMap, Add 5 Students' Data (Enrolment No and Name),
Retrieve and Display the Student's Name Using Enrolment No
Java Program:
import java.util.HashMap;
students.put("E001", "Alice");
students.put("E002", "Bob");
students.put("E003", "Charlie");
students.put("E004", "Diana");
students.put("E005", "Eve");
Output: