Selected Labs in Software Engineering Midterm Solved
Selected Labs in Software Engineering Midterm Solved
MCQ Questions
1. In OOP, which concept is demonstrated when a subclass provides a specific
implementation of a method already defined in its superclass?
A. Abstraction
B. Inheritance
C. Polymorphism
D. Method Overriding
B. Classes should be designed to be open for extension but closed for modification.
4. Which SOLID principle is primarily aimed at reducing the size of large interfaces?
7. What is the primary difference between an eager Singleton and a lazy Singleton?
A. Eager Singleton is initialized when required, while Lazy Singleton is created at startup.
C. Lazy Singleton is instantiated when the class is loaded, while Eager Singleton is
instantiated upon the first request.
D. Eager Singleton is created at startup, while Lazy Singleton is created upon the
first request.
8. Which method is commonly used in the Singleton pattern to provide the instance of
the class?
A. getObject()
B. newInstance()
C. getInstance()
D. createObject()
9. Which of the following scenarios is best suited for the Factory pattern?
C. When you have a superclass with multiple subclasses, and you need to return
an object based on input.
10. What does the Factory pattern provide over directly instantiating objects using the
new keyword?
A. Reduces the number of classes.
True
False
3. A Singleton class must have a static method that returns the single instance of the class.
True
4. Factory patterns help to reduce coupling between the client and the concrete classes.
True
5. The Singleton pattern is suitable for scenarios where global access to an instance is required.
True
True
7. The Interface Segregation Principle aims to combine several interfaces into one larger
interface.
False
8. The Single Responsibility Principle means a class should have more than one reason to
change.
False
9. Polymorphism allows the same method to behave differently based on the object it is acting
upon.
True
10. Liskov Substitution Principle allows using child classes interchangeably with parent classes
without altering expected behavior.
True
Essay Questions
1. Write Java code to solve the following questions:
a) Design a Factory Pattern in Java to create different types of shapes (Circle, Square, and
Rectangle). Implement an interface called Shape that has an abstract method called draw ,
and use a factory class to create objects. In the main function, use the factory class to create
shape objects and draw them. (5 Marks)
b) Create a Counter class using the Singleton design pattern to count how many times the
Singleton instance has been accessed. Implement the Counter class using the Lazy Singleton
approach. Write the necessary code in the main function to test this class. (5 Marks)
class Counter {
private static Counter instance;
private static int getInstanceCallCount = 0;
private Counter() {
}