OBJECT ORIENTED PROGRAMMING USING C++
Instructions:
- The marks are indicated in the right-hand margin.
- There are NINE questions in this paper.
- Attempt FIVE questions in all.
- Question No. 1 is compulsory.
What is the default access specifier for class members in C++?
What will be the output of the following code?
#include <iostream> class Test { public: Test() { std::cout << "Constructor"; } ~Test() { std::cout << "Destructor"; } }; int main() { Test t; return 0; }What will be the output of the following code?
#include <iostream> void display(int x) { std::cout << "Int"; } void display(double x) { std::cout << "Double"; } int main() { display(5.5f); }What is the output of the following code snippet?
int arr[] = {1, 2, 3}; int* p = arr; cout << *(p + 1);What is the output of the following code snippet?
string s = "C++"; s += " Rocks"; cout << s;
Which of the following statement is incorrect?
Identify the correct function template syntax.
What will be the output of this code?
#include <iostream> class Demo { public: void show() { std::cout << "Hello"; } }; int main() { Demo d, *ptr = &d; ptr->show(); }What happens when a derived class object is assigned to a base class pointer?
class Base { }; class Derived : public Base { }; int main() { Derived d; Base* b = &d; }What is the output of this code?
#include <iostream> class A { public: ~A() { std::cout << "Destructor "; } }; int main() { try { A a; throw 1; } catch (...) { std::cout << "Caught"; } }
Explain the features of Object-Oriented Programming (OOP). How does OOP differ from the procedural programming paradigm? Also, list the advantages of using OOP in C++.
Differentiate between while, do-while, and for loops in C++. Write a program to print the Fibonacci series using any loop. Hint: Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21….
Write a C++ program using function overloading to calculate the area of a square, rectangle, and circle using three overloaded area() functions. Demonstrate all three in main() and explain how function overloading works.
Write a C++ program using the string class to input two strings, concatenate them, compare them, and find the length of the resulting string.
Differentiate between a struct and a class in C++. Explain with examples how data hiding and default access specifiers differ between the two.
Write a C++ program to define a class Account with data members like accountNumber, holderName, and balance. Include a default constructor to initialize default values, a parameterized constructor to assign specific values, and a destructor to display a message when the object is destroyed. In the main() function, create objects using both constructors. Also, briefly explain the role of constructors and destructors in C++.
Write a program to overload the + operator to add two complex numbers using a class Complex. Also, discuss the rules and limitations of operator overloading.
What is a function template in C++? Write a template function maximum() that returns the larger of two values. Demonstrate its use with integers, floats, and characters.
What is a virtual base class in C++? Explain its role in resolving the diamond problem with an example involving classes A, B, C, and D, where D inherits from both B and C, and B and C inherit from A.
Explain single and multiple inheritance in C++ with suitable examples. Write a program to demonstrate both types of inheritance. Also, discuss how ambiguity is handled in multiple inheritance.
Write a C++ program to demonstrate the use of an abstract class with a pure virtual function. Explain how this supports runtime polymorphism.
What is a void pointer in C++? Explain its use with an example. Also, demonstrate how a void pointer can be typecast to access different data types.
Write a C++ program to demonstrate a pointer to a class and a pointer to an object. Create a class Student and use a pointer to access its member functions. Explain the concept of dynamic object access using pointers.
Explain how arrays and pointers are related in C++. Write a program to process an array using pointers (e.g., calculate the sum of elements). Discuss pointer arithmetic with arrays.
What is exception handling in C++? Explain the purpose and working of the try, throw, and catch keywords with proper syntax. Also, briefly explain the concept of stack unwinding in the context of exceptions.
Write a program to handle division by zero using exception handling. Also, describe how control is transferred during an exception.
