Week 3 CT
Week 3 CT
Week 3 CT
Questions (1 & 2)
Statement
The following pseudocode is executed using the “Words” dataset.
1 P = 0, Q = 0
2 while(Table 1 has more rows){
3 Read the first row X in Table 1
4 if(X.LetterCount % 2 == 0){
5 P = P + 1
6 }
7 else{
8 if(X.PartOfSpeech == "Adverb"){
9 Q = Q + 1
10 }
11 }
12 Move X to Table 2
13 }
Question 1 [4 Marks]
Statement
What will P represent at the end of the execution?(MCQ)
Options
(a)
(b)
(c)
(d)
Answer
(a)
Question 2 [4 Marks]
Statement
What will Q represent at the end of execution?(MCQ)
Options
(a)
(b)
(c)
(d)
Answer
(b)
Solution
The given pseudocode is executed on "Words" dataset.
Line 4: Condition checks if the word in current row has letter count divisible by 2. If so then P
will be incremented. This refers that P represents the number of words with an even letter
count.
Line 7: else is being used, this means if the condition in line 4 is not satisfied then else block
will be executed. Which refers that the letter count of the word is not divisible by 2.
If the condition in line 8 is True, this means the word is an adverb. If so then Q gets
incremented. Therefore, Q stores the number of adverbs with an odd letter count.
Question 3 [4 Marks]
Statement
The following pseudocode is executed using the “Scores” dataset.(MCQ)
1 E = True
2 while(Table 1 has more rows){
3 Read the first row X in Table 1
4 if(X.Gender == 'F'){
5 if(X.Physics < 60 or X.Chemistry < 60 or X.Maths < 60){
6 E = False
7 }
8 }
9 Move X to Table 2
10 }
Options
(a)
All female students have scores greater than or equal to 60 in either Physics, Chemistry or Maths
(b)
All female students have scores less than 60 in either Physics, Chemistry or Maths
(c)
There is at least one female student with scores less than 60 in Physics, Chemistry or Maths
(d)
All female students have scores greater than or equal to 60 in Physics, Chemistry and Maths
Answer
(d)
Solution
E is initialized with True. It will be updated to False when both the if conditions are True. So if any
one of the conditions is False, then the value of E will remain True.
1. If the row belongs to a male, then E will remain True. But as we can see in the question, all
the options given to us are regarding female students so we can assume that we are talking
about female students here.
2. If the row belongs to a female, to keep E True, the condition in nested "if" should be False.
For a female student, if the condition in nested "if" is False always, then it means there does
not exist even a single female student who is having scores less than 60 in either
Physics,Chemistry or Maths. Therefore, to keep E True, all the female students must have
scores greater than or equal to 60 in all subjects.
1 Procedure checkShoppingBills(Y)
2 count = 0, totalAmount = 0, minAmount = MAX_VALUE
3 while(Pile 1 has more cards){
4 Read the top card X from Pile 1
5 if(X.ShopName == Y.ShopName){
6 count = count + 1
7 totalAmount = totalAmount + X.TotalBillAmount
8 if(X.TotalBillAmount < minAmount){
9 minAmount = X.TotalBillAmount
10 }
11 }
12 Move card X to Pile 2
13 }
14 averageAmount = totalAmount / count
15 *********************
16 * Fill the code *
17 *********************
18 End checkShoppingBills
Options
(a)
(b)
(d)
Answer
(d)
Solution
If a card (let us say Y) is passed as the parameter to the procedure checkShoppingBills . Let the
card is generated from a shop XYZ, then the procedure will return True minimum total bill amount
is greater than the average total bill amount generated from the shop XYZ.
To do so, first we need to collect all the cards which are generated from the same shop i.e., XYZ
and then find its average of total bill amount.
We can check that if the card X is from the same shop or not by using the condition X.ShopName
== Y.ShopName. This is what being done in the Line 5.
Line 6: Variable count stores the number of cards from the same shop.
Line 7: Total bill amount of every card which are from same shop is being added into variable
totalAmount which was initialized as 0.
Line 8: Will check if the minimum amount is greater than total bill amount of shop XYZ.If the
condition is satisfied then it will update minAmount variable which was initialized as
MAX_VALUE.
Line 14: avg stores the average of total bill amount generated from the same shop.
Now we just need to check if the minimum total bill amount of shop Y is greater than average or
not.
Option A: The "if condition" checks whether the minimum total bill amount is greater than or
equal to average. Thus option A is Wrong.
Option B: The "if condition" checks for card Y whether the total bill amount is greater than
minimum total bill amount. Thus option B is Wrong.
Option C- The "if condition" checks for card Y whether the total bill amount is greater than or
equal to minimum total bill amount. Thus option C is Wrong.
Option D- The "if condition" checks whether the minimum total bill amount is greater than
average. Thus option D is Correct.
1 A = 0
2 while(Table 1 has more rows){
3 Read the first row X in Table 1
4 B = True
5 if(X.Physics >= 60 and X.Chemistry >= 60 and X.Mathematics >= 60){
6 B = False
7 }
8 if(B){
9 A = A + 1
10 }
11 Move X to Table 2
12 }
Options
(a)
(b)
(c)
A will always be 0
(d)
Answer
(b)
Solution
The value of B will become False if all of the below conditions are True :-
To keep B True, student shouldn't score greater than or equal to 60 in atleast any one of the
subject. Therefore A represents the number of students failing in at least one subject with a score
below 60.
So the correct option is (b).
Question 6 [4 Marks]
Statement
The following pseudocode is executed using the “Scores” dataset. At the end of the execution,
variable Count captures the number of students whose total marks are more than the average (of
total marks) of entire dataset but have scored below the subject average in any two subjects.
Assume that the variable AvgT holds the value of the average total marks. Similarly, the variables
AvgP, AvgC and AvgM hold the value of the average marks of Physics, Chemistry and
Mathematics respectively. Choose the correct code fragment to complete the pseudocode.It is a
Multiple Select Question(MSQ).
1 Count = 0
2 while(Table 1 has more rows){
3 Read the first row X from Table 1
4 A = False, B = False, C = False, D = False
5 if(X.Total > AvgT){
6 A = True
7 }
8 if(X.Mathematics < AvgM and X.Physics < AvgP){
9 B = True
10 }
11 if(X.Physics < AvgP and X.Chemistry < AvgC){
12 C = True
13 }
14 if(X.Chemistry < AvgC and X.Mathematics < AvgM){
15 D = True
16 }
17 *************************
18 ** Fill the code **
19 *************************
20 Move X to Table 2
21 }
Options
(a)
(b)
(d)
1 if(A or (B or C) or (C or D) or (D or B)){
2 Count = Count + 1
3 }
Answer
(a)
Solution
Let us first understand the meaning of each variable using the given pseudocode:
Now, as per the question statement Count will be incremented only if A is True and at least one
among B, C and D are True.
Here, in condition for if statement, we can't say A "or" other as we have to satisfy both 1 and 2
conditions mentioned above.
Option a: A and (B or C or D) --
Let A is True
Overall = True
This will be True if any one variables are True among B, C ,D.
Option b: A and (B and C) and (C and D) and (D and B) , it's checking for all the variables to be true
1 SumA = 0, SumB = 0
2 CountA = 0, CountB = 0
3 A = 0, J = 0, K = 0
4
5 while(Pile 1 has more cards){
6 Read the top card X from Pile 1
7 if(X.CityTown == "Vellore"){
8 SumA = SumA + X.Total
9 CountA = CountA + 1
10 }
11 if(X.CityTown == "Chennai"){
12 SumB = SumB + X.Total
13 CountB = CountB + 1
14 }
15 Move card X to Pile 2
16 }
17
18 J = SumA / CountA
19 K = SumB / CountB
20
21 while(Pile 2 has more cards){
22 Read the top card X from Pile 2
23 if(X.CityTown == "Madurai"){
24 if(X.Total > J){
25 if(X.Total < K){
26 A = A + 1
27 }
28 }
29 }
30 Move card X to Pile 1
31 }
At the end of the execution, A represents the number of students from Madurai having total
marks.............
Options
(a)
greater than the average marks of students from Vellore and Chennai
(c)
greater than the average marks of students from Vellore but less than that of Chennai
(d)
greater than the average marks of students from Chennai but less than that of Vellore
Answer
(c)
Solution
In the first while loop we are computing values of SumA, CountA, SumB and CountB. Let us
understand what these variable represent in this pseudocode:
After the first while loop we are calculating values of J and K which represents average marks of
students from Vellore and Chennai respectively.
first, the card is being checked to find if it belongs to the student from "Vellore" or not.
If True, then Total marks is being checked if it is greater than J or not. If so, then Total marks
is being checked if it is less than K or not. If this is also True then, A is being incremented.
So, basically, in the end, A represents the number of students from Madurai having total marks
"greater than the average marks of students from Vellore but less than that of Chennai".
1 1. A = 0
2 2. while(Table 1 has more rows){
3 3. B = True
4 4. Read the first row X from Table 1
5 5. if(X.Gender == 'F'){
6 6. if(X.Mathematics < M){
7 7. B = False
8 8. }
9 9. if(X.Physics < P){
10 10. B = True
11 11. }
12 12. if(X.Chemistry < C){
13 13. B = False
14 14. }
15 15. }
16 16. if(B){
17 17. A = A + 1
18 18. }
19 19. Move X to Table 2
20 20. }
Options
(a)
(b)
(c)
(d)
Answer
(b), (c), (d)
Solution
The variable A counts the number of female students who have scored less than the subject
average in at least one subject (either in one subject or in more than one subjects).
Line 16: A is being incremented whenever B is True. This means B should be True when the
card belongs to a female student and the subject marks of at least one subject is less than
the average of that subject.
Line 5 - Line 15: It is totally clear that B should be True in lines 7 and 13. Therefore options c
and d are correct.
Line 3: As A gets incremented only if B is True, then B should be initialized with False.
Question 9 [4 Marks]
Statement
The following pseudocode is executed using the “Words” dataset. What does A represent?(MCQ)
1 A = 0
2 while(Pile 1 has more cards){
3 Read the top card X from Pile 1
4 A = A + isSpecialWord(X)
5 if(X.Word ends with full stop){
6 A = 0
7 }
8 Move X to Pile 2
9 }
10 Procedure isSpecialWord(X)
11 if(X.Word == "the"){
12 return(1)
13 }
14 return(0)
15 End isSpecialWord
Options
(a)
(b)
(c)
(d)
Answer
(d)
Solution
As we can see in line 4, A is getting incremented if procedure isSpecialWord() returns 1.
In line 5, it checks whether the word ends with full stop, if yes reset A to 0. That means, A is
looking for "the" in every sentence.
Therefore, A is counting the occurrence of "the" in every sentence. Hence, Option (d) is correct.
Question 10 [4 Marks]
Statement
The following pseudocode is executed using the “Words” dataset. What will A represent at the end
of the execution?(MCQ)
1 SumT = 0, CountT = 0, B = 0
2 while(Table 1 has more rows){
3 Read the first row X in Table 1
4 CountT = CountT + 1
5 SumT = SumT + X.LetterCount
6 Move X to Table 2
7 }
8 B = SumT / CountT
9 A = 0
10 while(Table 2 has more rows){
11 Read the first row X in Table 2
12 if(X.Word ends with a comma){
13 if(X.LetterCount > B){
14 A = A + 1
15 }
16 }
17 Move X to Table 1
18 }
Options
(a)
(b)
Number of words that end with a comma and have a letter count greater than ratio of number of
words to sum of letter count.
(c)
Number of words that end with a comma and have a letter count less than or equal to the average
letter count of dataset.
(d)
Number of words that end with a comma and have a letter count greater than the average letter
count of dataset.
Answer
(d)
Solution
In first while loop,
CountT is keeping command of "count of cards (or words)" of dataset. And SumT is keeping
command of "sum of letter count of words" of whole dataset.
Then we can see, B = SumT / CountT , means "the average letter count of dataset."
If conditional says, that word ends with comma then A will get incremented by 1 if X.LetterCount
> B. That means, A will maintain the number of words that end with a comma and have a letter
count greater than the average letter count of dataset.