Java OOPs Mini Test Paper (40 Marks)
Q1: MCQs & Fill in the Blanks (1 mark each × 10 = 10 marks)
MCQs:
1. Which of the following is used for encapsulation in Java?
A. abstract B. private (Correct) C. static D. protected
2. Method overloading happens at:
A. Compile-time (Correct) B. Runtime C. Both D. Object creation
3. A constructor:
A. Must have a return type
B. Can be called manually
C. Runs automatically when an object is created (Correct)
D. Is always static
4. Which concept means "one name, many forms"?
A. Abstraction B. Polymorphism (Correct) C. Encapsulation D. Inheritance
5. Which keyword is used to define an abstract method?
A. interface B. super C. abstract (Correct) D. final
Fill in the Blanks:
6. In method overriding, the method must have the same name and parameters.
7. A constructor has the same name as the class.
8. In encapsulation, variables are made private to hide data.
9. Method overloading allows methods with the same name but different parameters.
10. new keyword is used to create a object in Java.
Q2: Scenario-Based Question (10 marks)
Java OOPs Mini Test Paper (40 Marks)
Scenario: You are building a food ordering system. There's a base class Food that contains a method
prepare().
Two dishes: Pizza and Burger extend this class and prepare their food differently.
a) OOP Concept: Method Overriding (Runtime Polymorphism)
b) Method header: void prepare()
c) Reason for parent reference: To implement runtime polymorphism and call the overridden method at
runtime.
d) Code:
class Food {
void prepare() {
[Link]("Preparing food...");
class Pizza extends Food {
void prepare() {
[Link]("Preparing pizza with cheese");
Q3: Ratta-Based Short Questions (10 marks)
a) Abstraction: Hiding internal details and showing only essential features. Achieved using abstract classes.
b) Features of constructor:
- Same name as class
- No return type
- Automatically called when object is created
c) Difference:
Java OOPs Mini Test Paper (40 Marks)
Encapsulation: hides data using private and getter/setters.
Abstraction: hides logic using abstract classes.
d) Method Overloading: Same method name, different parameters in the same class.
Example:
int add(int a, int b)
int add(int a, int b, int c)
Q4: Coding Question (10 marks)
Create a class Student with:
- private variables: name, marks
- Constructor to initialize both
- display() method to show student info
- In main(), create and display one student
Code:
class Student {
private String name;
private int marks;
Student(String n, int m) {
name = n;
marks = m;
void display() {
[Link]("Name: " + name);
[Link]("Marks: " + marks);
}
Java OOPs Mini Test Paper (40 Marks)
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Sharjeel", 90);
[Link]();