Practice Pseudo code for Capgemini

Question 1 :

Point out the error

#include<stdio.h>
int main()
{
char ch; int i;
scanf(“%c”, &i);
scanf(“%d”, &ch);
printf(“%c %d”, ch, i); return 0;
}

a. Error: suspicious char to in conversion in scanf()
b. Error: we may not get input for second scanf() statement
c. No error
d. None of above

Answers : 

b. Error: we may not get input for second scanf() statement

 

Question 2 :

Consider the following iterative implementation to find the factorial of a number. What statement should be added to complete the code?

int main()
{
int n = 6, i; int fact = 1;
for(i=1;i<=n;i++);
printf(“%d”,fact);
return 0;
}

a. fact = fact + i
b. fact = fact * i
c. i = i * fact
d. i = i + fact

Answers : 

b. fact = fact * i

 

Question 3 :

What is the output of this C code?

#include <stdio.h>
int main()
{
int i=12; int *p =&i;
printf(“%d\n”,*p++);
}

a. Address of i++
b. 12
c. Garbage value
d. Address of i

 

Answers : 

b. 12

 

Question 4 :

Convert the following 211 decimal number to 8-bit binary

a. 11011011
b. 11001011
c. 11010011
d. 11010011

Answers : 

d. 11010011

 

Question 5 :

Comment on the output of this code.

#include <stdio.h>
int main()
{
int a = 1;
switch (a)
case 1:
printf(“%d”, a);
case 2:
printf(“%d”, a);

case 3:
printf(“%d”, a);
default:
printf(“%d”, a);
}

a. No error, output is 1111
b. No error, output is 1
c. Compile time error, no break statements
d. Compile time error, case label outside switch statement

Answers : 

d. Compile time error, case label outside switch statement

 

Question 6 :

What is the output of the following code?

void my_recursive_function(int n)
{

if(n == 0) return; printf(“%d “,n);
my_recursive_function(n-1);
}

int main()
{
my_recursive_function(10); return 0;
}

a. 10
b. 1
c. 10 9 8 … 1 0
d. 10 9 8 … 1

Answers : 

d. 10 9 8 … 1

 

Question 7 :

Choose the output

#include <stdio.h>
int main()
{
int i = 0;
int x = i++, y = ++i;
printf(“%d % d\n”, x, y);
return 0;
}

a. 0,2

b. 1,1

c. 1, 2

d. Undefined

Answers : 

a. 0,2

 

Question 8 :

Choose the output

#include <stdio.h>
int main()
{
int i = 10; int *p = &i;
printf(“%d\n”, *p++);
}

a. 11
b. 10
c. Garbage value
d. Address of i

Answers : 

b. 10

 

Question 9 :

Choose the output

#include <stdio.h>
void main()
{
int x = 97;
int y = sizeof(x++);
printf(“X is %d”, x);
}

a. X is 99
b. X is 98
c. X is 97
d. Run time error

Answers : 

c. X is 97

 

Question 10 :

Choose the output

#include <stdio.h>
void main()
{
int x = 4, y, z;
y = –x;
z = x–;
printf(“%d%d%d”, x, y, z);
}


a. 3 2 3
b. 2 3 3
c. 3 2 2
d. 3 2 1

Answers : 

b. 2 3 3

 

Question 11 :

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

Answers : 

c) 87

 

Question 12 :

What will be the output of the following pseudo code?

Integer n
for (n = 3; n != 0; n-)
Print n n = n-1
end for

a) 3 1
b) 3 2 1
c) 3
d) Infinite Loop

Answers : 

d) Infinite Loop

 

Question 13 :

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

Answers : 

d) 1

 

Question 14 :

Where is the error in the given pseudo code to sort numbers 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

a) Line 4
b) Line 6
c) Line 7
d) No Error

Answers : 

c) Line 7

 

Question 15 :

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

Answers : 

b) 49056

 

Question 16 :

What will be the output?
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) 78
c) 72
d) 68

Answers :

c) 72 

 

Question 17 :

In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is….?
a) log 2 n
b) n⁄2
c) log 2 n – 1
d) n

Answers : 

d) n

 

Question 18 :

Which of the following will give the best performance?
a) O(n)
b) O(n!)
c) O(n log n)
d) O(n^C)

 

Answers : 

a) O(n)

 

Question 19 :

What is the time complexity of searching for an element in a circular linked list?
a) O(n)
b) O(nlogn)
c) O(1)
d) None of the mentioned

Answers : 

a) O(n)

 

Question 20 :

How many times the following loop is executed?
{

ch = ‘b’;
while(ch >= ‘a’ && ch <= ‘z’)
ch++;
}

a) 0
b) 25
c) 26
d) 1

Answers : 

b) 25

 

Question 21 :

What will be the output 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

Answers : 

c) 72

 

Question 22 :

What is the space complexity of the following code considering that the size of int is 2 bytes ?
int sum(int A[], int n)
{
int sum = 0, i; for(i = 0; i < n; i++)
sum = sum + A[i]; return sum;
}

a) 2n + 8
b) 2n + 4
c) 2n + 2
d) 2n + 6

Answers : 

d) 2n + 6

 

Question 23 :

What does the following code do ? public void func(Tree root)
{
func(root.left());
func(root.right());
System.out.println(root.data());
}

a) Preorder traversal
b) Inorder traversal
c) Postorder traversal
d) Level order traversal

Answers : 

c) Postorder traversal

 

Question 24 :

Let P be a Quick Sort Program to sort numbers in ascending order using the first element as a pivot. Let t1 and t2 be the number of comparisons made by P for the inputs {1, 2, 3, 4, 5} and {4, 1, 5, 3, 2} respectively. Which one of the following holds?

a) t1 = 5
b) t1 < t2
c) t1 > t2
d) t1 = t2

Answers : 

c) t1 > t2

 

Question 25 :

Let G be a graph with n vertices and m edges. What is the tightest upper bound on the running time on Depth First Search of G? Assume that the graph is represented using adjacency matrix.

a) O(n)
b) O(m+n)
c) O(n2)
d) O(mn)

Answers : 

c) O(n2)

 

Question 26 :

You have an array of n elements. Suppose you implement a quick sort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the worst case performance is:

a) O(n2)
b) O(nLogn)
c) Θ(nLogn)
d) O(n3)

Answers : 

a) O(n2)

 

Question 27 :

Consider a hash table with 9 slots. The hash function is h(k) = k mod 9. The collisions are resolved by chaining. The following 9 keys are inserted in the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The maximum, minimum, and average chain lengths in the hash table, respectively, are:

a) 3, 0, and 1
b) 3, 3, and 3
c) 4, 0, and 1
d) 3, 0, and 2

Answers : 

a) 3, 0, and 1

 

Question 28 :

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) 9
b) 15
c) 21
d) 6

Answers : 

c) 21

 

Question 29 :

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

Answers : 

d) 15

 

Question 30 :

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

Answers : 

c.87

 

Leave a Reply