Practice Question for CS101 Special Test
uestion 1 - Perimeter
Q
Question 2 - Speed
Question 3 - Positive, Negative, Zero
Question 4 - Library
Question 5 - Age
Question 6 - Factorial
Question 7 - Sum even numbers from 1 to N
Question 8 - Multiplication tables
Question 9 - Pattern
Question 10 - Quiz Average
Question 11 - Minimum and Maximum
Question 12 - Count less than N
Question 13 - Count even and odd
Question 14 - Celsius to Fahrenheit
Question 15 - Sum of the square of digits
Question 16 - Circle
Question 17 - Resize rectangle
Question 18 - Struct Rectangle
Question 19 - Struct Grade
Question 20 - Struct Book
Question 1 - Perimeter
riteaC++programtoaccepttwointegersrepresentingthelengthandbreadthofarectangleasinputfromthe
W
user. Calculate and print the perimeter of the rectangle.
(
2 * (length + breadth) )
est Cases:
T
- Input: 5 3
Output: 16
- Input: 7 2
Output: 18
- Input: 10 10
Output: 40
Question 2 - Speed
aj wenttoMatheranonacyclelastweek.HetraveledforadistanceD.Oncehereachedhisdestination,he
R
realized that it took a total of T hours. He is curious as to how fast he was cycling, for which he wants to
calculate average speed in km/hr using the formula:speed=distance/time.WriteaC++programtoinputD
and T and print the speed.
est Cases:
T
- Input: 100 2
Output: 50
- Input: 150 3
Output: 50
- Input: 200 4
Output: 50
Question 3 - Positive, Negative, Zero
Seema has a number and she wants to check if it is positive, negative, or zero. Write a C++ program to help her.
est Cases:
T
- Input: 5
Output: Positive
- Input: -3
Output: Negative
- Input: 0
Output: Zero
Question 4 - Library
ouarehelpingalibrariancategorizebooks.Ifthebook'sgenreis"fiction,"print"Thisisafictionalbook."Ifthe
Y
genre is "non-fiction," print "This is a non-fictional book." Otherwise, print " Genre unknown. "
est Cases:
T
- Input: fiction
Output: This is a fictional book
- Input: non-fiction
Output: This is a non-fictional book.
- Input: mystery
Output: Genre unknown.
Question 5 - Age
riteaC++programtoinputage.Iftheageislessthan13,print"Youareachild."Iftheageisbetween13and
W
19, print "You are a teenager." If the age is 20 or older, print "You are an adult."
est Cases:
T
- Input: 10
Output: You are a child.
- Input: 16
Output: You are a teenager.
- Input: 25
Output: You are an adult.
Question 6 - Factorial
Write a program that calculates the factorial of a number n (0 to 10) given by the user.
est Cases:
T
- Input: 5
Output: 120
- Input: 3
Output: 6
- Input: 1
Output: 1
Question 7 - Sum even numbers from 1 to N
oudecidetohostasmallpicnicwithyourfriendsinthepark.Asyouplanthesnacks,youwanttomakesure
Y
youhaveenougheven-numberedsandwichesforeveryone,soyoustartcounting.Youdecidetosumupallthe
evennumbersfrom1tothetotalnumberofsandwichesyouplantomake,justtoseehowmanyyou’llneedfor
the picnic. Write a C++ program to input N (the total number of sandwiches) and print the sum of all even
numbers from 1 to N.
estcases:
T
Input: 6
Output: 12 (2 + 4 + 6)
Input: 10
Output: 30 (2 + 4 + 6 + 8 + 10)
Input: 5
Output: 6 (2 + 4)
Question 8 - Multiplication tables
upposeyouarecreatingsoftwaretohelpschoolchildrenlearnmultiplicationtables.WriteaC++programthat
S
inputs a number, n, and prints its multiplication table (from 1 to 10).
est Cases:
T
- Input: 2
Output: 2 4 6 8 10 12 14 16 18 20
- Input: 5
Output: 5 10 15 20 25 30 35 40 45 50
- Input: 3
Output: 3 6 9 12 15 18 21 24 27 30
Question 9 - Pattern
Write a program that prints a right-angled triangle pattern of stars. The number of rows will be provided as input.
*
* *
* * *
est cases
T
Input: 3
Output:
*
* *
* * *
Input: 5
Output:
*
* *
* * *
* * * *
* * * * *
Question 10 - Quiz Average
S101theoryquizwasconducted.Theinstructorwantstoknowtheaverage.Youaretaskedwiththisactivity.
C
Write a C++ program to accept N i.e. the number of students, followed by N floating point numbers that
representthescoreofthestudent.Printtheaverage.AssumethatNwillbebetween1and100,bothinclusive
and marks will be between 1 and 14, both inclusive.
estcases
T
Input: 5 12.5 10.0 14.0 9.5 11.0
Output: 11.4 ((12.5 + 10.0 + 14.0 + 9.5 + 11.0)/5)
Input: 3 7.0 8.0 6.0
Output: 7.0
Input: 4 13.5 10.5 14.0 11.0
Output: 12.25
Question 11 - Minimum and Maximum
InarecentcricketmatchbetweenIndiaandAustralia,youneedtoanalyzetheperformanceofIndianbatsmen.
Write a C++ program that takes the runs scored by each Indian player in an array and determines both the
maximum and minimum runs scored by any player. It is assumed that all players had the opportunity to bat.
estcases
T
Input: 10 45 67 89 23 56 78 34 12 90 11
Output: 90 10
Input: 60 100 200 150 80 60 90 30 40 120 110
Output: 200 30
Input: 32 15 25 35 5 55 65 75 85 95 45
Output: 95 5
Question 12 - Count less than N
ajandSimranarelookingforahousetorent.Theyhavesetabudgetlimitforthemonthlyrenttheycanafford.
R
After visiting a certain number ofhouses,theywanttoknowhowmanyofthesehouseshaverentwithintheir
budget.WriteaC++programtoinputthebudgetlimitB(aninteger),thenumberofhousestheyvisitedN,and
therent(aninteger)foreachoftheseNhouses.Theprogramshouldoutputthecountofhousesthatfallwithin
their budget.
estcases
T
Input: 5000 5 4000 6000 3500 5000 7000
Output: 3
Explanation:
- Budget limit B = 5000
- House rents: 4000, 6000, 3500, 5000, 7000
- 3 houses (4000, 3500, and 5000) are within or equal to the budget.
Input: 3000 4 2500 1500 3500 2000
Output: 3
Input: 10000 6 8000 12000 9000 15000 10000 9500
Output: 4
Question 13 - Count even and odd
umbai city is known for its fast life and a lot of traffic. The city also faces issues duetothelimitedparking
M
space.Tosolvethisproblem,CS101studentscameupwiththeideathateachparkinglotbegivenanumber.
The cars with even licenseplatenumbersshouldbeparkedintheeven-numberedparkinglot,andthosewith
oddlicenseplatenumbersshouldbeparkedintheodd-numberedparkinglot.However,wemustfirstdetermine
how many cars have even and odd license plate numbers. WriteaC++programthatinputsthelicenseplate
numbers (1001 to 9999) in an array. Print the count of cars having even and odd license plates.
estcases
T
- Input: 3 1001 2002 3003
Output: 1 2
- Input: 4 1234 5678 9101 1121
Output: 2 2
- Input: 5 1357 2468 9753 8642 1359
Output: 2 3
Question 14 - Celsius to Fahrenheit
riteaC++programtoinputthetemperatureinCelsiusasafloating-pointnumberinthemainfunction.Passit
W
to a function called ‘convert’. In the function, convert the temperature to Fahrenheit and print the result.
(Formula: Fahrenheit = Celsius * (9/5) + 32)
est Cases:
T
- Input: 0
Output: 32
- Input: 25
Output: 77
- Input: -10
Output: 14
Question 15 - Sum of the square of digits
ayCart is an online shopping platform that offers various discount schemes to its users. The latest scheme
Y
allowscustomerstoreceiveadiscountbasedonthesumofthesquaresofthedigitsofthetotalcostofitemsin
their cart. Specifically, the calculated sum is subtracted from the original cost to determine the final amount
payable. Write a C++ program that accepts the number of customers, N, who are checking out. For each
customer,inputthecost(positiveintegers)oftheiritems.Passthecosttoafunctionthatcomputesandreturns
the discount. Output the final amount thecustomerneedstopayafterapplyingthediscount.Assumethatthe
sum of the squares of the digits of the number (cost) will always be less than the actual cost.
estcases
T
- Input: 2 150 998
Output: 124 772
- Input: 3 2674 100 90
Output: 2569 99 9
- Input: 4 874 123 962 556
Output: 745 109 841 470
Question 16 - Circle
riteaC++programthattakestheradiusofacircleasinput.Passtheradiustoafunctionthatcalculatesthe
W
diameter, circumference, and area of the circle. Finally, display these results in the main function.
estcaess
T
- Input: 5
Output: 10 31.4159 78.5397
- Input: 7
Output: 14 43.9823 153.938
- Input: 3
Output: 6 18.8495 28.2743
Question 17 - Resize rectangle
strawberryfarmerinMahabaleshwarhasarectangularplotoflandwherehecultivatesstrawberries.Theplot
A
isdefinedbyitstopleftcorneratcoordinates(x1,y1)andbottomrightcornerat(x2,y2).Thefarmerhasdecided
toexpandthisplotandhaschosenaresizingfactor.However,hewillkeepthetopleftcornerfixed.Heisfinding
it difficult to calculate and has asked you for help to compute the new coordinates (x2,y2).
rite a C++ program that accepts the coordinates x1, y1, x2, y2, and the factor,f,fromtheuserinthemain
W
function.Passx1,y1,andfactorbyvalue,andx2andy2byreferencetothefunction.Inthefunction,compute
the updated coordinates and print the result in the main.
estcases
T
- Input: 0 0 10 10 3
Output: 30 30
- Input: 2 3 5 7 2
Output: 8 11
- Input: 1 1 4 5 5
Output: 16 21
Question 18 - Struct Rectangle
reate a struct Rectangle withtwointegermembervariables:lengthandbreadth.Writeafunctionareainside
C
thestructthatreturnstheareaoftherectangle.WriteaprogramtocreateaRectangleobject,inputthelength
and breadth, and print the area.
est Cases:
T
- Input: 5 4
Output: 20
- Input: 6 7
Output: 42
- Input: 10 3
Output: 30
Question 19 - Struct Grade
efineastructStudentwithmembervariablesname(string)andmarks(integer).Writeafunctiongradeinside
D
thestructthatreturns'A'ifmarksaregreaterthanorequalto75,'B'ifbetween50and74,and'C'ifbelow50.
Write a program to create a Student object, input the name and marks, and print the grade.
est Cases:
T
- Input: John 85
Output: A
- Input: Alice 65
Output: B
- Input: Tom 45
Output: C
Question 20 - Struct Book
efineastructBookwithmembervariablestitle(string),author(string),andprice(float).Writeafunctiondisplay
D
insidethestructurethatprintsthetitleandpriceofthebook.WriteaprogramtocreateaBookobject,inputthe
title, author, and price, and display the title and price.
est Cases:
T
- Input: C++_Primer Stanley 500.50
Output: C++_Primer 500.50
- Input: Algorithms CLRS 650.75
Output: Algorithms 650.75
- Input: Data_Structures Mark 300.00
Output: Data_Structures 300