0% found this document useful (0 votes)
13 views74 pages

Oop Practise Question

This file contains a collection of C++ practice questions. It is designed for students and beginners to test their programming knowledge and improve problem-solving skills.

Uploaded by

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

Oop Practise Question

This file contains a collection of C++ practice questions. It is designed for students and beginners to test their programming knowledge and improve problem-solving skills.

Uploaded by

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

TABLE OF CONTENTS

Practical no. 01 .............................................................................................................. 3


Practical no. 02 .............................................................................................................. 5
Practical no. 04 .............................................................................................................. 7
Practical no 05 ............................................................................................................... 8
Practical no 06 ............................................................................................................... 9
Practical no. 07 ............................................................................................................ 10
Practical no. 08 ............................................................................................................ 12
Practical no. 09 ............................................................................................................ 13
Practical no. 10 ............................................................................................................ 14
Practical no. 11 ............................................................................................................ 16
Practical no. 12 ............................................................................................................ 17
Practical no. 13 ............................................................................................................ 19
Practical no. 14 ............................................................................................................ 21
Constructors ................................................................................................................ 24
Practical no. 15 ............................................................................................................ 24
Practical no. 16 ............................................................................................................ 25
Practical no. 17 ............................................................................................................ 27
Practical no. 18 ............................................................................................................ 29
Practical no. 19 ............................................................................................................ 30
Practical no. 20 ............................................................................................................ 31
Practical no. 21 ............................................................................................................ 33
Practical no 22:(Inheritance) .......................................................................................... 35
Practical no. 23 ............................................................................................................ 36
Practical no. 24 ............................................................................................................ 38
Practical no. 25 ............................................................................................................ 41
Practical no. 26 ............................................................................................................ 43
Practical no. 27 ............................................................................................................ 46
(String Handling) ......................................................................................................... 49

1
Practical no .28 ............................................................................................................ 49
Practical no. 29 ............................................................................................................ 50
Practical no. 30 ............................................................................................................ 51
Daily Life Example: Creating fun or secret messages by replacing vowels with symbols. . 51
(Polymorphism) ........................................................................................................... 53
Question no. 31 ............................................................................................................ 53
Practical no. 33 ............................................................................................................ 55
Practical no. 34 ............................................................................................................ 56
(Example Early Binding and Late Binding) ...................................................................... 56
Practical no.35 ............................................................................................................. 58
(Abstract classes Example) ............................................................................................ 58
(Templates: Function Template and Class template) ........................................................... 60
Practical no. 37 ............................................................................................................ 60
Practical no. 38 ............................................................................................................ 61
Practical no. 39 ............................................................................................................ 61
Practical no. 40 ............................................................................................................ 63
Practical no. 42 ............................................................................................................ 65
Practical no.44 ............................................................................................................. 67
Practical no. 46 ............................................................................................................ 70

2
Practical no. 01
Write a C++ Program to display a timetable for your college?

Objective: This program automates and simplifies the process of checking daily class
schedules, saving time and avoiding confusion.
e.g., a student can quickly view what subjects they have on a specific day, like checking
Monday’s timetable before packing their bag.

3
4
Practical no. 02
Write a C++ Program for an energy-saving system in the college department?
Objective: This program automates and simplifies the control of home or room appliances based
on the number of people and user input.
e.g., if more than one person is present, the fan and light turn ON automatically. The user can
also decide whether to turn ON the camera and LED manually.

5
Practical no .03
A Program to Find GCD?

Objective: This program simplifies the process of finding the greatest common divisor (GCD)
between two numbers, which is useful in reducing fractions or solving mathematical problems.
e.g., a student can use this to simplify 12/16 by finding the GCD (which is 4), so the fraction
becomes 3/4.

6
Practical no. 04
C++ Program to find LCM?

Objective:

To find the Least Common Multiple (LCM) of two user-given numbers using a loop.

Daily Life Example:


Suppose two bells ring every 4 and 6 minutes. The LCM helps find when both will ring together
— just like this program finds the first common time (LCM) both numbers divide evenly into.

7
Practical no 05
C++ Program to reverse a number?

Objective:

To take a number input from the user and reverse its digits using a loop.

e.g. (Daily Life Example):


A person wants to check the reverse of their phone number or birth year for pattern-based
passwords or codes.

8
Practical no 06
C++ Program to Display Armstrong Numbers Between Two Intervals?

Objective:

To find and display all Armstrong numbers within a given interval entered by the user.

e.g. (Daily Life Example):


A math student wants to identify special numbers (like Armstrong numbers) in a range to solve a
puzzle or prepare for a number theory test.
They enter 100 to 999 and find numbers like 153, 370, 371, and 407 as Armstrong numbers.

9
Practical no. 07
Write a C++ program that built a pyramid?

Objective:

To print a pyramid star pattern based on the number of rows entered by the user
using nested loops.e.g. (Daily Life Example):
A student wants to design a decorative star pattern for a festive banner or
assignment cover page.
They enter the number of rows, and the program generates a symmetric star
pyramid to be used in the design.

10
11
Practical no. 08
To find all roots of quadratic equations?

Objective:
To find the roots of a quadratic equation using the quadratic formula and display whether the
roots are real or complex.

e.g. (Daily Life Example):


An architecture student wants to solve a parabolic curve equation to determine where a bridge
arch meets the ground.
They input values of a, b, and c, and the program calculates the roots to find the intersection
points.

12
Practical no. 09
Write a program that declares a class with one integer data member and two-member function
in() and out () to input and output data in the data member.

Objective:
To demonstrate basic object-oriented programming in C++ using a class with input and output
functions.e.g. (Daily Life Example):
A teacher wants students to enter and display their roll number using a class-based approach to
understand the concept of encapsulation.
Each student runs the program and sees their number displayed using class functions.
13
Practical no. 10
Write a Class Marks with three data members to store three marks. Write three member functions
in () to input marks, sum() to calculate and return the sum, avg() to calculate and return marks.
Objective: To use a class in C++ to input three marks, calculate their total, and compute the
average using class functions.e.g. (Daily Life Example):
A student wants to calculate the total and average of marks in three subjects — like Math,
Science, and English — using a program to avoid manual calculation and ensure accuracy.

14
15
Practical no. 11
Write a class Circle with one data member radius. Write three member functions get-radius () to
set radius value with parameter value ,area() to display radius and circum() to calculate and
display the circumference of circle.

Objective: To create a class that stores the radius of a circle and calculates its area and
circumference. (Daily Life Example):
A carpenter wants to calculate the amount of paint needed to cover a circular table’s surface and
the length of the table’s edge to make a decorative border.
By entering the table’s radius, the program helps find these measurements quickly.

16
Practical no. 12
Write a Class Book with three data members Book id, pages and price.It also contains the
following member function:

• The get () function is used to input values


• The show () function is used to display values
• The set () function is used to set the values of data member using parameters
• The get price () function is used to return the value of price
The Program Should create two objects of the class and input values for these objects. The
program displays the details of the most costly book.
Objective: To create a class representing books, input their details (ID, pages, price), and find
and display the most expensive book. e.g. (Daily Life Example):
A bookstore manager wants to compare prices of two books and display details of the more
expensive one, helping to decide which book to promote or keep track of.

17
18
Practical no. 13
Write a Class Result that contains roll no, name and marks of three subjects. The marks are
stored in an array of integers. The class contains the following member function:

• The input () function is used to input the values in data members


• The show () function is used to display the value of data members
• The total () function returns the total marks of a student
• The avg () function returns the average marks of a student
The program should create an object of the class and call the member functions

19
Objective:To create a program that stores a student's roll number, name, and marks for 3
subjects, then calculates and displays the total and average marks.Daily Life Example:Imagine a
teacher who wants to keep records of each student's marks in three subjects. This program helps
the teacher quickly input marks and get the student's total and average scores, which can be used
for report cards or progress tracking.

20
Practical no. 14
Write a Class Array that contains an array of integers to store five values. It also contains the
following member function defined outside the class:

• The fill () function is used to fill the array with values from the user.
• The display () function is used to display the values of array.
• The max () function shows the maximum value in the array.
• The min () function shows the minimum value in the array.
• Objective:
• To create a program that inputs 5 integer values into an array, displays them, and finds
the maximum and minimum values from the array.
• Daily Life Example:

21
• Imagine you want to record the temperatures of 5 different days. This program helps you
enter those temperatures, then quickly tells you the hottest day (maximum temperature)
and the coldest day (minimum temperature).

22
23
Constructors

Practical no. 15
Write a Class that contain two integers’ data member which are initialized to 100 when an object
is created. It has a member function avg that displays the average of data members.
Objective:To create a class that initializes two numbers to 100 by default and calculates their
average. Daily Life Example:Imagine you have two test scores, both 100 by default. This
program automatically calculates the average score without needing you to enter anything.

24
Practical no. 16
Write a Class student that has marks and grade as data members. A constructor with two
parameters initializes data members with the given values and member function show () displays
the values of data members. Create two objects and display the values. Objective: To create a
class Student with a parameterized construction that initializes and displays the marks and
grade of students. Daily Life Example: Imagine a teacher is entering students' report card
information. Instead of setting each student's marks and grade separately later, the teacher gives
them directly while creating the student record. This program does exactly that — it creates
student records by passing values (marks and grade) at the time of object creation.

25
26
Practical no. 17
Write a class TV that contains the attribute Brand Name, Model and Retail Price .Write a method
to display all attributes and a method to change the attributes. Also write a constructor to
initialize all the attributes. Objective: To create a TV class that sets and updates TV details using
constructor and a function. Daily Life Example: You buy a TV and later exchange it — this
program helps update the TV info easily.

27
28
Practical no. 18
Write a class Over with num and Ch data member. A constructor with no parameter initializes the
num to 0 and Ch to ‘x’. A constructor with two parameter initializes data members with the given
values and member function show () displays the values of data member. Objective:To create a
class with constructors that set default or given values and display them.Daily Life Example:

Like a phone that can start with default or custom settings.

29
Practical no. 19
Write a class Travel that has the attributes of kilometer and hours. A constructor with no
parameter initializes both data member to 0.A member function get() input the values and
function show() display the values .It has a member function add() takes an object of type travel
to add kilometer and hours of calling object and the parameter .Objective:To create a class that
stores travel data (kilometers and hours), takes input, and adds two travel records together.Daily
Life Example:Imagine you took two trips in a day. This program helps you add both trips'
distance and time to know your total travel.

30
Practical no. 20
Write a class Player that contains attributes for the player’s name, average and team. Write three
functions to input change and display three attributes. Also write a constructor that asks for input
initialize all the attributes.Objective:To create a class Player that takes a player's name,
average, and team using a constructor, and allows updating the info later. Daily Life
Example: Like a cricket coach entering player info in a system and later updating it if the
player changes team or improves performance.

31
32
Practical no. 21
Define a class for a bank account includes the following data member:

• Name of the depositor


• Account Number
• Type of account
• Balance amount in the account
The class also contains the following member functions:
• A constructor to assign initial values.
• Deposit function to deposit some amount. It should accept the amount as
parameter.
• Display function to displays name and balance.
Objective: To create a class Bank Account that initializes account details and allows
depositing money and displaying updated balance. Daily Life Example:
Like opening a bank account, putting money in it, and checking the balance — just like
people do in real life at the bank.

33
34
Practical no 22:(Inheritance)
Write a C++ program to create a `Move` class that tracks position with a forward movement
method, and a derived `Move2` class that adds a backward movement method, displaying the
position after each move.

Objective:To demonstrate inheritance in C++ by creating a base class that moves forward and a
derived class that also moves backward. Daily Life Example:Like a remote-controlled car —
it can move forward and backward. One part handles forward motion, and another adds the
ability to go backward too.

35
Practical no. 23
Write a class Person that has the attributes of id, name, address. It has a constructor to initialize,
a member function to input and a member function to display data members. Create another class
Student that inherits a person class .It has additional attributes of roll number and marks . It also
has member function to input and display its data members.
Objective: To show how inheritance works by adding details step by step. Daily Life Example:
Like a college form — first personal info, then student info, then scholarship info.

36
37
Practical no. 24
Write a program that declares two classes. The parent is called Simple that has two data
members a and b to store two numbers. It also has four member functions:

• The add () function adds two numbers and display the result.
• The sub () function subtracts two numbers and display the result.
• The mul () function multiplies two numbers and displays the result.
• The div () function divides two numbers and display the result.
The child class is called Complex that overrides all four functions. Each function in child
class checks the values of data members. It calls corresponding member function

in the parent class if the values are greater than 0. Otherwise it display error message.

Objective:To demonstrate the concept of inheritance and method overriding in C++ by


allowing arithmetic operations only when both input values are positive.

Daily Life Example:Just like a smart calculator that performs calculations only when valid
(positive) numbers are entered; otherwise, it gives an error message instead of wrong results.

38
39
40
Practical no. 25
Write a base class Computer that contain data member of word Size (in bits),memory Size (in
megabytes),storage size(in megabytes)and speed(in megahertz).Derive a laptop class that is a
kind of Computer but also specifies the object’s length ,width, height, and weight. Member
functions for both classes should include a default constructor, a constructor to initialize all
components and a function to display data member.
Objective:To demonstrate inheritance in C++ by extending the base class Computer to a
derived class Laptop, adding specific attributes like dimensions and weight.Daily Life
Example:Just like when you buy a laptop, you check both its technical specs (like speed,
memory) and physical features (like size, weight). The laptop is just a portable computer with
some extra properties.

41
42
Practical no. 26
Write to a class person that has attributes of id, name and address. It has a constructor to
initialize, a member function to input and a member function to input and a member function
to display data members. Create 2nd class Student that inherits person class. It has additional
attributes of roll number and marks. It has a member function to input and display its data
members. Create 3rd class Scholarship that inherit Student class. It has additional attributes of
Scholarship name and amount. It also has a member function to input and display its data
members.

Objective:
To demonstrate how to create and manage multiple objects dynamically using pointers
and arrays in C++.Daily Life Example: Imagine a teacher taking names of students for
attendance. She creates a list and adds each student's name one by one, and then reads them
out. Here, each student is like a dynamic object created and handled individually.

43
44
45
Practical no. 27
Write a Class Result that has an array of three integers as attribute. It has a member function
to input and a member function to display average of array elements. Create another class
Student that inherits Result class. It has additional attribute of roll number, name and an
object of type class Result .It also has member functions to input and display its data
members.

Objective:
To understand inheritance and composition in C++. The program collects a student’s
personal data along with marks and calculates total and average using a separate Result
class.Daily Life Example:

Think of a school report card. A student has personal details like name and roll number.
Their academic performance (marks) is handled separately by the examination department.
At the end, both details are combined to create the report card.

46
47
48
(String Handling)

Practical no .28
Write a program that inputs a string from the user and then copies it another string.

Objective: To understand how to take string input from the user and copy one string into
another using assignment in C++.Daily Life Example:Imagine you are writing an email
and want to keep a backup copy of the message before sending it. You simply copy the
original text into another field or file — just like copying the string in this program.

49
Practical no. 29
Write a C++ program to accept a string and replace vowels with specific symbols ('a'
with '@', 'e' with '#', 'i' with '$', 'o' with '!', 'u' with '%') using a switch statement.

Objective: Learn how to replace vowels using a switch statement in C++.

Daily Life: Make secret or fun text by swapping vowels with symbols.

50
Practical no. 30
1. Write a C++ program to accept a string and replace vowels with specific symbols ('a'
with '$', 'e' with '@', 'i' with '&', 'o' with 'K', 'u' with '#') and display the modified string.

Objective: To practice using the switch statement for character replacement in a


string.
Daily Life Example: Creating fun or secret messages by replacing vowels with symbols.

51
52
(Polymorphism)

Question no. 31
Write a Class with an integer data member ,a function to input and a function to display it
.Creates an object of the class using pointers and class its member functions.

Objective:To demonstrate how to create an object dynamically using a pointer and access its
members using the arrow (->) operator in C++.Daily Life Example:

Suppose you're using an ATM machine — when you insert your card, the machine temporarily
creates your account session, takes your input (like PIN), shows your balance, and deletes the
session when you're done.
This is similar to how this program dynamically creates an object, uses it, and frees memory.

53
Practical no. 32
Write a class that contain an attribute name, a function to input and a function to display name.
Creates an array of pointers in which each element refers to an object of the class.
Objective: Demonstrates using dynamic memory (pointers) to create and manage multiple
objects at runtime. Daily Life Example:
Like creating separate ID cards for 3 different people dynamically and then displaying their
names, cleaning up when done.

54
Practical no. 33
What will be the output of the following C++ program that uses a base class pointer to call the
show() function in a class hierarchy with virtual functions?

Objective: Shows how a base class pointer can call overridden functions in derived classes using
virtual functions (polymorphism).Daily Life Example:
Like a remote control (base pointer) that can operate different devices (TV, DVD player, Radio),
and each device responds in its own way when you press the same button.

55
Practical no. 34

(Example Early Binding and Late Binding)


Objective: Demonstrates polymorphism by calling the correct show() method of Parent,
Child1, or Child2 objects through base class pointers. Daily Life Example:
Like a customer service system where different employee types (manager, technician)
respond differently to calls, but all are handled through a common interface.

56
57
Practical no.35

(Abstract classes Example)


Objective: Demonstrate how abstract classes and polymorphism allow different
derived classes to define their own versions of a function, and how base class pointers
can call these overridden functions. Daily Life Example:
Think of Parent as a general "Vehicle" interface with a show method. Different vehicles
like Car (Child1) and Bike (Child2) implement their own show method to display their
type. You can use a generic vehicle pointer to show any type of vehicle without knowing
the exact type.

58
Practical no. 36
(Virtual Base Classes Example)
Objective: To show how virtual inheritance prevents multiple copies of a base class in
multiple inheritance (solving the diamond problem).Daily Life Example:
A child inherits family traits from both parents, but the traits come from one common
grandparent only once, not duplicated.

59
(Templates: Function Template and Class template)

Practical no. 37
Write a template that accepts two numeric values and display the maximum number.
Objective:This program uses a function template to find the maximum of two values of
any type (int, float, etc.).Daily Life Example:
Imagine you're comparing:

• Two exam scores to see which is higher.

• Two prices to know the more expensive item.

60
Practical no. 38
Write a function template that accepts a values of any type and then display it.

Objective:To demonstrate a function template that can display any type of value (integer,
float, string, etc.) using one generic function.

Daily Life Example:Think of a digital display board:

• It can show temperature (float), visitor count (int), or custom message (string).

• This program behaves similarly — it displays any type of data using the same function.

Practical no. 39
Write a template for finding minimum value in an array.

Objective:To create a template function that can find the minimum value in an array
of any data type (int, float, etc.). Daily Life Example:Imagine a thermometer app:You
input multiple temperature readings.It tells you the lowest temperature recorded.

61
62
Practical no. 40
Write a class template that stores five values of any type in an array.

Objective: To create a template class that stores and displays 5 values of any data
type (e.g., int, string).Daily Life Example:

Imagine a grocery store:

• It stores the prices of 5 items (using int or float).

• Then it stores the names of 5 items (using string).


This program works just like that — storing and displaying different types of values.

63
Practical no. 41
Write a function template that finds the minimum values in the array and return it.

Objective:To find the minimum value from an array using a template function for any data
type. Daily Life Example:A teacher wants to find the lowest marks from students' scores in
different subjects. This program helps do that easily.

64
Practical no. 42
Write a function template that accepts three parameters and display them in reverse order.

Objective:To create a template function that displays 3 values in reverse order, no


matter the data type.Daily Life Example:

Imagine calling three friends in order: Ali, Sana, Zara.


But when announcing them on stage, you say: Zara, Sana, Ali — reverse order!

65
Practical no. 43
Write a Class template that inputs the index of the array and displays the value in specified
index.

Objective: To use templates in C++ for creating a class that works with any data type and
displays array values using index checking. Daily Life Example:
Like a shelf with 5 boxes — you can store anything in them.
You can check any box by number, but only if it's between 0 and 4 — otherwise, it shows an
error.

66
Practical no.44
Write a function to read a matrix of size m x n from the keyboard?
Objective: Input and display a matrix of numbers, useful for storing tabular data. Daily Life
Example:
Like filling out and showing a seating chart in a classroom or a schedule grid.

67
Practical no. 45
Write a function power() to raise a number m to power n .the function takes a double values of m
and int values of n .Both function should have same name write a main that calls both functions
use the concept of function overloading
Objective:Calculate powers (exponents) of numbers for both integer and decimal bases.Daily
Life Example:Calculating compound interest where the amount grows exponentially based on
the interest rate and time period.

68
69
Practical no. 46
Assume that a bank maintains two kinds of accounts for customers, one called as a savings
account and the other as a current account.
The savings account provides compound interest and withdrawal facilities but no cheque book
facility.
The current account provides no interest.
Current account holders should also maintain a minimum balance and if the balance falls below
this level, a service charge is imposed.

Create a class account that stores customer name, account number and type of account.
From this derive the classes cur_acct and sav_acct to make them more specific to their
requirements.
Include necessary member functions in order to achieve the following tasks:

(a) Accept deposit from a customer and update the balance.


(b) Display the balance.
(c) Compute and deposit interest.
(d) Permit withdrawal and update the balance.
(e) Check for the minimum balance, impose penalty if necessary and update the balance.

Do not use any constructors.Use member functions to initialize the class members.

Objective:
This program simulates a simple bank system with two account types—Savings and Current. It
lets users deposit money, withdraw money, and:
• Savings account earns interest over time.

70
• Current account checks for minimum balance and applies a penalty if the balance is too
low. Daily-life example:You open a Savings or Current account at a bank. In Savings,
your money grows with interest. In Current, you must keep a minimum balance to avoid
penalties. The program mimics these actions for practice with basic banking operations.

71
72
73
74

You might also like