TopicTestOOPs
Java Test Paper
Variables, Loops, Conditional Statements, Inheritance, Method
Overloading, Method Overriding, Encapsulation, and Abstraction
Section A: Multiple Choice
(1 mark each)
- What is the correct way to declare a
constant in Java?
a. var constantValue = 5; b. final
int constantValue = 5; c. const
constantValue = 5; d. static
final constantValue = 5;
- Which loop construct is most
appropriate when you want to iterate through a collection of elements,
like an array or a list?
a. for loop b. while loop c. do-while
loop d. foreach loop
- In Java, what does the break statement do?
a. Terminates the program. b.
Exits the loop or switch statement. c. Skips the current iteration of a loop.
d. Skips the next iteration of a loop.
- In the context of inheritance, what
does it mean when we say a subclass "overrides" a method from
its superclass?
a. The subclass provides a new
implementation of the method with the same name and parameters. b. The subclass
inherits the method from the superclass without any changes. c. The subclass
creates a completely new method unrelated to the superclass method. d. The
subclass removes the method from the superclass.
- What is the purpose of the abstract keyword in Java?
a. To make a class immutable. b.
To prevent a class from being extended (subclassed). c. To declare a class as
an abstract class, allowing for abstract methods. d. To hide class members from
other classes.
- In Java, which access modifier is used
to restrict access to only the same class and any subclasses?
a. public b. protected
c. private d. package-private (default)
Section B: True/False (1
mark each)
- True
or False: In Java, variables declared as final can be modified after their
initial assignment.
- True
or False: Method overloading is a feature that allows multiple methods in
the same class with the same name but different parameter lists.
- True
or False: Encapsulation is the practice of bundling data (attributes) and
methods (behaviors) that operate on the data into a single unit.
- True
or False: Inheritance in Java allows a subclass to inherit private members
from its superclass.
- True
or False: An abstract class can be instantiated (used to create objects)
in Java.
Section C: Code-related
Questions (2 marks each)
- Write
a Java class named "Rectangle" that represents a rectangle. It
should have private attributes for length and width, a constructor to
initialize these attributes, and methods to calculate the area and perimeter.
- Create
a class hierarchy for shapes, starting with a base class
"Shape." Define subclasses like "Circle,"
"Rectangle," "Triangle," "Square," and
"Ellipse," and provide methods to calculate the area of each
shape.
- Create
a Java class "BankAccount" with private attributes for the
account balance. Include methods for depositing and withdrawing money.
Demonstrate encapsulation by restricting direct access to the balance.
- Write
a Java program that demonstrates method overloading by creating a class
with multiple methods named "findMax" that can find the maximum
of different data types (e.g., int, double, String).
- Implement
the concept of method overriding by creating a superclass
"Vehicle" with a method named "start," and then create
subclasses like "Car" and "Bike" that provide their
own implementations of the "start" method.
- Design
a class "Book" with private attributes for the title, author,
and price. Create a subclass "EBook" that inherits from
"Book" and adds an attribute for the file size in MB. Implement
a constructor for "EBook" that sets the title, author, price,
and file size.
- Write
a Java program that demonstrates the use of the abstract keyword by
creating an abstract class "Shape" with an abstract method
"calculateArea." Create two subclasses, "Circle" and
"Rectangle," that provide implementations of the
"calculateArea" method.
- Create
a class hierarchy representing vehicles. Start with a base class
"Vehicle" and create subclasses like "Car,"
"Bike," "Boat," "Truck," and
"Airplane." Include attributes and methods relevant to each type
of vehicle.
- Implement
an interface named "Drawable" with a method "draw"
that takes no arguments. Create a class "Circle" that implements
the "Drawable" interface and provides an implementation of the
"draw" method.
- Extend
the shape hierarchy from question 13 by adding a "Trapezoid"
subclass. Provide an implementation for calculating its area.
- Write
a Java program that uses an abstract class "Animal" with
abstract methods for "eat" and "makeSound." Create
subclasses for specific animals like "Dog," "Cat," and
"Cow" that implement these methods.
- Write
a Java class "Student" with private attributes for name and age.
Provide a method to set the age, ensuring it is between 18 and 30.
Demonstrate encapsulation by restricting direct access to the age
attribute.
- Create
a Java program that implements a simple ATM machine. Define classes for
Account, Customer, and ATM. Use encapsulation to protect sensitive account
information.
- Write
a Java program that demonstrates the use of method overriding. Create a
superclass "Animal" with a method "makeSound." Create
subclasses "Dog" and "Cat" that override the
"makeSound" method to produce appropriate sounds.
- Design
a class "Employee" with private attributes for name, employee
ID, and salary. Provide methods to set and get the salary. Create a
subclass "Manager" that inherits from "Employee" and
overrides the method to get the salary. Managers receive a 10% bonus.
- Write
a Java program that demonstrates the use of interfaces for abstraction.
Create an interface "Shape" with a method
"calculateArea." Implement this interface in classes
"Circle" and "Rectangle."
- Create
a Java class hierarchy for vehicles. Define a superclass
"Vehicle" with attributes for make, model, and year. Create
subclasses "Car" and "Bike." Demonstrate method
overriding by overriding a method to display vehicle details.
- Write
a Java program that demonstrates method overloading by creating a class
with overloaded methods to calculate the area of a circle (using radius),
a rectangle (using length and width), and a triangle (using base and
height).
- Design
a class "Bank" that encapsulates a list of bank accounts.
Provide methods to add a new account, get the total balance of all
accounts, and find the account with the highest balance.
- Extend
the shape hierarchy from question 27 by adding a "Square"
subclass. Provide an implementation for calculating its area.
- Write
a Java program that demonstrates the use of an abstract class and method.
Create an abstract class "Fruit" with an abstract method
"calculatePrice." Create subclasses like "Apple" and
"Banana" that override the "calculatePrice" method to
provide different pricing logic.
- Explain
the difference between abstraction and encapsulation in Java. Provide
examples to illustrate each concept.
- Explain
the principles of polymorphism and how it is achieved in Java. Provide
examples to illustrate polymorphic behavior.
Instructions:
·
Answer all questions.
·
You have 240 minutes to complete the test.
·
Be concise and clear in your code-related
answers and add Algorithm for each Answer.
·
Good luck!
Comments
Post a Comment