0% found this document useful (0 votes)
160 views15 pages

C++ QUESTION PAPER 2022 (Oct )

bca quation paper

Uploaded by

dhagedatta87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views15 pages

C++ QUESTION PAPER 2022 (Oct )

bca quation paper

Uploaded by

dhagedatta87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

CPP QUESTION PAPER 2022 ( Oct )

Q1) A) Choose the correct option


a) ________ is used to format the data display.
iii) Manipulator

b) Operator overloading is ________.


ii) Giving new meaning to existing operators

c) An object is an instance of _________.


i) Class

d) _______ class supports opening file in write mode


i) ofstream

e) State whether following statements are true or false


1) Constructor should be declared in private section
2) Constructors are invoked automatically when the objects are

iii) False, True

B) Answer the following.


a) What is Abstract class?
Ans :- An abstract class is a class that is designed to be specifically used as a
base class. An abstract class contains at least one pure virtual function.
b) List the different types of constructor.
Ans :-
 Default Constructor: Initializes object attributes with default values.
 Parameterized Constructor: Accepts parameters to initialize attributes
with custom values.
 Copy Constructor: Creates a new object by copying the attributes of
another object.

C) Write the syntax to create a class.


Ans :- class ClassName {
// Member variables (data members)
// Member functions (member methods)
};

D ) List file opening modes.


Ans :-
Here are the commonly used file opening modes:
 ios::in: Open for reading.
 ios::out: Open for writing.
 ios::app: Append to the end of the file.
 ios::ate: Set the initial position at the end of the file.
 ios::trunc: If the file already exists, its content is truncated before
opening.
Q2) Answer the following. (Any Five)

c) Write a program to find area of rectangle using constructor.


Ans :- #include <iostream>
using namespace std;

class Rectangle {
private:
double length;
double width;

public:
Rectangle(double len, double wid) {
length = len;
width = wid;
}

double calculateArea() {
return length * width;
}
};

int main() {
double length, width;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;

Rectangle rectangleInstance(length, width);

double area = rectangleInstance.calculateArea();


cout << "The area of the rectangle is: " << area << endl;
return 0;
}
d) What is static data member? List its characteristics.
Ans :- a static data member is a member of a class that is shared by all
instances (objects) of that class. It is associated with the class itself
rather than with individual objects. Here are the characteristics of
static data members
Characteristics :-
 Shared Among All Objects
 Declared using static Keyword
 Memory Allocation
 Can be Accessed without Object
 Initialization
 Visibility

e) What is Exception? Which Keywords are used? Write its general


syntax.

Ans :- An exception in programming is an abnormal event or error condition


that occurs during the execution of a program and disrupts the normal flow
of instructions.
Keyword :=
the keywords used for exception handling are
 try,
 catch,
 throw,
 throw().

f) Write a program to display factors of a number.


Ans :- #include <iostream>
using namespace std;
void displayFactors(int number) {
cout << "Factors of " << number << ": ";

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


if (number % i == 0) {
cout << i << " ";
}
}

cout << endl;


}

int main() {
int number;

cout << "Enter a positive integer: ";


cin >> number;

if (number <= 0) {
cerr << "Please enter a positive integer." << endl;
return 1; // Exit with an error code
}

displayFactors(number);
return 0;
}
Q3) Answer the following (any five)
e) Write a program to create a class student having roll no, name and
percentage. Write a member function to accept and display details of
students (use Array of objects)
Ans :- #include <iostream>
#include <iomanip>

using namespace std;

class Student {
private:
int rollNo;
string name;
float percentage;

public:
void acceptDetails() {
cout << "Enter Roll Number: ";
cin >> rollNo;
cin.ignore();
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Percentage: ";
cin >> percentage;
}

void displayDetails() const {


cout << setw(15) << "Roll Number: " << rollNo << endl;
cout << setw(15) << "Name: " << name << endl;
cout << setw(15) << "Percentage: " << fixed << setprecision(2) <<
percentage << "%" << endl;
cout << "-----------------------------------------" << endl;
}
};

int main() {
const int numStudents = 3;

Student students[numStudents];

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


cout << "Enter details for Student " << i + 1 << ":" << endl;
students[i].acceptDetails();
}

cout << "Details of Students:" << endl;


for (int i = 0; i < numStudents; ++i) {
students[i].displayDetails();
}

return 0;
}
f) What is Manipulator? Explain syntax and use of any three.
Ans :- a manipulator is an object or function that modifies the
behavior of the input or output stream. Manipulators are part of the
<iomanip> header and are used to format or control the display of
data on the console.
Example :- #include <iostream>
#include <iomanip>
Using namespace std;
int main() {
double pi = 3.1415926535;
cout << fixed << pi << endl; // Outputs: "3.141593"
return 0;
}

g) What are the various file operations performed write a program to read
the contents from the text file.
Ans :- #include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
ifstream inputFile; // Input file stream

inputFile.open("example.txt");
if (!inputFile.is_open()) {
cerr << "Unable to open the file." << endl;
return 1;
}

string line;
while (getline(inputFile, line)) {
cout << line << endl;
}

inputFile.close();

return 0;
}

Q4) Answer the following: (Any five)


a) Explain virtual Base class concept with example
Ans :- the virtual base class concept is used to address the "diamond
problem" that can occur in multiple inheritance. The diamond problem
occurs when a class inherits from two classes that both inherit from a
common base class. This can lead to ambiguities and complications in the
program.
Example :- #include <iostream>

using namespace std;


class A {
public:
void display() const {
cout << "Class A" << endl;
}
};

class B : virtual public A {};

class C : virtual public A {};

class D : public B, public C {};

int main() {
D obj;
obj.display();
return 0;
}
b) What is function overloading? Write a program to overload function
volume to calculate volume of cube and cylinder
Ans :-

#include <iostream>
#include <cmath>

using namespace std;


const double PI = 3.14159265358979323846;

double volume(double side) {


return pow(side, 3);
}

double volume(double radius, double height) {


return PI * pow(radius, 2) * height;
}

int main() {
double cubeSide = 4.5;
cout << "Volume of Cube: " << volume(cubeSide) << endl;

double cylinderRadius = 2.0;


double cylinderHeight = 5.0;
cout << "Volume of Cylinder: " << volume(cylinderRadius, cylinderHeight)
<< endl;

return 0;
}
b) Read the code and answer the questions.

Ans :-
i) The object-oriented feature illustrated here is inheritance.
ii) This explicitly calls the display function from class A on the object
b of class B.
iii) b.display();
iv) iv) Data members x and y are declared in the private section

d) Explain Basic to class type conversion with example.


Ans :-
involves converting one data type into another. There are two types of type
conversions: implicit (automatic) and explicit (manual).
basic to class type conversion is allowed through the use of constructors. If a
constructor with a single parameter is defined in the class, that constructor can
be used for implicit conversion from a basic type to the class type.
Example :- #include <iostream>

class Distance {
private:
int feet;

public:
Distance(int ft) {
feet = ft;
}

void display() {
std::cout << "Feet: " << feet << std::endl;
}
};

int main() {
int feetValue = 5;
Distance distanceObj = feetValue;
distanceObj.display();

return 0;
}
f) Write the significance of the following.
Ans :-
i) New Operator:

 The new operator in C++ is used for dynamic memory allocation during
runtime.
 It allocates memory for a variable or an object on the heap and returns
a pointer to the allocated memory.

ii) Delete Operator:

 The delete operator is used to deallocate memory that was allocated


using new.
 It is essential for preventing memory leaks in a program.
iii) Destructor:

 A destructor in C++ is a special member function that is called when an


object goes out of scope or is explicitly deleted.
 It is used for cleaning up resources held by an object, such as releasing
memory or closing files.
iv) Friend Function:

 A friend function in C++ is a function that is not a member of a class but


is granted access to its private and protected members.
 It can be useful when external functions need to access private
members of a class.
v) Insertion and Extraction Operator (<< and >>):

 These operators are used for formatted input and output in C++.
 The << operator is used for output (insertion) to the stream, and >> is
used for input (extraction) from the stream.
g) Find output and justify
Ans :- 1) First code justification :-
Justification:

 The header <iostream.h> is not standard. In C++, the correct header is


<iostream>.
 The usage of cout should be qualified with std:: since the correct
namespace is std:
2) second code justification :-
Justification:

 Similar to the first snippet, the header <iostream.h> is not standard.


Use <iostream>.

 The usage of cout should be qualified with std:::

===============End================

You might also like