0% found this document useful (0 votes)
14 views9 pages

CA402 CPP Answers

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)
14 views9 pages

CA402 CPP Answers

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

CA-402: Object Oriented Concepts Through C++

CA-402: Object Oriented Concepts Through C++ (2019 Pattern)

Semester - IV

Answers

This document contains detailed answers to the CA-402 exam paper based on the 2019 pattern. Each

section addresses core concepts with code examples and explanations to assist in exam preparation.

Q1. Attempt any eight of the following:

(a) Define Encapsulation:

Encapsulation is one of the fundamental concepts of object-oriented programming (OOP). It refers to the

bundling of data and methods that operate on that data into a single unit or class. It restricts direct access to

some of the object's components, helping prevent accidental modification. Data hiding is achieved through

access specifiers (private, protected, public).

(b) What is inline function?

An inline function is a function that is expanded in line when it is invoked. When the inline function is called,

the compiler replaces the function call with the function code itself, saving the overhead of a function call.

Declared using the inline keyword.

(c) What is Class? Give its syntax.

A class is a user-defined data type that binds data and functions together. It acts as a blueprint for creating

objects.

Syntax:

class ClassName {

private:

// data members

public:

// member functions

};

Page 1
CA-402: Object Oriented Concepts Through C++

(d) What is Constructor?

A constructor is a special member function that is automatically invoked when an object of the class is

created. It has the same name as the class and no return type.

(e) Explain any two use of Scope Resolution operator.

1. To define a function outside the class.

2. To access a global variable when a local variable has the same name.

(f) List the operators which cannot be overloaded?

- Scope resolution operator (::)

- Sizeof operator (sizeof)

- Member access operator (.)

- Ternary conditional operator (? :)

(g) What is virtual function?

A virtual function is a member function in a base class that can be overridden in a derived class. It is declared

using the keyword virtual and supports runtime polymorphism.

(h) What is stream?

Stream refers to the flow of data. In C++, input and output are performed in the form of streams of bytes

using classes like istream, ostream, etc.

(i) Explain get() and put() function.

- get(): Used to read a single character from input.

- put(): Used to write a single character to output.

(j) What are the access specifiers used in C++?

- Private

- Public

- Protected

Q2. Attempt any four of the following:

Page 2
CA-402: Object Oriented Concepts Through C++

(a) Explain Memory management operators in detail.

C++ provides dynamic memory management operators:

- new: Allocates memory.

- delete: Deallocates memory.

Example:

int *ptr = new int;

*ptr = 5;

delete ptr;

(b) What is friend function? Explain with example.

A friend function can access private and protected members of a class. It is not a member of the class but is

allowed access.

class A {

private:

int x;

public:

A() { x = 10; }

friend void show(A);

};

void show(A a) {

cout << a.x;

(c) Explain array of object in C++ with example.

An array of objects is a collection of objects of the same class.

class Student {

public:

int roll;

void getdata() { cin >> roll; }

void display() { cout << roll; }

};

Page 3
CA-402: Object Oriented Concepts Through C++

Student s[5];

(d) Explain the different types of Inheritance with example.

Types:

- Single Inheritance

- Multiple Inheritance

- Multilevel Inheritance

- Hierarchical Inheritance

- Hybrid Inheritance

Example (Single Inheritance):

class A {

public:

void display() { cout << "Class A"; }

};

class B : public A {};

(e) List different types of constructor. Explain any one with example.

Types:

- Default Constructor

- Parameterized Constructor

- Copy Constructor

Default Constructor Example:

class A {

public:

A() { cout << "Default Constructor"; }

};

Q3. Attempt any four of the following:

(a) C++ program to find maximum of two integers using inline function.

Page 4
CA-402: Object Oriented Concepts Through C++

#include<iostream>

using namespace std;

inline int max(int a, int b) {

return (a > b) ? a : b;

int main() {

int x, y;

cout << "Enter two numbers: ";

cin >> x >> y;

cout << "Maximum is: " << max(x, y);

return 0;

(b) C++ program to find area of circle and area of triangle using function overloading.

#include<iostream>

using namespace std;

float area(float r) { return 3.14 * r * r; }

float area(float b, float h) { return 0.5 * b * h; }

int main() {

cout << "Area of Circle: " << area(5) << endl;

cout << "Area of Triangle: " << area(4, 6);

return 0;

(c) C++ program to swap two values using call by reference.

#include<iostream>

using namespace std;

void swap(int &a, int &b) {

int temp = a;

a = b;

b = temp;

Page 5
CA-402: Object Oriented Concepts Through C++

int main() {

int x = 5, y = 10;

swap(x, y);

cout << "x = " << x << ", y = " << y;

return 0;

(d) C++ program to create class Employee, accept and display info.

#include<iostream>

using namespace std;

class Employee {

public:

int id;

string name;

float salary;

void getdata() {

cout << "Enter ID, Name, Salary: ";

cin >> id >> name >> salary;

void display() {

cout << id << " " << name << " " << salary << endl;

};

int main() {

Employee e1, e2;

e1.getdata();

e2.getdata();

if(e1.salary > e2.salary)

e1.display();

else

e2.display();

return 0;

Page 6
CA-402: Object Oriented Concepts Through C++

(e) C++ Program to overload unary - operator.

#include<iostream>

using namespace std;

class Test {

int a;

public:

void getdata() { cin >> a; }

void operator-() { a = -a; }

void display() { cout << a; }

};

int main() {

Test t;

t.getdata();

-t;

t.display();

return 0;

Q4. Attempt any four of the following:

(a) What is Template? Explain its types in detail.

Templates allow writing generic programs. Types:

- Function Templates

- Class Templates

They enable code reusability for different data types.

(b) Explain file opening methods in C++ with syntax.

File can be opened using:

- ifstream for reading

Page 7
CA-402: Object Oriented Concepts Through C++

- ofstream for writing

- fstream for both

Syntax:

ifstream fin("file.txt");

ofstream fout("file.txt");

fstream file("file.txt", ios::in | ios::out);

(c) Explain the types of polymorphism in detail.

Types:

- Compile-time Polymorphism (Function Overloading, Operator Overloading)

- Runtime Polymorphism (Virtual Functions)

(d) C++ Program to calculate Simple Interest.

#include<iostream>

using namespace std;

int main() {

float p, r, t, si;

cout << "Enter Principal, Rate, and Time: ";

cin >> p >> r >> t;

si = (p * r * t) / 100;

cout << "Simple Interest: " << si;

return 0;

(e) What is the output of the given program?

#include<iostream>

using namespace std;

void stat() {

int m = 0;

static int n = 0;

n++;

Page 8
CA-402: Object Oriented Concepts Through C++

cout << m << " " << n << "

";

int main() {

stat();

stat();

stat();

return 0;

Output:

01

02

03

Conclusion:

This document provides answers and C++ program examples for CA-402. It is designed to help students

understand concepts and prepare effectively for exams.

Page 9

You might also like