0% found this document useful (0 votes)
23 views8 pages

C

The document contains 8 questions asking to write C++ programs that demonstrate various programming concepts like operators, loops, functions, classes, objects, constructors and overloading. Each question provides sample code to write a program that implements the specified concept using examples like calculating arithmetic operations, finding the maximum of numbers, calculating area and volume using classes etc.

Uploaded by

Swarna Sajjanar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
23 views8 pages

C

The document contains 8 questions asking to write C++ programs that demonstrate various programming concepts like operators, loops, functions, classes, objects, constructors and overloading. Each question provides sample code to write a program that implements the specified concept using examples like calculating arithmetic operations, finding the maximum of numbers, calculating area and volume using classes etc.

Uploaded by

Swarna Sajjanar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Final Practical Exam questions C++

1. Write a C++ program to demonstrate any five different types return 0;


of Operators using suitable examples.
}
// Online C++ compiler to run C++ program online

#include <iostream>

using namespace std;

int main() {

int num1,num2,num3;

cout<<"Enter num1 & num2: ";

cin>>num1>>num2;

cout<<"Arithematic operators: "<<endl;

cout<<"num3=num1+num2: "<<(num3=num1+num2)<<endl;

cout<<"num3=num1-num2: "<<(num3=num1-num2)<<endl;

cout<<"num3=num1*num2: "<< (num3=num1*num2)<<endl;

cout<<"num3=num1/num2: "<<(num3=num1/num2)<<endl;

cout<<"num3=num1 modulo num2:


"<<(num3=num1%num2)<<endl;

cout<<endl;

cout<<"Assignment Operators: "<<endl;

cout<<"num3=num1: "<<(num3=num1)<<endl;

cout<<"num1+=num2: "<<(num1+=num2)<<endl;

cout<<"num1-=num2: "<< (num1*=num2)<<endl;

cout<<"num1*=num2: "<<(num1*=num2)<<endl; 2. Write a C++ program to accept any number between 1-7 from
user and print respective day, i.e, 1- Monday 2- Tuesday etc.
cout<<"num1 &= num2: "<<(num1 &= num2)<<endl;
#include<iostream>
cout<<endl;
using namespace std;
cout<<"Relational Operators";
int main() {
cout << "num1 == num2 is " << (num1 == num2) << endl;
int day;
cout << "num1 > num2 is " << (num1 > num2) << endl;
cout<<"Enter a number between 1 and 7: ";
cout << "num1 >= num2 is " << (num1 >= num2) << endl;
cin>>day;
cout << "num1 < num2 is " << (num1 < num2) << endl;
switch(day) {
cout << "num1 <= num2 is " << (num1 <= num2) << endl;
case 1: cout<<"Monday"; break;
cout << "num1 != num2 is " << (num1 != num2) << endl;
case 2: cout<<"Tuesday"; break;
cout<<endl;
case 3: cout<<"Wednesday"; break;
cout<<"Logicnum2l Operators";
case 4: cout<<"Thursday"; break;
cout << "num1 && num2 is " << (num1 && num2) <<
endl; case 5: cout<<"Friday"; break;

cout << "num1 ! num2 is " << (num1 > num2) << endl; case 6: cout<<"Saturday"; break;

cout << "!num2 is " << (!num2) << endl; case 7: cout<<"Sunday"; break;
default: cout<<"Invalid input! Please enter number between #include<iostream>
1 and 7.";
using namespace std;
}
int main() {
return 0;
int i = 1;
}
cout << "This is a while loop:" << endl;

while(i <= 5) {

cout << "i = " << i << endl;

i++;
3. Write a C++ program to find the sum and average of the array
}
elements. Accept the array
cout << "\nThis is a for loop:" << endl;
elements from the user.
for(int j = 1; j <= 5; j++) {
#include<iostream>
cout << "j = " << j << endl;
using namespace std;
}
int main() {
return 0;
int n, i, sum = 0;
}
float avg;

cout<<"Enter the size of the array: ";

cin>>n;

int arr[n];

for(i=0; i<n; i++) {

cout<<"Enter element "<<i+1<<": ";

cin>>arr[i];

sum += arr[i];

}
5. Write a C++ program to find a maximum of two numbers using
avg = float(sum)/n; a user defined function.
cout<<"Sum of array elements: "<<sum<<endl; #include<iostream>
cout<<"Average of array elements: "<<avg<<endl; using namespace std;
return 0; int max(int a, int b) {
} return (a > b) ? a : b;

int main() {

int num1, num2;

cout<<"Enter two numbers: ";

cin>>num1>>num2;

int maximum = max(num1, num2);

cout<<"Maximum is: "<<maximum<<endl;

return 0;
4. Write a C++ program to demonstrate the while loop and for }
loop. Use suitable examples.
int main() {

Room room1;

6. Write a C++ program to add numbers using function room1.length = 42.5;


overloading. First function will add two integer numbers and the room1.breadth = 30.8;
second function will add three float numbers.
room1.height = 19.2;
#include<iostream>
cout << "Area of Room = " << room1.calculateArea() << endl;
using namespace std;
cout << "Volume of Room = " << room1.calculateVolume() <<
int add(int a, int b) { endl;
return a + b; return 0;
} }
float add(float a, float b, float c) {

return a + b + c;

int main() { 8. Write a C++ program to demonstrate constructor overloading.

int num1 = 5, num2 = 10; #include<iostream>

float num3 = 2.5f, num4 = 3.5f, num5 = 4.5f; using namespace std;

cout<<"Sum of two integers: "<<add(num1, num2)<<endl; class Rectangle {

cout<<"Sum of three floats: "<<add(num3, num4, public:


num5)<<endl; int length;
return 0; int breadth;
} Rectangle() {

length = 5;

breadth = 7;

}
7. Write a C++ program to find the area and volume of the room
using classes and objects. Rectangle(int l, int b) {

#include<iostream> length = l;

using namespace std; breadth = b;

class Room { }

public: int getArea() {

double length; return length * breadth;

double breadth; }

double height; };

double calculateArea() { int main() {

return length * breadth; Rectangle rect1;

} Rectangle rect2(10, 12);

double calculateVolume() { cout<<"Area of rect1: "<<rect1.getArea()<<endl;

return length * breadth * height; cout<<"Area of rect2: "<<rect2.getArea()<<endl;

}; return 0;
} Department(string c, string d, string p, string dept, string hod)
: College(c, d, p) {

departmentName = dept;

hodName = hod;
9. Create a class named “College” with following attributes:
}
a. College name
void displayDepartmentDetails() {
b. Director name
displayCollegeDetails();
c. Principal name
cout << "Director Name: " << directorName << endl;
Create a derived class named “Department” which inherits
“College” with following attributes: cout << "Department Name: " << departmentName << endl;

a. Department name cout << "HOD Name: " << hodName << endl;

b. HOD name }

Create objects of Department class and demonstrate single };


inheritance. Also demonstrate the use of private, public and
int main() {
protected specifiers.
Department dept("ABC College", "Mr. Ramesh", "Mr. Suresh",
#include<iostream>
"EXTC", "Mr. Mukesh");
using namespace std;
dept.displayDepartmentDetails();
class College {
return 0;
private:
}
string collegeName;

protected:

string directorName;

public:

string principalName;
10. Create a class named “College” with following attributes:
College(string c, string d, string p) {
a. College name
collegeName = c;
b. Director name
directorName = d;
c. Principal name
principalName = p;
Create a derived class named “Department” which inherits
}
“College” with following
void displayCollegeDetails() {
attributes:
cout << "College Name: " << collegeName << endl;
a. Department name
cout << "Principal Name: " << principalName << endl;
b. HOD name
}
Create a “Student” class and inherit “Department” with following
}; features.

class Department : public College { a. Student name

private: b. Student class

string departmentName; c. Student roll no.

public: Create objects of Student class and demonstrate multilevel


inheritance.
string hodName;

#include<iostream>
using namespace std; a. College name

class College { b. Director name

protected: c. Principal name

string collegeName; Create a derived class named “Department” which inherits


“College” with following
public:
attributes:
College(string c) {
a. Department name
collegeName = c;
b. HOD name
}
Create a “Student” class and inherit “Department” with following
};
features.
class Department : public College {
a. Student name
protected:
b. Student class
string departmentName;
c. Student roll no.
public:
Create objects of Student class and demonstrate multilevel
Department(string c, string d) : College(c) { inheritance.

departmentName = d; #include<iostream>

} using namespace std;

}; class College {

class Student : public Department { protected:

private: string collegeName;

string studentName; string directorName;

public: string principalName;

Student(string c, string d, string s) : Department(c, d) { public:

studentName = s; College(string c, string d, string p) {

} collegeName = c;

void display() { directorName = d;

cout<<"College Name: "<<collegeName<<endl; principalName = p;

cout<<"Department Name: "<<departmentName<<endl; }

cout<<"Student Name: "<<studentName<<endl; };

} class Department : public College {

}; protected:

int main() { string departmentName;

Student stud("ABC College", "EXTC", "Anuj "); string hodName;

stud.display(); public:

return 0; Department(string c, string d, string p, string dept, string hod)


: College(c, d, p) {
}
departmentName = dept;

hodName = hod;

}
11. Create a class named “College” with following attributes: };
class Student : public Department { Create a derived class named “Department” which inherits
“College” with following attributes:
private:
a. Department name
string studentName;
b. HOD name
string studentClass;
Create a “Student” class and inherit “Department” with following
int rollNo;
features.
public:
a. Student name
Student(string c, string d, string p, string dept, string hod, string
b. Student class
s, string cls, int r) : Department(c, d, p, dept, hod) {
c. Student roll no.
studentName = s;
Create a class named “Library” with following features.
studentClass = cls;
a. Library_card_no
rollNo = r;
b. No_of_books_issued
}
Make the “Student” class inherit “Department” and “Library”.
void display() {
Create objects of “Student” class and demonstrate Multiple
cout<<"College Name: "<<collegeName<<endl; inheritance.

cout<<"Director Name: "<<directorName<<endl; #include<iostream>

cout<<"Principal Name: "<<principalName<<endl; using namespace std;

cout<<"Department Name: "<<departmentName<<endl; class College {

cout<<"HOD Name: "<<hodName<<endl; protected:

cout<<"Student Name: "<<studentName<<endl; string collegeName;

cout<<"Student Class: "<<studentClass<<endl; string directorName;

cout<<"Roll No.: "<<rollNo<<endl; string principalName;

} public:

}; College(string c, string d, string p) {

int main() { collegeName = c;

Student stud("ABC College", "Mr. Ramesh", "Mr. Suresh", directorName = d;


"EXTC", "Mr. Mukesh", "Anuj", "12th", 25);
principalName = p;
stud.display();
}
return 0;
};
}
class Department : public College {

protected:

string departmentName;

string hodName;

public:

Department(string c, string d, string p, string dept, string hod)


: College(c, d, p) {
12. Create a class named “College” with following attributes:
departmentName = dept;
a. College name
hodName = hod;
b. Director name
}
c. Principal name
};
class Library { stud.display();

protected: return 0;

int libraryCardNo; }

int noOfBooksIssued;

public:

Library(int cardNo, int booksIssued) {

libraryCardNo = cardNo;

noOfBooksIssued = booksIssued;

};
13. Create a base class called shape to store two double type
class Student : public Department, public Library { values that could be used to compute the area of rectangular
private: and/or triangle. Two separate classes are derived from the base
class for the same. The input for the computation and the output
string studentName; is to be displayed in the base class. Make the function to display
as a virtual function and redefine this function in the derived class
string studentClass;
to suit their requirement.
int rollNo;
#include<iostream>
public:
using namespace std;
Student(string c, string d, string p, string dept, string hod, int
class Shape {
cardNo, int booksIssued, string s, string cls, int r) : Department(c,
d, p, dept, hod), Library(cardNo, booksIssued) { protected:
studentName = s; double a, b;
studentClass = cls; public:
rollNo = r; void getData(double x, double y) {
} a = x; b = y;
void display() { }
cout<<"College Name: "<<collegeName<<endl; virtual void displayArea() { }
cout<<"Director Name: "<<directorName<<endl; };
cout<<"Principal Name: "<<principalName<<endl; class Rectangle : public Shape {
cout<<"Department Name: "<<departmentName<<endl; public:
cout<<"HOD Name: "<<hodName<<endl; void displayArea() {
cout<<"Library Card No.: "<<libraryCardNo<<endl; cout << "Area of Rectangle is " << a * b << endl;
cout<<"No. of Books Issued: "<<noOfBooksIssued<<endl; }
cout<<"Student Name: "<<studentName<<endl; };
cout<<"Student Class: "<<studentClass<<endl; class Triangle : public Shape {
cout<<"Roll No.: "<<rollNo<<endl; public:
} void displayArea() {
}; cout << "Area of Triangle is " << 0.5 * a * b << endl;
int main() { }
Student stud("ABC College", "Mr. Suresh", "Mr. ramesh", };
"EXTC", "Mr. Mukesh", 12345, 2, "Anuj", "12th", 25);
int main() {
Rectangle rect;

Triangle tri;

rect.getData(5.0, 3.0);

tri.getData(5.0, 3.0);

rect.displayArea();

tri.displayArea();

return 0;

14. Write a C++ program to add two complex numbers using


operator overloaded by a friend function. Create a class Time
which contains: Hours, Minutes, Seconds. Write a C++ program
using operator overloading for the following:

1. = = : To check whether two Time are the same or not.

2. >> : To accept the time.

3. << : To display the time.

You might also like