Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

CPP - Solutions

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

CPP - Solutions

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CPP Solutions

Q1) Attempt any EIGHT of the following. (Out of Ten)

1. Explain any two manipulators.


Ans:
Manipulators are special functions that can be used to modify the input/output characteristics of a
stream. Some Important Manipulators are: -
 setw (val): It is used to set the field width in output operations.
 setfill (c): It is used to fill the character ‘c’ on the output stream.

2. What is an inline function?


Ans:
An inline function is a function whose code is inserted directly at the point of call to reduce function
call overhead.
 Efficiency: Reduces overhead of function calls, useful for small, frequently used functions.
 Code Bloat: Can increase the executable size if used extensively.
 Compiler Choice: The inline keyword is a suggestion, and compilers may choose not to inline
some functions.
 Syntex:

inline int add(int a, int b) {

return a + b;

3. What is a reference variable? What is its major use?


Ans:
A reference variable is an alias for another variable, allowing two variable names to refer to the same
memory location. Some of the major uses:
 They are commonly used to pass objects to functions more efficiently, avoiding copying large
structures or classes. By using references, functions can modify the actual arguments passed to
them.
 References can create a second name for a variable, useful in complex programs where direct
variable access isn't feasible or clear.
 References can create a second name for a variable, useful in complex programs where direct
variable access isn't feasible or clear.

4. What is Abstraction and Encapsulation?


Ans:
 ABSTRACTION: -
 Abstraction is the process or method of gaining the information.
 In abstraction, problems are solved at the design or interface level.
 The objects that help to perform abstraction are encapsulated.
 ENCAPSULATION: -
 While encapsulation is the process or method to contain the information.
 While in encapsulation, problems are solved at the implementation level.
 Whereas the objects that result in encapsulation need not be abstracted.
5. What is compile-time polymorphism?
Ans:
 Compile-time polymorphism is also known as static or early binding polymorphism.
 The function calls are resolved by the compiler.
 It can be implemented through function overloading and operator overloading.
 This method has a much faster execution rate since all the methods that need to be executed are
called during compile time.

6. What is the default argument?


Ans:
 A default argument is a value provided in a function declaration that is automatically assigned by
the compiler if the calling function doesn’t provide a value for the argument. In case any value is
passed, the default value is overridden.
 This allows functions to be called with fewer arguments than are defined in the declaration.

7. What is the use of a scope resolution operator?


Ans:
 Accessing Class Members: It is commonly used to access variables and methods defined within a
class.
 By using the scope resolution operator, you can access class members from outside the class or
even in derived classes.
 The scope resolution operator allows you to access variables, functions, or classes within a specific
namespace.
 Accessing Global Variables: the scope resolution operator lets you access the global variable
from within a function or block.

8. What are the access specifiers used in C++


Ans:
 Public: Members are accessible from anywhere in the program.
 Private: Members are accessible only within the class itself.
 Protected: Members are accessible within the class and its derived classes.

Q5) Write a short note - Any TWO

9. Exception Handling: -
 An exception is an unexpected problem that arises during the execution of a program our program
terminates suddenly with some errors/issues. Exception occurs during the running of the program
(runtime).
 Types of Exception: -
1. Synchronous- the execution of each operation depends on completing the one before it.
2. Asynchronous - the execution of one task isn't dependent on another.

Q2) Attempt any four of the following. (Out of Five) – For 4 Marks Each

10. Explain memory management operators with the help of a suitable example
Ans:
 Memory management is a process of managing computer memory, assigning the memory space to
the programs to improve the overall system performance.
 memory management is primarily handled using operators new and delete, which allow for
dynamic memory allocation and deallocation.
 we use the malloc () or calloc () functions to allocate the memory dynamically at run time.
 Example:

int *p;

p = new int;

11. Explain memory allocation for objects with non-static data members and static data members.
Ans:
 Non-static data members:
 Non-static data members: are part of each instance of a class.
 Allocated as part of each object's individual memory.
 Non-static data members are the variables that are declared in a member specification of a class.

 Static Data Members:


 Static Data Members are shared among all instances of a class and are not part of the memory
allocated for individual objects.
 Static members are stored in a separate single memory location that is shared by all objects of that
class.
 Allocated separately in a single location for the class, shared by all instances.

12. Explain the array of objects in C++ with an example.


Ans:
 Arrays hold multiple objects of the same class type in continuous memory.
 Objects are automatically initialised using the class’s default constructor upon creation.
 Objects within the array are accessible using index notation, similar to basic data type arrays.
 Ideal for managing collections of similar items, such as groups of users or products in a system.
 Example: -

#include<iostream> void putdata() {


using namespace std; cout << id << " " << name <<
class Employee { endl;
int id; }
char name[30]; };
public: int main() {
void getdata() { Employee emp; // Declare an
cout << "Enter Id: "; cin >> id; Employee object
cout << "Enter Name: "; cin >> emp.getdata(); // Input data for the
name; object
} emp.putdata(); // Output data for
the object
return 0;
}

13. When do we make a class virtual base class? Explain it with a suitable example
Ans:
 Virtual base class is used to solve the "diamond problem" of multiple inheritance.
 a particular class is inherited indirectly by a subclass through multiple paths.
When to Use a Virtual Base Class

 Multiple classes inherit from the same base class.


 You want to prevent multiple copies of the base class from appearing in derived classes.
 Example: -
#include <iostream>
using namespace std;
class Person { public: int age; };
class Teacher : virtual public Person {};
class Researcher : virtual public Person {};
class Professor : public Teacher, public Researcher {};
int main() {
Professor prof;
prof.age = 45; // No ambiguity accessing 'age'
cout << "Professor's age: " << prof.age << endl;
return 0;
}

Q3) Attempt any four of the following. (Out of Five) – For 4 Marks Each

14. Write a C++ program to calculate the square and cube of integer numbers by using an inline
function.
Ans:

#include <iostream>
using namespace std;
// Inline functions to calculate square and cube
inline int square(int x) { return x * x; }
inline int cube(int x) { return x * x * x; }
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
cout << "Square of " << num << " is " << square(num) << endl;
cout << "Cube of " << num << " is " << cube(num) << endl;
return 0;
}

15. Write a C++ program to create a class that contains two data members. Write member functions
to accept, display and swap two entered numbers using call by reference.
Ans:
#include <iostream> }
using namespace std; void swapNumbers() {
class NumberSwapper { int temp = num1;
public: num1 = num2;
int num1, num2; num2 = temp;
void accept() { }
cout << "Enter first number: "; };
cin >> num1; int main() {
cout << "Enter second number: "; NumberSwapper ns;
cin >> num2; ns.accept();
} cout << "Before swapping: ";
// Function to display numbers ns.display();
void display() { ns.swapNumbers();
cout << "First number: " << num1 << cout << "After swapping: ";
" Second number: " << num2 << endl; ns.display();
return 0;
}

16. Write a program to perform the addition of two matrices using operator overloading.
Ans:
#include <iostream>
using namespace std; void display() {
for (int i = 0; i < SIZE; i++) {
const int SIZE = 2; // Matrix size for (int j = 0; j < SIZE; j++)
cout << mat[i][j] << " ";
class Matrix { cout << endl;
public: }
int mat[SIZE][SIZE]; }
};
Matrix operator+(const Matrix& b) {
Matrix result; int main() {
for (int i = 0; i < SIZE; i++) Matrix mat1, mat2;
for (int j = 0; j < SIZE; j++) cout << "Input for matrix 1:" << endl;
result.mat[i][j] = mat[i][j] + mat1.input();
b.mat[i][j]; cout << "Input for matrix 2:" << endl;
return result; mat2.input();
} cout << "Result:" << endl;
(mat1 + mat2).display();
void input() { return 0;
for (int i = 0; i < SIZE; i++) }
for (int j = 0; j < SIZE; j++)
cin >> mat[i][j];
}

You might also like