Top 14 Capgemini Pseudocode Questions
Q1) What is the output of this C code?
#include <stdio.h>
int main()
{
int i= 0, j = 0;
while (I1: i < 2)
{
i++;
while (j < 3)
{
printf("loop\n");
goto I1;
}
}
}
A. loop loop loop loop
B. loop loop
C. Compile time error
D. Infinite loop
Answer : Option C
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.
Q2) What is the output of this code?
#include<stdio.h>
int main()
{
static int i = 5;
if (--i){
printf("%d ",i);
main();
}
}
A. 4 3 2 1
B. 4 4 4 4
C. 0 0 0 0
D. 1 2 3 4
Answer : Option A
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.
Q3) Consider the below given fragment of code.
int f(int &x, int c) {
c = c - 1;
if (c == 0) retum 1;
x = x + 1;
return f(x, c) * x;
}
In above program the first parameter is passed by reference and the second parameter is passed by value. If the value of p = 5 before the call then what is the value that returned by f(p, p)?
A. 6561
B. 161051
C. 55440
D. 3024
Answer : Option A
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
Q4) Comment on the output of this C code?
#include <stdio.h>
int main()
{
int x = 3,i=0;
do{
X= x ++;
i++;
} while (i != 3);
printf(“%d\n", x);
}
A. Output will be 5
B. Output will be 6
C. Output will be 3
D. Undefined behaviour
Answer : Option C
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.
Q5) What will be the value of s if n=127?
Read n
i=0,s=0
Function Sample(int n)
while(n>0)
r=n%l0
p=8^i
s=s+p*r
i++
n=n/10
End While
Return s;
End Function
A. 27
B. 187
C. 87
D. 120
Answer : Option C
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
Q6) What will be the output if limit = 6?
Read limit n1 = 0, n2= 1, n3=1, count = 1;
while count <= limit
count=count+1
print n3
n3 = n1 + n2
n1 = n2
n2 = n3
End While
A. 1235813
B. 12358
C. 123581321
D. 12358132
Answer : Option A
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
Q7) What will be the value of even_counter if number = 2630?
Read number
Function divisible(number)
even_counter = 0, num_remainder = number;
while (num_remainder)
digit = num_remainder % 10;
if digit != 0 AND number % digit == 0
even_counter= even_counter+1
End If
num_remainder= num_remainder / 10;
End While
return even_counter;
A. 3
B. 4
C. 2
D. 1
Answer : Option D
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.
Q8) What will be the value of t if a = 56 , b = 876?
Read a,b
Function mul(a, b)
t = 0
while (b != 0)
t = t + a
b=b-1
End While
return t;
End Function
A. 490563
B. 49056
C. 490561
D. None of the mentioned
Answer : Option B
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
Q9) Code to sort given array in ascending order:
Read size
Read a[1],a[2],…a[size]
i=0
While(i<size)
j=i+1
While(j<size)
If a[i] < a[j] then
t= a[i];
a[i] = a[j];
a[j] = t;
End If
j=j+1
End While
i=i+1
End While
i=0
While (i<size)
print a[i]
i=i+1
End While
wrong statement?
A. Line 4
B. Line 6
C. Line 7
D. No Error
Answer : Option C
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].
Q10) Consider the following piece of code. What will be the space required for this code?
int sum(int A[], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
} // sizeof(int) = 2 bytes
A. 2n + 8
B. 2n + 4
C. 2n + 2
D. 2n
Answer : Option A
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
Q11) What will be the output of the following pseudo code?
For input a=8 & b=9.
Function(input a,input b)
If(a<b)
return function(b,a)
elseif(b!=0)
return (a+function(a,b-1))
else
return 0
A. 56
B. 88
C. 72
D. 65
Answer : Option C
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)
- function(9,8)
- function(9,7) + 9
- function(9,6) + 9
- function(9,5) + 9
- function(9,4) + 9
- function(9,3) + 9
- function(9,2) + 9
- function(9,1) + 9
- function(9,0) + 9
- when b = 0, recursion ends , returns 0
So, 0 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 = 72
Q12) What will be the output of the following pseudo code?
Input m=9,n=6
m=m+1
N=n-1
m=m+n
if (m>n)
print m
else
print n
A. 6
B. 5
C. 10
D. 15
Answer : Option D
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
Q13) What will be the output of the following pseudo code?
Input f=6,g=9 and set sum=0
Integer n
if(g>f)
for(n=f;n<g;n=n+1)
sum=sum+n
End for loop
else
print error message
print sum
A. 21
B. 15
C. 9
D. 6
Answer : Option A
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.
Q14) What will be the output if limit = 6?
Read limit n1 = 0, n2= 1, n3=1, count = 1;
while count <= limit
count=count+1
print n3
n3 = n1 + n2
n1 = n2
n2 = n3
End While
A. 1235813
B. 12358
C. 23581321
D. 12358132
Answer : Option B
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