05 August 2023
Capgemini Pseudocode Questions
Explanation: goto label is expected to give at any line except, conditional areas. In the given code, compiler fails to identify I1 as label & it will consider I1 as an undeclared identifier.
Explanation: Here, main() is a recursive function which will keep on executing till if condition is false. Initial value of static variable i = 5. i--(Pre-decrement) will update the i value as 4. As long as value inside if brackets are Non-zero numbers, condition will be true. As I is a static variable, re-initialization in the next function call will not affect I value. i value will keep on decrement and print 4 3 2 1. After 1, i becomes 0 and the if condition fails followed by ending recursive calls.
Explanation: As X is call by reference, last updated value of X will be assigned to the variable for all the recursive calls. When c value becomes 0, x value will be 9. So,
f(5,5)
->f(6,4) * 9
-> f(7,3) * 9
-> f(8,2) * 9
-> f(9,1) * 9
-> c == 0 & returns 1
Ans => 1 * 9 * 9 * 9 * 9 = 6561
Explanation: Here i = 0, x = 3.
x = x++( x = 3)
Post increment will assign value before gets updated.
I value will increment by 1 and loop will run 3 times.
When I becomes 3, while loop fails(3 != 3 returns false)
X value remains 3 because always value gets reassigned before the updation.
Explanation: Here i=0,s=0,n=127
(127>0) while condition true
r = 127 % 10 = 7
p = 8 ^ 0 = 8
s = 0 + 8 * 7 = 56
i = 1
n = 127/10 = 12
(12>0) while condition true
r = 12 % 10 = 2
p = 8 ^ 1 = 9
s = 56 + 9 * 2 = 74
i = 2
n = 12/10 = 1
(1>0) while condition true
r = 1 % 10 = 1
p = 8^2 = 10
s = 74 + 1 * 10 = 84
I = 3
N = 1/10 = 0
(0>0) While condition fails
S = 84
Explanation: The above pseudocode will print Fibonacci numbers from 1 to 13 without any space
(1<=6) while condition is true
count = 2
print 1
n3 = 1 + 1 = 2
n1 = 1
n2 = 2
(2<=6) while condition is true
count = 3
print 2
n3 = 1 + 2 = 3
n1 = 2
n2 = 3
(3<=6) while condition is true
count = 4
print 3
n3 = 2 + 3 = 5
n1 = 3
n2 = 5
(4<=6) while condition is true
count = 5
print 5
n3 = 5 + 3 = 8
n1 = 5
n2 = 8
(5<=6) while condition is true
count = 6
print 8
n3 = 5 + 8 = 13
n1 = 8
n2 = 13
(6<=6) while condition is true
count = 7
print 13
n3 = 8 + 13 = 21
n1 = 13
n2 = 21
(7<=6) while condition false
Ans : 1235813
Explanation: The above pseudocode counts the number of digits which are factors of given input.
even_counter = 0, num_remainder = 2630
(2630) while condition is true
digit = 2630 % 10 = 0
digit = 0. Therefore, if condition fails
num_remainder= 263
even_counter = 0, num_remainder = 263
(263) while condition is true
digit = 263 % 10 = 3
3 != 0 is true. But, 2630%3 == 0 is false. Therefore, if condition fails
num_remainder= 26
even_counter = 0, num_remainder = 26
(26) while condition is true
digit = 26 % 10 = 6
6 != 0 is true.But, 2630%6 == 0 is false. Therefore, if condition fails
num_remainder= 2
even_counter = 0, num_remainder = 2
(2) while condition is true
digit = 2 % 10 = 2
3 != 0 is true and 2630%2 == 0 is also true. Therefore, if condition is true
even_counter = 0 + 1 = 1
num_remainder= 0
even_counter = 1
(0) while condition is false
So, even_counter = 1.
Explanation: Here a = 56, b = 876
While loop condition is (876 != 0) and the updation is b = b-1. So, the loop runs 876 times. During each iteration, a value is updated as a = a + 56.
So, Ans = 876 * 56 = 49056
Explanation: The given pseudocode will sort array elements in descending order.
Here when i = 0, j starts from 1 and goes till n-1
In line no 7, a[0] < a[1] swaps both elements. So, eventually the greatest value will be stored in a[0].
Explanation: n is the size of the array. So, to store array elements we need 2n bytes.
2 bytes for n, sum & i variables = 6 bytes.
2 bytes to store the return value.
So 2n + 8
Explanation: Here, a=8 & b=9.
If first value is small, recursive call will happen with first value as large. Else if condition will be true as long as b !=0.
Recursive calls are ..
function(8,9)
So, 0 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 = 72
Explanation: Here m = 9, n = 6
m = 9 + 1 = 10
n = n – 1 = 5
m = 10 + 5 = 15
if(15>5) condition is true.
print m(15)
So, Ans = 15
Explanation: Here f=6,g=9 and sum = 0
If(9>6) condition is true
Loop will run 3 times
When n = 6, sum = 0 + 6 = 6
When n = 7, sum = 6 + 7 = 13
When n = 8, sum = 13 + 8 = 21
So, sum = 21.
Explanation : The above pseudocode will print Fibonacci numbers from 1 to 13 without any space
(1<=6) while condition is true
count = 2
print 1
n3 = 0 + 1 = 1
n1 = 1
n2 = 1
(2<=6) while condition is true
count = 3
print 1
n3 = 1 + 1 = 2
n1 = 1
n2 = 2
(3<=6) while condition is true
count = 4
print 2
n3 = 1 + 2 = 3
n1 = 2
n2 = 3
(4<=6) while condition is true
count = 5
print 3
n3 = 2 + 3 = 5
n1 = 3
n2 = 5
(5<=6) while condition is true
count = 6
print 5
n3 = 3 + 5 = 8
n1 = 5
n2 = 8
(6<=6) while condition is true
count = 7
print 8
n3 = 5 + 8 = 13
n1 = 8
n2 = 13
(7<=6) while condition false
Ans : 112358