OOP MCQs - Exceptions, Generics, SOLID and Design Patterns (Set 6)

OOP MCQs - Exceptions, Generics, SOLID and Design Patterns (Set 6) - CodingHumans

This final set moves from language mechanics to design. It covers exception handling and stack unwinding, finally blocks, checked versus unchecked exceptions, C++ templates and Java generics with type erasure, the five SOLID principles through short scenarios, and the Singleton, Factory Method and Observer patterns. It suits university exams, UGC NET preparation and the design rounds of software engineering interviews.

How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 18 questions.

Object Oriented Programming MCQ series: Set 1 | Set 2 | Set 3 | Set 4 | Set 5 | Set 6

Topics covered in this set

  • try, catch, finally and stack unwinding
  • Checked vs unchecked exceptions
  • C++ templates and specialisation
  • Java generics, wildcards and type erasure
  • SOLID principles with examples
  • Singleton, Factory Method and Observer patterns

Object Oriented Programming MCQs with answers and explanations

Q1. What is printed by this C++ program?

#include <iostream>
using namespace std;
struct G {
    char c;
    G(char x) : c(x) {}
    ~G() { cout << c; }
};
void f() { G a('a'); throw 1; }
int main() {
    try { G b('b'); f(); }
    catch (int) { cout << "C"; }
}

a) baC

b) abC

c) Cba

d) aC

Q2. What does this Java program print?

public class Main {
    static int f() {
        try { return 1; }
        finally { System.out.print("F"); }
    }
    static int g() {
        try { throw new RuntimeException(); }
        catch (RuntimeException e) { return 2; }
        finally { return 3; }
    }
    public static void main(String[] args) {
        System.out.print(f());
        System.out.print(g());
    }
}

a) 1F3

b) F12

c) F13

d) 1F2

Q3. What is printed by this C++ program?

#include <iostream>
using namespace std;
struct E {};
struct F : E {};
int main() {
    try { throw F(); }
    catch (E&) { cout << "E"; }
    catch (F&) { cout << "F"; }
}

a) E

b) F

c) EF

d) terminate called

Q4. What is printed by this C++ program?

#include <iostream>
using namespace std;
template <class T> int sz(T) { return 1; }
template <> int sz<char>(char) { return 2; }
int main() {
    cout << sz(5) << sz('x') << sz(2.0);
}

a) 111

b) 122

c) Compilation error

d) 121

Q5. What does this Java program print?

import java.util.*;
public class Main {
    public static void main(String[] args) {
        System.out.print(new ArrayList<String>().getClass()
                         == new ArrayList<Integer>().getClass());
    }
}

a) false

b) true

c) Compilation error

d) ClassCastException

Q6. What is printed by this C++ program?

#include <iostream>
using namespace std;
template <typename T, int N>
T sum(T a) { return a * N; }
int main() {
    cout << sum<int, 3>(4) << " " << sum<double, 2>(1.5);
}

a) 12 3

b) 7 3.5

c) 12 3.0

d) Compilation error: non-type template parameter not allowed

Q7. Which of the following is a checked exception in Java?

a) NullPointerException

b) ArithmeticException

c) IOException

d) ArrayIndexOutOfBoundsException

Q8. In C++, why should exceptions usually be caught by reference, as in catch (const std::exception& e)?

a) Catching by value is a compile error

b) References make the exception rethrowable only once

c) Catching by reference disables stack unwinding

d) It avoids copying and prevents slicing, so virtual functions like what() use the derived class's version

Q9. An Invoice class computes totals, writes itself to a database and also generates its PDF layout. Which SOLID principle is most directly violated?

a) Single Responsibility Principle

b) Liskov Substitution Principle

c) Interface Segregation Principle

d) Dependency Inversion Principle

Q10. An AreaCalculator uses a switch on a shape-type string, and every new shape requires editing that switch. Which principle does this design break?

a) Single Responsibility Principle

b) Open/Closed Principle

c) Liskov Substitution Principle

d) Interface Segregation Principle

Q11. Square extends Rectangle, and Square.setWidth() also changes the height. A function that sets width 5 and height 4 on a Rectangle and expects area 20 gets 16 for a Square. Which principle is violated?

a) Open/Closed Principle

b) Dependency Inversion Principle

c) Liskov Substitution Principle

d) Single Responsibility Principle

Q12. A Machine interface declares print(), scan() and fax(), and a basic printer class must implement scan() and fax() by throwing UnsupportedOperationException. Which principle does this break?

a) Single Responsibility Principle

b) Open/Closed Principle

c) Liskov Substitution Principle

d) Interface Segregation Principle

Q13. OrderService creates a MySqlOrderRepository directly with new inside its constructor. Which change best applies the Dependency Inversion Principle?

a) Make MySqlOrderRepository a subclass of OrderService

b) Have OrderService depend on an OrderRepository interface and receive the implementation through its constructor

c) Make every method of MySqlOrderRepository static

d) Copy the database code into OrderService

Q14. Which Java technique gives a lazily created, thread-safe singleton without writing any explicit synchronisation?

a) A public static non-final field assigned in main()

b) Checking if (instance == null) without synchronisation

c) A private static nested holder class whose static final field holds the instance

d) Creating a new instance in every getInstance() call

Q15. What is the intent of the Factory Method design pattern?

a) To ensure a class has only one instance

b) To notify many objects when one object changes state

c) To add responsibilities to an object dynamically by wrapping it

d) To define an interface for creating an object while letting subclasses decide which concrete class to instantiate

Q16. A chart in a spreadsheet must refresh automatically whenever its data cells change, and several charts may watch the same cells. Which design pattern fits best?

a) Observer

b) Singleton

c) Factory Method

d) Adapter

Q17. Which of the four Java statements S1 to S4 compiles?

S1: List<Object> a = new ArrayList<String>();
S2: List<? extends Number> b = new ArrayList<Integer>();
S3: List<int> c = new ArrayList<>();
S4: List<Number> d = new ArrayList<Integer>();

a) S1

b) S2

c) S3

d) S4

Q18. What is a key difference between C++ templates and Java generics?

a) Java generics can take primitive types as type arguments, but C++ templates cannot

b) C++ templates are checked only at run time

c) Java generics generate a separate class file for every type argument

d) C++ generates separate code for each set of template arguments at compile time, while Java compiles one erased version that uses casts

Quick revision notes

  • Stack unwinding destroys local objects in reverse order before the handler runs.
  • Order catch blocks from most derived to most general; catch by reference.
  • A return inside finally overrides any earlier return or exception.
  • Java checked exceptions must be caught or declared; RuntimeException subclasses need not be.
  • Java generics are invariant and erased at run time; use wildcards for flexibility.
  • SOLID: Single responsibility, Open/closed, Liskov substitution, Interface segregation, Dependency inversion.
  • Singleton = one instance; Factory Method = subclasses choose the product; Observer = one-to-many notification.

Frequently asked questions

What is the difference between checked and unchecked exceptions in Java?

Checked exceptions, such as IOException, are verified by the compiler and must be caught or declared with throws. Unchecked exceptions extend RuntimeException, usually signal programming errors and need not be declared.

What are the SOLID principles?

SOLID is a set of five object oriented design guidelines: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation and Dependency Inversion. Together they aim to make code easier to extend, test and maintain.

Why is Singleton sometimes called an anti-pattern?

A singleton acts like global state, which hides dependencies and makes unit testing and parallel code harder. Many teams prefer creating one instance and passing it in through dependency injection.

Comments

Answered 0/0Correct 0