Download Question Paper (Link Below)
Question Paper Solution
1. A. Write down a C++ program that demonstrates how to use conditional operators to make decisions and perform conditional assignments according to the question. In the program, three integer variables are defined: num1. num2 and num3. The conditional operator is used to find and store the maximum value between num1, num2 and num3. Next, use the conditional operator to determine whether num3 is even or odd.
The question is based on - Conditional Operator in C++
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3;
cout << "Enter three integers: ";
cin >> num1 >> num2 >> num3;
// Find maximum value
int maxVal = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
cout << "Maximum value: " << maxVal << endl;
// Check if num3 is even or odd
if (num3 % 2 == 0)
cout << "num3 is even." << endl;
else
cout << "num3 is odd." << endl;
return 0;
}Output
Enter three integers: 9 8 4
Maximum value: 9
num3 is even.B. Create C++ program to find the sum of all even numbers between 1 and a user- defined positive integer. The program should also ensure that the user enters a positive number.
#include <iostream>
using namespace std;
int main() {
int limit, sum = 0;
cout << "Enter a positive integer: ";
cin >> limit;
if (limit < 1) {
cout << "Please enter a positive number." << endl;
return 1;
}
for (int i = 2; i <= limit; i += 2) {
sum += i;
}
cout << "Sum of all even numbers between 1 and " << limit << " is: " << sum << endl;
return 0;
}Output
Enter a positive integer: 7
Sum of all even numbers between 1 and 7 is: 122. A. Write a C++ program to create an array where each element, starting from the third element onward, is the sum of the two preceding elements.
#include <iostream>
using namespace std;
int main() {
int size = 8;
int arr[size] = {10, 15}; // Initializing the first two elements
// Fill the rest of the array
for (int i = 2; i < size; i++) {
arr[i] = arr[i - 1] + arr[i - 2];
}
// Display the array
cout << "Array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}Ouput
Array: 10 15 25 40 65 105 170 275B. Using C++, develop a program that demonstrates how structures are used to represent and store student information. Create a structure named "Student" and define its members as follows:
a) rollNumber (an integer}: to store the student's roll number.
b) name (a string): to store the student's name.
c) marks (an array of floating-point numbers): to store the student's marks in three subjects and calculate the average marks.
#include <iostream>
#include <string>
using namespace std;
// Define the structure to represent a student
struct Student {
int rollNumber; // Member to store roll number
string name; // Member to store name
float marks[3]; // Member to store marks in three subjects
// Function to calculate average marks
float averageMarks() {
float sum = 0.0;
for (int i = 0; i < 3; i++) {
sum += marks[i];
}
return sum / 3; // Return average
}
};
int main() {
Student student; // Create an instance of Student
// Input student information
cout << "Enter roll number: ";
cin >> student.rollNumber;
cin.ignore(); // Ignore newline character left in the input buffer
cout << "Enter name: ";
getline(cin, student.name);
cout << "Enter marks for three subjects (separated by space): ";
for (int i = 0; i < 3; i++) {
cin >> student.marks[i];
}
// Display student information
cout << "\nStudent Information:" << endl;
cout << "Roll Number: " << student.rollNumber << endl;
cout << "Name: " << student.name << endl;
cout << "Marks: ";
for (int i = 0; i < 3; i++) {
cout << student.marks[i] << " ";
}
cout << endl;
// Calculate and display average marks
float avg = student.averageMarks();
cout << "Average Marks: " << avg << endl;
return 0;
}Output
Enter roll number: 39
Enter name: Archana Singh
Enter marks for three subjects (separated by space): 100 100 100
Student Information:
Roll Number: 39
Name: Archana Singh
Marks: 100 100 100
Average Marks: 1003. A. Define a "Book" class with the following private members:
a) title (a string): to store the title of the book.
b) author (a string): to store the author's name.
c) publicationYear (an integer): to store the year the book was published.
d) isbn (a string): to store the International Standard Book Number (ISBN) of the book.
In the "Book" class, create a constructor that takes parameters for initializing its title, author, publication year, and ISBN. The private members in the constructor are set based on the parameters provided. Using the constructor, create a book object and initialize it. Display the details of the book object, including its title, author, publication year, and ISBN.
#include <iostream>
using namespace std;
class Book {
private:
string title;
string author;
int publicationYear;
string isbn;
public:
// Constructor to initialize book details
Book(string t, string a, int pYear, string i) : title(t), author(a), publicationYear(pYear), isbn(i) {}
// Function to display book details
void display() {
cout << "\nTitle: " << title << endl;
cout << "Author: " << author << endl;
cout << "Publication Year: " << publicationYear << endl;
cout << "ISBN: " << isbn << endl;
}
};
int main() {
string title, author, isbn;
int year;
// Input details
cout << "Enter title of the book: ";
getline(cin, title);
cout << "Enter author of the book: ";
getline(cin, author);
cout << "Enter publication year: ";
cin >> year;
cout << "Enter ISBN: ";
cin >> isbn;
// Create a Book object
Book book(title, author, year, isbn);
// Display book details
book.display();
return 0;
}Output
Enter title of the book: Archana AI
Enter author of the book: Archana Singh
Enter publication year: 2024
Enter ISBN: 978-3-16-148410-0
Title: Archana AI
Author: Archana Singh
Publication Year: 2024
ISBN: 978-3-16-148410-0B. Create a C++ program with a hierarchy of vehicle classes. Start with the "Vehicle" base class, including private members for "make" (string) and "year" (integer), a constructor for initialization, and a displaylnfo() method. Then, derive the "Car" class from the "Vehicle class" with an additional "model" (string) member and a drive() method. Also, derive the "Bicycle" class from the "Vehicle class" with a "type" (string) member and a pedal() method. In the main program, create instances of both "Car" and "Bicycle" classes, display their information, and demonstrate the drive() and pedal() methods.
#include <iostream>
#include <string>
using namespace std;
// Base class
class Vehicle {
private:
string make;
int year;
public:
// Constructor
Vehicle(string m, int y) : make(m), year(y) {}
// Method to display vehicle info
void displayInfo() const {
cout << "Make: " << make << ", Year: " << year << endl;
}
};
// Derived class Car
class Car : public Vehicle {
private:
string model;
public:
// Constructor
Car(string m, int y, string mdl) : Vehicle(m, y), model(mdl) {}
// Method to display car info and drive
void drive() {
displayInfo();
cout << "Model: " << model << endl;
cout << "Driving the car!" << endl;
}
};
// Derived class Bicycle
class Bicycle : public Vehicle {
private:
string type;
public:
// Constructor
Bicycle(string m, int y, string t) : Vehicle(m, y), type(t) {}
// Method to display bicycle info and pedal
void pedal() {
displayInfo();
cout << "Type: " << type << endl;
cout << "Pedaling the bicycle!" << endl;
}
};
int main() {
// Create instances of Car and Bicycle
Car myCar("Toyota", 2020, "Corolla");
Bicycle myBicycle("Trek", 2021, "Mountain");
// Display their information and demonstrate their methods
myCar.drive();
cout << endl;
myBicycle.pedal();
return 0;
}Output
Make: Toyota, Year: 2020
Model: Corolla
Driving the car!
Make: Trek, Year: 2021
Type: Mountain
Pedaling the bicycle!4. A. Write a C++ program demonstrating polymorphism with geometric shapes, creating a base class "Shape" with a pure virtual method calculateArea(). The "Circle" class calculates its area based on its radius, while the "Rectangle" class calculates its area based on its length and width. Create instances of the "Circle" and "Rectangle" in the main program and utilize an array of "Shape" pointers to demonstrate polymorphism. Calculate and display areas by iterating through an array of "Shape" pointers.
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
virtual double calculateArea() = 0; // Pure virtual function
virtual ~Shape() {} // Virtual destructor
};
// Derived class Circle
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() override {
return 3.14 * radius * radius; // Area of a circle: πr²
}
};
// Derived class Rectangle
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double calculateArea() override {
return length * width; // Area of a rectangle: length × width
}
};
int main() {
// Create an array of Shape pointers
Shape* shapes[2];
// Add shapes to the array
shapes[0] = new Circle(5.0); // Circle with radius 5.0
shapes[1] = new Rectangle(4.0, 6.0); // Rectangle with length 4.0 and width 6.0
// Calculate and display areas
for (int i = 0; i < 2; ++i) {
cout << "Area: " << shapes[i]->calculateArea() << endl;
}
// Clean up memory
for (int i = 0; i < 2; ++i) {
delete shapes[i]; // Free memory
}
return 0;
}Output
Area: 78.5
Area: 24B. Develop a C++ program in which you demonstrate how files can be handled for student record management. Create a structure entitled "Student" with members for student IDs (an integer), names (strings), and GPAs (floats). Create a text file named "student_records.txt" in write mode and allow the user to input student records, including ID, name, and GPA, until they decide to stop. Each student record should be written to the file in a structured format and closed afterwards. Reopen the "student_records.txt" file in read mode and display the student records on the screen, one record per line.
This question is based on - File Handling in C++
#include <iostream>
#include <fstream>
using namespace std;
struct Student {
int id;
string name;
float gpa;
};
int main() {
ofstream outFile("student_records.txt");
if (!outFile) {
cout << "Error opening file for writing!" << endl;
return 1;
}
char choice;
do {
Student student;
cout << "Enter ID: ";
cin >> student.id;
cout << "Enter Name: ";
cin.ignore(); // Clear the input buffer
getline(cin, student.name);
cout << "Enter GPA: ";
cin >> student.gpa;
outFile << student.id << " " << student.name << " " << student.gpa << endl;
cout << "\nDo you want to enter another record? (y/n): ";
cin >> choice;
cout<<endl;
} while (choice == 'y' || choice == 'Y');
outFile.close();
ifstream inFile("student_records.txt");
if (!inFile) {
cout << "Error opening file for reading!" << endl;
return 1;
}
cout << "Student Records:\n";
Student student;
while (inFile >> student.id) {
inFile >> ws; // Skip whitespace before reading the name
getline(inFile, student.name, ' '); // Read until whitespace
inFile >> student.gpa;
cout << "ID: " << student.id << ", Name: " << student.name << ", GPA: " << student.gpa << endl;
}
inFile.close();
return 0;
}Output
Enter ID: 4466
Enter Name: Shubham
Enter GPA: 4.2
Do you want to enter another record? (y/n): y
Enter ID: 4467
Enter Name: Eniv
Enter GPA: 5.0
Do you want to enter another record? (y/n): y
Enter ID: 4468
Enter Name: Archana
Enter GPA: 5.0
Do you want to enter another record? (y/n): n
Student Records:
ID: 4466, Name: Shubham, GPA: 4.2
ID: 4467, Name: Eniv, GPA: 5
ID: 4468, Name: Archana, GPA: 55. A. What is a function? Differentiate between call by value and call by reference.
B. 5 + 3 * 2 - 4/2. Calculate the result based upon the operator precedence.
Solution: Operator precedence:
5 + 3 * 2 - 4 / 2= 5 + 6 - 2= 9
C. Differentiate between class and structure.
D. Program to find the smallest element in any given array.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
cout << "Smallest element is: " << min << endl;
return 0;
}Output
Enter the number of elements: 5
Enter the elements: 1 2 3 4 5
Smallest element is: 1E. Define constructor and elaborate the types of constructors.
F. What is inheritance? With diagrams, explain inheritance types.
G. Differentiate between compile-time and run-time polymorphism.
H. Need for exception handling in C++ programming.
Exception handling ensures the program handles errors gracefully, preventing crashes and allowing recovery from runtime errors.
I. Significance of error handling in file operations in C++.
Error handling in file operations prevents data loss, handles file opening failures, and ensures data integrity.
J. Example of cascading for code readability and efficiency.