OOP Final Model Paper 1st Sem
OOP Final Model Paper 1st Sem
OOP Final Model Paper 1st Sem
Instruction to Invigilators
The examination paper IS TO BE REMAINED in the examination hall.
Students are to be provided with answer sheets.
Provide extra answer sheets on request.
Students may not remove any part of the examination paper from the examination
hall and must return any used and unused answer sheets with their examination paper.
Instruction to students
The total marks for this paper is 100.
Ensure your student ID is written on all answer sheets during writing time.
Students are to base their answers on Java 11+.
This paper has 6 questions.
State clearly and justify any assumptions made.
Write legibly in blue or black pen.
Mobile phones, tablets, laptops, and other electronic devices, wallets and purses are
prohibited in the examination hall.
Section A:Multiple Choice Questions (20 marks)
This section consists of 20 multiple-choice questions, each worth 1 mark. Please answer all
questions to the best of your ability.
2. Which of the following correctly describes the purpose of the hashCode() method in Java?
a) Generates a random hash value for every object
b) Determines where objects are stored in a hash-based collection
c) Converts objects into unique string identifiers
d) Is used to compare objects by content
class A {
private int x = 10;
}
class B extends A {
public int getX() { return x; }
}
public class Test {
public static void main(String[] args) {
B obj = new B();
System.out.println(obj.getX());
}
}
What is the result?
a) 10
b) Compile-time error
c) 0
d) Runtime exception
class Parent {
void display() { System.out.println("Parent"); }
}
class Child extends Parent {
void display() { System.out.println("Child"); }
}
public class Test {
public static void main(String[] args) {
Parent p = new Child();
p.display();
}
}
a) Parent
b) Child
c) Compile-time error
d) Runtime exception
13. Which statement correctly explains the use of the final keyword with a class?
a) The class cannot be instantiated
b) The class cannot have child classes
c) The class cannot have methods
d) The class cannot define constants
a) Object
b) String
c) Compile-time error
d) Runtime error
class A {
static void method() { System.out.println("Static A"); }
}
class B extends A {
static void method() { System.out.println("Static B"); }
}
public class Test {
public static void main(String[] args) {
A a = new B();
a.method();
}
}
a) Static A
b) Static B
c) Compile-time error
d) Runtime exception
Section B: Short Answer Questions (60 marks)
1.
a. Explain the concept of method overriding and how Java supports runtime polymorphism.
(5 marks)
● Define method overriding and explain how it allows a subclass to provide a specific
implementation of a method from the parent class.
● Discuss how Java uses dynamic method dispatch to achieve runtime polymorphism.
● Provide an example of overriding a method in a subclass and show how
polymorphism works.
● Explain the importance of runtime polymorphism in achieving flexible and
maintainable code.
b. Differentiate between HashMap and TreeMap in Java.
(5 marks)
● Describe how HashMap stores key-value pairs using hashing and discuss its
performance in terms of retrieval.
● Explain how TreeMap stores key-value pairs in a red-black tree, providing sorted
order of keys.
● Provide a performance comparison of both data structures in terms of insertion,
deletion, and lookup.
● Describe use cases where each data structure is preferred based on requirements
like ordering or speed.
c. What is the role of constructors in Java? Explain constructor overloading with an example.
(5 marks)
● Define a constructor and explain its role in object initialization.
● Discuss constructor overloading and how Java supports multiple constructors with
different parameters.
● Provide a Java code example demonstrating constructor overloading.
● Explain how overloading constructors improves flexibility in creating objects with
varying initializations.
d. Explain the significance of the toString() method in Java. How is it used in custom classes?
(5 marks)
● Describe the purpose of the toString() method and how it provides a string
representation of an object.
● Explain the default behavior of toString() and why it is recommended to override it in
custom classes.
● Provide a Java code example where toString() is overridden to display meaningful
information about an object.
● Discuss the impact of overriding toString() on improving debugging and output
readability.
3.
a. How does Java handle memory management? Describe the concept of garbage collection.
(5 marks)
● Explain how Java uses automatic memory management through the Garbage
Collector.
● Discuss how heap memory is allocated and freed for objects during the program's
lifecycle.
● Describe the process of garbage collection, including the mark-and-sweep algorithm.
● Provide an example of how objects become eligible for garbage collection when no
longer referenced.
b. What is an interface in Java? How does it differ from an abstract class?
(5 marks)
● Define an interface and describe its role in specifying methods that must be
implemented by a class.
● Compare interfaces and abstract classes, discussing the differences in method
implementation and inheritance.
● Provide an example of a class implementing multiple interfaces.
● Explain when to use interfaces versus abstract classes in real-world scenarios.
c. What is method overloading in Java? Explain the rules for method overloading.
(5 marks)
● Define method overloading and describe how it allows multiple methods with the
same name but different parameters.
● Explain the rules for method overloading, including differences in method signatures,
number/type of parameters, and return types.
● Provide a Java code example that demonstrates method overloading in a class.
● Discuss how method overloading improves code flexibility and readability.
d. What is a default method in an interface? How does Java 8 introduce default methods?
(5 marks)
● Define a default method in an interface and explain how it provides a method body
within the interface.
● Discuss how Java 8 introduced default methods to allow interfaces to evolve without
breaking existing implementations.
● Provide an example of an interface with a default method and explain how it is used
in implementing classes.
● Describe use cases where default methods are beneficial for backward compatibility.
4.
This section consists of 2 questions. You are required to choose and answer only 1 question from
the available options.
Design a simple E-commerce Shopping Cart System that manages product listings,
customers, and shopping carts. The system should allow customers to add products to their
cart, view the cart, and check out. The system should include:
Requirements:
Provide detailed code snippets for key parts of the system, such as adding products to the
cart and processing payments. Discuss how your design could be scaled to support more
complex features like discounts, product categories, and order history.
2. Create a Bank Account Management System in Java
Requirements:
● Use inheritance to model different types of accounts, ensuring that each account
type has specific behavior (e.g., minimum balance for SavingsAccount, fixed maturity
for FixedDepositAccount).
● Implement methods for deposit, withdrawal, and balance inquiry for each account
type.
● Ensure that error handling is in place to prevent overdrawing or exceeding
withdrawal limits.
● Implement a system to calculate interest on deposits for applicable accounts.
Provide code examples to demonstrate key functionality, such as withdrawals and transfers
between accounts. Discuss how the system could be expanded to include features like
loans, recurring payments, and account statements.