MOD 3 and 4
MOD 3 and 4
2 Marks Questions
REFERENCE: STUDY MATRERIAL
1) What are the different ways to implement method overloading?
2) Compare and contrast overloading and overriding methods.
3) Explain the significance of using Interface.
4) Explain the purpose of abstract classes and how they differ from concrete classes.
5) Discuss the role of abstract methods in abstract classes and how they are implemented in
concrete subclasses.
6) Discuss the role of the @Override annotation in method overriding.
7) Describe the concept of strings in object-oriented programming (OOP) and explain how they are
represented as objects.
8) Explain String Tokenizer with an example.
9) Explain the need for StringEquals function in java with suitable example.
10) Explain the term immutable String in java with suitable example.
11) Can you call one constructor from another if a class has multiple constructors?
12) Explain the syntax of BufferedReader object creation.
13) Can we override a method by using the same method name and arguments but different return
types?
14) Explain the syntax of BufferedReader object creation.
15) Can we override a method by using the same method name and arguments but different return
types?
16) In a class implementing an interface, can we change the value of any variable defined in the
interface?17) Can we make constructors static?
18) Can we use this() and super() both in a same constructor?
19) Distinguish between private and protected access modifier with suitable examples.
20) Explain var-args array.
21) Write down the significance of static keyword.
22) Distinguish the use of extends and implements.
23) What happens if the constructors are defined as private?
24) If two interfaces contain method with same signature and what happen if a class inherits both?
25) Is it possible to override super-class constructor? Why?
26) Explain "Is-A" relationship in inheritance.
27) Explain "HAS-A" relationship in inheritance.
28) What is method overriding and how does it differ from method overloading in Java?
29) When can a method in super class be prevented from being overridden by a method in subclass?
30) Can we declare the static variables and methods in an abstract class?
Here are the 2-mark answers based on your study material reference:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/34
By changing the order of parameters (if they are of different types).
Overriding is runtime polymorphism; same method name, parameters, and return type in
subclass.
4) Explain the purpose of abstract classes and how they differ from concrete classes.
Abstract classes can't be instantiated and may contain abstract methods. Concrete classes are fully
implemented and can be instantiated.
5) Discuss the role of abstract methods in abstract classes and how they are implemented in
concrete subclasses.
Abstract methods define a method without implementation. Subclasses must provide the method body
(implementation).
7) Describe the concept of strings in object-oriented programming (OOP) and explain how they are
represented as objects.
In Java, strings are instances of the `String` class and are immutable. They are objects with built-in
methods for manipulation.
java
9) Explain the need for StringEquals function in java with suitable example.
`equals()` compares contents of strings, unlike `==` which compares references.
java
`String a = "Hello";
String b = new String("Hello");
System.out.println(a.equals(b)); // true
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/34
10) Explain the term immutable String in java with suitable example.
Once created, a `String` object cannot be changed.
java
`String s = "Hello";
s.concat("World");
System.out.println(s); // prints "Hello"
`
11) Can you call one constructor from another if a class has multiple constructors?
Yes, using `this()` to call another constructor from the same class.
java
13) Can we override a method by using the same method name and arguments but different
return types?
No, overriding requires the same return type or a subtype (covariant return type).
java
16) In a class implementing an interface, can we change the value of any variable defined in the
interface?
No. Variables in interfaces are implicitly `public static final` and cannot be changed.
19) Distinguish between private and protected access modifier with suitable examples.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/34
20) Explain var-args array.
Var-args allow passing variable number of arguments:
java
24) If two interfaces contain method with same signature and what happen if a class inherits
both?
The class provides a single implementation, no conflict occurs if method signature is same.
28) What is method overriding and how does it differ from method overloading in Java?
29) When can a method in super class be prevented from being overridden by a method in
subclass?
By declaring it as `final`, `private`, or `static`.
30) Can we declare the static variables and methods in an abstract class?
Yes. Abstract classes can have static members.
Let me know if you want this in a printable format or explained with diagrams/examples.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/34
5 Marks Questions
REFERENCE: STUDY MATRERIAL
1) Demonstrate constructor overloading with a suitable example.
2) Demonstrate the application of the super keyword in conjunction with a method in Java.
3) Design a Java program that illustrates the concept of static variables by showcasing their unique
property of allocating memory only once throughout the execution of the program.
4) Evaluate the role of abstract classes in Java inheritance. Analyse the advantages and limitations of
using abstract classes compared to interfaces in object-oriented design.
5) Discuss the concept of interface inheritance in Java. Explain how interfaces can extend other
interfaces and inherit methods and constants from parent interfaces. Provide examples to illustrate
interface inheritance and discuss its advantages.
6) Implement a java code to check if the String contains 'e' in umbrella.
7) Develop a java code to examine whether interfaces can be extended in Java.
8) Explain yield() and join() with proper code snippet.
9) Demonstrate different types of polymorphism with a suitable example.
10) How does the abstract class differ from the interface? Discuss with program.
11) Write down the differences between this, super, and final keywords.
12) Develop a Java Program to create an abstract class named Shape that contains two integers and
an empty method named printArea(). Provide three classes named Rectangle, Triangle and Circle
such that each one of the classes extends the class Shape. Each one of the classes contains only the
method printArea () that prints the area of the given shape.
13) What happens if any value is not assigned to a final and static variable during declaration?
Explain and provide a solution with a program.
14) Write down the advantages and disadvantages of Object Cloning.
15) Is it possible to use the keyword super within a static method? Give justification for your answer.
Here are the 5-mark answers based on your study material reference:
java
`class Student {
String name;
int age;
Student() {
name = "Unknown";
age = 0;
}
Student(String n) {
name = n;
age = 0;
}
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/34
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Alice");
Student s3 = new Student("Bob", 22);
s1.display();
s2.display();
s3.display();
}
}
`
2) Demonstrate the application of the super keyword in conjunction with a method in Java.
java
`class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
java
`class Counter {
static int count = 0;
Counter() {
count++;
System.out.println("Object count: " + count);
}
}
`static` variable `count` is shared across all objects and memory is allocated only once.
Code reusability.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/34
Allows partial abstraction.
Limitations:
Example:
java
java
`interface A {
void methodA();
}
interface B extends A {
void methodB();
}
Advantages:
Promotes abstraction.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/34
java
java
`interface A {
void methodA();
}
interface B extends A {
void methodB();
}
java
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/34
}
`
java
`class PolymorphismExample {
void show(int a) {
System.out.println("int: " + a);
}
void show(double a) {
System.out.println("double: " + a);
}
}
class A {
void display() {
System.out.println("Class A");
}
}
class B extends A {
void display() {
System.out.println("Class B");
}
}
`
java
interface Flyable {
void fly();
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/34
`this`: Refers to current object.
java
`class Parent {
final void show() {}
}
java
java
`class Example {
static final int VALUE;
static {
VALUE = 100;
}
}
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/34
14) Advantages and Disadvantages of Object Cloning
Advantages:
Disadvantages:
No. `super` refers to instance of the parent class, and static methods are class-level, so no instance
context is available.
Let me know if you want a PDF export or formatted version of these for printing!
10 Marks Questions
REFERENCE: STUDY MATRERIAL
1) Discuss the difference between start() and run() methods by demonstrating through a
Multithreaded Java program. Justify your observations.
2) Design an abstract class shape having two methods calculate_area() and display(). Create
rectangle, triangle, and ellipse classes by inheriting the shape class and overriding the above
methods to suitably implement rectangle, triangle, and ellipse classes.
3) Evaluate the design of a banking application in Java that uses abstract classes and interfaces to
model various account types. Analyse the role of abstract classes in providing common behaviour
among account types and interfaces in defining a contract for account operations.
4) Generate a class where a Company manufactures Vehicles, which could be a Helicopter, a Car, or
a Train depending on the customer’s demand. Each Vehicle instance has a method called move,
which prints on the console the nature of movement of the vehicle. For example, the Helicopter Flies
in Air, the Car Drives on Road and the Train Runs on Track. Write a program that accepts input from
the user on the kind of vehicle the user wants to order, and the system should print out nature of
movement. Implement all Java coding best practices to implement this program.
5) Develop a java code to create Thread by implementing the Runnable interface. Write a java
program to implement the yield( ) method in thread programming.
6) Design a Java program to display the car no., model, colour and price of a car using class and
object concept.(use Scanner class).
7) What is array of objects? Describe the use of Access Modifiers in Java? Draw a table showing the
scope of accessibility? Justify: I. Why we declare main () method as public and static member. II. Why
we generally declare constructor as public member. III. Why there is no destructor in java.
8) Differentiate between abstract class and Interface. The abstract Vegetable class has three
subclasses named Potato, Brinjal and Tomato. Write an application that demonstrates how to
establish this class hierarchy. Declare one instance variable of type String that indicates the colour
of a vegetable. Create and display instances of these objects. Override the toString() method of
Object to return a string with the name of the vegetable and its colour.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/34
Here are the 10-mark answers based on your study material reference:
Example:
java
Justification:
java
void calculate_area() {
area = length * breadth;
}
void display() {
System.out.println("Rectangle Area: " + area);
}
}
void calculate_area() {
area = 0.5 * base * height;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/34
}
void display() {
System.out.println("Triangle Area: " + area);
}
}
void calculate_area() {
area = 3.14 * a * b;
}
void display() {
System.out.println("Ellipse Area: " + area);
}
}
java
`interface Transaction {
void deposit(double amount);
void withdraw(double amount);
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/34
balance -= amount;
else
System.out.println("Insufficient Balance");
}
}
Analysis:
java
`import java.util.Scanner;
switch (type) {
case "helicopter": v = new Helicopter(); break;
case "car": v = new Car(); break;
case "train": v = new Train(); break;
default:
System.out.println("Invalid type");
return;
}
v.move();
sc.close();
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 14/34
}
}
`
java
java
`import java.util.Scanner;
class Car {
String carNo, model, color;
double price;
void inputDetails() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Car No: ");
carNo = sc.nextLine();
System.out.print("Enter Model: ");
model = sc.nextLine();
System.out.print("Enter Colour: ");
color = sc.nextLine();
System.out.print("Enter Price: ");
price = sc.nextDouble();
}
void display() {
System.out.println("Car No: " + carNo + ", Model: " + model + ", Colour: " + color + ", Pric
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/34
7) Array of Objects & Access Modifiers
Array of objects:
java
`class Student {
String name;
Student(String n) { name = n; }
}
java
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/34
public class VegDemo {
public static void main(String[] args) {
Vegetable v1 = new Potato("Brown");
Vegetable v2 = new Brinjal("Purple");
Vegetable v3 = new Tomato("Red");
System.out.println(v1);
System.out.println(v2);
System.out.println(v3);
}
}
`
Let me know if you'd like diagrams, formatted notes, or a PDF for easy study!
Here are the 2 Marks Java Questions with concise answers based on your study material reference:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 17/34
Criteria extends Thread implements Runnable
Inheritance Can't extend other class Can extend other classes
Flexibility Less flexible More flexible (favors OOP)
Thread scheduling determines the execution order of threads based on priority and CPU availability,
managed by the JVM thread scheduler (usually preemptive or time-slicing).
Threads enable concurrent execution, improving performance and responsiveness, especially in I/O-
bound and CPU-bound tasks.
A thread is a lightweight subprocess that runs concurrently within a program, allowing multitasking.
Static method: Can be called without creating an object; often used for utility or helper methods.
Thread-based multitasking allows a single program to perform multiple tasks concurrently using
threads.
Each program runs as a separate process, with its own memory and resources; the OS manages
multiple such processes simultaneously.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 18/34
java
`synchronized(obj) {
// critical section
}
`
java
`class A {
private int x = 5;
protected int y = 10;
}
`
When an exception is unhandled, Java Runtime Environment (JRE) provides a default handler that
prints the exception name, description, and stack trace.
java
The default exception handler of the JVM is invoked, and the program terminates abruptly with an
error message.
The `join()` method waits for a thread to finish its execution before continuing.
java
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 19/34
16) Statement to move a thread to blocked state:
java
`synchronized(obj) {
obj.wait(); // Causes thread to enter blocked/waiting state
}
`
java
`interface A {
void display();
}
interface B extends A {
void show();
}
`
java
`interface A {
void test();
}
interface B extends A {
void print();
}
`
java
`synchronized(obj) {
obj.wait(); // Moves thread to blocked/waiting state
}
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 20/34
5 Marks Questions REFERENCE: STUDY MATRERIAL
1) Can we have multiple catch blocks for a single try block? Justify your answer with an example.
2) Implement a java program to remove all the repeated element from the ArrayList.
3) Differentiate between values() and valuesOf() in JAVA
4) Develop a java program to create a package and a subpackage then access the methods from
both.
5) Consider a scenario where two classes with same name are defined in two different packages.
How to access members of both from a same method. Write a program to support that.
6) Write a java program to create two threads, one for writing odd numbers and another for writing
even numbers up to 150 in two different files.
7) How polymorphism property can be achieved by constructors?
10 Marks Questions REFERENCE: STUDY MATRERIAL
1) Create a user-defined exception i.e. "LowBalanceException". Write the code to throw that
"LowBalanceException" if your savings account balance goes below 500 Rs. and arrange to handle
that "LowBalanceException". Also, write the output.
2) Develop a Java program that demonstrates the implementation of the Runnable interface to
create threads effectively.
3) Explain the concept of multithreading in Java. Provide a code example demonstrating the creation
and execution of multiple threads. Discuss the advantages of using multithreading in Java
applications.
4) Explain the role of interfaces in achieving multiple inheritance in Java. Discuss how interfaces
allow a class to implement multiple interfaces, thereby inheriting behaviors from multiple sources.
Provide examples to demonstrate the use of multiple interfaces in Java.
5) Create a parent class Computer with three child classes Laptop,TabletComputers and SmartPhone
justify with suitable code showing all Laptops ,TabletComputers and SmartPhones belongs to
Computer class.
6) Distinguish between the „throw‟ and „throws‟. Give proper codes. What happens precisely when
the code in the try block throws an exception?
7) Write a Java program that creates two threads. First thread displays Good Morning! in every one
second, and the second thread displays Hello! in every two seconds.
8) A)Explain about Thread Life Cycle.
B) Describe how to set the priority to threads. what are the different ranges?
9) Write a Java program that creates three threads. First thread generates a random positive
number (>1) every 2 second. If the number is even, the second thread prints all even numbers
between 1 and the generated number. If the number is odd, the third thread will print all odd
numbers between 1 and the generated number.
10) Discuss the working and meaning of the “static” modifier with example and explain static
variable and static methods with an example program. Java program is to be compiled first and then
to be interpreted for execution. True or false? Justify your answer.
11) Write a java program to show the use of throw, throws, try, catch and finally. Explain each.
12) What are the types of Exceptions in Java. What is the implication of catching all the exceptions
with the type “Exception”? Write a program to create a user defined exception to check correctness
of mobile number and also handle that using try, catch. For correctness of mobile number consider
length is 10 and that of numeric only. Take user input to validate that.
13) Name the superclass of all exception classes in Java. Build a Java program that simulates a
banking transaction. Use exception handling to ensure that the account balance is never negative
after a withdrawal.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 21/34
Awesome! You've got a solid collection of Java questions here, and it's great to see you're preparing
across all types—2, 5, and 10 marks. Do you want brief answers now for the 5-mark and 10-mark
questions like I did earlier, or would you prefer I create detailed Java programs and explanations for
specific ones? Just let me know which ones you’d like help with first!
semi detailed
Perfect! Here's a semi-detailed explanation for your 5-mark and 10-mark Java questions based on
your study material. I'll cover the first few from each section to keep it manageable—let me know if
you want more after this!
✅ 5 MARKS QUESTIONS
1) Can we have multiple catch blocks for a single try block? Justify your answer with an example.
Yes, Java allows multiple `catch` blocks for a single `try` block to handle different types of exceptions
separately.
Example:
java
java
`import java.util.*;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 22/34
3) Differentiate between `values()` and `valueOf()` in Java
Method Description
`values()` Returns an array of all enum constants.
`valueOf()` Returns enum constant with the specified name (throws exception if invalid).
Example:
java
File structure:
markdown
`myPackage/
├── SubPackage/
└── SubClass.java
└── MainClass.java
`
SubClass.java:
java
`package myPackage.SubPackage;
MainClass.java:
java
`package myPackage;
import myPackage.SubPackage.SubClass;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 23/34
Compile with:
swift
`javac myPackage/SubPackage/SubClass.java
javac myPackage/MainClass.java
`
✅ 10 MARKS QUESTIONS
1) User-defined Exception – LowBalanceException
java
class Account {
private double balance = 1000;
Output:
makefile
java
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 24/34
}
}
Multithreading allows multiple threads to run concurrently, improving CPU utilization and
responsiveness.
Advantages:
Code Example:
java
Would you like me to continue with the rest of the questions? Or would you prefer a downloadable
version (like PDF or Word)?
continue
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 25/34
Great! Continuing with more 5-mark and 10-mark questions in semi-detailed format from your list.
Suppose we have:
`package1.ClassA`
`package2.ClassA`
Code Structure:
`package1/ClassA.java`
java
`package package1;
public class ClassA {
public void show() {
System.out.println("ClassA from package1");
}
}
`
`package2/ClassA.java`
java
`package package2;
public class ClassA {
public void show() {
System.out.println("ClassA from package2");
}
}
`
Main.java
java
`import package1.ClassA;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 26/34
java
`import java.io.*;
java
`class Student {
String name;
int age;
Student(String n) {
name = n;
}
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 27/34
}
`
java
`interface Printable {
void print();
}
interface Showable {
void show();
}
Explanation:
java
`class Computer {
void specs() {
System.out.println("Generic Computer Specs");
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 28/34
}
Difference:
`throw` `throws`
Used to throw exception Declares exceptions that a method might throw
Followed by instance Followed by exception class name
Example:
java
`class ThrowThrowsExample {
static void checkAge(int age) throws ArithmeticException {
if (age < 18)
throw new ArithmeticException("Not eligible to vote");
}
Would you like me to continue with the next set (from question 7 onward in the 10-mark list), or would
you like the output as a document/PDF too?
continue
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 29/34
Perfect! Let’s continue with the next set of 10-mark questions in semi-detailed format:
java
Explanation:
Phases:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 30/34
8B) Thread Priority in Java
java
To hint JVM about order of execution. Not guaranteed but often respected.
java
`import java.util.Random;
EvenPrinter(int limit) {
this.limit = limit;
}
OddPrinter(int limit) {
this.limit = limit;
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 31/34
}
java
`class StaticDemo {
static int count = 0;
True or False?
java
`class ExceptionDemo {
static void divide(int a, int b) throws ArithmeticException {
if (b == 0)
throw new ArithmeticException("Cannot divide by zero");
System.out.println("Result: " + (a / b));
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 32/34
12) Exception types + Mobile Number Checker
Types:
java
`import java.util.Scanner;
try {
validateMobile(number);
System.out.println("Valid mobile number");
} catch (InvalidMobileException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
`
Superclass: `Throwable`
Banking Simulation Example:
java
class BankAccount {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 33/34
private double balance = 1000;
Would you like a compiled document/PDF version of all these questions and answers next?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 34/34