You are on page 1of 18

Week-4, Graded Assignment

Question 1 [4 Marks]

Statement
For which of the following situations is a nested loop needed? Assume that no procedure is used. It is a
Multiple Select Question (MSQ).

Options
(a)

To find the number of authors who have written a book in "French" in 2007 from the "Library" dataset.

(b)

To find the number of pair of students who have scored the same Physics marks from the "Scores" dataset

(c)

To find the number of players who have won the medal in "Wrestling" from the "Sports" dataset

(d)

To find the number of students with at least two vowels in their name from the "Scores" dataset.

Answer
(b), (d)

Solution
We will check each and options one by one:

Option (a): This requires only one loop.

Option (b): For finding the pair of students who scored the same Physics mark requires nested loop. The
outer loop fix one of the student's mark and the inner loop compares with remaining student's mark.

Option (c): This requires only one loop.

Option (d): It requires nested loop to find the number of students with at least two vowels in their name
from the "Sports" dataset. The outer loop iterates through the each student's name and the inner loop will
iterate through the each letters of the student's name.

Therefore the correct options are (b), (d).

Question 2 [4 Marks]
Question 2 [4 Marks]

Statement
The following pseudocode is executed using the “Scores” dataset. Assume that length(x) is a procedure that
returns the number of letters present in a word x. What will A represent at the end of the execution?

 A = 0
while(Table 1 has more rows){
    Read the first row X in Table 1
    i = 1, B = True
    C = X.Name
    while(i <= length(C)){
        if(ith letter of C is a vowel){
            B = False
        }
        i = i + 1
    }
    if(B){
        A = A + 1
    }
    Move X to Table 2
}

Options
(a)

Number of students whose names are without vowels

(b)

Number of students whose names have at most one vowel

(c)

Number of students whose names have exactly one vowel

(d)

Number of students whose names have at least one vowel

Answer
(a)

Solution
In this pseudocode nested while loop is used in which outer loop iterates through each student. Variable C
stores the name of the student, procedure length(x) counts the number of letters present in C and the
inner loop iterates through the each letter of C. The variable B is updated to False whenever a vowel is
found in a word. Therefore variable A counts those words which do not have vowels. Therefore the correct
option is (a).

Question 3 [4 Marks]
Question 3 [4 Marks]

Statement
The following pseudocode is executed using the “Library” dataset. What will C represent at the end of the
execution? Assume that there is only one row corresponding to each author in the "Library" dataset.

 C = 0
while(Table 1 has more rows){
    Read the first row X in Table 1
    Move X to Table 2
    while(Table 1 has more rows){
        Read the first row Y in Table 1
        if(findpair(X, Y)){
            C = C + 1
        }
        Move Y to Table 3
    }
    Move all rows from Table 3 to Table 1
}
 
Procedure findPair(X, Y)
    if(X.Genre == Y.Genre and X.Language != Y.Language){
        return(True)
    }
    else{
        return(False)
    }
End findPair

Options
(a)

Number of authors who have written different genre books in same language

(b)

Number of pairs of authors who have written different genre books in same language

(c)

Number of authors who have written same genre books in different language

(d)

Number of pairs of authors who have written same genre books in different language

Answer
(d)
Solution
In this pseudocode nested while loop is used which find pairs of authors. Now we investigate the
pseudocode closely. The variable C is incremented whenever procedure findPair(X, Y) returns True. The
procedure findPair(X, Y) returns True for pair of authors who authored in the different language but same
genre . Therefore, variable C counts the number of pairs of authors who have written books in different
language in same genre. Therefore option (d) is correct.

Question 4 [5 Marks]
Question 4 [5 Marks]

Statement
Two words are said to be special if they fulfill following conditions:

Number of vowels are same in both the words


Number of consonants are not same in both the words

The given pseudocode is executed using the “Words” dataset. The variable count in the given pseudocode
counts the number of special pairs. Choose the correct code fragment(s) to complete the pseudocode.

 count = 0
while(Table 1 has more rows){
    Read the first row X in Table 1
    Move X to Table 2
    while(Table 1 has more rows){
        Read the first row Y in Table 1
        **********************
            Fill here 
         **********************
        Move Y to Table 3
    }
    Move all rows from Table 3 to Table 1
}
 
Procedure vCount(Z)
    vowelCount = 0, i = 1
    while(i <= Z.LetterCount){
        if(ith letter of Z.Word is a vowel){
            vowelCount = vowelCount + 1
        }
        i = i + 1
    }
    return(vowelCount)
End vCount

Options
(a)

 if(X.LetterCount == Y.LetterCount){
     if(vCount(X) == vCount(Y)){
            count = count + 1
     }
}

(b)
 if(X.LetterCount != Y.LetterCount){
    if(vCount(X) == vCount(Y)){
            count = count + 1
    }
}

(c)

 if(X.LetterCount == Y.LetterCount){
     if(vCount(X) != vCount(Y)){
            count = count + 1
     }
}

(d)

 if(X.LetterCount != Y.LetterCount){
     if(vCount(X) != vCount(Y)){
            count = count + 1
     }
}

Answer
(b)

Solution
The given pseudocode counts the number of special pairs. We need to find the pair of words have same
number of vowels and different number of consonants. This is possible only when the letter count of both
the words are not same. Let us check the options one by one.

Option (a) : This options checks if the letter count of both the words are same and if same the number of
vowels should also be same. This means that number of consonants will also be the same. Hence, this is an
incorrect option.

Option (b) : This options checks if the letter count of both the words are not same and if same the number
of vowels be same. This means that number of consonants will not be the same. This is the correct option.

Option (c) : This options checks if the letter count of both the words are same and if same the number of
vowels should not be same. This does not satisfy the condition of the question. Hence, this is an incorrect
option.

Option (d) : This options checks if the letter count of both the words are not same and if same the number
of vowels are also not same. This does not satisfy the condition of the question. Hence, this is an incorrect
option.

Question (5-6)
Question (5-6)

Statement
The following pseudocode is executed using the “Scores” dataset. The variables M, P and C store the
number of students in Mathematics, Physics and Chemistry clubs respectively.

 M = 0, P = 0, C = 0
while(Table 1 has more rows){
    Read the first row X in Table 1
    subject = maxSubject(X)
    if(X.subject > 90){
        if(subject == "Mathematics"){
            M = M + 1
        }
        if(subject == "Physics"){
            P = P + 1
        }
        if(subject == "Chemistry"){
            C = C + 1
        }
    }
    Move X to Table 2
}
Procedure maxSubject(Z)
    if(Z.Chemistry > Z.Mathematics){
        if(Z.Chemistry > Z.Physics){
            return("Chemistry")
        }
        else{
            return("Physics")
        }
    }
    else{
        if(Z.Mathematics > Z.Physics){
            return("Mathematics")
        }
        else{
            return("Physics")
        }
    }
End maxSubject

Questions 5 [5 marks]
Questions 5 [5 marks]

Statement
Which club can a student join if he/she gets 91 marks in all three subjects?

Options
(a)

Physics

(b)

Chemistry

(c)

Mathematics

(d)

The student cannot join any club

Answer
(a)

Solution
The joining of a club will depend on the return value of the procedure maxSubject. In this question it is
given that a student got 90 marks in all subjects. The procedure maxSubject will be called. In the procedure
firstly Chemistry marks is compared with Mathematics marks. Since Chemistry marks is same as
Mathematics marks, the if-block sets to be False and flow of the code will go to else-block. Inside the else-
block, Mathematics marks is compared with Physics marks. Since Mathematics marks is same as Physics
marks, the if-block sets to be False and flow of the code will go to else-block and "Physics" will be returned.
Now the value stored in variable subject is "Physics". Since Physics marks is which is more than 90, variable
C will be incremented. Therefore a student who will get more 9and same marks in all subjects. He/she will
join Physics club. The correct option is (a).

Question 6 [4 Marks]
Question 6 [4 Marks]

Statement
Can a student join more than one club ?

Options
(a)

Yes, if the student has scored more than 90 marks in at least two subject

(b)

Yes, if the student has scored more than 90 marks in Mathematics and Chemistry

(c)

Yes, if the student has secured more than 90 marks and the same marks in Mathematics and Chemistry

(d)

No, a student cannot join more than one club

Answer
(d)

Solution
From the previous question we saw that joining of a club depends upon the return value of procedure
maxSubject and it returns only one value which is further compared in the main part of the code. Hence a
student can join only one club if he/she gets more than 90 marks in that subject. Hence the correct option is
(d).
Question 7 [4 Marks]

Statement
The given pseudocode is executed using the “Shopping Bills” dataset. frac stores the ratio of the number of
customers who purchased both “Soap” and “Facewash” to the number of customers who purchased
“Facewash”. Choose the correct code fragment to complete the pseudocode. (Assume there is at least one
customer who has purchased “Facewash”).

 mCount = 0, bCount = 0
while(Pile 1 has more cards){
    Read the top card X in Pile 1
    *********************
    * Fill the code *
    *********************
    Move X to Pile 2.
}
frac = bCount / mCount
 
Procedure isItem (Y, A)
    C = False
    while(Card Y has more items){
        Read an item Z from ItemList of card Y
        if(Z.Item == A){
            C = True
        }
        Remove Z from ItemList of Y
    }
    return(C)
End isItem
 

Options
(a)

 if(isItem(X, “Facewash”)){
    mCount = mCount + 1
}
if(isItem(X, “Soap”)){
    bCount = bCount + 1
}

(b)
 if(isItem(X, “Soap”)){
    bCount = bCount + 1
    if(isItem(X, “Facewash”)){
        mCount = mCount + 1
    }
}
 

(c)

 if(isItem(X, “Facewash”) and isItem(X, “Soap”)){
    mCount = mCount + 1
    bCount = bCount + 1
}

(d)

 if(isItem(X, “Facewash”)){
    mCount = mCount + 1
    if(isItem(X, “Soap”)){
        bCount = bCount + 1
    }
}

Answer
(d)

Solution
Procedure isItem (Y, A) returns True if item A is there in the item list of card Y otherwise returns False. As
we are trying to find the ratio of customers who purchased both Soap and Facewash to the number of
customers who purchased Facewash, bCount should stores the number of customers who buy Soap and
Facewash together and mCount should store the number of customers who buy Facewash.

Option a. bCount stores the number of customers who buy Soap (irrespective of the customers who buy
Facewash or not). This is not the situation we want.

Option b. mCount stores the number of customers who buy Facewash only if they buy Soap. This is not the
situation we want.

Option c. bCount and mCount are always same and stores the number of customers who buy both Soap
and Facewash. This is not the case here.

Option d. mCount stores the number of customers who have bought the Facewash and bCount stores the
number of customers who buy Soap only if they buy Facewash. This the required condition. Therefore,
option d is correct.

Question 8 [4 Marks]
Question 8 [4 Marks]

Statement
The following pseudocode is executed using the “Olympics” dataset. At the end of the execution, A, B and C
store the number of pairs of players from the same nation who have won same medal in different year.
Assume that one player has won only one medal. Choose the correct code fragment(s) to complete the
pseudocode. It is a Multiple Select Questions(MSQ)

 A = 0, B = 0, C = 0
while(Table 1 has more rows){
    Read the first row X in Table 1
    if(X.Medal == "Gold"){
        Move X to Table 2
    }
    if(X.Medal == "Silver"){
        Move X to Table 3
    }
    if(X.Medal == "Bronze"){
        Move X to Table 4
    }
}
A = similarPlayers(Table 2)
B = similarPlayers(Table 3)
C = similarPlayers(Table 4)
  
Procedure similarPlayers(Table T1)
    D = 0
    while(Table T1 has more rows){
        Read the first row Y in Table T1
        Move Y to Table T2
        while(Table T1 has more rows){
            Read the first row Z in Table T1
            Move Z to Table T3
            *****************************
                * Fill the code *
            *****************************
        }
        Move all rows from Table T3 to Table T1
    }
    return(D)
End similarPlayers

Options
(a)
 if(Y.Nationality == Z.Nationality){
    if(Y.Year != Z.Year){
        D = D + 1
    }
}

(b)

 if(Y.Year != Z.Year){
    if(Y.Nationality == Z.Nationality){
        D = D + 1
    }
}

(c)

 if(Y.Year != Z.Year and Y.Nationality == Z.Nationality)
    D = D + 1
}

(d)

 if(Y.Year != Z.Year or Y.Nationality == Z.Nationality)
    D = D + 1
}

Answer
(a), (b) , (c)

Solution
The above pseudocode is executed on the “Olympics” dataset. At the end of the execution, variables A, B
and C store the number of pairs of players from the same nation who have won same medal in different
year. The pseudocode has to be completed with the correct code fragment from the given options. Let us
observe the given pseudocode.

In the main part of the pseudocode, three bins are created based on the medal won by the player. The first
bin i.e. Table 2 stores the players who won the medal in "Gold", the second bin i.e. Table 3 stores players
who won the medal in "Silver" and the third bin i.e. Table 4 stores the players who won the medal in
"Bronze". Then procedure similarPlayers is called separately with created bins as an input (argument). The
procedure similarPlayers contains a nested while loop. This nested while loop should create pairs of players
from the created bins as mentioned in the problem. The condition for checking the same nation and
different year of winning should be in the inner while loop of the procedure similarPlayers. The missing
code fragment can be written as:
 if(Y.Year != Z.Year and Y.Nationality == Z.Nationality)
    D = D + 1
}

Here two conditions are connected with word “and”, which could be converted in two conditions using
nested loop which is given in option a and b. Therefore, options a, b, and c are correct.
Question (9-10)

Statement
The following pseudocode is executed using the “Words” dataset.

 A = 0, B = 0
P = True, Q = False
while(Table 1 has more rows){
    Read the first row X in Table 1
    if(X.PartOfSpeech == "Adjective" and P){
        P = False
        Q = True
    }
    if(X.PartOfSpeech == "Noun" and Q){
        B = B + 1
    }
    else{
        if(not Q){
            A = A + 1
        }
    }
    if(X.Word ends with a full stop){
        P = True
        Q = False
    }
    Move X to Table 2
}

Question 9 [4 Marks]
Question 9 [4 Marks]

Statement
What will A represent at the end of execution?

Options
(a)

Number of words after the first noun in every sentence which are not adjectives

(b)

Number of words which appear before the first adjective in every sentence

(c)

Number of words other than nouns which appear before the first adjective in every sentence

(d)

Number of words after the first adjectives in every sentence which are not nouns

Answer
(b)

Solution
The given pseudocode is executed on the "Words" dataset. The increment of variable A depends on the
variable Q. Variable A is incremented for False value of Q. Since it check for "not Q". The variable Q is set as
False initially and set to True when an adjective is found. Hence variable A will count the number of words
which appear before the first adjective in every sentence.

Question 10 [4 Marks]
Question 10 [4 Marks]

Statement
What will B represent at the end of execution?

Options
(a)

Number of nouns after the second adjective in every sentence

(b)

Number of nouns before the second adjective in every sentence

(c)

Number of nouns before the first adjective in every sentence

(d)

Number of nouns after the first adjective in every sentence

Answer
(d)

Solution
The increment of B depends on Q even though part of speech of a word is adjective. Q is initially False and
set to be True when an adjective is found. Hence B will count number of nouns after the first adjective in
every sentence.

You might also like