CT Week 8 GA
CT Week 8 GA
Question-1 [3 Marks]
Statement
The following pseudocode is executed using the "Scores" table. What will the values of A and B
represent at the end of the execution?
1 D = {}
2 while(Table 1 has more rows){
3 Read the fist row X in Table 1
4 if(isKey(D, X.Town/City)){
5 if(D[X.Town/City] > X.Mathematics){
6 D[X.Town/City] = X.Mathematics
7 }
8 }
9 else{
10 D[X.Town/City] = X.Mathematics
11 }
12 Move X to Table 2
13 }
14
15 A = 0, B = 100
16 foreach Y in keys(D){
17 if(B == D[Y]){
18 A = A + 1
19 }
20 if(B > D[Y]){
21 A = 1
22 B = D[Y]
23 }
24 }
Options
(a)
(b)
(c)
(d)
Answer
(c)
Solution
D is initialized as an empty dictionary. The scores table is iterated using the iterator X. In each
iteration, the if-condition check whether the X.CityTown is a key in D. If the condition evaluates to
True, then another if-condition (nested inside) checks whether
the Mathematics marks of X less than D[X.CityTown]. If the condition evaluates to true, then value
D[X.CityTown] is replaced by X.Mathematics. More precisely, D[X.CityTown] is updated by a new
value if X.Mathematics is lesser than it. Then, D[X.CityTown] stores the lowest mathematics mark of
students from X.CityTown. There is no else-block for the nested (inner) if-block. Then we come out
of the outer if-block.
The else-part of the outer if-block is executed when X.CityTown is not a key of D. Here, we create a
key X.CityTown in D, and initialize the value for the key as X.Mathematics. At the end of the while-
block, D captures the following information: For each CityTown C, D[C] is the lowest Mathematics
marks of the students from the city C.
Variables A and B are initialized with the value zero. The keys of D are iterated using the iterator
Y. In each iteration, The first if-condition checks whether B and D[Y] are same. If the condition
evaluates to True, then the value of A is incremented by one. The second if-condition checks B >
D[Y]. If the condition evaluates to True, then A is set to one and B is set to D[Y]. Based on the
usage of variables, A is used as a counter variable and Bis used to store some value and it is
updated when there is a smaller value than B is assigned for a key in D. Therefore, whenever a
key Y is encountered with B > D[Y], B is updated to D[Y], and A is set to one. Whenever a key
encountered with B == D[Y], the value of A is incremented by one.
Thus, B captures the lowest value among the values in D, and A captures the number of
occurrence of the lowest value. Since D captures the lowest mathematics marks of each city, A
and B capture the following information:
A = Number of cities where students score the lowest marks in Mathematics
B = The lowest marks in Mathematics
Question-2 [4 Marks]
Statement
The following pseudocode is executed using the "Scores" dataset. What will the value of B
represent at the end of the execution?
1 D = {}, B = 0
2 while(Table 1 has more rows){
3 Read the first row X in Table 1
4 D = updateDictByField(D, X.Physics)
5 Move X to Table 2
6 }
7 B = findAKey(D)
8
9 Procedure updateDictByField(D, Value)
10 if(isKey(D, Value)){
11 D[Value] = D[Value] + 1
12 }
13 else{
14 D[Value] = 1
15 }
16 return(D)
17 End updateDictByField
18
19 Procedure findAKey(D)
20 Key = -1, Value = 0
21 foreach A in keys(D){
22 if(D[A] > Value){
23 Value = D[A]
24 Key = A
25 }
26 }
27 return(Key)
28 End findAKey
Options
(a)
(b)
(c)
Answer
(d)
Solution
First we loot at the procedure updateDictByField. The parameters passed to the procedure are a
dictionary and a value. The if-condition checks whether the Value is a key of D. If the condition
evaluates to True, then the value of D[Value] is incremented by one. Otherwise, D[Value] is
initialized with value one. Overall, the procedure updates the dictionary D as follows: if Value is
not a key then create it as a key of D and initialize with a value one, otherwise increment the value
of D[Value] by one.
Now, we look at the procedure findAKey. Given a parameter D, the procedure computes the
following: Two variables Key and Value are initialized with values -1 and 0. The keys of D are
iterated using the iterator A. During the iteration, Key captures the key which holds the maximum
value assigned in D, and Value captures the maximum value. At the end of the procedure, Key is
returned.
In the main pseudocode, D is initialized as an empty dictionary. The scores table is iterated using
an iterator X. In each step of iteration, the procedure updateDictByField is called with the
parameters D and X.Physics. At the end of the iteration, the keys of D are the Physics marks
scored by the students, and the values are the number of times each marks are scored by the
students. The procedure findAKey is called with the parameter D. It returns the key which
assigned with a maximum value. Since the keys of D are the Physics marks, the returned value
from the procedure is the Physics marks which is scored by maximum number of people.
Therefore, B captures the following information: Most frequent marks in Physics.
Question-3 [6 Marks]
Statement
The following pseudocode is executed using the "Scores" dataset. What will be the value of B at
the end of the execution?
Answer
10
Solution
The procedure updateDictByField receives a dictionary D, a row Y and a subject Subject as
parameters. First, we check whether Y.CityTown is a key in D[Subject]. If it evaluates to True, then
we update the value of the key Y.CityTown as Y.Subject when existing value of the key is less than
Y.Subject. That is, D captures the maximum marks of a student in the subject Subject in each city.
The procedure eligible receives the dictionary D and a student row X as parameters. For each
subject, we check whether the student is city topper in the subject where the maximum marks of
the subject in the city is stored in the dictionary D. Therefore, C captures the number of subjects
in which X is a city topper in the subject.
Question-4 [5 Marks]
Statement
The following pseudocode is executed using the "Shopping Bills" dataset. At the end of the
execution, the variable D captures the following information: for each customer Z, D[Z] ["Shop"]
stores the shops visited by Z, and D[Z] ["Category"] stores the categories of the items purchased
by Z. But the pseudocode may have mistakes. Identify all such mistakes (if any). It is a Multiple
Select Question (MSQ).
1 D = {}
2 while(Pile 1 has more cards){
3 Read the top card X in Pile 1
4 D = updateDictionary(D, X)
5 Move X to Pile 2
6 }
7
8 Procedure updateDictionary(D, Y)
9 if(not isKey(D, Y.CustomerName)){
10 D[Y.CustomerName] = {"Shop": [], "Category": []}
11 }
12 D[Y.CustomerName]["Shop"][Y.ShopName] = True
13 foreach A in Y.ItemList{
14 D[Y.CustomerName]["Shop"][A.Category] = True
15 }
16 return(D)
17 End updateDictionary
Options
(a)
Error in Line 1
(b)
Error in Line 10
(c)
Error in Line 13
(d)
Error in Line 14
(e)
Error in Line 16
(g)
No error
Answer
(b), (d)
Solution
The objective of the pseudocode is to nd the following: for each customer Z, find the shops visited
by Z, and the item categories which are bought by C. In the pseudocode, D is used to store the
above information. For each customer Z, D[Z] is a dictionary with keys "Shop" and "Category".
D[Z] ["Shop"] stores the shops that are visited by Z and D[Z] ["Category"] stores the categories of
the items purchased by Z. So, D[Z] ["Shop"] is a dictionary that assigns True value for all shop
visited by Z. Similarly, D[Z] ["Category"] is also a dictionary.
The first step is to read the cards and generate the dictionary D. Line 1 - 6 perform the reading
and invoke the procedure updateDictionary. Clearly, the pseudocode has no error in this part.
Let us look at the procedure updateDictionary. Here we have to update the dictionary fields for
the key Y.CustomerName. The first step is to check whether Y.CustomerName is a key in D. If it is
not a key, then create X.CustomerName as a key. The value for the key is defined as follows:
In Line 9, the keys are assigned to empty lists. This is a mistake in the pseudocode. Therefore,
Error in Line 9:
Note that the keys are assigned to empty dictionaries since we encounter the customer for the
first time. If the customer name is exists in the keys list, then we move to the next step. Next we
have to add the shop name to the dictionary. This is done in Line 11. Note that the shop name is
already exists in the dictionary, then it overwrite the true assignment. It will not harm the
objective in any aspect. Finally, we check the item categories. Iterate the item list of the bill using
the iterator A. Add the item category to the dictionary in the field "Category" as follows:
In Line 13, the item category is added to the key "Shop". Therefore,
1 STN = {}
2 while(Pile 1 has more cards){
3 Read the top card X in Pile 1
4 STN[X.StationName] = getInfo(STN, X)
5 Move X to Pile 2
6 }
7
8 Procedure getInfo(STN, X)
9 *********************
10 * Fill the code *
11 *********************
12 return(D)
13 End getInfo
Question-5 [4 Marks]
Statement
Choose the correct code fragment to complete the pseudocode.
Options
(a)
1 D = {}
2 foreach A in X.TrainList{
3 foreach B in A.Days{
4 D[B] = 1
5 }
6 }
(b)
1 D = {}
2 foreach A in X.TrainList{
3 foreach B in A.Days{
4 D[B] = D[B] + 1
5 }
6 }
(d)
Answer
(d)
Solution
The dictionary STN captures the following information. For each station X, and for each day of a
week A, STN[X] [A] is an integer that represents the number of trains are running through X on
the day A. First we have to understand the structure of D. First of all, D is a dictionary where
stations are the keys. For each station X, STN[X] is another dictionary where days of a week are
the keys.
In the pseudocode, the cards are iterated using the iterator X. In each step of the iteration,
STN[X.StationName] is obtained from the procedure getInfo.
Now we look at the procedure getInfo. As we know that STN[X.StationName] is a dictionary, the
procedure getInfo should return a dictionary. Since it returns D, D should be a dictionary which
can be initialized as an empty dictionary.
D={}
With this initialization of D, we need to create/initialize keys when they found for the first time. If
the key already exists, then we can update the keys. But, none of our answers are checking the
existence of any key in D. Therefore, D should be initialized with keys and initial values for the
keys.
We know that the keys of D are days of a week. Then, the keys of D is fixed and the values are
non-negative integers. Therefore, D can be initialized as follows:
We assign the values are zero since no train is encountered for any day so far. Train list of the
station is iterated using the iterator A.
foreach A in X.TrainList
D[B] = D[B] + 1
Question-6 [3 Marks]
Statement
The following pseudocode is executed using the "station wise" cards of the "Train" dataset.
Consider the dictionary STN computed in the previous question. Choose the correct statement(s)
from the options based on the pseudocode. It is a Multiple Select Question (MSQ).
1 Z = {}, D = {}
2 foreach A in keys(STN){
3 C = 0, Y = 0
4 D = STN[A]
5 foreach B in keys(D){
6 if(Y == D[B]){
7 C = C + 1
8 }
9 if(Y < D[B]){
10 C = 1
11 Y = D[B]
12 }
13 }
14 if(not isKey(Z, C)){
15 Z[C] = 0
16 }
17 Z[C] = Z[C] + 1
18 }
Options
(a)
(b)
(c)
(d)
Answer
(a), (c)
Solution
First, we understand the pseudocode. D is initialized as an empty dictionary. keys(STN) is iterated
using the iterator A. In each step of iteration, D stores STN[A]. We know that STN[A] is a
dictionary. In the subsequent foreach-block, we find the maximum value assigned to any key and
number of times the value appeared in D. These information are stored in Y and C, respectively.
Once we computed C, we check whether C is a key in Z. Since C is an integer and Z[C] is also
assigned to an integer, both keys and values of Z are integers. Therefore,
Observe that the initial value of each key is set to zero. But in the next line, the value is
incremented by one. That is, for each key C, Z[C] is non-zero. Therefore,
C stores the maximum occurrence of the max value in dictionary D. Since keys of D are days of a
week, the maximum possible value of C is seven.
Question-7 [4 Marks]
Statement
Consider the dictionary STN computed in the previous question. Choose the correct pseudocode
to compute the number of stations which have trains passing through all days of a week.
Options
(a)
1 Z = 0
2 foreach A in keys(STN){
3 foreach B in keys(STN[A]){
4 if(STN[A][B] < 1){
5 C = False
6 }
7 }
8 if(C){
9 Z = Z + 1
10 }
11 }
(b)
1 Z = 0
2 foreach A in keys(STN){
3 C = True
4 foreach B in keys(STN[A]){
5 if(STN[A][B] < 1){
6 C = False
7 }
8 }
9 if(C){
10 Z = Z + 1
11 }
12 }
(c)
1 Z = 0
2 foreach A in keys(STN){
3 C = True
4 foreach B in keys(STN[A]){
5 if(STN[A][B] < 1){
6 C = False
7 }
8 else{
9 C = True
10 }
11 }
12 if(C){
13 Z = Z + 1
14 }
15 }
(d)
1 Z = 0
2 foreach A in keys(STN){
3 C = False
4 foreach B in keys(STN[A]){
5 if(STN[A][B] < 1){
6 C = True
7 }
8 }
9 if(C){
10 Z = Z + 1
11 }
12 }
Answer
(b)
Solution
For a station A and a day B, STN[A] [B] denotes the number of trains passing through the station
A on the day B. We iterate the keys of STN using A, and keys of STN[A] is iterated using B. The
role of C is to capture that the station A is busy on all days. Then, we should initialize C to True.
C = True
If we nd a day in which no trains are passing through station A, then we have to set C to False.
That is,
The variable Z should be incremented when C remains True at the end of the iteration over the
keys of STN[A].
if(C){
Z=Z+1
}
Question-(8 to 9) [7 Marks]
Statement
The following pseudocode is executed using the "Words" dataset.
1 D = {}
2 while(Table 1 has more rows){
3 Read the first row X in Table 1
4 D = updateDictionary(D, X)
5 Move X to Table 2
6 }
7
8 Procedure updateDictionary(D, Y)
9 i = 1
10 while (i <= Y.LetterCount){
11 B = ith letter in Y.Word
12 if(isKey(D, B)){
13 D[B] = D[B] + 1
14 }
15 else{
16 D[B] = 1
17 }
18 i = i + 1
19 }
20 return(D)
21 End updateDictionary
Question-8 [3 Marks]
Statement
What will D represent at the end of the execution?
Options
(a)
(b)
(c)
(d)
Solution
First we consider the procedure updateDictionary. It receives a dictionary D and a word Y as
parameters. Based on the usage of D, keys of D are letters and values are integers. We iterate the
letters in the word Y using B. In the if-condition, we check whether the letter B is a key in D. If it
evaluates to True, then we increment the value of D[B] by one. Otherwise, add B as a key to D and
initialize its value to one. Therefore, we add the number of occurrence of each letter of Y to D.
In the main pseudocode, D is initialized as an empty dictionary. For each word X in the table, D
and X are passed to the procedure updateDictionary. Thus, the number of occurrence of each
letter in X is updated to the dictionary D.
Question-9 [4 Marks]
Statement
Consider the dictionary D and the procedure updateDictionary() in the previous question. Let
POS be a list that contains all part of speeches. Assume that given a dictionary D, there exists a
procedure max such that max(D) returns a list of keys which are mapped to the maximum value.
Choose the correct statement(s) from the options based on the following pseudocode. It is a
Multiple Select Question (MSQ).
1 C = {"Overall": max(D)}
2 foreach A in POS{
3 Move all rows to Table 1
4 B = {}
5 while (Table 1 has more rows){
6 Read the first row X in Table 1
7 if(X.PartOfSpeech == A){
8 B = updateDictionary(B, X)
9 }
10 Move X to Table 2
11 }
12 C[A] = max(B)
13 }
Options
(a)
length(keys(C)) is same as the number of different part of speeches in the input dataset
(b)
C captures the list of most frequent alphabet occurred overall in the dataset as well as for each
part of speech
(c)
C captures the most frequent alphabet occurred overall in the dataset as well as for each part of
speech
(d)
length(keys(C)) is one more than the number of different part of speeches in the dataset
Answer
(b), (d)
Solution
The variable C is declared as a dictionary and initialized with a key "Overall" with value as max(D)
where max(D) is a list of keys which are assigned to the maximum value. Therefore, max(D) is a
list of letters with maximum number of occurrence. As it defined, POS is a list of part of speeches.
POS is iterated using A. In each iteration, all the rows of "Words" table are moved to Table 1. Rows
in the table 1 are iterated using X. For each row X with part of speech as A, the dictionary D load
the number of occurrence of the letters in the word. That is, at the end of each iteration, B stores
the frequency count of letter in the words whose part of speech is A. max(B) will be stored as
value for the key A in C. That is,
C captures the list of most frequent alphabet occurred overall in the data set as well as for each
part of speech.
length(keys(C)) is one more than the number of different part of speeches in the table.
Question-10 [4 Marks]
Statement
The following pseudocode is executed using the "Shopping bills" dataset. What will the value of D
represent at the end of the execution?
1 D = {}
2 while(Pile 1 has more cards){
3 Read the top card X in Pile 1
4 D = updateDictionary(D, X)
5 Move X to Pile 2
6 }
7
8 Procedure updateDictionary(D, Y)
9 foreach A in Y.ItemList{
10 C = A.ItemName
11 if(isKey(D, C)){
12 if(isKey(D[C], Y.ShopName)){
13 if(D[C][Y.ShopName]["Price"] != A.Price){
14 D[C][Y.ShopName]["Flag"] = True
15 }
16 }
17 else{
18 D[C][Y.ShopName] = {"Price": A.Price, "Flag" : False}
19 }
20 }
21 else{
22 D[C] = {}
23 D[C][Y.ShopName] = {"Price": A.Price, "Flag" : False}
24 }
25 }
26 return (D)
27 End updateDictionary
Options
(a)
For an item C, and a shop S, D[C] [S] ["Flag"] is set to True if and only if the item is sold for a
constant price
(b)
For an item C, and a shop S, D[C] [S] ["Flag"] is set to True if and only if the item is billed in more
than one bill
(c)
For an item C, and a shop S, D[C] [S] ["Flag"] is set to True if and only if the item is sold for variable
price
(d)
For an item C, and a shop S, D[C] [S] ["Flag"] is set to True if and only if the item is billed exactly
one bill
Answer
(c)
Solution
D is initialized as an empty dictionary. For each card X in the dataset, D is updated by the
procedure updateDictionary.
The procedure updateDictionary takes a dictionary and a card as parameters. For each item C in
the item list of Y, the if-condition checks whether C is a key in D. If the condition evaluates to True,
then check whether X.ShopName is a key in D[C]. If the condition evaluates to true, then set the
value for the key "Flag" to true when the value of the key "Price" is not equal to A.Price. Therefore,
in a shop S, for an item C, if C is sold for different prices in the shop S, then D[C] [S] ["Flag"] is set
to True.
For an item C, and a shop S, D[C] [S] ["Flag"] is set to True if and only if the item is sold for variable
price.