CT Week 7
CT Week 7
Week-7, Graded
Question-1 [2 Marks]
Statement
Options
(a)
(b)
(c)
(d)
Answer
Solution
Question 2 [4 Marks]
Statement
Answer
Solution
Question 3 [3 Marks]
Statement
Options
(a)
(b)
(c)
(d)
Answer
Solution
Question 4 [4 Marks]
Statement
Options
(a)
(b)
(c)
(d)
Answer
Solution
Question (5 - 7)
Statement
Question 5 [3 Marks]
Statement
(a)
(b)
(c)
(d)
Answer
Solution
Question 6 [4 Marks]
Statement
Options
(a)
(b)
(c)
(d)
Answer
Solution
Question 7 [4 Marks]
Options
(a)
(b)
(c)
(d)
Answer
Solution
Question (8 - 9)
Statement
Explanation
Question 8 [4 Marks]
Statement
Options
(a)
(b)
(c)
(d)
Answer
Question 9 [3 Marks]
Statement
Options
(a)
(b)
(c)
(d)
Answer
Question 10 [4 Marks]
Statement
Options
(a)
(b)
(c)
(d)
Answer
Solution
Question-1 [2 Marks]
Statement
Let D be a non-empty dictionary. Choose the correct option(s). It is a Multiple Select Question
(MSQ).
Options
(a)
(c)
(d)
Let D = { ‘a’: 5, ‘b’ : 4, ‘c’ : 6}, then the value of D[4] is 'b'.
Answer
(c)
Solution
a. Key of a dictionary is not necessary to an integer.
d. 4 is a value of key 'b', therefore, D['b'] will be 4 not D[4] will be 'b'.
Question 2 [4 Marks]
Statement
Let Z be a row in the "Words" table such that Z.Word = "reluctant". What will be the value of
alphaDict['t'] at the end of the execution of the following pseudocode? (NAT)
Answer
4
Solution
Procedure updateDict accepts two parameters as key, one row Z and other Dictionary D. It
returns an updated dictionary using the information in row Z.
Line 8 - line 10: If the letter x is not in the keys of D, store x in D as a key and map it to 1 (first time
seen.)
Line 11 - line 13: If the letter x is already a key of D (already seen before), then increment its value
by 1. Which means Dictionary D stores a letter as key mapped to the number of times it is seen
(frequency count of letter.)
In dictionary alphaDict, 't' is mapped to 2 before calling the procedure. The word "reluctant" has
two 't'. Due to line 12, 't' will be mapped to 4 (2 + 1 + 1). Therefore, alphaDict['t'] = 4.
Question 3 [3 Marks]
Statement
The following pseudocode is executed using the "Library" dataset. At the end of the execution, A
stores a dictionary with the author’s name as key mapped to the number of books written by
him/her. But the code may have mistakes. Identify all such mistakes (if any). Assume that all
statements not listed in the options below are free of errors. It is a Multiple Select Question
(MSQ).
1 A = {}
2 while(Table 1 has more rows){
3 Read the first row X from Table 1
4 if(isKey(A, X.Author)){
5 A[X.Author] = 1
6 }
7 else{
8 A[X.Author] = A[X.Author] + 1
9 }
10 Move X to Table 2
11 }
Options
(a)
Replacing the condition given in line 4 with the statement given below will provide the correct
result.
(b)
(c)
(d)
The pseudocode will provide the correct result even without getting modified.
Answer
(a), (c)
Solution
At the end of the execution, A should store the author's name as key mapped to the number of
books written by them. The concept here would be that A should be initialized as an empty
dictionary first and then iterated through the rows of the table to update A.
Whenever a new author is found, the author name should be stored in the dictionary A as key
mapped to 1 (first book seen ever by the author).
But in Line 4: Opposite is being done. Therefore, to make the condition True in opposite situation
there should not. Therefore, option a is correct.
Once we find the author who is already in A, the value of author should be incremented and it is
being done properly by the else block.
As the condition in line 4 is opposite to what we want. We can swap line 5 with 8. This means, if
the author is already in A, then increment else initialize. Therefore, option c is also correct.
Question 4 [4 Marks]
Statement
The following pseudocode is executed using the "Library" Dataset. At the end of the execution,
firstBook stores a dictionary with the author's name as key mapped to the genre of their first
published book. But the code may have mistakes. Identify all such mistakes (if any). Assume that
all statements not listed in the options below are free of errors. Assume that the rows are
arranged in ascending order of publication years of the books. It is a Multiple Select Question
(MSQ).
1 firstBook = {}
2 while(Table 1 has more rows){
3 Read the first row X from Table 1
4 if(isKey(firstBook, X.Author)){
5 firstBook[X.Author] = X.Genre
6 }
7 Move X to Table 2
8 }
Options
(a)
(b)
(c)
(d)
No mistakes
Answer
(b)
Solution
At the end of the execution, firstBook should store author's names as keys mapped to the genre
of their first book. The concept here would be:
1 Procedure doSomething(aList)
2 bDict = {}
3 bList = []
4 foreach a in aList{
5 if(not isKey(bDict, a)){
6 bDict[a] = True
7 bList = bList ++ [a]
8 }
9 }
10 return(bList)
11 End doSomething
Question 5 [3 Marks]
Statement
What will be the value of B at the end of the execution of the following pseudocode.
1 B = []
2 A = [3, 4, 5, 3, 1, 9, 4, 6, 5, 9]
3 B = doSomething(A)
(a)
[3, 4, 5, 1, 9, 4, 6, 5, 9]
(b)
[3, 4, 5, 1, 9, 6]
(c)
[1, 6]
(d)
[4, 1, 3, 4, 6, 5, 9]
Answer
(b)
Solution
Line 4: One element is being picked up from the list which is passed as parameter.
Line 5: If the element is not the key of dictionary bDict, i.e., the element is seen first time, then line
6 will be executed.
Line 6: The element is being stored in Dictionary and the same element is being appended to the
list bList.
Overall, An element of aList is being appended in bList if and only if it is seen first time. If an
element is repeated like 3, then the first time line 6 will be executed and then for rest 3, nothing
will happen.
Therefore, doSomething accepts a list as input and returns a list with the unique(distinct)
elements of input list.
Question 6 [4 Marks]
Statement
The following pseudocode is executed using the "Words" dataset and the procedure
doSomething mentioned above. What will sList store at the end of the execution.
Options
(a)
(b)
(c)
(d)
List of lists of unique words of each sentence from the "Words" dataset
Answer
(d)
Solution
Line 4: wList stores the list of each word of dataset. But wList is being re-initialized to an empty list
in line 8. Line 8 is executed whenever full stop comes into the picture. Therefore, wList stores the
list of words of every sentence before it gets re-initialized.
Line 6: As wList is being updated with doSomething(wList). This procedure will return a list with
unique elements of wList. This means in line 6, wList is being updated with the list of unique
words of the current sentence.
Line 7: The current wList is being appended in sList, but wList is not being appended directly.
There is one square bracket with wList i.e., [wList], which means the list of the unique words of
the current sentence is being appended.
Therefore, sLists stores the list of lists of unique words of every sentence from the dataset.
Question 7 [4 Marks]
The following pseudocode is executed using the "Words" dataset and the procedure
doSomething mentioned above. Let explode(W) returns a list of characters of word W in the
same order as they are in the word. For example explode("thinking") returns ['t', 'h', 'i', 'n', 'k', 'i',
'n', 'g']. Ignore the punctuations like comma, full stop and so on. What will someList store at the
end of the execution.
1 W = [], someList = []
2 while(Table 1 has more rows){
3 Read the first row X from Table 1
4 W = explode(X.Word)
5 someList = someList ++ W
6 Move row X to Table 2
7 }
8
9 someList = doSomething(someList)
Options
(a)
(b)
(c)
(d)
List of lists of unique alphabets of each word from the "Words" dataset
Answer
(b)
Solution
Line 4: W stores the list of alphabets of current word.
Line 5: W is being appended in someList. Which means someList stores the list of the alphabets
seen so for. As this whole process is being happened within the while loop, someList will store all
the list of of the alphabets seen so for till while loop gets ended.
Once the while loop gets ended, someList will be the list of all the alphabets present in the
dataset.
Line 9: The procedure doSomething is being called in the last. This returns the list of unique
elements. Therefore, someList will store the list of unique (distinct) alphabets of the dataset.
Question (8 - 9)
Statement
Let M, P, and C be a list of sequence numbers of students who have scored more than the
average marks in Mathematics, Physics and Chemistry respectively using the "Scores" dataset.
Answer question 8 and 9 after executing the following pseudocode.
1 seqDict = {}
2 foreach index in M{
3 seqDict[index] = 0
4 }
5
6 foreach index in P{
7 if(isKey(seqDict, index)){
8 seqDict[index] = seqDict[index] + 1
9 }
10 }
11
12 foreach index in C{
13 if(isKey(seqDict, index)){
14 seqDict[index] = seqDict[index] + 1
15 }
16 }
Explanation
Line 3: If a student scores more than average in Mathematics, the sequence number of the
student is being stored in dictionary and is being mapped to 0.
Line 7: The statement will only be True when the sequence number of the student is already a key
of seqDict.
Line 13: The statement will only be True when the sequence number of the student is already a
key of seqDict.
This means a student's sequence number will be stored as key in seqDict, if and only if the
sequence number is in M.
Therefore, if a student's sequence number is a key of seqDict, then the student must have scored
more than average in Mathematics. If it is mapped to 0, means the student has more than
average only in Mathematics, as there is a possibility of the value being updated in line 8 or line
14 based on the students scores more or less than average in other two subjects.
If the student scored more than average in Mathematics and Physics only, then the sequence
number will be mapped to 1. This means line 8 is executed but line 14 is not executed, which
means the student did not score more than average in Chemistry.
If the student scored more than average in Mathematics and Chemistry only, then the sequence
number will be mapped to 1. This means line 14 is executed but line 8 is not executed, which
means the student did not score more than average in Physics.
If the student scored more than average in all three subjects, then the sequence number will be
mapped to 2. This means line 8 and line 14 both are executed.
Question 8 [4 Marks]
Statement
If seqDict[n] == 0 is a True statement, then choose the correct option(s) regarding the student
with sequence number n. It is a Multiple Select Question (MSQ).
Options
(a)
The student has scored more than the average marks in any one of the three subjects.
(b)
The student has not scored more than the average marks in any subject.
(c)
The student might have scored more than the average marks either in Physics or in Chemistry
(d)
The student has scored more than the average marks in Mathematics only
Answer
(d)
Question 9 [3 Marks]
Statement
If seqDict[n] == 1 is a True statement, then choose the correct option(s) regarding the student
with sequence number n. It is a Multiple Select Question (MSQ).
Options
(a)
The student has scored more than the average marks in Physics and Chemistry but not in
Mathematics .
(b)
The student has scored more than the average marks in Mathematics and might have scored
more than the average marks in Physics but not in Chemistry.
(c)
The student has scored more than the average marks in Mathematics and might have scored
more than the average marks in Chemistry but not in Physics.
(d)
The student has scored more than average marks in any two of three subjects.
Answer
(b), (c)
Question 10 [4 Marks]
Statement
The following pseudocode is executed using the "Words" dataset. Let unique(L) returns the list of
unique elements of L. What will wCount represent at the end of the execution of the
pseudocode? Ignore the upper and lower case, and punctuation symbols while comparing with
other words.
Options
(a)
Dictionary with words as keys mapped to the number of sentences in which the word is present.
(b)
Dictionary with words as keys mapped to the frequency count of the word in the dataset.
(c)
Dictionary with words as keys mapped to the maximum frequency of the word in a sentence.
(d)
Dictionary with words as keys mapped to the number of sentences in which the word is present
more than one time.
Answer
(a)
Solution
Line 5: wList stores the list of each word of dataset. But wList is being re-initialized to an empty list
in line 16. Line 16 is executed whenever full stop comes into the picture. Therefore, wList stores
the list of words of the current sentence before it gets re-initialized.
Line 7: uniqueList = unique(wList), uniqueList stores the list of unique(distinct) words of the
current sentence.
Line 8: The loop is running for every element of uniqueList, which means the iteration will happen
for every unique word of the current sentence.
Line 9 - Line 14: If the word is seen first time till the current sentence, it is being mapped to 1 in
wCount. If it has been seen before then it would already be the key of wCount, then the value of
word will be incremented.
It is clear that for every word only one time the condition in line 9 will be checked because of
uniqueList. So the word appears first time then it is not a key. Which also means the word was not
present in any sentence before the current sentence. If the word's value gets incremented, this
means the word was seen before. As the value is being incremented by 1, and a word can be seen
only one time in a sentence, then actually it is being mapped to the number of sentences in which
the word was present.