Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Week 3 PA Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Week-3, Practice

Week-3, Practice
Question- (1)
Statement
Options
(a)
(b)
(c)
(d)
Solution
Question 2
Statement
Options
(a)
(b)
(c)
(d)
Solution
Question 3
Statement
Option
(a)
(b)
(c)
(d)
Solution
Question 4
Statement
Option
(a)
(b)
(c)
(d)
Solution
Question (5 - 8)
Statement
Solution
Question 9
Statement
Options
(a)
(b)
(c)
(d)
(e)
Solution
Question 10
Statement
Options
(a)
(b)
(c)
(d)
Answer
Question 11
Statement
Options
(a)
(b)
(c)
Solution
Question 12
Statement
Options
(a)
(b)
(c)
(d)
(e)
(f)
Solution

Question- (1)
Statement
In the “Shopping Bills” dataset, the procedure countBills counts the number of bills from Big
Bazaar with total amount more than the average total bill amount. Assume that the variable Avg
holds the value of the average total bill amount. Choose the correct code fragments to complete
the procedure. It is a Multiple Select Question (MSQ).

1 Procedure countBills()
2 Count = 0
3 while(Pile 1 has more cards){
4 Read the top card X from Pile 1
5 *********************
6 ** Fill the code **
7 *********************
8 Move card X to Pile 2
9 }
10 return(Count)
11 End countBills

Options
(a)
1 if(X.ShopName == "BigBazaar"){
2 if(X.TotalBillAmount > Avg){
3 Count = Count + 1
4 }
5 }

(b)

1 if(X.TotalBillAmount > Avg){


2 if(X.ShopName == "BigBazaar"){
3 Count = Count + 1
4 }
5 }

(c)

1 if(X.TotalBillAmount > Avg and X.ShopName == "BigBazaar"){


2 Count = Count + 1
3 }

(d)

1 if(X.TotalBillAmount < Avg and X.ShopName == "BigBazaar"){


2 Count = Count + 1
3 }

Solution
As per the given information in the question statement, we are supposed to find
the total number of shopping bills which satisfy two conditions: (i) ShopName == "BigBazaar" and,
(ii) TotalBillAmount > Avg.
Now, this can be achieved in many ways:

1. Check condition (i) and then check condition (ii) inside it (Option a)

2. Check condition (ii) first and then check condition (i) inside it (Option b)

3. Check both these conditions in a single if statement using “and” operator (Option c)

Therefore the correct options are (a), (b), (c).


Question 2
Statement
The following pseudocode is executed using the “Scores” dataset. What will be the value of the
variable Z at the end of the execution?

1 SumT = 0, SumM = 0, SumP = 0, SumC = 0


2 while(Pile 1 has more cards){
3 Read the top card X from Pile 1
4 SumT = SumT + X.Total
5 SumM = SumM + X.Mathematics
6 SumP = SumP + X.Physics
7 SumC = SumC + X.Chemistry
8 Move card X to Pile 2
9 }
10 Z =0
11 if((SumM + SumP + SumC)/SumT >= 0){
12 Z = 1
13 }
14 if((SumM + SumP + SumC)/SumT <= 1){
15 Z = -1
16 }

Options
(a)

(b)

(c)

-1

(d)

None of the above

Solution
Let us first understand what each variable used in the pseudocode represents at
the end of the execution.
SumT: Sum of total marks of all cards in the dataset.
SumM: Sum of Mathematics marks of all cards in the dataset.
SumP: Sum of Physics marks of all cards in the dataset.
SumC: Sum of Chemistry marks of all cards in the dataset.
We know that the sum of SumM, SumP and SumC is always going to be equal to SumT.
Therefore, (SumM + SumP + SumC) / SumT will always return 1. So, the first if statement
(1 ≥ 0) will be True and value of Z will become 1. Similarly, the second if condition (1 ≤ 1)
will also be True and the value of Z will be updated to -1. Therefore, the correct option is (c)
Question 3
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 class
average (of total marks) but have scored below the subject average in at least one subject.
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.

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){
9 B = True
10 }
11 if(X.Physics < AvgP){
12 C = True
13 }
14 if(X.Chemistry < AvgC){
15 D = True
16 }
17 *********************
18 ** Fill the code **
19 *********************
20 }
21 Move X to Table 2
22 }

Option
(a)

1 if(A and (B or C or D)){


2 Count = Count + 1
3 }

(b)

1 if((A or B) and (C or D)){


2 Count = Count + 1
3 }
(c)

1 if((A and B) and (C or D)){


2 Count = Count + 1
3 }

(d)

1 if((A and B) or (C and D)){


2 Count = Count + 1
3 }

Solution
Let us first understand the meaning of each variable using the given pseudocode:
AvgT: Average total marks
AvgM: Average Mathematics marks
AvgP: Average Physics marks
AvgC: Average Chemistry marks
A: Initially False but it will become True if Total > AvgT
B: Initially False but it will become True if MathematicsMarks < AvgM
C: Initially False but it will become True if PhysicsMarks < AvgP
D: Initially False but it will become True if ChemistryMarks < AvgC

Now, as per the question statement Count will be incremented only if A is True and at
least one of B, C and D is True. And the programmatic way to right this is option (a).
Question 4
Statement
The following pseudocode is executed using the “Shopping bills” dataset. What will the values of
the variables A and B represent at the end of the execution?

1 SumSV = 0, SumBB = 0
2 CountSV = 0, CountBB = 0
3 while(Pile 1 has more cards){
4 Read the top card X from Pile 1
5 if(X.ShopName == "SV Stores"){
6 SumSV = SumSV + X.TotalBillAmount
7 CountSV = CountSV + 1
8 }
9 if(X.ShopName == "Big Bazaar"){
10 SumBB = SumBB + X.TotalBillAmount
11 CountBB = CountBB + 1
12 }
13 Move card X to Pile 2
14 }
15 MSV = SumSV / CountSV
16 MBB = SumBB / CountBB
17 A = 0, B = 0
18 while(Pile 2 has more cards){
19 Read the top card X from Pile 2
20 if(X.ShopName == "SV Stores" and X.TotalBillAmount > MSV){
21 A = A + 1
22 }
23 if(X.ShopName == "Big Bazaar" and X.TotalBillAmount < MBB){
24 B = B + 1
25 }
26 Move card X to Pile 1
27 }

Option
(a)

A = Number of bills from “Big Bazaar” with total bill amount greater than the average total bill
amount of “SV Stores”
B = Number of bills from “SV Stores” with total bill amount less than the average total bill amount
of “Big Bazaar”

(b)

A = Number of bills from “SV Stores” with total bill amount greater than the average total bill
amount of “SV Stores”
B = Number of bills from “Big Bazaar” with total bill amount less than the average total bill
amount of “Big Bazaar”
(c)

A = Number of bills from “SV Stores” with total bill amount less than the average total bill amount
of “Big Bazaar”
B = Number of bills from “Big Bazaar” with total bill amount greater than the average total bill
amount of “SV Stores”

(d)

A = Number of bills with total bill amount greater than the average total bill amount
B = Number of bills with total bill amount less than the average total bill amount

Solution
In the first while loop we are computing values of SumSV, CountSV, SumBB and
CountBB. Let us understand what these variable represent in this pseudocode:

SumSV: Sum of total bill amount of SV Stores


CountSV: Number of bills of SV Stores
SumBB: Sum of total bill amount of Big Bazaar
CountBB: Number of bills of Big Bazaar

After the first while loop we are calculating values of MSV and MBB which represents
average bill amount of SV Stores and Big Bazaar respectively.

In the second while loop we are iterating over Pile 2 where all cards are stacked as a result of
first while loop. In this loop, value of A will be incremented if ShopName == “SV Stores”
and TotalBillAmount > MSV. Similarly, value of B will be incremented if ShopName ==
“Big Bazaar” and TotalBillAmount < MBB. Therefore, the option (b) is the correct answer.
Question (5 - 8)
Statement
The following pseudocode is executed using the “Scores” dataset. Assume that the variable AvgP
holds the average Physics marks. What will be the values of CountA, CountB, CountC and
CountD at the end of the execution? Fractions can be rounded to next nearest integer. For
example 50.1 and 50.9 can be rounded to 51.

1 AvgP = 73
2 Mid2 = 1.25*AvgP
3 Mid1 = 0.75*AvgP
4 CountA = 0, CountB = 0, CountC = 0, CountD = 0
5 while(Pile 1 has more cards){
6 Read the top card X from Pile 1
7 if(X.Gender == 'F' ){
8 if(X.Physics >= Mid2){
9 CountA = CountA + 1
10 }
11 if(X.Physics < Mid2 and X.Physics >= AvgP){
12 CountB = CountB + 1
13 }
14 if(X.Physics < AvgP and X.Physics >= Mid1){
15 CountC = CountC + 1
16 }
17 if(X.Physics < Mid1){
18 CountD = CountD + 1
19 }
20 }
21 Move card X to Pile 2
22 }

Solution
It is a numerical input type question where we are supposed to find the values
of variable CountA, CountB, CountC and CountD.

Given: AvgP = 73 therefore, Mid2 = 91.25 and Mid1 = 54.75


As per the outer if statement, we will consider only those cards where gender is female.
Then as per in inner if statements,
CountA will be incremented if PhysicsMarks ≥ Mid2,
CountB will be incremented if PhysicsMarks < Mid2 and PhysicsMarks ≥ AvgP,
CountC will be incremented if PhysicsMarks < AvgP and PhysicsMarks ≥ Mid1,
CountD will be increamented if PhysicsMarks < Mid1. Therefore, the answers will be,
CountA = 2 (Card numbers: 5, 18)
CountB = 3 (Card numbers: 23, 25, 28)
CountC = 7 (Card numbers: 4, 10, 12, 14, 15, 21, 22)
CountD = 1 (Card number: 3)
Question 9
Statement
The following pseudocode is executed using the “Scores” dataset. What will count represent at
the end of execution?

1 count = 0
2 while(Table 1 has more rows){
3 Read the first row X in Table 1
4 Move the row X to Table 2
5 while(Table 1 has more rows){
6 Read the first row Y in Table 1
7 count = count + compareSomething(X.Total, Y.Total)
8 count = count + compareSomething(Y.Total, X.Total)
9 Move the row Y to Table 3
10 }
11 Move all rows from Table 3 to Table 1
12 }
13 Procedure compareSomething(A, B)
14 if(A > B){
15 return (-1)
16 }
17 else {
18 return (1)
19 }
20 End compareSomething

Options
(a)

Number of pairs of students who have different total marks

(b)

Number of pairs of students who have same total marks

(c)

Twice the number of pairs of students who have different total marks

(d)

Twice the number of pairs of students who have same total marks

(e)

It is always zero
Solution
The given pseudocode contains nested while loops and we are iterating on scores
dataset. So, every student’s total marks will be compared against every other student’s
total marks. This comparison will happen twice with different sequence of arguments for
the procedure compareSomething(). The procedure is comparing parameters A, B and it
will return -1 if A > B, 1 otherwise that is if A < B or A == B.

Based on this information we can analyse 3 results of comparisons:

1. X.Total > Y.Total : This will return -1 and decrements the value of count
2. X.Total < Y.Total : This will return 1 and increments the value of count
3. X.Total == Y.Total : This will return 1 and increments the value of count

Now, because of the two successive procedure calls with different order of arguments,
comparison
results 1 and 2 will nullify each others effect. But due to result 3, count will be incremented
twice when X.Total == Y.Total. Therefore, the correct option will be (d).
Question 10
Statement
The following pseudocode is executed using the “Shopping bills” dataset to find the number of
eligible bills for the lowest three total bill amounts. Choose the correct code fragment to complete
the pseudocode.

1 FirstT = 10000, SecondT = 10000, ThirdT = 10000


2 while(Pile 1 has more cards){
3 Read the top card X from Pile 1
4 if(X.TotalBillAmount < FirstT){
5 ThirdT = SecondT
6 SecondT = FirstT
7 FirstT = X.TotalBillAmount
8 }
9 if(X.TotalBillAmount > FirstT and X.TotalBillAmount < SecondT){
10 ThirdT = SecondT
11 SecondT = X.TotalBillAmount
12 }
13 if(X.TotalBillAmount > SecondT and X.TotalBillAmount < ThirdT){
14 ThirdT = X.TotalBillAmount
15 }
16 Move card X to Pile 2
17 }
18 Count = 0
19 while(Pile 2 has more cards){
20 Read the top card X from Pile 2
21 *********************
22 ** Fill the code **
23 *********************
24 Move card X to Pile 1
25 }

Options
(a)

1 if(X.TotalBillAmount < ThirdT){


2 Count = Count + 1
3 }

(b)

1 if(X.TotalBillAmount <= ThirdT){


2 Count = Count + 1
3 }
(c)

1 if(X.TotalBillAmount > ThirdT){


2 Count = Count + 1
3 }

(d)

1 if(X.TotalBillAmount >= ThirdT){


2 Count = Count + 1
3 }

Answer
As per the given question statement we are supposed to find the number of eligible
bills for the lowest three total bill amounts. At the end of first while loop variables FirstT,
SecondT and ThirdT will store the lowest bill amount, second lowest bill amount and third
lowest bill amount respectively. Therefor, in order to find the number of eligible bills for the
lowest three total bill amounts, we should filter all such shopping bills where total bill amount
is less than or equal to ThirdT. The corerct option is (c).
Question 11
Statement
The following pseudocode is executed using the “Scores” dataset. Let minTotal stores the
minimum total marks obtained by any student. What will the value of Flag be at the end of the
execution?

1 A = 101, B = 101, C = 101, D = 101


2 Flag = False
3 while(Pile 1 has more cards){
4 Read the top card X from Pile 1
5 A, B, C = doSomething(X, A, B, C)
6 Move X to Pile 2
7 }
8 D = A + B + C
9 if((minTotal − D) >= 0){
10 Flag = True
11 }
12 Procedure doSomething(Y, A, B, C){
13 if(Y.Mathematics < A){
14 A = Y.Mathematics
15 }
16 if(Y.Physics < B){
17 B = Y.Physics
18 }
19 if(Y.Chemistry < C){
20 C = Y.Chemistry
21 }
22 return ([A, B, C])
23 End doSomething

Options
(a)

False

(b)

True

(c)

Can not be determined

Solution
In the given pseudocode, variables A, B and C store the minimum Mathematics,
Physics and Chemistry marks respectively. And, D is sum of all these minimum subject marks
whereas minTotal stores the minimum total marks in the dataset.
If the student who scored minimum total marks has also scored minimum subject marks in all
three subjects then in such a case minTotal − D == 0, minTotal - D > 0 otherwise.
In either scenario the value of Flag will True.
Question 12
Statement
The following pseudocode is executed using the “Scores” dataset. At the end of the execution,
CountM captures the number of male students whose Mathematics marks are less than the
average Mathematics marks of female students. But the pseudocode may have mistakes in one or
more lines. Identify all such lines (if any). Assume that all statements not listed in the options
below are free of errors. It is a Multiple Select Question (MSQ).

1 Sum = 0, Count = 0
2 while(Pile 1 has more cards){
3 Read the top card X in Pile 1
4 Sum, Count = addMarks(X, Sum, Count)
5 Move card X to Pile 2
6 }
7 AvgT = Sum / Count
8 CountM = 0
9 while(Pile 2 has more cards){
10 Read the top card X in Pile 2
11 if(X.Mathematics < AvgT){
12 CountM = CountM + 1
13 }
14 Move card X to Pile 1
15 }
16 Procedure addMarks (Y, SumF, CountF)
17 if(Y.Gender == 'F'){
18 SumF = SumF + Y.Mathematics
19 CountF = CountF +1
20 }
21 return ([SumF, CountF])
22 End addMarks

Options
(a)

Error in Line 4

(b)

Error in Line 11

(c)

Error in Line 17

(d)

Error in Line 18
(e)

Error in Line 21

(f)

No error in the code

Solution
The given pseudocode is supposed to compute the number of male students whose
Mathematics marks are less than the average Mathematics marks of female students in varibale
CountM.
Let us understand the purpose of each variable used in the pseudocode.
Sum: Sum of Mathematics marks of female students
Count: Number of female students
AvgT: Average Mathematics marks of female students
CountM (Expected): Number of male students whose Mathematics marks are less than the
average Mathematics marks of female students
CountM (Actual): Number of students whose Mathematics marks are less than the average
Mathematics marks of female students
Therefore, there is an error in line 11, the correct statement should be,
if (X.Gender == ‘M’ and X.Mathematics < AvgT)

You might also like