0% found this document useful (0 votes)
14 views5 pages

Selected Labs in Software Engineering Midterm Solved

The document consists of a midterm exam covering multiple-choice questions, true/false questions, and essay questions related to Object-Oriented Programming (OOP) concepts and design patterns. Key topics include SOLID principles, Singleton and Factory patterns, and their implementations in Java. The essay section requires students to write code for a Factory Pattern and a Singleton Counter class.

Uploaded by

israahussein737
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Selected Labs in Software Engineering Midterm Solved

The document consists of a midterm exam covering multiple-choice questions, true/false questions, and essay questions related to Object-Oriented Programming (OOP) concepts and design patterns. Key topics include SOLID principles, Singleton and Factory patterns, and their implementations in Java. The essay section requires students to write code for a Factory Pattern and a Singleton Counter class.

Uploaded by

israahussein737
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Midterm Exam

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

2. Which of the following is an example of violating the Liskov Substitution Principle?

A. Using inheritance to extend a class.

B. Creating an abstract class that is not implemented by any subclass.

C. Subclass modifying the behavior of a method such that it cannot be used in


place of its parent class.

D. Implementing an interface with an abstract class.

3. Which of the following best describes the Dependency Inversion Principle?

A. High-level modules should not depend on low-level modules. Both should


depend on abstractions.

B. Classes should be designed to be open for extension but closed for modification.

C. Subtypes must be substitutable for their base types.

D. One class should have only one reason to change.

4. Which SOLID principle is primarily aimed at reducing the size of large interfaces?

A. Single Responsibility Principle

B. Interface Segregation Principle

C. Liskov Substitution Principle

D. Dependency Inversion Principle

5. Which of the following is an example of violating the Open/Closed Principle?

A. Using inheritance to extend a class's functionality.

B. Modifying existing classes to add new features.

C. Implementing an interface to add new behaviors.

D. Using abstract classes for common functionality.

6. Which of the following statements about the Singleton pattern is true?

A. A Singleton class can have multiple instances if required.

B. A Singleton class uses a private constructor to restrict instantiation.

C. Singleton is used for creating multiple instances of a class.

D. The Singleton pattern cannot be used in multi-threaded applications.

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.

B. Lazy Singleton consumes more memory than Eager Singleton.

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?

A. When a class needs to ensure a single instance.

B. When a class needs to be extended.

C. When you have a superclass with multiple subclasses, and you need to return
an object based on input.

D. When you want to copy objects.

10. What does the Factory pattern provide over directly instantiating objects using the
new keyword?
A. Reduces the number of classes.

B. Improves code readability.

C. Encapsulates object creation and increases flexibility.

D. Reduces memory usage.

True and False Questions


1. Lazy initialization in Singleton is more memory efficient but may cause delays during object
creation.

True

2. Factory patterns eliminate the need to use inheritance between classes.

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

6. An abstract class can implement multiple interfaces in Java.

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)

// Step 1: Define the abstract Shape class


abstract class Shape {
// Abstract method to be implemented by concrete classes
abstract void draw();
}

// Step 2: Implement concrete classes for Circle, Square, and Rectangle


class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a Circle.");
}
}

class Square extends Shape {


@Override
void draw() {
System.out.println("Drawing a Square.");
}
}

class Rectangle extends Shape {


@Override
void draw() {
System.out.println("Drawing a Rectangle.");
}
}
// Step 3: Create the Factory class
class ShapeFactory {
// Factory method to create shape objects
public static Shape getShape(String shapeType) {
switch (shapeType.toLowerCase()) {
case "circle":
return new Circle();
case "square":
return new Square();
case "rectangle":
return new Rectangle();
default:
System.out.println("Invalid shape type!");
return null;
}
}
}

// Step 4: Test the Factory Pattern in the Main class


public class FactoryPatternWithStaticMethod {
public static void main(String[] args) {

// Create and draw a Circle


Shape circle = ShapeFactory.getShape("circle");
if (circle != null) circle.draw();

// Create and draw a Square


Shape square = ShapeFactory.getShape("square");
if (square != null) square.draw();

// Create and draw a Rectangle


Shape rectangle = ShapeFactory.getShape("rectangle");
if (rectangle != null) rectangle.draw();
}
}

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() {
}

public static Counter getInstance() {


getInstanceCallCount++;
if (instance == null) {
instance = new Counter();
}
return instance;
}

public int getInstanceCallCount() {


return getInstanceCallCount;
}
}

public class CounterSingletonTest {


public static void main(String[] args) {
Counter counter1 = Counter.getInstance();
System.out.println("getInstance called: " +
counter1.getInstanceCallCount() + " time(s)");

Counter counter2 = Counter.getInstance();


System.out.println("getInstance called: " +
counter2.getInstanceCallCount() + " time(s)");

Counter counter3 = Counter.getInstance();


System.out.println("getInstance called: " +
counter3.getInstanceCallCount() + " time(s)");
}
}

You might also like