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

Practice Questions on C++ by Career Credentials (1)

The document contains a collection of practice questions on C++ programming, organized into various sections including patterns, variables, operators, and decision-making structures. Each section presents multiple programming tasks aimed at enhancing the reader's understanding and skills in C++. It serves as a resource for learners to practice and apply their knowledge of C++ through hands-on coding exercises.
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)
6 views74 pages

Practice Questions on C++ by Career Credentials (1)

The document contains a collection of practice questions on C++ programming, organized into various sections including patterns, variables, operators, and decision-making structures. Each section presents multiple programming tasks aimed at enhancing the reader's understanding and skills in C++. It serves as a resource for learners to practice and apply their knowledge of C++ through hands-on coding exercises.
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

Practice Questions

on C++
By Career Credentials

70+ Pages
of notes by professionals

info@careercredentials.in 9930999956 www.careercredentials.in


Practice questions on Cpp
Practice questions on Let's start
1. Write a C++ program to print
*
**
***
****
on screen.
Answer:

2. Store an integer in a variable x and print it on the screen.


Answer:

3. Write a program to take the input of an int and print it on the screen.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭ

4. Print the following pattern on the screen
****
**
*
**
****
5. Write a program to print Hello and CodesDope in two different lines.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮ

Practice questions on Variables and literals
1. Store two integers in two variables x and y. Print the sum of the two.

Answer:

2. Store two integers in two variables x and y. Print the product of the two.

Answer:

3. Write a C++ program to take two integer inputs from user and print sum and product
of them.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯ

4. Take two integer inputs from user. First calculate the sum of two then product of

two. Finally, print the sum and product of both obtained results.

Answer:

5. Write a program to take input of length and breadth of a rectangle from the user and
print its area.
Answer:

6. Write a C++ program to print an int, a double and a char on screen.

Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰ

7. Print the ASCII value of the character 'h'.

Answer:

8. Write a program to assign a value of 100.235 to a double variable and then convert
it to int.
Answer:

9. Write a program to add 3 to the ASCII value of the character 'd' and print the

equivalent character.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱ

Answer:

10. Write a program to add an integer variable having value 5 and a double variable
having value 6.2.
11. Write a program to find the square of the number 3.9.
12. Take value of length and breath of a rectangle from user as float. Find its area and
print it on screen after type casting it to int.
13. Take a char input from user and print it's ASCII value.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲ

Practice questions on Operators
Level 1:

1. Length and breadth of a rectangle are 5 and 7 respectively. Write a program to


calculate the area and perimeter of the rectangle.
Answer:

2. Write a program to input the value of the radius of a circle from keyboard and then
calculate its perimeter and area.
Answer:

3. Write a program to enter a 4 digit number from the keyboard. Add 8 to the number
and then divide it by 3. Now, the modulus of that number is taken with 5 and then
multiply the resultant value by 5. Display the final result.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϳ

4. Now, solve the above question using assignment operators (eg. +=, -=, *=).
5. Enter two numbers from keyboard. Write a program to check if the two numbers are

equal.

Answer:

6. Write a program to enter the values of two variables 'a' and 'b' from keyboard and
then check if both the conditions 'a < 50' and 'a < b' are true.
Answer:

7. Now solve the above question to check if atleast one of the conditions 'a < 50' and 'a
< b' is true.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϴ

8. If the marks of Robert in three subjects are 78,45 and 62 respectively (each out of
100 ), write a program to calculate his total marks and percentage marks.
9. Write a program to enter the values of two variables from the keyboard and then
interchange the values of the two variables. E.g.-
If entered value of x is 5 and y is 10 then
cout <<x << " and " << y
should print 10 and 5.
Answer:

10. Take input of some length in meter and covert it to feet and inches.
11. Write a program to convert Fahrenheit into Celsius.
12. The total number of students in a class are 45 out of which 25 are boys. If 80% of the
total students secured grade 'A' out of which 17 are boys, then write a program to
calculate the total number of girls getting grade 'A'.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϵ

Level 2:

1. Write a program to calculate the sum of the first and the second last digit of a 5 digit.
E.g.- NUMBER : 12345
OUTPUT : 1+4=5
Answer:

2. Take a 4 digit number. Write a program to display a number whose digits are 2
greater than the corresponding digits of the number TAKEN.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭϬ

For example, if the number which was taken is 5696, then the displayed number
should be 7818.
3. Write a program to calculate the sum of the digits of a 3-digit number.
Number : 132
Output : 6
4. Write a program to reverse a 3-digit number. E.g.-
Number : 132
Output : 231

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭϭ

Practice questions on Decide if/else

Level 1:

1. Take values of length and breadth of a rectangle from user and check if it is square or
not.
Answer:

2. Take two int values from user and print greatest among them.
3. A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
Ask user for quantity
Suppose, one unit will cost 100.
Judge and print total cost for user.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭϮ

4. A company decided to give bonus of 5% to employee if his/her year of service is more
than 5 years.
Ask user for their salary and year of service and print the net bonus amount.
5. A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭϯ

6. Take input of age of 3 people by user and determine oldest and youngest among
them.
7. Write a program to print absolute vlaue of a number entered by user. E.g.-
INPUT: 1 OUTPUT: 1
INPUT: -1 OUTPUT: 1
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭϰ

8. A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from user
Number of classes held
Number of classes attended.
And print
percentage of class attended
Is student is allowed to sit in exam or not.
9. Modify the above question to allow student to sit if he/she has medical cause. Ask
user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly.
10. If
x=2
y=5
z=0
then find values of the following expressions:
a. x == 2
b. x != 5
c. x != 5 && y >= 5
d. z != 0 || x == 2
e. !(y < 10)

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭϱ

11. Write a program to check whether a entered character is lowercase ( a to z ) or
uppercase ( A to Z ).

Level 2:

1. Write a program to check if a year is leap year or not.


If a year is divisible by 4 then it is leap year but if the year is century year like 2000,
1900, 2100 then it must be divisible by 400.
2. Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following
rules print their place of service.
if employee is female, then she will work only in urban areas.
if employee is a male and age is in between 20 to 40 then he may work in anywhere
if employee is male and age is in between 40 t0 60 then he will work in urban areas
only.
And any other input of age should print "ERROR".
3. A 4 digit number is entered through keyboard. Write a program to print a new
number with digits reversed as of orignal one. E.g.-
INPUT : 1234 OUTPUT : 4321
INPUT : 5982 OUTPUT : 2895

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭϲ

Practice questions on Loops
Level 1:

1. Print multiplication table of 24, 50 and 29 using loop.

Answer:

2. Take 10 integers from keyboard using loop and print their average value on the
screen.
Answer:

3. Print the following patterns using loop :


a.
*
**
***
****

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭϳ

b.
*
***
*****
***
*
c.
1010101
10101
101
1
Answer:

4. Print ASCII values and their equivalent characters. ASCII value vary from 0 to 255.

Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭϴ

5. Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1
0! = 0
Write a C++ program to calculate factorial of a number.
Answer:

6. Write a program to find greatest common divisor (GCD) or highest common factor
(HCF) of given two numbers.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϭϵ

7. Take integer inputs from user until he/she presses q ( Ask to press q to quit after
every integer input ). Print average and product of all numbers.
8. Write an infinite loop.
A inifinte loop never ends. Condition is always true.

Level 2:

1. Take as input a fraction in the form a/b. Convert the same into lowest terms and print.
(Lowest terms examples 3/12 = 1/4).
2. Calculate the sum of digits of a number given by user. E.g.-
INUPT : 123 OUPUT : 6
INUPT : 12345 OUPUT : 15
3. A three digit number is called Armstrong number if sum of cube of its digit is equal to
number itself.
E.g.- 153 is an Armstrong number because (13)+(53)+(33) = 153.
Write all Armstrong numbers between 100 to 500.
Answer:

4. Write a program to print all prime number in between 1 to 100.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮϬ

5. Write a program to find prime factor of a number.
If a factor of a number is prime number then it is its prime factor.
Answer:

6. Write a program to find the sum of the even and odd digits of the number which is
given as input.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮϭ

Practice questions on Function
Level 1:

1. Write a program to print the sum of two numbers entered by user by defining your
own function.
Answer:

2. Define a function that returns the product of two numbers entered by user.
3. Write a program to print the circumference and area of a circle of radius entered by
user by defining your own function.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮϮ

4. Define two functions to print the maximum and the minimum number respectively
among three numbers entered by user.
5. Define a program to find out whether a given number is even or odd.
Answer:

6. A person is elligible to vote if his/her age is greater than or equal to 18. Define a
function to find out if he/she is elligible to vote.
7. Define a function to find out if number is prime or not.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮϯ

8. Write a program which will ask the user to enter his/her marks (out of 100). Define a
function that will display grades according to the marks entered as below:
Marks Grade
91-100 AA
81-90 AB
71-80 BB
61-70 BC
51-60 CD
41-50 DD
<=40 Fail
9. Write a program to print the factorial of a number by defining a function named
'Factorial'.
Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1
0! = 0
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮϰ

Level 2:

1. Print the multiplication table of 15 using recursion.


Answer:

2. Define a function to print the prime factors of a number.


3. Using recursion, define a function to know nth term of a Fibonacci series.
Nth term of Fibonacci series is
F(n) = F(n-1)+F(n-2)
F(0) = 0
F(1) = 1
F(2) = F(1)+F(0) = 1+0 = 1
F(3) = F(2)+F(1) = 1+1 = 2
F(4) = F(3)+F(2) = 2+1 = 3
4. Define a function named 'perfect' that determines if parameter number is a perfect
number. Use this function in a program that determines and prints all the perfect
numbers between 1 and 1000.
[An integer number is said to be "perfect number" if its factors, including 1(but not
the number itself), sum to the number. E.g., 6 is a perfect number because 6=1+2+3].
5. Define a function to calculate power of a number raised to other i.e. ab using
recursion where the numbers 'a' and 'b' are to be entered by the user
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮϱ

6. Write a program that takes as input your gross salary and your total saving and uses
another function named taxCalculator() to calculate your tax. The taxCalculator()
function takes as parameters the gross salary as well as the total savings amount.
The tax is calculated as follows:
(a) The savings is deducted from the gross income to calculate the taxable income.
Maximum deduction of savings can be Rs. 100,000, even though the amount can be
more than this.
(b) For up to 100,000 as taxable income the tax is 0 (Slab 0); beyond 100,000 to
200,000 tax is 10% of the difference above 100,000 (Slab 1); beyond 200,000 up to
500,000 the net tax is the tax calculated from Slab 0 and Slab 1 and then 20% of the
taxable income exceeding 200,000 (Slab 2); if its more than 500,000, then the tax is
tax from Slab 0, Slab 1, Slab 2 and 30% of the amount exceeding 500,000.
7. Write a function that takes your date of birth in YYYY, MM and DD format (separated
by spaces) as input as well as the current date, in same format, and calculates your
age in years, months and days. You must check for leap years also. Write a separate
function to check for leap year.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮϲ

Practice questions on Pointers
Level 1:

1. Write a program to print the address of a variable whose value is input from user.
Answer:

2. Write a program to print the address of the pointer to a variable whose value is input
from user.
3. Write a program to print the value of the address of the pointer to a variable whose
value is input from user.
Answer:

4. Write a program to print a number which is entered from keyboard using pointer.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮϳ

Answer:

5. Write a function which will take pointer and display the number on screen. Take
number from user and print it on screen using that function.
Answer:

6. Write a program to find out the greatest and the smallest among three numbers
using pointers

Level 2:

1. Write a program to find the factorial of a number using pointers.


2. Write a program to reverse the digits a number using pointers.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮϴ

Practice questions on Array
Level 1:

1. Take 10 integer inputs from user and store them in an array and print them on
screen.
Answer:

2. Take 10 integer inputs from user and store them in an array. Again ask user to give a
number. Now, tell user whether that number is present in array or not.
3. Take 20 integer inputs from user and print the following:
number of positive numbers
number of negative numbers
number of odd numbers
number of even numbers
number of 0.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϮϵ

4. Take 10 integer inputs from user and store them in an array. Now, copy all the
elements in another array but in reverse order.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯϬ

5. Write a program to find the sum and product of all elements of an array.
6. Initialize and print all elements of a 2D array.
7. Find the largest and smallest elements of an array.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯϭ

8. Write a program to check if elements of an array are same or not it read from front or
back. E.g.-

2 3 15 15 3 2
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯϮ

9. Take an array of 10 elements. Split it into middle and store the elements in two
dfferent arrays. E.g.-
INITIAL array :

58 24 13 15 63 9 8 81 1 78

After spliting :

58 24 13 15 63
9 8 81 1 78

10. Consider an integer array, the number of elements in which is determined by the
user. The elements are also taken as input from the user. Write a program to find
those pair of elements that has the maximum and minimum difference among all
element pairs.
11. If the input array is [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60], your program should
be able to find that the subarray lies between the indexes 3 and 8.
12. Write a program to print sum, average of all numbers, smallest and largest element
of an array.

Level 2:

1. Take an array of length n where all the numbers are nonnegative and unique. Find
the element in the array possessing the highest value. Split the element into two
parts where first part contains the next highest value in the array and second part
hold the required additive entity to get the highest value. Print the array where the
highest value get splitted into those two parts.
Sample input: 4 8 6 3 2
Sample output: 4 6 2 6 3 2
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯϯ

2. Write a program to shift every element of an array to circularly right. E.g.-
INPUT : 1 2 3 4 5
OUTPUT : 5 1 2 3 4

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯϰ

Answer:

3. Initialize a 2D array of 3*3 matrix. E.g.-

1 2 3
4 5 6
7 8 9
Check if the matrix is symmetric or not.
Answer:

a[i][j] = a[j][i]

4. Sorting refers to arranging data in a particular format. Sort an array of integers in


ascending order. One of the algorithm is selection sort. Use below explanation of
selection sort to do this.
INITIAL ARRAY :

2 3 1 45 15

First iteration : Compare every element after first element with first element and if it
is larger then swap. In first iteration, 2 is larger than 1. So, swap it.

1 3 2 45 15

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯϱ

Second iteration : Compare every element after second element with second element
and if it is larger then swap. In second iteration, 3 is larger than 2. So, swap it.

1 2 3 45 15

Third iteration : Nothing will swap as 3 is smaller than every element after it.

1 2 3 45 15

Fourth iteration : Compare every element after fourth element with fourth element
and if it is larger then swap. In fourth iteration, 45 is larger than 15. So, swap it.

1 2 3 15 45
Answer:

5. Input any number. Find the sum of the digits of the number using a recursive function.

6. Pass a 2D array to function and access all its elements.

7. Write a program to add and multiply two 3x3 matrices.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯϲ

Practice questions on String
1. Write a program to find the first and the last occurence of the letter 'o' and character
',' in "Hello, World".
Answer:

For the last occurence, iterate from end.

2. Write a program to print a string entered by user.


Answer:

3. Write a program to print every character of a string entered by user in a new line
using loop.
Answer:

4. Write a program to input and display the sentence I love candies.


5. Write a program to find the length of the string "refrigerator".
6. Create an array of characters and then print the address of each of the elements of
the array. Take difference of two consecutive addresses and compare this with array
of integers.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯϳ

7. Write a program to enter a string s1 and copy it to another string s2.
Answer:

You can directly copy std::string with =. You can use strcpy for C-strings or you can m
ake your own function using for loop and copy each character.

8. Write a program to compare if the two strings entered by user are equal or not
without using predefined String functions.
Answer:

9. Write a program to check if the letter 'e' is present in the word 'Umbrella'.
10. Write a program to check if the word 'orange' is present in the "This is orange juice".

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯϴ

Answer:

11. Write the string after the first occurrence of ',' and the string after the last occurrence
of ',' in the string "Hello, Good, Morning".
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϯϵ

12. Write a program that takes your full name as input and displays the abbreviations of
the first and middle names except the last name which is displayed as it is. For
example, if your name is Robert Brett Roser, then the output should be R.B.Roser.
Answer:

1. Start from the end.


2. Check for the last word. There will be a space before the first character of the last
word or the first character will also be the first character of the string if the string con
tains only one word.
3. Copy this last word in a new string.
4. Repeat step 2 for the middle word but this time only insert the first character and a
dot(.) instead of the whole word in the new string.
5. Repeate till the loop ends by encountering the first character of the string.

13. Write a program to find the number of vowels, consonents, digits and white space
characters in a string.
14. Write a program to delete all consonents from the string "Hello, have a good day".
15. Input a string of alphabets. Find out the number of occurrence of all alphabets in that
string. Find out the alphabet with maximum occurrence.

Level 2:

1. Write a program to reverse a string with and without using any predefined function.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰϬ

2. Write a program to find the length of a string without using predefined functions.
3. Check the occurrence of the letter 'e' and the word 'is' in the sentence "This is
umbrella" without using predefined functions.
4. Write a program to find the number of vowels, consonents, digits and white space
characters in a string
5. Write a program to concatenate two strings "Hello" and "World".
6. Write a program to find out the largest and smallest word in the string "This is an
umbrella".
Answer:

You can assume that there is a single space between two words, except the last wor
d.

7. Write a program to check if a given string is a Palindrome.


A palindrome reads same from front and back e.g.- aba, ccaacc, mom, etc.
Answer:

8. Write down the names of 10 of your friends in an array and then sort those in
alphabetically ascending order.
Answer:

You can use any sorting algorithm (like one in the practice section of the chapter arra
y) and compare using strcmp function.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰϭ

9. Write a program to delete all the consonents from the string "Hello, have a good
day".
10. Write a program to delete the word "the" in the sentence "This is the lion in the
cage".
11. Write a program to check if the two strings entered by user are anagrams or not.
Two words are said to be anagrams if the letters of one word can be rearranged to
form the other word. For example, jaxa and ajax are anagrams of each other.
Answer:

A better approach would be to map every character of english alphabets with eleme
nts of an array having 26 (or 26*2 considering cases) elements representing each alp
habet. Try it yourself.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰϮ

12. Input a string which contains some palindrome substrings. Find out the position of
palindrome substrings if exist and replace it by *. (For example if input string is “bob
has a radar plane” then it should convert in “*** has a ***** plane”.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰϯ

13. Write a program to replace a given substring in a sentence with another string. For
example, in the sentence, ” A batman with bat” if we replace ”bat” with ”snow”, the
new sentence should be printed as ”A snowman with snow”.
14. Write a program to reverse individual words in a string, where each word may be
delimited by a dot, comma, space or tab, like www.google.com should become
www.elgoog.moc.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰϰ

Practice questions on Pre-processor
Level 1:

1.Write a macro to calculate area and perimeter of a rectangle.


Answer:

2.Write a macro to compare two numbers.


Answer:

3.Write a macro to find average of two numbers.


Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰϱ

4.Write a macro to find absolute value of number.
Answer:

5.Write a macro to calculate simple interest from principal, rate of interest and time.
Simple interest = (principal*rate of interest*time)/100.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰϲ

Practice questions on Structure
Level 1:

1.Write a program to store and print the roll no., name , age and marks of a student
using structures.
Answer:

2.Write a program to store the roll no. (starting from 1), name and age of 5 students
and then print the details of the student with roll no. 2.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰϳ

3.Write a program to store and print the roll no., name, age, address and marks of 15
students using structure.

4.Write a program to add two distances in inch-feet using structure. The values of
the distances is to be taken from the user.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰϴ

5.Enter the marks of 5 students in Chemistry, Mathematics and Physics (each out of
100) using a structure named Marks having elements roll no., name, chem_marks,
maths_marks and phy_marks and then display the percentage of each student.

6.Write a program to add, subtract and multiply two complex numbers using
structures to function.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϰϵ

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱϬ

7.Write a structure to store the roll no., name, age (between 11 to 14) and address of
students (more than 10). Store the information of the students.
1 - Write a function to print the names of all the students having age 14.
2 - Write another function to print the names of all the students having even roll no.
3 - Write another function to display the details of the student whose roll no is given
(i.e. roll no. entered by the user).

8.Write a structure to store the name, account number and balance of customers
(more than 10) and store their information.
1 - Write a function to print the names of all the customers having balance less than
$200.
2 - Write a function to add $100 in the balance of all the customers having more
than $1000 in their balance and then print the incremented value of their balance.
Answer:

Use array of structures. Functions will take the array of structures

9.Write a program to compare two dates entered by user. Make a structure named
Date to store the elements day, month and year to store the dates. If the dates are
equal, display "Dates are equal" otherwise display "Dates are not equal".

Level 2:

1.Write a structure to store the names, salary and hours of work per day of 10
employees in a company. Write a program to increase the salary depending on the
number of hours of work per day as follows and then print the name of all the
employees along with their final salaries.
Hours of work per day 8 10 >=12
Increase in salary $50 $100 $150

2.Let us work on the menu of a library. Create a structure containing book


information like accession number, name of author, book title and flag to know
whether book is issued or not.
Create a menu in which the following can be done.
1 - Display book information
2 - Add a new book
3 - Display all the books in the library of a particular author
4 - Display the number of books of a particular title
5 - Display the total number of books in the library
6 - Issue a book
(If we issue a book, then its number gets decreased by 1 and if we add a book, its
number gets increased by 1)

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱϭ

Answer:

3.Create a structure named Date having day, month and year as its elements. Store
the current date in the structure. Now add 45 days to the current date and display
the final date.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱϮ

Practice questions on Classes and objects

Level1:

1.Create a class named 'Student' with a string variable 'name' and an integer variable
'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an
object of the class Student.
Answer:

2.Assign and print the roll number, phone number and address of two students
having names "Sam" and "John" respectively by creating two objects of the class
'Student'.

3.Write a program to print the area and perimeter of a triangle having sides of 3, 4
and 5 units by creating a class named 'Triangle' with a function to print the area and
perimeter.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱϯ

4.Write a program to print the area and perimeter of a triangle having sides of 3, 4
and 5 units by creating a class named 'Triangle' with the constructor having the three
sides as its parameters.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱϰ

5.Write a program to print the area of two rectangles having sides (4,5) and (5,8)
respectively by creating a class named 'Rectangle' with a function named 'Area'
which returns the area. Length and breadth are passed as parameters to its
constructor.

6.Write a program to print the area of a rectangle by creating a class named 'Area'
having two functions. First function named as 'setDim' takes the length and breadth
of the rectangle as parameters and the second function named as 'getArea' returns
the area of the rectangle. Length and breadth of the rectangle are entered through
keyboard.
Answer:

7.Write a program to print the area of a rectangle by creating a class named 'Area'
taking the values of its length and breadth as parameters of its constructor and
having a function named 'returnArea' which returns the area of the rectangle. Length
and breadth of the rectangle are entered through keyboard.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱϱ

8.Print the average of three numbers entered by the user by creating a class named
'Average' having a function to calculate and print the average without creating any
object of the Average class.

9.Print the sum, difference and product of two complex numbers by creating a class
named 'Complex' with separate functions for each operation whose real and
imaginary parts are entered by the user.

Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱϲ

10.Write a program to print the volume of a box by creating a class named 'Volume'
with an initialization list to initialize its length, breadth and height. (just to make you
familiar with initialization lists)

11. Write a program that would print the information (name, year of joining, salary,
address) of three employees by creating a class named 'Employee'. The output
should be as follows:
Name Year of joining Address
Robert 1994 64C- WallsStreat
Sam 2000 68D- WallsStreat
John 1999 26B- WallsStreat

12.Add; two distances in inch-feet by creating a class named 'AddDistance'.

Level 2:

1.Write a program by creating an 'Employee' class having the following functions


and print the final salary.
1 - 'getInfo()' which takes the salary, number of hours of work per day of employee
as parameters
2 - 'AddSal()' which adds $10 to the salary of the employee if it is less than $500.
3 - 'AddWork()' which adds $5 to the salary of the employee if the number of hours
of work per day is more than 6 hours.

2.Create a class called 'Matrix' containing constructor that initializes the number of
rows and the number of columns of a new Matrix object. The Matrix class has the
following information:
1 - number of rows of matrix
2 - number of columns of matrix
3 - elements of matrix (You can use 2D vector)
The Matrix class has functions for each of the following:
1 - get the number of rows
2 - get the number of columns
3 - set the elements of the matrix at a given position (i,j)
4 - adding two matrices.
5 - multiplying the two matrices
You can assume that the dimensions are correct for the multiplication and addition.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱϳ

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱϴ

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϱϵ

Practice questions on Subclass

Level 1:

1.Create a class with a function that prints "This is parent class" and its subclass with
another function that prints "This is child class". Now, create an object for each class
and call
1 - function of the parent class by the object of the parent class
2 - function of the child class by the object of the child class
3 - function of the parent class by the object of the child class
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲϬ

2.Create a class named 'Member' having the following members:
Data members
1 - Name
2 - Age
3 - Phone number
4 - Address
It also has a function named 'printSalary' which prints the salary of the members.
Two classes 'Employee' and 'Manager' inherits the 'Member' class. The 'Employee'
and 'Manager' classes have data members 'specialization' and 'department'
respectively. Now, assign name, age, phone number, address and salary to an
employee and a manager by making an object of both of these classes and print the
same.

3.Create a class named 'Rectangle' with two data members 'length' and 'breadth'
and two functions to print the area and perimeter of the rectangle respectively. Its
constructor having parameters for length and breadth is used to initialize the length
and breadth of the rectangle. Let class 'Square' inherit the 'Rectangle' class with its
constructor having a parameter for its side (suppose s) calling the constructor of its
parent class. Print the area and perimeter of a rectangle and a square.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲϭ

4.Now repeat the above example to print the area of 10 squares.
Hint-Use array of objects

5.Create a class named 'Shape' with a function to print "This is a shape". Then create
two other classes named 'Rectangle' and 'Circle' inheriting the Shape class, both
having a function to print "This is rectangular shape" and "This is circular shape"
respectively. Create a subclass 'Square' of 'Rectangle' having a function to print
"Square is a rectangle". Now call the function of the 'Shape' and the 'Rectangle' class
by the object of the 'Square' class.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲϮ

Practice questions on Constructor overloading

Level 1:

1.Write a program to print the names of students by creating a Student class. If no


name is passed while creating an object of the Student class, then the name should
be "Unknown", otherwise the name should be equal to the String value passed while
creating the object of the Student class.
Answer:

2.Create a class named 'Rectangle' with two data members- length and breadth and
a function to calculate the area which is 'length*breadth'. The class has three
constructors which are :
1 - having no parameter - values of both length and breadth are assigned zero.
2 - having two numbers as parameters - the two numbers are assigned as length

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲϯ

and breadth respectively.
3 - having one number as parameter - both length and breadth are assigned that
number.
Now, create objects of the 'Rectangle' class having none, one and two parameters
and print their areas.

3.Suppose you have a Piggie Bank with an initial amount of $50 and you have to add
some more amount to it. Create a class 'AddAmount' with a data member named
'amount' with an initial value of $50. Now make two constructors of this class as
follows:
1 - without any parameter - no amount will be added to the Piggie Bank
2 - having a parameter which is the amount that will be added to the Piggie Bank
Create an object of the 'AddAmount' class and display the final amount in the Piggie
Bank.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲϰ

4.Create a class named 'Programming'. While creating an object of the class, if
nothing is passed to it, then the message "I love programming languages" should be
printed. If some String is passed to it, then in place of "programming languages" the
name of that String variable should be printed.
For example, while creating the object if we pass "cpp", then "I love cpp" should be
printed.

5.Create a class named 'PrintNumber' to print various numbers of different datatypes


by creating different functions with the same name 'printn' having a parameter for
each datatype.
Answer:

6.Create a class to print an integer and a character using two functions having the
same name but different sequence of the integer and the character parameters.

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲϱ

For example, if the parameters of the first function are of the form (int n, char c), then
that of the second function will be of the form (char c, int n).

7.Create a class to print the area of a square and a rectangle. The class has two
functions with the same name but different number of parameters. The function for
printing the area of rectangle has two parameters which are its length and breadth
respectively while the other function for printing the area of square has one
parameter which is the side of the square.
Answer:

8.Create a class 'Student' with three data members which are name, age and
address. The constructor of the class assigns default values to name as "unknown",
age as '0' and address as "not available". It has two functions with the same name
'setInfo'. First function has two parameters for name and age and assigns the same
whereas the second function takes has three parameters which are assigned to
name, age and address respectively. Print the name, age and address of 10 students.
Hint - Use array of objects

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲϲ

9.Create a class 'Degree' having a function 'getDegree' that prints "I got a degree". It
has two subclasses namely 'Undergraduate' and 'Postgraduate' each having a
function with the same name that prints "I am an Undergraduate" and "I am a
Postgraduate" respectively. Call the function by creating an object of each of the
three classes.

10.A boy has his money deposited $1000, $1500 and $2000 in banks-Bank A, Bank
B and Bank C respectively. We have to print the money deposited by him in a
particular bank.
Create a class 'Bank' with a function 'getBalance' which returns 0. Make its three
subclasses named 'BankA', 'BankB' and 'BankC' with a function with the same name
'getBalance' which returns the amount deposited in that particular bank. Call the
function 'getBalance' by the object of each of the three banks.
Answer:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲϳ

11.A class has an integer data member 'i' and a function named 'printNum' to print
the value of 'i'. Its subclass also has an integer data member 'j' and a function named
'printNum' to print the value of 'j'. Make an object of the subclass and use it to assign
a value to 'i' and to 'j'. Now call the function 'printNum' by this object.

12. All the banks operating in India are controlled by RBI. RBI has set a well defined
guideline (e.g. minimum interest rate, minimum balance allowed, maximum
withdrawal limit etc) which all banks must follow. For example, suppose RBI has set
minimum interest rate applicable to a saving bank account to be 4% annually;
however, banks are free to use 4% interest rate or to set any rates above it.
Write a program to implement bank functionality in the above scenario. Note: Create
few classes namely Customer, Account, RBI (Base Class) and few derived classes
(SBI, ICICI, PNB etc). Assume and implement required member variables and
functions in each class.
Hint:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲϴ

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϲϵ

Practice questions on Array of objects
1.Write a program to print the name, salary and date of joining of 10 employees in a
company. Use array of objects.

2.Write a program to print the roll number and average marks of 8 students in three
subjects (each out of 100). The marks are entered by the user and the roll numbers
are automatically assigned.

3.Write a program to calculate the average height of all the students of a class. The
number of students and their heights are entered by the user.

4.Lets create a bank account. Create a class named 'BankAccount' with the following
data members
1 - Name of depositor
2 - Address of depositor
3 - Type of account
4 - Balance in account
5 - Number of transactions
Class 'BankAccount' has a function for each of the following
1 - Generate a unique account number for each depositor
For the first depositor, account number will be BA1000, for the second depositor it
will be BA1001 and so on
2 - Display information and balance of depositor
3 - Deposit more amount in the balance of any depositor
4 - Withdraw some amount from the balance deposited
5 - Change the address of depositor
After creating the class, do the following operations
1 - Enter the information (name, address, type of account, balance) of the depositors.
Number of depositors are to be entered by the user.
2 - Print the information of any depositor.
3 - Add some amount to the account of any depositor and then display the final
information of that depositor
4 - Remove some amount from the account of any depositor and then display the
final information of that depositor
5 - Change the address of any depositor and then display the final information of that
depositor
6 - Randomly repeat these processes for some other bank accounts and after that
print the total number of transactions.

5.Write a program to create a directory that contains the following information.


(a) Name of a person

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϳϬ

(b) Address
(c) Telephone Number (if available with STD code)
(d) Mobile Number (if available)
(e) Head of the family

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϳϭ

Practice questions on Multiple inheritance
1.Create two classes named Mammals and MarineAnimals. Create another class
named BlueWhale which inherits both the above classes. Now, create a function in
each of these classes which prints "I am mammal", "I am a marine animal" and "I
belong to both the categories: Mammals as well as Marine Animals" respectively.
Now, create an object for each of the above class and try calling
1 - function of Mammals by the object of Mammal
2 - function of MarineAnimal by the object of MarineAnimal
3 - function of BlueWhale by the object of BlueWhale
4 - function of each of its parent by the object of BlueWhale

2.Make a class named Fruit with a data member to calculate the number of fruits in a
basket. Create two other class named Apples and Mangoes to calculate the number
of apples and mangoes in the basket. Print the number of fruits of each type and the
total number of fruits in the basket.

3.We want to calculate the total marks of each student of a class in


Physics,Chemistry and Mathematics and the average marks of the class. The number
of students in the class are entered by the user. Create a class named Marks with
data members for roll number, name and marks. Create three other classes inheriting
the Marks class, namely Physics, Chemistry and Mathematics, which are used to
define marks in individual subject of each student. Roll number of each student will
be generated automatically.

4.We want to store the information of different vehicles. Create a class named
Vehicle with two data member named mileage and price. Create its two subclasses
*Car with data members to store ownership cost, warranty (by years), seating
capacity and fuel type (diesel or petrol).
*Bike with data members to store the number of cylinders, number of gears, cooling
type(air, liquid or oil), wheel type(alloys or spokes) and fuel tank size(in inches)
Make another two subclasses Audi and Ford of Car, each having a data member to
store the model type. Next, make two subclasses Bajaj and TVS, each having a data
member to store the make-type.
Now, store and print the information of an Audi and a Ford car (i.e. model type,
ownership cost, warranty, seating capacity, fuel type, mileage and price.) Do the
same for a Bajaj and a TVS bike.

5.Create a class named Shape with a function that prints "This is a shape". Create
another class named Polygon inheriting the Shape class with the same function that
prints "Polygon is a shape". Create two other classes named Rectangle and Triangle
having the same function which prints "Rectangle is a polygon" and "Triangle is a

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϳϮ

polygon" respectively. Again, make another class named Square having the same
function which prints "Square is a rectangle".
Now, try calling the function by the object of each of these classes.

6.All the banks operating in India are controlled by RBI. RBI has set a well defined
guideline (e.g. minimum interest rate, minimum balance allowed, maximum
withdrawal limit etc) which all banks must follow. For example, suppose RBI has set
minimum interest rate applicable to a saving bank account to be 4% annually;
however, banks are free to use 4% interest rate or to set any rates above it.

Write a program to implement bank functionality in the above scenario. Note: Create
few classes namely Customer, Account, RBI (Base Class) and few derived classes
(SBI, ICICI, PNB etc). Assume and implement required member variables and
functions in each class.
Hint:

ĞǀĞůŽƉĞĚĂŶĚŝƐƚƌŝďƵƚĞĚďLJĂƌĞĞƌƌĞĚĞŶƟĂůƐϳϯ


You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy