0% found this document useful (0 votes)
22 views

Model Test Paper 4

The document contains a test paper with multiple sections covering topics like destructors, inheritance, polymorphism, exception handling and more. It provides definitions, examples and code snippets for each topic. The test paper has questions on OOP concepts in C++ and includes model answers with code.

Uploaded by

s06162321
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)
22 views

Model Test Paper 4

The document contains a test paper with multiple sections covering topics like destructors, inheritance, polymorphism, exception handling and more. It provides definitions, examples and code snippets for each topic. The test paper has questions on OOP concepts in C++ and includes model answers with code.

Uploaded by

s06162321
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/ 9

MODEL TEST PAPER 4

### 1. What is destructors? Give an example.

A destructor in C++ is a special member function that is invoked automatically when an object goes
out of scope or is explicitly deleted. It is used to perform cleanup operations such as releasing
resources or closing files. Here's an example:

```cpp

#include <iostream>

class MyClass {

public:

// Constructor

MyClass() {

std::cout << "Constructor called" << std::endl;

// Destructor

~MyClass() {

std::cout << "Destructor called" << std::endl;

};

int main() {

MyClass obj; // Constructor called

// Some code inside the main function

return 0; // Destructor called

}
```

### 2. Give the significance of 'Protected' access specifiers.

The 'Protected' access specifier in C++ allows the members of a class to be accessible within the class
itself and its derived classes. It ensures encapsulation while allowing derived classes to access the
base class's members. This is significant for implementing inheritance and building hierarchies of
classes where certain members should be accessible to derived classes.

### 3. How can the ambiguity in multiple inheritance be resolved?

Ambiguity in multiple inheritance arises when a class inherits from more than one class, and there is
a conflict in member names or functions. It can be resolved using scope resolution operator (::) to
specify from which base class the member should be accessed. Additionally, virtual inheritance can
be used to ensure that only one instance of a common base class is present in the object hierarchy.

### 4. What do you mean by Exception Handling? Write a program to show how it is achieved in
C++.

Exception handling in C++ is a mechanism to handle runtime errors or exceptional situations. It


involves the use of `try`, `catch`, and `throw` keywords. Here's a simple example:

```cpp

#include <iostream>

int main() {

try {

// Code that might throw an exception

int result = 10 / 0;

} catch (std::exception& e) {

std::cerr << "Exception caught: " << e.what() << std::endl;

return 0;
}

```

### 5. What is polymorphism? Write a code to show the use of polymorphism.

Polymorphism in C++ allows objects of different types to be treated as objects of a common base
type. It includes both compile-time (function overloading) and runtime (function overriding)
polymorphism. Here's an example demonstrating runtime polymorphism using virtual functions:

```cpp

#include <iostream>

class Shape {

public:

virtual void draw() {

std::cout << "Drawing a shape" << std::endl;

};

class Circle : public Shape {

public:

void draw() override {

std::cout << "Drawing a circle" << std::endl;

};

int main() {

Shape* shapePtr = new Circle();

shapePtr->draw(); // Calls the draw() method of Circle

delete shapePtr;
return 0;

```

### Section-C

#### 10. Write a program in C++ to compute the average marks of 50 students in the class. Take
necessary assumptions.

```cpp

// Code for computing the average marks of 50 students

#include <iostream>

int main() {

// Assume marks are stored in an array

int marks[50];

// Code to input marks from the user

int sum = 0;

for (int i = 0; i < 50; ++i) {

sum += marks[i];

float average = static_cast<float>(sum) / 50;

std::cout << "Average marks: " << average << std::endl;

return 0;

}
```

#### 11. What are constructors? Write sample code to show the working of constructors with
inheritance.

A constructor is a special member function that gets called when an object is created. It is used to
initialize the object. Here's an example demonstrating constructors with inheritance:

```cpp

#include <iostream>

class Base {

public:

Base() {

std::cout << "Base Constructor called" << std::endl;

};

class Derived : public Base {

public:

Derived() {

std::cout << "Derived Constructor called" << std::endl;

};

int main() {

Derived obj; // Outputs: Base Constructor called, Derived Constructor called

return 0;

```
#### 12. Write a program to overload unary + and unary - operators.

```cpp

#include <iostream>

class Number {

private:

int value;

public:

Number(int val) : value(val) {}

Number operator+() {

return Number(value); // Unary plus operator

Number operator-() {

return Number(-value); // Unary minus operator

void display() {

std::cout << "Value: " << value << std::endl;

};

int main() {

Number num(5);

+num; // Calls unary plus operator

num.display(); // Outputs: Value: 5

-num; // Calls unary minus operator


num.display(); // Outputs: Value: -5

return 0;

```

### Section-B

#### 3. What do you mean by Inheritance?

Inheritance is a fundamental concept in object-oriented programming that allows a new class


(derived or child class) to inherit properties and behaviors from an existing class (base or parent
class). It promotes code reuse and establishes a relationship between classes.

#### 4. What is data hiding?

Data hiding is a principle in object-oriented programming that involves encapsulating the


implementation details of a class and exposing only what is necessary for the outside world to
interact with the class. It is achieved by using access specifiers like private, protected, and public.

#### 5. Differentiate between call by value and call by reference.

In call by value, the actual value of the argument is passed to the function, and any changes made to
the parameter inside the function do not affect the original value. In call by reference, the memory
address (reference) of the argument is passed, allowing changes made to the parameter inside the
function to affect the original value.

### Section-B

#### 6. Write a sample code to show the difference between C and C++.

```c

// C code
#include <stdio.h>

int main() {

printf("Hello, World!\n");

return 0;

```

```cpp

// C++ code

#include <iostream>

int main() {

std::cout << "Hello, World!" << std::endl;

return 0;

```

#### 7. Write a code to compute the factorial of a number; use of constructors shall be done.

```cpp

#include <iostream>

class FactorialCalculator {

private:

int number;

long long result;

public:

FactorialCalculator(int n) : number(n), result(1) {}


void computeFactorial() {

for (int i = 1; i <= number; ++i) {

result *= i;

void displayResult() {

std::cout << "Factorial of " << number << " is: " << result << std::endl;

};

int main

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy