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

CPP Question Paper

The document contains a comprehensive set of C++ practice questions organized into five units, covering topics such as Object-Oriented Programming, dynamic memory management, function overloading, operator overloading, inheritance, polymorphism, exception handling, and templates. Each question requires detailed explanations, code demonstrations, and real-world applications to solidify understanding of C++ concepts. The document encourages collaboration among students through a Discord community for resource sharing and assistance.
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)
12 views5 pages

CPP Question Paper

The document contains a comprehensive set of C++ practice questions organized into five units, covering topics such as Object-Oriented Programming, dynamic memory management, function overloading, operator overloading, inheritance, polymorphism, exception handling, and templates. Each question requires detailed explanations, code demonstrations, and real-world applications to solidify understanding of C++ concepts. The document encourages collaboration among students through a Discord community for resource sharing and assistance.
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

C++ Practice Questions

Join Our Student Community! 🌟


Study together, share resources, and get help from peers at our Discord server!
🔗 Join us at: [Link]
UNIT 1:
Q1. Compare Object-Oriented Programming with Procedural Programming paradigms.
Discuss at least five key advantages of OOP over procedural programming with real-world
scenarios where OOP provides better solutions.

Q2. Explain dynamic memory management in C++ with focus on:

a) The role of new and delete operators


b) Difference between stack and heap allocation
c) Write a program demonstrating dynamic allocation of an array, manipulation of
elements, and proper deallocation to avoid memory leaks

Q3. What is function ambiguity in C++? Explain the function call resolution process and
signature matching with an example. Write code showing:

A case of ambiguous function calls


How the compiler resolves function overloading based on signatures

Q4. Discuss the following OOP concepts with suitable examples:

a) Encapsulation and data hiding


b) The role of access specifiers (private, public, protected)
c) How these principles improve code maintainability and security

UNIT 2:
Q5. Write a complete C++ program to demonstrate function overloading by creating a class
Calculator with overloaded functions add() that can:

Add two integers


Add three integers
Add two floating-point numbers
Add two complex numbers (create a simple Complex class)
Include a main() function to test all variations.

Q6. Explain inline functions in C++:


a) What are inline functions and when should they be used?
b) Advantages and limitations of inline functions
c) Write a program with both inline and regular functions showing the difference in
declaration
d) Discuss scenarios where the compiler might ignore the inline request

Q7. Discuss different ways to pass arguments to functions in C++:

a) Pass by value
b) Pass by reference
c) Pass by pointer
d) Const reference
Write a single program demonstrating all four methods with a function that swaps or
manipulates values, showing which methods affect the original variables.

Q8. What is the difference between function overloading and function overriding? Explain
with examples. Also discuss how default arguments interact with function overloading and
potential issues that may arise.

UNIT 3
Q9. Write a C++ program to demonstrate operator overloading by creating a Matrix class
that overloads the following operators:

+ (addition of two matrices)


== (equality check)
<< (output stream operator for displaying matrix)
Use a 2x2 matrix for simplicity. Include appropriate constructors and a main() function to
demonstrate usage.

Q10. Explain inheritance in C++ covering:

a) Types of inheritance (single, multiple, multilevel, hierarchical, hybrid)


b) The role of access modifiers (public, private, protected) in inheritance
c) The Diamond Problem in multiple inheritance
d) How virtual base classes solve the diamond problem
Write code demonstrating the diamond problem and its solution using virtual inheritance.

Q11. Discuss constructors and destructors in detail:

a) Types of constructors (default, parameterized, copy)


b) Constructor invocation order in inheritance hierarchies
c) Destructor invocation order in inheritance
Write a program with a three-level inheritance hierarchy (Grandparent → Parent →
Child) demonstrating the order of constructor and destructor calls with appropriate
messages.

Q12. Design and implement a Time class using Object-Oriented Programming principles to
handle time conversion between 24-hour and 12-hour formats. Your implementation should
include:

a) Data members to store hours, minutes, and seconds


b) A constructor to initialize time in 24-hour format
c) Member functions to:
Convert and display time in 12-hour format (with AM/PM)
Convert and display time in 24-hour format
Validate time input (handle invalid hours, minutes, seconds)
d) Overload the << operator to display time in both formats
e) Demonstrate proper encapsulation and data validation
Write the complete program with a main() function showing conversion examples in both
directions.

UNIT 4:
Q13. Explain runtime polymorphism (dynamic polymorphism) in C++:

a) How does it differ from compile-time polymorphism?


b) The role of virtual functions
c) Write a program with a base class Shape and derived classes Circle and
Rectangle . Implement a virtual function calculateArea() and demonstrate runtime
polymorphism using base class pointers.

Q14. Discuss virtual functions in detail:

a) What are virtual functions and why are they needed?


b) Difference between pure virtual functions and normal virtual functions
c) Virtual destructor - necessity and implementation
Write code showing:
A class with normal virtual functions
A class with pure virtual functions
Demonstrate the binding behavior with base class pointers

Q15. What are abstract classes in C++?

a) How are they different from regular classes?


b) Can we create objects of abstract classes?
c) Write a program with an abstract class Animal having pure virtual functions
makeSound() and move() . Create derived classes Dog , Bird , and Fish
implementing these functions appropriately.

Q16. Explain the concept of virtual table (vtable) and virtual pointer (vptr):

a) How does C++ implement runtime polymorphism internally?


b) Memory overhead of virtual functions
c) Performance implications
d) When should you NOT use virtual functions?

UNIT 5:
Q17. Explain exception handling in C++ with try-catch-throw mechanism:

a) The process and sequence of error handling


b) Multiple catch blocks and catch-all handler
c) Write a program that:
Defines a custom exception class DivisionByZeroError
Implements a function divide(int a, int b) that throws this exception
Uses try-catch blocks to handle the exception
Demonstrates exception propagation through nested function calls

Q18. Discuss class templates in C++:

a) What are class templates and their advantages?


b) How do class templates differ from function templates?
c) Template instantiation process
d) Write a generic class template Pair<T1, T2> that stores two values of potentially
different types with methods to get and set both values.

Q19. Write a complete C++ program implementing a generic Stack class using templates:

Use an array-based implementation


Include operations: push(), pop(), peek(), isEmpty(), isFull()
Handle overflow and underflow using exceptions
In main(), demonstrate the stack working with:
Integer data type
String data type

Q20. Write a complete C++ program implementing a generic Queue class using templates:

Use an array-based circular queue implementation


Include operations: enqueue(), dequeue(), front(), isEmpty(), isFull()
Handle overflow and underflow appropriately
In main(), demonstrate the queue working with:
Float data type
Character data type
Explain why circular queue is preferred over simple queue implementation.

You might also like