Practice Questions CPP
Practice Questions CPP
Write a C++ program to calculate the area of a circle given its radius.
Write a C++ program to count the number of vowels and consonants in a string.
1.Write a C++ program to create a class called Rectangle that represents a rectangle. The
class should have the following private attributes:
2.Write a C++ program to create a class called Circle that represents a circle. The class
should have a private attribute radius. Implement member functions to set the radius,
calculate and return the area of the circle, and display the radius and area.
3. Write a C++ program to create a class called Student that represents a student. The
class should have private attributes name, rollNumber, and marks. Implement member
functions to set the student details, calculate and return the total marks, and display
the student details.
4. Write a C++ program to create a class called Employee that represents an employee. The
class should have private attributes empId, name, and salary. Implement member functions
to set the employee details, calculate and return the annual salary (assuming 12 months),
and display the employee details.
5. Write a C++ program to create a class called Book that represents a book. The class
should have private attributes title, author, and price. Implement member functions to set
the book details, display the book details, and calculate and return the discounted price
(assuming a 10% discount).
6. Write a C++ program to create a class called BankAccount that represents a bank
account. The class should have private attributes accountNumber, holderName, and balance.
Implement member functions to set the account details, deposit and withdraw money, and
display the account details.
7. Write a C++ program to create a class called Car that represents a car. The class
should have private attributes brand, model, and year. Implement member functions to set
the car details, display the car details, and calculate and return the age of the car
(current year - year of manufacture).
8. Write a C++ program to create a class called Rectangle that represents a rectangle. The
class should have private attributes length and width. Implement member functions to set
the rectangle dimensions, calculate and return the area and perimeter, and display the
rectangle details.
9. Write a C++ program to create a class called Person that represents a person. The class
should have private attributes name, age, and gender. Implement member functions to set
the person details, display the person details, and check if the person is an adult (age
>= 18).
10. Write a C++ program to create a class called Triangle that represents a triangle. The
class should have private attributes side1, side2, and side3. Implement member functions
to set the triangle sides, calculate and return the area and perimeter, and display the
triangle details.
11. Write a C++ program to create a class called Time that represents time in hours,
minutes, and seconds. The class should have private attributes hours, minutes, and
seconds. Implement member functions to set the time, add a given number of seconds to the
time, and display the time.
12. Write a C++ program to create a class called Bank that represents a bank. The class
should have private attributes bankName, branch, and IFSCCode. Implement member functions
to set the bank details, display the bank details, and check if the IFSC code is valid
(should contain 11 characters).
13. Write a C++ program to create a class called Employee that represents an employee. The
class should have private attributes empId, name, and designation. Implement member
functions to set the employee details, display the employee details, and check if the
employee is a manager (designation should be "Manager").
14. Write a C++ program to create a class called Temperature that represents temperature
in Celsius. The class should have private attributes celsius. Implement member functions
to set the temperature, convert it to Fahrenheit (F = (C * 9/5) + 32), and display both
Celsius and Fahrenheit temperatures.
15. Write a C++ program to create a class Shape representing geometric shapes. Implement
the following functionalities:
16 Write a C++ program to create a class Vehicle representing vehicles. Implement the
following functionalities:
17. Write a C++ program to create a class Animal representing animals. Implement the
following functionalities:
18 Write a C++ program to handle division by zero exceptions. The program should prompt
the user to enter two integers and then perform division. If the divisor is zero, catch
the exception and display an appropriate error message.
22 Write a C++ program to create a class FileReader that reads a file and displays its
content. Implement exception handling to catch file not found exceptions and display an
appropriate error message if the file does not exist.
23 Write a C++ program to create a function that calculates the square root of a number.
If the input number is negative, throw an exception and handle it by displaying an
appropriate error message.
24 Write a C++ program to create a class BankAccount with a method withdraw that throws an
exception if the withdrawal amount is greater than the balance. Handle the exception and
display an appropriate error message.
25 Write a C++ program to implement a simple calculator using exception handling. The
program should support basic operations (addition, subtraction, multiplication, division)
and throw an exception for invalid operations or division by zero.
26 Write a C++ program to read integers from the user until a non-integer is entered. Use
exception handling to catch input errors and display an appropriate error message.
27 Write a C++ program that demonstrates polymorphism using a base class Shape and derived
classes Circle and Rectangle. Implement a method calculateArea() in each derived class.
The base class should have a virtual method calculateArea(). Use exception handling to
manage any runtime errors, such as negative values for dimensions.
28 Write a C++ program that creates an abstract base class Employee with a pure virtual
function calculateSalary(). Create two derived classes PermanentEmployee and
ContractEmployee that implement the calculateSalary() method. Use polymorphism to
calculate salaries for a list of employees and handle exceptions for any invalid input,
such as negative salary values.
29 Write a C++ program that demonstrates polymorphism using a base class Account and
derived classes SavingsAccount and CurrentAccount. Implement a method withdraw() in each
derived class. Use exception handling to manage errors like insufficient balance or
negative withdrawal amounts.
30 Write a C++ program that creates a base class Vehicle with a virtual function
displayDetails(). Derive two classes Car and Bike from Vehicle. Implement the
displayDetails() method in each derived class to display specific details. Use exception
handling to handle invalid input, such as negative values for engine capacity or number of
wheels.
31 Write a C++ program that demonstrates polymorphism using a base class Person and
derived classes Student and Teacher. Implement a method printDetails() in each derived
class. Use exception handling to handle any errors, such as invalid age or negative ID
numbers.
32 Write a C++ program that creates an abstract base class Shape with a pure virtual
function draw(). Derive two classes Square and Triangle from Shape. Implement the draw()
method in each derived class to display specific shapes. Use exception handling to handle
errors like invalid side lengths or angles.
33 Write a C++ program that demonstrates polymorphism using a base class Animal and
derived classes Dog and Cat. Implement a method makeSound() in each derived class. Use
exception handling to manage errors, such as invalid input for age or weight of the
animals.
34 Write a C++ program that demonstrates polymorphism using a base class Appliance and
derived classes WashingMachine and Refrigerator. Implement a method showSpecifications()
in each derived class. Use exception handling to manage runtime errors, such as invalid
power consumption values or negative capacities.
35 Write a C++ program that creates an abstract base class Product with a pure virtual
function displayInfo(). Derive two classes Electronics and Clothing from Product.
Implement the displayInfo() method in each derived class to display specific product
information. Use exception handling to handle invalid input, such as negative prices or
quantities.
36 Write a C++ program that demonstrates polymorphism using a base class Payment and
derived classes CreditCardPayment and PayPalPayment. Implement a method processPayment()
in each derived class. Use exception handling to manage errors like invalid payment
amounts or unsupported currencies.