Cognizant Pseudo Code Questions
Cognizant Pseudo Code Questions
A.44
B.0
C.1
D.12
Integer x, y
x=x+2
end for
Print x
A.11
B.10
C.12
D.13
Q3) What will be the value of the following pseudocode?
Integer j, m
Set m = 1, j = 1
if(a[0])
a[j] = 5
End if
m = m + a[j]
Print m
A.3
B.2
C.6
D.4
Q4) What will be the value of the following pseudocode for k=150?
fun(integer k)
if(k>155)
return
end if
print k
fun(k+2)
print k
C. 150
for(each k from 0 to 4)
end for
for(each i from 0 to 4)
Print c[i]
end for
A. 7 13 13 11 11
B. 3 5 1 -5 -9
C. -3 -5 -1 5 9
D. None
Q6) How many times “A” will be printed in the following pseudocode?
Integer a, b, c
for (a = 0 to 4)
for(b = 0 to 2)
Print “A”
End if
End for
End for
A.8
B.7
C.9
D.10
Integer p, q, r
Set q = 13
for(each p from 1 to 4)
r = q mod p
p=p+5
q=p+r
end for
r=q/5
Print q, r
A.6 4
B.1 3
C.7 2
D.6 1
Answer - Option D
Explanation:
Then, r = q % p
= 13 % 1
=0
p will be p + 5
p=1+5
=6
Then q is overwritten as
q=p+r
=6+0
=6
Now, r = q / 5
=6/5
=1
So q = 6 and r = 1
Answer is option D
Integer x
Set x = 259
if(x EQUALS 0)
Print “0”
Print “9”
otherwise
Print x MOD 9
end if
A. 8
B. 16
C. 7
D. None
Integer a, b
Set a = 12, b = 25
a = (a + b) MOD 2
b=b=a
a = a + b - 13
Print a, b
A. -11 1
B. -12 00
C. 11 22
D. 37 24
Integer a, b, c
Set a = 4, b = 4, c = 4
if (a & (b ^ b) & c)
a = a >> 1
End if
Print a + b + c
A.16
B.24
C.8
D.12
Q11) What will be the output of the following pseudocode for a = 10, b = 11?
if(0)
End if
a=a+a+a+a
return a
A.40
B.30
C.44
D.0
a = a+b+b-2
return 3-a
Else
return a-b+1
End if
return a+b
A.0
B.5
C.16
D.11
Q13) What will be the output of the following pseudocode for a = 5, b = 1?
a=a^b
Else
return a-b
End if
return a+b
A.-9
B.5
C.6
D.21
if(a < b)
b=b^a
a=a^b
End if
Endif
return a + b
End function funn()
A. 35
B. 20
C. 14
D. 25
Integer x
Set x = 2
if(x is EQUAL TO 1)
if(x IS EQUAL TO 0)
Print “A”
else
Print “B”
end if
else
Print “C”
end if
A.B C
B.C
C.A
D.B
15625
625
3125
525
initialize char c;
set c= a;
printf ("%d",a);
64
97
error
18. What will be the output of the following code ?
#include<stdio.h>
int main ()
{
char c,a,b;
c='f';
a='s';
b='x';
int sum= c+a+b;
printf ("%d", sum);
}
324
315
320
337
19. What will be the output of the following pseudo code for arr[]= 1,2,3,4,5
initialize i,n
intialize an array of size n
accept the values for the array
for i= 0 to n
arr[i] = arr[i]+arr[i+1]
end for
print the array elements
35795
3 5 7 9 11
3 5 9 15 20
error
#include<stdio.h>
int fun(int x, int y);
int main()
{
int i,n;
i=5;
n=7;
int f = fun(5,7);
printf("%d", f);
}
0(zero)
#include <stdio.h>
#include <stdlib.h>
#define LIMIT 10 /*size of integers array*/
int main(){
unsigned long long int i,j;
int *primes;
int z = 1;
primes = malloc(sizeof(int)*LIMIT);
for (i=2;i<LIMIT;i++)
primes[i]=1;
for (i=2;i<LIMIT;i++)
if (primes[i])
for (j=i;i*j<LIMIT;j++)
primes[i*j]=0;
for (i=2;i<LIMIT;i++)
if (primes[i])
printf("%dth prime = %dn\n",z++,i);
return 0;
1th prime - 2n
2th prime - 3n
3th prime - 5n
1th prime - 2n
2th prime - 3n
3th prime - 5n
4th prime - 7n
5th prime - 9n
6th prime - 11n
1th prime - 2n
2th prime - 3n
3th prime - 5n
4th prime - 7n
#include <stdio.h>
#include <stdlib.h>
int main(){
int m = 2, c=1;
int n,a,b, limit=10;
while(c < limit)
{
for (int n = 1; n < m; ++n)
{
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break;
printf("%d %d %d\n", a, b, c);
}
m++;
}
}
157
257
7 9 10
123
456
789
345
8 6 10
#include<stdio.h>
int main( )
{
int n=5, k, f1, f2, f;
if ( n < 2 )
return n;
else
{
f1 = f2 = 1;
for(k=2;k<n;k++)
{
f = f1 + f2;
f2 = f1;
f1 = f;
}
printf("%d",f) ;
}
}
13
int main()
{
int array[] = {5, 3, 1, 9, 8, 2, 4, 7};
int size = sizeof(array)/sizeof(array[0]);
int i, j, min_idx,temp;
for (i = 0; i < size-1; i++)
{
min_idx = i;
for (j = i+1; j < size; j++)
{
if (array[j] < array[min_idx])
min_idx = j;
}
temp = array[min_idx];
array[min_idx] = array[i];
array[i] = temp; }
}
It accepts a string
#include<stdio.h>
int func(int a)
{
return a--;
}
int main()
{
int a= func(5);
printf("%d",a++);
return 0;
}
#include<stdio.h>
int main()
{
float m=3.0;
switch((int)m)
{
case 1:
printf("Prepinsta");
break;
case 2:
printf("Prime");
break;
case 3:
printf("Prepinsta Prime");
break;
}
return 0;
}
Prepinsta
Prime
Prepinsta Prime
error
#include<stdio.h>
int main()
{
int val=5;
do{
val++;
++val;
}while(val++>7);
printf("%d",val);
return 0;
}
11
10
#include<stdio.h>
int main()
{
int m=0;
if(m==0)
{
m=((5,(m=3)),m=1);
printf("%d",m);
}
else
printf("Test");
return 0;
}
Test
#include<stdio.h>
int main () {
char *str1 = "Prepinsta", *str2 = "Prime";
fun1(str1, str2); printf("%s %s ", str1, str2);
fun2(&str1, &str2); printf("%s %s ", str1, str2);
return 0;
}
#include<stdio.h>
void main()
{
int i=0;
while(+(+i--)!=0)
i=i+5;
printf("%d",i);
}
-1
-2
#include<stdio.h>
int main()
{
char c= 'Z';
printf(“%d”,c);
}
'Z'
90
122
33. What will be the output of the following pseudo code ?
#include<stdio.h>
int func(int n)
{
int i=0;
while(n%10!=0)
{
n=n+3;
i++;
}
n=n-i;
return n;
}
void main()
{
printf("%d",func(35));
}
50
55
53
45
#include<stdio.h>
int func(int no)
{
static int count=0;
count=count+no;
return count;
}
void main()
{
int i,j;
for(i=0;i<=5;i++)
j=func(i);
printf("%d",j);
}
18
15
20
25
#include<stdio.h>
void main()
{
int a=5,b=2,c=1;
if(b>a && a>c && c>b)
b=a+1;
else
a=b+1;
printf("%d",a+b+c);
}
11
19
7
Question 36
What will be the output of the following pseudocode for arr[] = 1,2,3,4,5
Initialize i, n
Set arr[]= 1, 2, 3, 4, 5
for i = 0 to n - 2
arr[i] = arr[i] + arr[i+1]
End for
print the array of elements
33495
43752
35795
12345
Question 37
Predict the output of the following pseudocode.
Integer x, y, z
Set x = 0, y = 3, z = 11
if(x + (1 | 2) && y + (2 | 3))
x=x-2
y=x
Else
x=z
y=y^2
End if
Print x + y + z
15
10
13
7
Question 38
What operation does the following pseudocode perform?
Declare an array "word" of string data type
Declare a variable l
Take a string as input in the word
for l = (length of word) – 1 to 0
print word[l]
End for
Algorithm End
None of the above
Question 39
Predict the output of the following pseudocode for x = 10, y = 5.
Integer solve(Integer x, Integer y)
if(y > 0)
if(x > 0)
return x + y + solve(2, y - 5) + solve(x - 10, 1)
End if
End if
return x + y
End function solve()
15
18
13
17
Question 40
What will be the output of the following pseudocode for arr[]= 5, 4, 3, 2, 1?
Initialize i, n
Initialize an array of size n
Take the values for the array
for i = 0 to n - 2
arr[i] = arr[i] - arr[i+1]
End for
print the array of elements
3 3 4 9 5
1 1 1 1 1
3 5 7 9 5
2 2 2 2 2
Question 41
Predict the output of the following pseudocode for n = 5, m = 6.
Integer solve(Integer n, Integer m )
if(n > 4 && m < 7)
return solve(n +1, m + 1)
Else
Return n + m
End if
End function solve()
15
21
13
10
Question 42
What operation does the following pseudocode perform?
Declare an array "word" of string data type
Declare a variable l
Take a string as input in the word
for l = 0 to (length of word) – 1
print word[l]
End for
Algorithm End
None of the above
It prints the original string
It deletes the string
It reverses the string
Question 43
Integer x, y, z
Set x = 10, y = 16,z = 3
if(x > y)
x=2*y
Else
y=x/2
End if
if(z > y)
z=2*y
Else
y=z/2
End if
Print x + y + z
38
20
33
14
Question 44
What will be the output of the following pseudocode for arr[] = 5, 4, 3, 2, 1
Initialize i, n
Initialize an array of size n
Take the values for the array
for i = 0 to n - 2
arr[i] = arr[i] * arr[i+1]
End for
print the array of elements
10 12 8 2 1
20 12 6 2 1
5 10 6 2 0
20 4 6 1 1
Question 45
Calculate the output of the following pseudocode.
Integer w, x, y, z
Set w = 21, x = 11
for (each y from 21 to 30 )
for (each z from -1 to 0 )
w=w-1
if(w > y)
Continue
End if
w=1
if(w > z)
Jump out of the loop
End if
End for
End for
Print w + x
8
12
3
2
Question 46
What operation does the following pseudocode perform?
Declare an array "word" of string data type
Declare a variable l
Take a string as input in the word
for l = 0 to 0
print word[l]
End for
Algorithm End
It prints the last character of the string
It prints characters in increasing order
It prints the first character of the string
It reverses the string
Question 47
Predict the output of the following pseudocode.
Integer x, y, z
Set x = -2, y = 3, z = 1
if(x + (2 & 2) && y + (3 & 3) && z + (2 ^ 2))
x=x-2
y=x
Else
x=z
y=y^2
End if
Print x+ y+ z
2
11
3
0
Question 48
Print the output of the following pseudocode for x = 9, y = 7.
Integer funn(Integer x, Integer y)
Integer z
Set z = 2
y = y mod z
x = x mod z
return x + y
End function funn()
2
3
17
5
Question 49
Integer x, y, z
Set x = 8, y = 6, z = 4
if(x > y)
x=y
Else
y=x
End if
if(z > y)
z=y
Else
y=z
End if
Print x + y + z
13
17
14
23
Question 50
Calculate the output of the following pseudocode.
Integer a, b, c, d
Set a = 1, b = 1
for (each c from 1 to 2 )
for (each d from -2 to 0 )
a=a+2
if(a > c)
Continue
End if
a=1
if(a > d)
Jump out of the loop
End if
End for
End for
Print a + b
8
22
30
14
Question 51
Calculate the output of the following pseudo code for input p = 3, q = 8, r = 1.
Integer p, q, r, sum
Read p, q, r
Set sum = p + q + r
if ((p NOT EQUALS 0) and (sum EQUALS 11) and (q EQUALS 4) and (r NOT
EQUALS 0))
Print " Success"
Otherwise
Print "Fail"
End if
Success
Fail
Error
None of the above
Question 52
Calculate the output of the following pseudo code.
Integer w, x, y, z
Set w = 1, x = 1
for (each y from 11 to 20 )
for (each z from -3 to 0 )
w=w+5
if(w > y)
Continue
End if
w=1
if(w > z)
Jump out of the loop
End if
End for
End for
Print w + x
8
22
3
2
Question 53
Integer x, y, z
Set x = 10, y = 16, z = 3
if(x > y)
x=y
Else
y=x
End if
if(z > y)
z=y
Else
y=z
End if
Print x + y + z
13
12
16
20
Question 54
Predict the output of the following pseudo code for a = 11, b = 12.
Integer solve(Integer a, Integer b )
if(a < 3 && b < 4)
return solve(a + 1, b + 1)
Else
Return a + b
End if
End function solve()
13
12
22
23
Question 55
Predict the output of the following pseudo code for x = 4, y = 9.
Integer solve(Integer x, Integer y)
if(y > 0)
if(x > 0)
return x + y + solve(0, y + 1) + solve(0, y + 2) + solve(x + 3, 0)
End if
End if
return x + y
End function solve()
20
15
41
30