C++ Final Exam Prep

Exceptions &
Polymorphism

Interactive Study Deck • Click or Press Space to Reveal Answers

USE ARROW KEYS TO NAVIGATE

Topic 1: Exception Handling

Standard Library Exceptions

  • std::out_of_range: Thrown by .at()
  • std::length_error: Thrown by .substr()
  • std::underflow_error: Custom stack underflow
  • std::overflow_error: Custom stack overflow

Key Syntax

try {
    // risky code
} 
catch (const std::exception& e) {
    cout << e.what() << endl;
}
Always catch by const reference. Use e.what() to print the error message.
Click to see syntax

Fill in the Blanks

#1 Vector Out of Bounds

vector<int> v = {1,2,3};
cout << v.at(5); // Index 5 doesn't exist
catch (???) {
    cout << ???
}
catch (const out_of_range& e)
cout << e.what()
.at() throws out_of_range when index is invalid.
Click to reveal answer

Fill in the Blanks

#2 String Out of Bounds

string str = "hello";
cout << str.at(10); 
catch (???) {
    cout << ???
}
catch (const out_of_range& e)
cout << e.what()
Strings also throw out_of_range with .at().
Click to reveal answer

Fill in the Blanks

#3 Substr Length Error

string s = "abc";
cout << s.substr(10, 2); // Pos 10 > len 3
catch (???) {
    cout << ???
}
catch (const length_error& e)
cout << e.what()
.substr() throws length_error if pos > string length.
Click to reveal answer

Fill in the Blanks

#4 Empty Vector

vector<int> v;
cout << v.at(0); // Vector is empty
catch (???) {
    cout << ???
}
catch (const out_of_range& e)
cout << e.what()
Accessing any index on an empty vector throws out_of_range.
Click to reveal answer

Fill in the Blanks

#5 Vector Index 3

vector<int> v = {10,20};
cout << v.at(3); // Size is 2, index 3 invalid
catch (???) {
    cout << ???
}
catch (const out_of_range& e)
cout << e.what()
Index 3 is out of bounds for a size-2 vector.
Click to reveal answer

Fill in the Blanks

#6 String Index 5

string s = "CS";
cout << s.at(5); 
catch (???) {
    cout << ???
}
catch (const out_of_range& e)
cout << e.what()
Index 5 is out of bounds for a 2-char string.
Click to reveal answer

Fill in the Blanks

#7 Underflow

if (top == -1) {
    throw underflow_error("Stack underflow");
}
catch (???) {
    cout << e.what() << endl;
}
catch (const underflow_error& e)
Custom exceptions must be caught by their specific type.
Click to reveal answer

Fill in the Blanks

#8 Overflow

if (top == 2) {
    throw overflow_error("Stack overflow");
}
catch (???) {
    cout << e.what() << endl;
}
catch (const overflow_error& e)
Catch the specific exception type thrown.
Click to reveal answer

Polymorphism Quiz

Q1: What is polymorphism?

  • a) Multiple parameters
  • b) Multiple inheritance
  • c) Same interface, different behavior
  • d) Operator overloading
Correct!
Polymorphism allows objects of different classes to be treated through a unified interface.
Click to verify

Polymorphism Quiz

Q2: Keyword for virtual function?

  • a) override
  • b) virtual
  • c) abstract
  • d) dynamic
Correct!
The virtual keyword enables dynamic binding.
Click to verify

Polymorphism Quiz

Q3: What is an abstract class?

  • a) Has at least one pure virtual function
  • b) Can be instantiated directly
  • c) Only static members
  • d) Cannot inherit
Correct!
Abstract classes cannot be instantiated because they have incomplete functionality.
Click to verify

Polymorphism Quiz

Q4: Pure virtual declaration?

  • a) virtual void foo();
  • b) void foo() = 0;
  • c) virtual void foo() = 0;
  • d) void virtual foo();
Correct!
The = 0 syntax makes a virtual function "pure".
Click to verify

Polymorphism Quiz

Q5: Concrete classes?

  • a) No member functions
  • b) Can be instantiated, implement all abstracts
  • c) Must have pure virtual
  • d) Cannot inherit
Correct!
Concrete classes are "real" objects that can be created.
Click to verify

Polymorphism Quiz

Q6: Instantiate abstract class?

  • a) Works normally
  • b) Compile-time error
  • c) Runtime error
  • d) Partially created
Correct!
The compiler prevents instantiation of abstract classes.
Click to verify

Polymorphism Quiz

Q7: Why useful?

  • a) Reduces reusability
  • b) Treat different objects uniformly
  • c) Prevents inheritance
  • d) Only works with abstracts
Correct!
One interface, many underlying types.
Click to verify

Polymorphism Quiz

Q8: Runtime binding mechanism?

  • a) Operator overloading
  • b) Function hiding
  • c) Dynamic binding
  • d) Friend function
Correct!
Dynamic binding decides which function to call at runtime.
Click to verify

Predict the Output

#1 Basic Virtual

class Employee { 
    virtual void work() {...} 
};
class Manager : public Employee { 
    void work() {...} 
};

Employee* e = new Manager();
e->work();
Manager working
Virtual dispatch calls the derived class implementation.
Click to reveal output

Predict the Output

#2 Vector of Pointers

vector<Employee*> staff;
staff.push_back(new Employee());
staff.push_back(new Engineer());
staff.push_back(new Intern());

for (auto e : staff) {
    e->work();
}
Employee working
Engineer working
Intern working
Each pointer calls its own virtual function.
Click to reveal output

Predict the Output

#3 Abstract Instantiation

class Employee { 
    virtual void work() = 0; 
};
class Clerk : public Employee { 
    void work() {...} 
};

int main() {
    Employee e; // ERROR!
    e.work();
}
Compile-time Error
Cannot instantiate an abstract class directly.
Click to reveal result

Predict the Output

#4 Incomplete Override

class PartTime : public Employee { }; 
// Employee has pure virtual work()

int main() {
    PartTime pt; // ERROR!
    pt.work();
}
Compile-time Error
PartTime is still abstract because it didn't implement work().
Click to reveal result

Destructors

#5 Virtual Destructor

class Employee { 
    virtual ~Employee() {...} 
};
class Developer : public Employee { 
    ~Developer() {...} 
};

Employee* e = new Developer();
delete e;
Developer destroyed
Employee destroyed
Derived destructor called first, then base.
Click to reveal output

Static vs Dynamic Binding

#6 Missing Virtual Keyword

class Employee { 
    void work() {...} // NOT virtual 
};
class CEO : public Employee { 
    void work() {...} 
};

Employee* e = new CEO();
e->work();
Employee working
Static binding occurs because 'virtual' is missing on base.
Click to reveal output

Exam Cheat Sheet

Exceptions

  • .at()out_of_range
  • .substr()length_error
  • Catch by const reference
  • Use e.what()

Polymorphism

  • Base function must be virtual
  • Pure virtual = = 0
  • Abstract classes cannot be instantiated
  • Destructors should be virtual
  • No virtual? → Static binding
Review key concepts

Good Luck!

You've got this.

Refresh page to restart quiz
Slide 1 / 26