0% found this document useful (0 votes)
12 views75 pages

Psuedocode Infosys 100

The document contains a series of pseudocode questions and their corresponding answers, focusing on various programming concepts such as recursion, conditionals, and loops. Each question is followed by an explanation of the logic behind the answer. The answers range from basic arithmetic operations to more complex iterations and conditions.

Uploaded by

sonakshigoyat04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views75 pages

Psuedocode Infosys 100

The document contains a series of pseudocode questions and their corresponding answers, focusing on various programming concepts such as recursion, conditionals, and loops. Each question is followed by an explanation of the logic behind the answer. The answers range from basic arithmetic operations to more complex iterations and conditions.

Uploaded by

sonakshigoyat04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 75

Q1) What will be the output of the following pseudocode for p = 3

and q = 4?
int fun1(int p, int q)
if(q EQUALS 0)
return 0
if(q mod 2 EQUALS 0)
return fun1(p + p, q/2)
return fun1(p + p, q/2) + p
End function fun1()
A. None of the options
B. 7
C. 12
D. 8
Answer: C
Explanation: The function is recursive. Initially, p = 3 and q = 4. The
process will double p and halve q until q becomes 0. Finally, it returns
12.

Q2) What will be the output of the following pseudocode?


Integer x, y, z, a
Set x = 2, y = 1, z = 5
a = (x AND y) OR (z + 1)
Print a
A. 5
B. 3
C. 2
D. 6
Answer: D
Explanation: The operation x AND y results in 0, and (z + 1) equals 6

Q3) What will be the output of the following pseudocode for a = 2,


b = 3?
doSomething(Integer a, Integer b)
if(b EQUALS 1)
return 0
else
return a + doSomething(a, b-1)
End function doSomething()
A. 3
B. 4
C. 2
D. 1
Answer: B
Explanation: This is a recursive function. Starting with a = 2, b = 3, it
eventually returns 4 after adding 2 recursively.

Q4) What will be the output of the following pseudocode?


Integer a, n, sum, q, r
Set a = 123, n = 0, sum = 0
Set q = a
while (q > 0) // Correct condition to count digits
n=n+1
q = q / 10
End while
Print n
while (a > 0) // Sum digits without dividing by power
r = a mod 10
sum = sum + r
a = a / 10
End while
Print sum
A. 3 2
B. 6 4
C. 6 5
D. 3 6
Answer: D
Explanation: The first loop counts how many digits are there in the
number. The second loop sums the digits. Number of digits 3 and
sum of digits 6.

Q5) What will be the output for the following pseudocode?


Integer a = 5, b = 8, c
if(a > 8 OR b < 7)
c=a+b
elseif(a + b > 9)
c=a-b
else
a=a+5
c=c-2
end if
a=a+b
Print a and c
b=b-5
A. None of the mentioned
B. 5 -3
C. 5 1
D. 13 -3
Answer: D
Explanation: The first condition fails, but the second condition is
true, so c = 5 - 8 = -3. After adding a and b, we get a = 13. So, the final
output is 13 and -3.

Q6) What will be the output of the following pseudocode for a = 4


and b = 3?
Integer a, b, result
Set a = 4, b = 3
if(a mod 2 EQUALS 0)
result = a + b
else
result = a - b
End if
Print result
A. 7
B. 1
C. 5
D. 6
Answer: A
Explanation: Since a mod 2 == 0 (4 is even), result = a + b = 4 + 3 = 7.

Q7) What will be the output of the following pseudocode for x = 6


and y = 3?
Integer x, y, z
Set x = 6, y = 3
z = (x * y) - (x mod y)
Print z
A. 15
B. 18
C. 14
D. 12
Answer: B
Explanation: x × y = 6 × 3 = 18.
X mod y = 6 mod 3 = 0 (as 6 is divisible by 3)
Z = 18 – 0 = 18.

Q8) What will be the output of the following pseudocode for p = 2


and q = 5?
Integer p, q
Set p = 2, q = 5
while(q > 0)
p=p*2
q=q-1
End while
Print p
A. 32
B. 16
C. 8
D. 64
Answer: D
Explanation: The loop multiplies pp by 2 in each iteration and
decrements q by 1, repeating until q=0. Starting with p=2 and q = 5, p
becomes 64 after 5 iterations. the final value of p is 64.

Q9) What will be the output of the following pseudocode for a = 6,


b = 4, and c = 2?
Integer a, b, c, result
Set a = 6, b = 4, c = 2
if(a > b)
result = a + c
elseif(b > c)
result = b - c
else
result = a * b
End if
Print result
A. 8
B. 10
C. 6
D. 12
Answer: A
Explanation: Since a > b (6 > 4), the first condition is true, so result =
a + c = 6 + 2 = 8.

Q10) What will be the output of the following pseudocode for a = 8


and b = 3?
Integer a, b, result
Set a = 8, b = 3
if(a mod b == 0)
result = a / b
else
result = a * b
End if
Print result
A. 24
B. 3
C. 2
D. 5
Answer: A
Explanation: The condition a mod b == 0 checks if 8 is divisible by 3,
which is False since 8 mod 3 = 2. Therefore, the else block executes,
calculating result= 8 × 3 = 24.
Q11) What will be the output of the following pseudocode for a =
10 and b = 5?
Integer a, b, result
Set a = 10, b = 5
if(a > b)
result = a * b
elseif(a < b)
result = a / b
else
result = a + b
End if
Print result
A. 50
B. 2
C. 15
D. 5
Answer: A
Explanation: Since a > b, the condition is true, so result = a * b = 10 *
5 = 50.

Q12) What will be the output of the following pseudocode for p = 7


and q = 3?
Integer p, q, result
Set p = 7, q = 3
while(q > 0)
p=p*2
q=q-1
End while
Print p
A. 56
B. 28
C. 14
D. 7
Answer: A
Explanation: Initially p = 7, q = 3. In the first iteration, p = 7 * 2 = 14,
and q = 3 - 1 = 2. In the second iteration, p = 14 * 2 = 28, and q = 2 - 1
= 1. In the third iteration, p = 28 * 2 = 56, and q = 1 - 1 = 0. The final
value of p is 56.

Q13) What will be the output of the following pseudocode for a = 4


and b = 2?
Integer a, b, result
Set a = 4, b = 2
if(a mod 2 == 0)
if(b mod 2 == 0)
result = a + b
else
result = a - b
else
result = a * b
End if
Print result
A. 6
B. 2
C. 8
D. 10
Answer: A
Explanation: Since both a and b are even, the first if condition is true,
and the second if condition for b is also true. Therefore, result = a + b
= 4 + 2 = 6.

Q14) What will be the output of the following pseudocode for x = 2


and y = 10?
Integer x, y, result
Set x = 2, y = 10
while(y > 0)
result = x * y
y=y/2
End while
Print result
A. 2
B. 10
C. 40
D. 80
Answer: A
Explanation: The loop repeatedly multiplies x by y and halves y until
y becomes 0. The last value of result is 2, as it was calculated when y
= 1.
Q15) What will be the output of the following pseudocode for a = 4
and b = 6?
Integer a, b, result
Set a = 4, b = 6
if(a < b)
result = a + b
elseif(a > b)
result = a * b
else
result = a - b
End if
Print result
A. 10
B. 24
C. 2
D. 0
Answer: A
Explanation: Since a < b (4 < 6), the first condition is true, so result =
a + b = 4 + 6 = 10.

Q16) What will be the output of the following pseudocode for x = 3,


y = 2, and z = 5?
Integer x, y, z, result
Set x = 3, y = 2, z = 5
if(x > y)
if(z mod 2 == 0)
result = x + y
else
result = x * z
else
result = y + z
End if
Print result
A. 15
B. 6
C. 10
D. 30
Answer: A
Explanation: Since x > y (3 > 2), the first condition is true. Then, since
z mod 2 == 0 (5 is odd, so this is false), result = x * z = 3 * 5 = 15.

Q17) What will be the output of the following pseudocode for a = 9,


b = 4, and c = 3?
Integer a, b, c, result
Set a = 9, b = 4, c = 3
if(a > b)
result = a - c
elseif(b > a)
result = b - c
else
result = a + b + c
End if
Print result
A. 6
B. 16
C. 10
D. 12
Answer: A
Explanation: Since a > b (9 > 4), the first condition is true, so result =
a - c = 9 - 3 = 6.

Q18) What will be the output of the following pseudocode for a = 3


and b = 9?
Integer a, b, result
Set a = 3, b = 9
if(a * b > 15)
result = a + b
elseif(a + b == 12)
result = a - b
else
result = a * b
End if
Print result
A. 12
B. 27
C. 10
D. 8
Answer: A
Explanation: Since a * b = 3 * 9 = 27 is greater than 15, the first
condition is true, so result = a + b = 3 + 9 = 12.

Q19) What will be the output of the following pseudocode for a =


12 and b = 2?
Integer a, b, result
Set a = 12, b = 2
if(a mod b == 0)
result = a / b
else
result = a * b
End if
Print result
A. 6
B. 24
C. 12
D. 2
Answer: A
Explanation: Since a mod b == 0 (12 is divisible by 2), result = a / b =
12 / 2 = 6.

Q20) What will be the output of the following pseudocode for a = 8


and b = 2?
Integer a, b, result
Set a = 8, b = 2
while(b > 0)
result = a * b
a=a/2
b=b-1
End while
Print result
A. 16
B. 8
C. 4
D. 32
Answer: C
Explanation: In the first iteration, result=8×2=16 and after updating a
and b, the second iteration gives result=4×1=4. The final value of
result is 4, as the loop terminates when b = 0.

Q21) What will be the output of the following pseudocode for x = 5


and y = 3?
Integer x, y, result
Set x = 5, y = 3
while(x > 0 AND y > 0)
if(x > y)
result = x - y
else
result = y - x
End if
x=x-1
y=y-1
End while
Print result
A. 2
B. 1
C. 0
D. None of the above
Answer: A
Explanation:
 Iteration 1: x = 5, y = 3 → result = 5 - 3 = 2 → x = 4, y = 2
 Iteration 2: x = 4, y = 2 → result = 4 - 2 = 2 → x = 3, y = 1
 Iteration 3: x = 3, y = 1 → result = 3 - 1 = 2 → x = 2, y = 0
The loop stops as y = 0. The last printed result is 2.

Q22) What will be the output of the following pseudocode for a = 7


and b = 4?
Integer a, b, result
Set a = 7, b = 4
while(b > 0)
result = result + a
b=b-1
End while
Print result
A. 28
B. 21
C. 15
D. 12
Answer: A
Explanation:
 b = 4: result = 0 + 7 = 7
 b = 3: result = 7 + 7 = 14
 b = 2: result = 14 + 7 = 21
 b = 1: result = 21 + 7 = 28
The final value of result is 28.

Q23) What will be the output of the following pseudocode for n =


4?
Integer n, result
Set n = 4, result = 1
while(n > 0)
result = result * n
n=n-1
End while
Print result
A. 24
B. 12
C. 4
D. 6
Answer: A
Explanation:
 n = 4: result = 1 * 4 = 4
 n = 3: result = 4 * 3 = 12
 n = 2: result = 12 * 2 = 24
 n = 1: result = 24 * 1 = 24
The final result is 24.

Q24) What will be the output of the following pseudocode for a = 5


and b = 3?
Integer a, b, result
Set a = 5, b = 3, result = 0
while(b > 0)
if(b mod 2 == 1)
result = result + a
End if
a=a*2
b=b/2
End while
Print result
A. 15
B. 20
C. 25
D. 30
Answer: A
Explanation:
 Iteration 1: b = 3 (odd), result = 0 + 5 = 5, a = 5 * 2 = 10, b = 3 / 2
=1
 Iteration 2: b = 1 (odd), result = 5 + 10 = 15, a = 10 * 2 = 20, b =
1/2=0
Loop ends, and result = 15.

Q25) What will be the output of the following pseudocode for x =


6?
Integer x, result
Set x = 6, result = 0
while(x > 1)
result = result + (x mod 2)
x=x/2
End while
Print result
A. 2
B. 1
C. 3
D. 0
Answer: B
Explanation:
 Iteration 1: x = 6, result = 0 + (6 mod 2 = 0) = 0, x = 6 / 2 = 3
 Iteration 2: x = 3, result = 0 + (3 mod 2 = 1) = 1, x = 3 / 2 = 1
Loop ends. The final result is 1.

Q26) What will be the output of the following pseudocode for p = 4


and q = 3?
Integer p, q, result
Set p = 4, q = 3, result = 0
for(i = 1 to q)
result = result + (p * i)
End for
Print result
A. 12
B. 20
C. 24
D. 18
Answer: C
Explanation:
 Iteration 1: i = 1, result = 0 + (4 * 1) = 4
 Iteration 2: i = 2, result = 4 + (4 * 2) = 12
 Iteration 3: i = 3, result = 12 + (4 * 3) = 24
Final result is 24.

Q27) What will be the output of the following pseudocode for a = 3


and b = 2?
Integer a, b, result
Set a = 3, b = 2
if(a + b > 4)
result = a * b
elseif(a - b < 1)
result = a - b
else
result = a + b
End if
Print result
A. 6
B. 1
C. 5
D. 0
Answer: A
Explanation: Since a + b = 3 + 2 = 5, the first condition is true, so
result = a * b = 3 * 2 = 6.

Q28) What will be the output of the following pseudocode for x = 8


and y = 5?
Integer x, y, result
Set x = 8, y = 5, result = 1
while(y > 0)
if(y mod 2 == 1)
result = result * x
End if
x=x*x
y=y/2
End while
Print result
A. 32768
B. 1024
C. 4096
D. 65536
Answer: A
Explanation:
 Iteration 1: y = 5 (odd), result = 1 * 8 = 8, x = 8 * 8 = 64, y = 5 / 2
=2
 Iteration 2: y = 2 (even), result = 8, x = 64 * 64 = 4096, y = 2 / 2 =
1
 Iteration 3: y = 1 (odd), result = 8 * 4096 = 32768, x = 4096 *
4096, y = 1 / 2 = 0
Final result = 32768.

Q29) What will be the output of the following pseudocode for n =


4?
Integer n, sum
Set n = 4, sum = 0
for(i = 1 to n)
for(j = 1 to i)
sum = sum + j
End for
End for
Print sum
A. 10
B. 20
C. 30
D. 15
Answer: B
Explanation:
 i = 1: sum = 0 + 1 = 1
 i = 2: sum = 1 + 1 + 2 = 4
 i = 3: sum = 4 + 1 + 2 + 3 = 10
 i = 4: sum = 10 + 1 + 2 + 3 + 4 = 20
Final sum = 20.

Q30) What will be the output of the following pseudocode for x = 3


and y = 2?
Integer x, y, result
Set x = 3, y = 2, result = 0
for(i = 1 to y)
result = result + (x * i)
End for
Print result
A. 9
B. 12
C. 15
D. 6
Answer: A
Explanation: In the given pseudocode, the variable result is updated
in each iteration of the loop by adding the product of x and i. For x =
3 and y=2, the result is calculated as 3+6=9 after two iterations.

Q31) What will be the output of the following pseudocode for n =


5?
Integer n, factorial
Set n = 5, factorial = 1
while(n > 0)
factorial = factorial * n
n=n-1
End while
Print factorial
A. 120
B. 24
C. 60
D. 100
Answer: A
Explanation:
 Iteration 1: factorial = 1 * 5 = 5
 Iteration 2: factorial = 5 * 4 = 20
 Iteration 3: factorial = 20 * 3 = 60
 Iteration 4: factorial = 60 * 2 = 120
Final factorial is 120.

Q32) What will be the output of the following pseudocode for a = 7


and b = 4?
Integer a, b, result
Set a = 7, b = 4, result = 0
for(i = 1 to b)
result = result + a
End for
Print result
A. 21
B. 28
C. 35
D. 32
Answer: B
Explanation:
 b = 4: Add 7 four times → 7 + 7 + 7 + 7 = 28.

Q33) What will be the output of the following pseudocode for x =


10 and y = 2?
Integer x, y, result
Set x = 10, y = 2, result = 0
while(x > 0)
if(x mod y == 0)
result = result + x
End if
x=x-1
End while
Print result
A. 20
B. 30
C. 10
D. 12
Answer: B
Explanation:
 x = 10: Add 10 + 8 + 6 + 4 + 2 = 30 (all divisible by 2).

Q34) What will be the output of the following pseudocode for n =


123?
Integer n, sum
Set n = 123, sum = 0
while(n > 0)
sum = sum + (n mod 10)
n = n / 10
End while
Print sum
A. 5
B. 6
C. 7
D. 10
Answer: D
Explanation:
 n = 123: sum = 0 + 3 = 3 → sum = 3 + 2 = 5 → sum = 5 + 1 = 10.

Q35) What will be the output of the following pseudocode for x =


15 and y = 4?
Integer x, y, result
Set x = 15, y = 4, result = 0
while(x > 0 AND y > 0)
if(x mod y == 0)
result = result + x
else
result = result + y
End if
x=x-2
y=y-1
End while
Print result
A. 25
B. 22
C. 24
D. 27
Answer: C
Explanation: The pseudocode sums values based on the condition
x mod y = 0 and x mod y != 0, updating x and y in each iteration. After
four iterations, the final result is 24, as the values are added based on
the condition within the loop.

Q37) What will be the output of the following pseudocode for a = 3


and b = 4?
Integer a, b, result
Set a = 3, b = 4, result = 1
while(b > 0)
if(b mod 2 == 1)
result = result * a
End if
a=a*a
b=b/2
End while
Print result
A. 27
B. 81
C. 64
D. 12
Answer: B
Explanation:
 Iteration 1: b = 4 (even), a = 3 * 3 = 9, b = 4 / 2 = 2.
 Iteration 2: b = 2 (even), a = 9 * 9 = 81, b = 2 / 2 = 1.
 Iteration 3: b = 1 (odd), result = 1 * 81 = 81, b = 1 / 2 = 0.
Final result = 81.

Q38) What will be the output of the following pseudocode for n =


5?
Integer n, result
Set n = 5, result = 0
for(i = 1 to n)
for(j = 1 to i)
result = result + i * j
End for
End for
Print result
A. 135
B. 155
C. 145
D. 140
Answer: D
Q39) What will be the output of the following pseudocode for a =
12?
Integer a, count
Set a = 12, count = 0
while(a > 1)
if(a mod 2 == 0)
a=a/2
else
a=3*a+1
End if
count = count + 1
End while
Print count
A. 5
B. 8
C. 9
D. 10
Answer: C
Explanation:
 Iteration 1: a = 12 → a = 12 / 2 = 6, count = 1.
 Iteration 2: a = 6 → a = 6 / 2 = 3, count = 2.
 Iteration 3: a = 3 → a = 3 * 3 + 1 = 10, count = 3.
 Iteration 4: a = 10 → a = 10 / 2 = 5, count = 4.
 Iteration 5: a = 5 → a = 3 * 5 + 1 = 16, count = 5.
 Iteration 6: a = 16 → a = 16 / 2 = 8, count = 6.
 Iteration 7: a = 8 → a = 8 / 2 = 4, count = 7.
 Iteration 8: a = 4 → a = 4 / 2 = 2, count = 8.
 Iteration 9: a = 2 → a = 2 / 2 = 1, count = 9.
Final count = 9.

Q40) What will be the output of the following pseudocode for x =


20?
Integer x, sum
Set x = 20, sum = 0
for(i = 1 to x)
if(i mod 3 == 0 AND i mod 5 == 0)
sum = sum + i
End if
End for
Print sum
A. 0
B. 15
C. 45
D. 30
Answer: C
Explanation:
 Numbers divisible by 3 and 5 up to 20 are 15.
Final sum = 15.
Q41) What will be the output of the following pseudocode for x =
9?
Integer x, sum
Set x = 9, sum = 0
for(i = 1 to x)
if(i mod 2 == 1)
sum = sum + i * i
else
sum = sum - i
End if
End for
Print sum
A. 145
B. 130
C. 125
D. 180
Answer: A
Explanation:
 i = 1: sum = 0 + (1 * 1) = 1.
 i = 2: sum = 1 - 2 = -1.
 i = 3: sum = -1 + (3 * 3) = 8.
 i = 4: sum = 8 - 4 = 4.
 i = 5: sum = 4 + (5 * 5) = 29.
 i = 6: sum = 29 - 6 = 23.
 i = 7: sum = 23 + (7 * 7) = 72.
 i = 8: sum = 72 - 8 = 64.
 i = 9: sum = 64 + (9 * 9) = 145.

Q42) What will be the output of the following pseudocode for x = 5


and y = 3?
Integer x, y, result
Set result = 0
for(i = 1 to y)
result = result + x
x=x+2
End for
Print result
A. 35
B. 21
C. 40
D. 25
Answer: B
Explanation:
 Iteration 1: result = 0 + 5 = 5, x = 5 + 2 = 7.
 Iteration 2: result = 5 + 7 = 12, x = 7 + 2 = 9.
 Iteration 3: result = 12 + 9 = 21.

Q43) What will be the output of the following pseudocode for n =


7?
Integer n, result
Set result = 1
while(n > 1)
if(n mod 2 == 0)
result = result + n
else
result = result * n
End if
n=n-1
End while
Print result
A. 200
B. 250
C. 209
D. 150
Answer: C
Explanation:
 n = 7: result = 1 * 7 = 7.
 n = 6: result = 7 + 6 = 13.
 n = 5: result = 13 * 5 = 65.
 n = 4: result = 65 + 4 = 69.
 n = 3: result = 69 * 3 = 207.
 n = 2: result = 207 + 2 = 209.
 n = 1: loop exits.

Q44) What will be the output of the following pseudocode for a = 4


and b = 6?
Integer a, b, result
Set result = 0
for(i = 1 to b)
for(j = 1 to a)
result = result + (i * j)
End for
End for
Print result
A. 120
B. 80
C. 72
D. 210
Answer: D
Explanation: The pseudocode calculates the sum of the products of i
and j for each combination of i (from 1 to 6) and j (from 1 to 4). The
final result is 210, which is the cumulative sum after iterating through
all combinations.

Q45) What will be the output of the following pseudocode for x = 4


and y = 5?
Integer x, y, result
Set result = 1
while(x > 0 AND y > 0)
if(x > y)
result = result * x
x=x-1
else
result = result + y
y=y-2
End if
End while
Print result
A. 120
B. 140
C. 163
D. 148
Answer: C
Explanation: The pseudocode iterates while both x > 0 and y > 0,
performing different operations based on whether x is greater than y
or not. In each iteration, the result is updated, and after exiting the
loop, the final value of result is 163.

Q46) What will be the output of the following pseudocode for n =


6?
Integer n, result
Set result = 0
for(i = 1 to n)
for(j = 1 to i)
result = result + (i * j)
End for
End for
Print result
A. 250
B. 266
C. 256
D. 172
Answer: B
Explanation: The pseudocode calculates the sum of products of the
current value of i and the inner loop's j for all iterations. For each
value of i, the sum of i * j (where j ranges from 1 to i) is added to the
result, leading to a total sum of 266.

Q47) What will be the output of the following pseudocode for n =


5?
Integer n, result
Set result = 1
for(i = 1 to n)
if(i mod 2 == 0)
result = result + i
else
result = result * i
End if
End for
Print result
A. 48
B. 30
C. 65
D. 60
Answer: C
Explanation:
 i = 1: result = 1 * 1 = 1.
 i = 2: result = 1 + 2 = 3.
 i = 3: result = 3 * 3 = 9.
 i = 4: result = 9 + 4 = 13.
 i = 5: result = 13 * 5 = 65.

Q48) What will be the output of the following pseudocode for a = 3,


b = 4?
Integer a, b, result
Set result = 0
while(a > 0 OR b > 0)
if(a mod 2 == 1)
result = result + a
else
result = result - b
End if
a=a-1
b=b-2
End while
Print result
A. 10
B. 2
C. 5
D. 8
Answer: B
Explanation:
 a = 3, b = 4: result = 0 + 3 = 3, a = 2, b = 2.
 a = 2, b = 2: result = 3 - 2 = 1, a = 1, b = 0.
 a = 1, b = 0: result = 1 + 1 = 2.

Q49) What will be the output of the following pseudocode for n


= 4?
Integer n, sum
Set sum = 0
for(i = 1 to n)
for(j = i to n)
sum = sum + (i * j)
End for
End for
Print sum
A. 55
B. 65
C. 50
D. 55
Answer: B
Explanation: The pseudocode calculates the sum of products of
pairs (i, j) where i ranges from 1 to n and j ranges from i to n. The
sum accumulates values like (i * j) for each valid pair, resulting in a
final sum of 65 when n = 4.
Q50) What will be the output of the following pseudocode for x =
7 and y = 9?
Integer x, y, count
Set count = 0
while(x > 0 AND y > 0)
if(x mod 2 == 0)
count = count + y
x=x/2
else
count = count + x
y=y-1
End if
End while
Print count
A. 40
B. 63
C. 72
D. 24
Answer: B
Explanation:
 Loop continues until x = 0 and final count = 63.

Q51) What will be the output of the following pseudocode for m


= 2 and n = 3?
Integer m, n, sum
Set sum = 1
for(i = 1 to m)
for(j = 1 to n)
sum = sum * i + j
End for
End for
Print sum
A. 14
B. 25
C. 20
D. 30
Answer: A
Explanation:
 i = 1: sum = (1 * 1 + 1) + (1 * 1 + 2) + (1 * 1 + 3).
 i = 2: Repeat for values.

Q52) What will be the output of the following pseudocode for n


= 5?
Integer n, i, j, count
Set count = 0
for(i = 1 to n)
for(j = 1 to i)
if((i + j) mod 2 == 0)
count = count + 1
End if
End for
End for
Print count
A. 10
B. 8
C. 6
D. 12
Answer: A

Q53) What will be the output for the following pseudocode with
a = 6 and b = 3?
Function calc(Integer a, Integer b)
if(b == 0)
return 1
else
return a * calc(a, b - 1)
End if
End function

Print calc(a, b)
A. 6
B. 18
C. 216
D. 729
Answer: C
Explanation:
Recursive function calculates a^b. Here, 6^3 = 216.
Q54) What is the output for the following pseudocode if x = 7
and y = 2?
Function mystery(Integer x, Integer y)
if(y == 0)
return x
else
return mystery(x + y, y - 1)
End if
End function

Print mystery(x, y)
A. 7
B. 10
C. 12
D. 15
Answer: B
Explanation:
 Call stack: mystery(7, 2) -> mystery(9, 1) -> mystery(10, 0)..

Q55) What will be the result of the following pseudocode for n =


4?
Integer n, sum, temp
Set sum = 0
for(i = 1 to n)
temp = i
while(temp > 0)
sum = sum + (temp mod 2)
temp = temp / 2
End while
End for
Print sum
A. 6
B. 4
C. 8
D. 5
Answer: D

 1: 1 -> 1 bit.
 2: 10 -> 1 bit.
 3: 11 -> 2 bits.
 4: 100 -> 1 bit.
Total = 5 bits.

Q56) What will the following pseudocode return for a = 9 and b =


4?
Function gcd(Integer a, Integer b)
if(b == 0)
return a
else
return gcd(b, a mod b)
End if
End function

Print gcd(a, b)
A. 1
B. 4
C. 2
D. 9
Answer: A
Explanation:
The pseudocode implements the Euclidean algorithm for finding
GCD.
 gcd(9, 4) -> gcd(4, 1) -> gcd(1, 0).

Q57) What will be the output of the following pseudocode for n


= 3?
Integer n, sum
Set sum = 0
for(i = 1 to n)
for(j = 1 to i)
sum = sum + (i * j)
End for
End for
Print sum
A. 10
B. 25
C. 20
D. 12
Answer: B
Explanation:
 i = 1: sum = 1.
 i = 2: sum = 1 + (2*1 + 2*2) = 7.
 i = 3: sum = 7 + (3*1 + 3*2 + 3*3) = 25.

Q58) What will be the result for the pseudocode below for a =
10, b = 2?
Integer a, b, count
Set count = 0
while(a > 0)
if(a mod b == 0)
count = count + 1
a=a-1
End while
Print count
A. 4
B. 5
C. 10
D. 2
Answer: B
Explanation:
The pseudocode counts numbers divisible by b from 1 to a.
 Divisible: 2, 4, 6, 8, 10. Count = 5.
Q59) What will be the output of the following pseudocode for n
= 4?
Function computeSum(Integer n)
if(n == 0)
return 0
else
return n + computeSum(n - 1)
End if
End function

Print computeSum(n)
A. 10
B. 6
C. 15
D. 4
Answer: A
Explanation: The pseudocode calculates the sum of the first n
natural numbers.
 For n = 4, sum = 4 + 3 + 2 + 1 = 10.

Q60) What will be the output of the following pseudocode for a =


3, b = 4?
Function mystery(Integer a, Integer b)
if(b == 0)
return 1
else
return mystery(a, b - 1) * a
End if
End function

Print mystery(a, b)
A. 12
B. 81
C. 64
D. 243
Answer: B
Explanation:
The pseudocode computes a^b.
 For a = 3 and b = 4, result = 3^4 = 81.

Q61) Find the output of the pseudocode below for x = 5 and y =


3:
Function calcGCD(Integer x, Integer y)
if(y == 0)
return x
else
return calcGCD(y, x mod y)
End if
End function

Print calcGCD(x, y)
A. 1
B. 2
C. 3
D. 5
Answer: A
Explanation:
The pseudocode implements the Euclidean algorithm.
 calcGCD(5, 3) → calcGCD(3, 2) → calcGCD(2, 1) → calcGCD(1,
0).
 Final result: 1.

Q62) What will be the output of the following pseudocode if arr


= [1, 2, 3, 4]?
Function reverseArray(Array arr, Integer n)
if(n <= 0)
return
else
Print arr[n - 1]
reverseArray(arr, n - 1)
End if
End function

reverseArray(arr, length(arr))
A. 4 3 2 1
B. 1 2 3 4
C. 4
D. None
Answer: A
Explanation:
The pseudocode prints the array in reverse order using recursion.

Q63) What is the output for the following pseudocode for n = 3?


Function factorial(Integer n)
if(n == 0 OR n == 1)
return 1
else
return n * factorial(n - 1)
End if
End function

Print factorial(n)
A. 3
B. 6
C. 9
D. 1
Answer: B
Explanation:
The pseudocode calculates n!.
 For n = 3, result = 3 * 2 * 1 = 6.

Q64) What will the following pseudocode return for n = 6?


Function isEven(Integer n)
if(n == 0)
return true
else if(n == 1)
return false
else
return isEven(n - 2)
End if
End function

Print isEven(n)
A. true
B. false
C. Error
D. None
Answer: A
Explanation:
The pseudocode checks if a number is even recursively.
 For n = 6, result = true.

Q65) Find the output of the following pseudocode if a = 5 and b =


3:
Function mystery(Integer a, Integer b)
if(b == 0)
return 0
else
return a + mystery(a, b - 1)
End if
End function

Print mystery(a, b)
A. 15
B. 8
C. 5
D. 3
Answer: A
Explanation:
The pseudocode calculates the product of two numbers using
recursion.
 For a = 5 and b = 3, result = 5 * 3 = 15.

Q66) What will be the output of the following pseudocode for x =


1234?
Function countDigits(Integer x)
if(x == 0)
return 0
else
return 1 + countDigits(x / 10)
End if
End function

Print countDigits(x)
A. 4
B. 5
C. 3
D. None
Answer: A
Explanation:
The pseudocode counts the number of digits in x using recursion.
 For x = 1234, result = 4.

Q75) What is the output for n = 1234 in the following


pseudocode?
Function reverse(Integer n, Integer rev)
if(n == 0)
return rev
else
return reverse(n / 10, rev * 10 + (n mod 10))
End if
End function

Print reverse(n, 0)
A. 4321
B. 1234
C. 2143
D. 0
Answer: A
Explanation:
The pseudocode reverses the digits of n recursively.
 For n = 1234, result = 4321.
Q76) What will be the output of the following pseudocode for n
= 4?
Function factorial(Integer n)
if(n == 0)
return 1
else
return n * factorial(n - 1)
End if
End function

Print factorial(n)
A. 16
B. 24
C. 8
D. None
Answer: B
Explanation:
The pseudocode calculates n! recursively.
 For n = 4, result = 4 * 3 * 2 * 1 = 24.

Q77) What is the output of the pseudocode for arr = [4, 6, 8]?
Function maxArray(Array arr, Integer n, Integer maxVal)
if(n == 0)
return maxVal
else
return maxArray(arr, n - 1, max(arr[n - 1], maxVal))
End if
End function

Print maxArray(arr, length(arr), arr[0])


A. 6
B. 4
C. 8
D. None
Answer: C
Explanation:
The pseudocode finds the maximum value in an array recursively.
 For arr = [4, 6, 8], result = 8.

Q78) What is the output for a = 12 in the following pseudocode?


Function sumDigits(Integer a)
if(a == 0)
return 0
else
return (a mod 10) + sumDigits(a / 10)
End if
End function

Print sumDigits(a)
A. 12
B. 3
C. 6
D. None
Answer: B
Explanation:
The pseudocode calculates the sum of digits of a recursively.
 For a = 12, result = 1 + 2 = 3.

Q79) What is the output of the following pseudocode for n = 6?


Function evenSum(Integer n)
if(n == 0)
return 0
else if(n mod 2 == 0)
return n + evenSum(n - 1)
else
return evenSum(n - 1)
End if
End function

Print evenSum(n)
A. 12
B. 20
C. 18
D. 6
Answer: A
Explanation:
The pseudocode calculates the sum of even numbers up to n.
 For n = 6, result = 6 + 4 + 2 = 12.

Q80) What will the following pseudocode return for n = 10?


Function gcd(Integer a, Integer b)
if(b == 0)
return a
else
return gcd(b, a mod b)
End if
End function

Print gcd(10, 15)


A. 5
B. 15
C. 10
D. None
Answer: A
Explanation:
The pseudocode calculates the greatest common divisor (GCD)
using recursion.
 For 10 and 15, GCD = 5.

Q81) What is the result of the following pseudocode for n = 5?


Function printNumbers(Integer n)
if(n == 0)
return
else
Print n
printNumbers(n - 1)
End if
End function

printNumbers(n)
A. 5 4 3 2 1
B. 1 2 3 4 5
C. Error
D. None
Answer: A
Explanation:
The pseudocode prints numbers from n to 1 recursively.
 For n = 5, result = 5 4 3 2 1.

Q82) What is the output for x = 3 in the following pseudocode?


Function mystery(Integer x)
if(x == 0)
return 0
else
return x + mystery(x - 1)
End if
End function
Print mystery(x)
A. 3
B. 6
C. 10
D. None
Answer: B
Explanation:
The pseudocode calculates the sum of the first x natural numbers.
 For x = 3, result = 3 + 2 + 1 = 6.

Q83) What is the output of the following pseudocode for n = 7?


Function isPrime(Integer n, Integer i)
if(i == 1)
return true
else if(n mod i == 0)
return false
else
return isPrime(n, i - 1)
End if
End function

Print isPrime(n, n - 1)
A. True
B. False
C. Error
D. None
Answer: A
Explanation:
The pseudocode checks if n is prime recursively.
 For n = 7, no divisor exists between 7 and 1. Output = True.

Q84) What is the output for the array arr = [1, 2, 3, 4, 5] in the
pseudocode?
Function sumArray(Array arr, Integer n)
if(n == 0)
return 0
else
return arr[n - 1] + sumArray(arr, n - 1)
End if
End function

Print sumArray(arr, length(arr))


A. 10
B. 15
C. 20
D. 25
Answer: B
Explanation:
The pseudocode calculates the sum of the array elements
recursively.
 For arr = [1, 2, 3, 4, 5], result = 1 + 2 + 3 + 4 + 5 = 15.

Q85) What is the output for n = 4 in the following pseudocode?


Function fibonacci(Integer n)
if(n == 0)
return 0
else if(n == 1)
return 1
else
return fibonacci(n - 1) + fibonacci(n - 2)
End if
End function

Print fibonacci(n)
A. 5
B. 3
C. 2
D. 8
Answer: B
Explanation:
For n = 4, the Fibonacci sequence follows this recursive pattern:
 fibonacci(4) = fibonacci(3) + fibonacci(2)
o fibonacci(3) = fibonacci(2) + fibonacci(1)
 fibonacci(2) = fibonacci(1) + fibonacci(0)
 fibonacci(1) = 1 (base case)
 fibonacci(0) = 0 (base case)
 fibonacci(2) = 1 + 0 = 1
 fibonacci(1) = 1
 fibonacci(3) = 1 + 1 = 2
o fibonacci(2) = 1
o fibonacci(4) = 2 + 1 = 3
Thus, the final output for fibonacci(4) is 3.

Q86) What will the following pseudocode return for x = 3 and y =


2?
Function power(Integer x, Integer y)
if(y == 0)
return 1
else
return x * power(x, y - 1)
End if
End function

Print power(x, y)
A. 6
B. 8
C. 9
D. None
Answer: C
Explanation:
The pseudocode calculates x^y recursively.
 For x = 3, y = 2, result = 3 * 3 = 9.

Q87) What is the output for x = 4 in the following pseudocode?


Function printPattern(Integer x)
if(x == 0)
return
else
Print x
printPattern(x - 1)
Print x
End if
End function

printPattern(x)
A. 4 3 2 1
B. 4 3 2 1 1 2 3 4
C. 4 3 2 1 2 3 4
D. None
Answer: C
Explanation:
The pseudocode prints a pattern recursively.
 For x = 4, result = 4 3 2 1 2 3 4.

Q88) What is the output of the pseudocode for a = 5?


Function factorialSum(Integer a)
if(a == 0)
return 1
else
return a * factorialSum(a - 1) + a
End if
End function

Print factorialSum(a)
A. 125
B. 130
C. 150
D. None
Answer: D

Q89) What will the pseudocode return for arr = [2, 4, 6]?
Function evenProduct(Array arr, Integer n)
if(n == 0)
return 1
else
return arr[n - 1] * evenProduct(arr, n - 1)
End if
End function

Print evenProduct(arr, length(arr))


A. 48
B. 24
C. 12
D. 36
Answer: A
Explanation:
The pseudocode calculates the product of all elements recursively.
 For arr = [2, 4, 6], result = 2 * 4 * 6 = 48.

Q90) What is the output of this pseudocode for n = 5?


Function seriesSum(Integer n)
if(n == 0)
return 0
else
return n + seriesSum(n - 2)
End if
End function

Print seriesSum(n)
A. 9
B. 15
C. 8
D. None
Answer: A
Explanation:
The pseudocode calculates the sum of numbers skipping by 2.
 For n = 5, result = 5 + 3 + 1 = 9.

Q91) What will the following pseudocode output for n = 6?


Function sumEvenNumbers(Integer n)
if(n == 0)
return 0
else if(n mod 2 == 0)
return n + sumEvenNumbers(n - 1)
else
return sumEvenNumbers(n - 1)
End if
End function

Print sumEvenNumbers(n)
A. 12
B. 20
C. 18
D. 6
Answer: A
Explanation:
The pseudocode sums all even numbers up to n.
 For n = 6, result = 6 + 4 + 2 = 12.

Q92) What is the output for the following pseudocode if arr = [1,
2, 3, 4, 5]?
Function maxValue(Array arr, Integer n)
if(n == 1)
return arr[0]
else
return max(arr[n - 1], maxValue(arr, n - 1))
End if
End function

Print maxValue(arr, length(arr))


A. 1
B. 5
C. 4
D. None
Answer: B
Explanation:
The pseudocode recursively finds the maximum value in the array.
 For arr = [1, 2, 3, 4, 5], the maximum value is 5.

Q93) What will be the output for x = 3 and y = 2 in this


pseudocode?
Function gcd(Integer x, Integer y)
if(y == 0)
return x
else
return gcd(y, x mod y)
End if
End function

Print gcd(x, y)
A. 1
B. 2
C. 3
D. None
Answer: A
Explanation:
The pseudocode calculates the greatest common divisor (GCD).
 For x = 3, y = 2, result = 1.

Q94) What is the output of this pseudocode for n = 4?


Function countDigits(Integer n)
if(n == 0)
return 0
else
return 1 + countDigits(n / 10)
End if
End function

Print countDigits(n)
A. 4
B. 3
C. 1
D. None
Answer: C
Explanation:
The pseudocode counts the number of digits in n.
 For n = 4, result = 1 (single digit).
Q95) What is the output for this pseudocode for arr = [2, 3, 5, 7]?
Function sumPrimes(Array arr, Integer n)
if(n == 0)
return 0
else if(isPrime(arr[n - 1]))
return arr[n - 1] + sumPrimes(arr, n - 1)
else
return sumPrimes(arr, n - 1)
End if
End function

Print sumPrimes(arr, length(arr))


A. 17
B. 15
C. 16
D. 18
Answer: A
Explanation:
The pseudocode sums prime numbers in the array.
 For arr = [2, 3, 5, 7], result = 2 + 3 + 5 + 7 = 17.

Q96) What is the output for this pseudocode for n = 6?


Function factorialDifference(Integer n)
if(n == 0)
return 1
else
return n - factorialDifference(n - 1)
End if
End function

Print factorialDifference(n)
A. 1
B. 4
C. 7
D. 6
Answer: B
Explanation:
The pseudocode alternates subtracting the factorial difference.

Q97) What will the pseudocode output for x = 8?


Function reverseBinary(Integer x)
if(x == 0)
return 0
else
return (x mod 2) + 10 * reverseBinary(x / 2)
End if
End function

Print reverseBinary(x)
A. 1000
B. 1
C. 0001
D. 111
Answer: A
Explanation:
The pseudocode calculates the reverse binary of x.
 For x = 8, binary = 1000. Reverse remains 1000.

Q98) What will be the output for the following pseudocode with
n = 4?
Function calculate(Integer n)
if(n == 1)
return 1
else
return n * calculate(n - 1) + n
End if
End function

Print calculate(n)
A. 64
B. 24
C. 20
D. 22
Answer: A
Explanation:
This pseudocode calculates a modified factorial with an addition at
each step.
Q99) What is the output of the following pseudocode for n = 5?
Function mysterySum(Integer n)
if(n == 0)
return 0
else
return n + mysterySum(n - 2)
End if
End function

Print mysterySum(n)
A. 9
B. 12
C. 6
D. 15
Answer: A
Explanation:
This pseudocode adds alternate integers.
 For n = 5, result = 5 + 3 + 1 = 9.

Q100) What will the following pseudocode output for arr = [10,
20, 30, 40, 50]?
Function rotateLeft(Array arr, Integer n)
if(n == 1)
return arr
else
temp = arr[0]
for i = 0 to length(arr) - 2
arr[i] = arr[i + 1]
End for
arr[length(arr) - 1] = temp
return rotateLeft(arr, n - 1)
End if
End function

Print rotateLeft(arr, 2)
A. [30, 40, 50, 10, 20]
B. [40, 50, 10, 20, 30]
C. [20, 30, 40, 50, 10]
D. [10, 20, 30, 40, 50]
Answer: A

Q101) What is the output of this pseudocode for n = 6?


Function fibonacciMod(Integer n)
if(n == 0)
return 0
else if(n == 1)
return 1
else
return (fibonacciMod(n - 1) + fibonacciMod(n - 2)) mod 5
End if
End function

Print fibonacciMod(n)
A. 3
B. 1
C. 0
D. 4
Answer: A

Q102) What will the output be for this pseudocode with x = 2


and y = 3?
Function powerSum(Integer x, Integer y)
if(y == 0)
return 1
else
return x + powerSum(x, y - 1)
End if
End function

Print powerSum(x, y)
A. 8
B. 6
C. 9
D. 7
Answer: B
Explanation:
The pseudocode sums x repeated y times (equivalent to
multiplication).
 For x = 2, y = 3, result = 2 + 2 + 2 = 6.

Q103) What will be the output for the following pseudocode


with arr = [2, 4, 6, 8]?
Function reverseArray(Array arr, Integer n)
if(n <= 1)
return arr
else
temp = arr[0]
arr[0] = arr[n - 1]
arr[n - 1] = temp
return reverseArray(arr + 1, n - 2)
End if
End function

Print reverseArray(arr, length(arr))


A. [8, 6, 4, 2]
B. [2, 6, 4, 8]
C. [2, 4, 6, 8]
D. None
Answer: A
Explanation:
The pseudocode reverses the array recursively.
 Result = [8, 6, 4, 2].
Q104) What is the output for this pseudocode for n = 12345?
Function reverseDigits(Integer n, Integer reverse)
if(n == 0)
return reverse
else
return reverseDigits(n / 10, reverse * 10 + (n mod 10))
End if
End function

Print reverseDigits(n, 0)
A. 54321
B. 12345
C. 0
D. 123
Answer: A
Explanation:
The pseudocode reverses the digits of n.
 For n = 12345, result = 54321.

You might also like