0% found this document useful (0 votes)
16 views

Practice Questions CPP

Uploaded by

2022a1r010
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Practice Questions CPP

Uploaded by

2022a1r010
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Question:

Write a C++ program to find the sum of all elements in an array.

Write a C++ program to check if a number is prime or not.

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:

length: representing the length of the rectangle


width: representing the width of the rectangle
Implement the following member functions:

setData(): a function to set the length and width of the rectangle.


calculateArea(): a function to calculate and return the area of the rectangle.
calculatePerimeter(): a function to calculate and return the perimeter of the rectangle.
display(): a function to display the length, width, area, and perimeter of the rectangle.
The program should demonstrate the usage of this class by creating an object of Rectangle,
setting its data, calculating its area and perimeter, and displaying the details.

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:

Create a constructor to initialize the shape type.


Create a destructor to display a message when an object is destroyed.
Create a derived class Rectangle from Shape representing rectangles. Implement a
constructor to initialize the length and width of the rectangle.
Create a derived class Circle from Shape representing circles. Implement a constructor to
initialize the radius of the circle.
Create an abstract class Polygon derived from Shape representing polygons. Implement a
pure virtual function calculateArea() to calculate the area of polygons.
Create a derived class Triangle from Polygon representing triangles. Implement the
calculateArea() function to calculate the area of triangles.

16 Write a C++ program to create a class Vehicle representing vehicles. Implement the
following functionalities:

Create a constructor to initialize the vehicle's make and model.


Create a destructor to display a message when an object is destroyed.
Create a derived class Car from Vehicle representing cars. Implement a constructor to
initialize the vehicle's make, model, and color.
Create a derived class Bike from Vehicle representing bikes. Implement a constructor to
initialize the vehicle's make, model, and engine capacity.
Use an array of objects to store both cars and bikes, and demonstrate the creation and
destruction of objects.

17. Write a C++ program to create a class Animal representing animals. Implement the
following functionalities:

Create a constructor to initialize the animal's name and species.


Create a destructor to display a message when an object is destroyed.
Create a derived class Mammal from Animal representing mammals. Implement a constructor to
initialize the animal's name, species, and number of legs.
Create a derived class Bird from Animal representing birds. Implement a constructor to
initialize the animal's name, species, and wingspan.
Use an array of objects to store both mammals and birds, and demonstrate the creation and
destruction of objects.

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.

19 Write a C++ program to create a custom exception class NegativeValueException. The


program should prompt the user to enter a positive integer. If the user enters a negative
integer, throw and catch the NegativeValueException and display an appropriate error
message.
20 Write a C++ program to handle multiple exceptions. The program should prompt the user
to enter two integers and then perform division and multiplication. If the divisor is
zero, catch the division by zero exception. If any of the integers are negative, catch the
negative value exception and display an appropriate error message.

21 Write a C++ program to handle out-of-range exceptions when accessing elements of an


array. The program should create an array of 5 integers and prompt the user to enter an
index to access. If the index is out of range, 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.

You might also like