Programming
Question 1 
#include <stdio.h>
int counter = 0;
int calc (int a, int b) {
int c;
counter++;
if (b==3) return (a*a*a) ;
else {
c = calc (a, b/3) ;
return (c*c*c) ;
}
}
int main () {
calc (4, 81);
printf ("%d", counter) ;
}
The output of this program is ______.
4  
5  
6  
7 
Question 2 
Consider the following C program.
#include <stdio.h> struct Outnode { char x, y, z ; }; int main () { struct Outnode p = {'1', '0', 'a'+2} ; struct Outnode *q = &p ; printf ("%c, %c", *((char*)q+1), *((char*)q+2)) ; return 0 ; }
The output of this program is
0, c  
0, a+2  
‘0’, ‘a+2’  
‘0’, ‘c’ 
The x variable here stores a character ‘c’ in it.
Because +2 will increment ascii value of a from 92 to 95.
Hence the structure p contains 3 character values and they are ‘1’, ‘0’, and ‘c’.
q is a pointer pointing to structure p.
Hence q is pointing to ‘1’, q+1 pointing to ‘0’ and q+2 pointing to ‘c’.
Option d cannot be correct, as though they are characters, printf will not print them in single quotes.
Question 3 
Consider the following C program:
#include<stdio.h> void fun1(char *s1, char *s2) { char *tmp; tmp = s1; s1 = s2; s2 = tmp; } void fun2(char **s2, char **s2) { char *tmp; tmp = *s1; *s1 = *s2; *s2 = tmp; } int main () { char *str1 = "Hi", *str2 = "Bye"; fun1(str1, str2); printf("%s %s", str1, str2); fun2(&str1, &str2); printf("%s %s", str1, str2); return 0; }
The output of the program above is
Hi Bye Bye Hi  
Hi Bye Hi Bye  
Bye Hi Hi Bye  
Bye Hi Bye Hi 
Hence, any change in the formal parameters are NOT reflected in actual parameters.
Hence, str1 points at “hi” and str2 points at “bye”.
The second call to the function ‘func2(&str1, &str2);’ is call by reference.
Hence, any change in formal parameters are reflected in actual parameters.
Hence, str1 now points at “bye” and str2 points at “hi”.
Hence answer is “hi bye bye hi”.
Question 4 
Consider the following C code. Assume that unsigned long int type length is 64 bits.
unsigned long int fun(unsigned long int n) { unsigned long int i, j, j=0, sum = 0; for (i = n; i > 1; i = i/2) j++; for ( ; j > 1; j = j/2) sum++; return sum; }
The value returned when we call fun with the input 2^{40} is
4  
5  
6  
40 
Next for loop will divide j value (which is 40) by 2, each time until j>1.
j loop starts:
j=40 & sum=1
j=20 & sum=2
j=10 & sum=3
j=5 & sum=4
j=2 & sum=5
j=1 & break
So, sum = 5.
Question 5 
Consider the following program written in pseudocode. Assume that x and y are integers.
Count (x, y) { if (y != 1) { if (x != 1) { print("*") ; Count (x/2, y) ; } else { y = y  1 ; Count (1024, y) ; } } }
The number of times that the print statement is executed by the call Count(1024, 1024) is ______.
10230  
10231  
10232  
10233 
int count=0;
Count(x,y){
if(y!=1){
if(x!=1){
printf("*");
count = count +1;
Count(x/2,y);
}
else{
y=y1;
Count(1024,y);
}
}
}
void main()
{
Count(1024,1024);
printf("\n%d\n",count);
}
Count ( ) is called recursively for every (y = 1023) & for every y, Count ( ) is called (x = 10) times = 1023 × 10 = 10230
Question 6 
Consider the following C code:
#include <stdio.h> int *assignval(int *x, int val) { *x = val; return x; } void main ( ) { int *x = malloc(sizeof(int)); if(NULL == x) return; x = assignval(x, 0); if(x) { x = (int *)malloc(size of(int)); if(NULL == x) return; x = assignval(x, 10); } printf("%dn", *x); free(x); }
The code suffers from which one of the following problems:
compiler error as the return of malloc is not typecast approximately  
compiler error because the comparison should be made as x==NULL and not as shown  
compiles successfully but execution may result in dangling pointer  
compiles successfully but execution may result in memory leak 
In C++, we need to perform type casting, but in C Implicit type casting is done automatically, so there is no compile time error, it prints10 as output.
Option B:
NULL means address 0, if (a == 0) or (0 == a) no problem, though we can neglect this, as it prints 10.
Option C:
x points to a valid memory location. Dangling Pointer means if it points to a memory location which is freed/ deleted.
int*ptr = (int*)malloc(sizeof(int));
free(ptr); //ptr becomes a dangling pointer
ptr = NULL; //Removing Dangling pointers condition
Option D:
x is assigned to some memory location
int*x = malloc(sizeof(int));
→ (int*)malloc(sizeof(int)) again assigns some other location to x, previous memory location is lost because no new reference to that location, resulting in Memory Leak.
Hence, Option D.
Question 7 
Consider the following two functions.
void fun1(int n) { void fun2(int n) { if(n == 0) return; if(n == 0) return; printf("%d", n); printf("%d", n); fun2(n  2); fun1(++n); printf("%d", n); printf("%d", n); } }
The output printed when fun1(5) is called is
53423122233445  
53423120112233  
53423122132435  
53423120213243 
In fun2, we increment (pre) the value of n, but in fun1, we are not modifying the value.
Hence increment in value in recursion (back).
Hence, 5 3 4 2 3 1 2 2 2 3 3 4 4 5.
Question 8 
Consider the C functions foo and bar given below:
int foo(int val) { int x = 0; while (val>0) { x = x + foo(val); } return val; } int bar(int val) { int x = 0; while (val>0) { x = x + bar(val1); } return val; }
Invocations of foo(3) and bar(3) will result in:
Return of 6 and 6 respectively.  
Infinite loop and abnormal termination respectively.  
Abnormal termination and infinite loop respectively.  
Both terminating abnormally. 
{
x = x + foo(val);
}
In this case foo(val) is same as foo(val) & val ;
Because the recursive function call is made without changing the passing argument and there is no Base condition which can stop it.
It goes on calling with the same value ‘val’ & the system will run out of memory and hits the segmentation fault or will be terminated abnormally.
The loop will not make any difference here.
while(val>0)
{
x = x + bar(val1);
}
bar(3) calls bar(2)
bar(2) calls bar(1)
bar(1) calls bar(0) ⇾ Here bar(0) will return 0.
bar(1) calls bar(0)
bar(1) calls bar(0)……..
This will continue.
Here is a problem of infinite loop but not abrupt termination.
Some compilers will forcefully preempt the execution.
Question 9 
Consider the following C program.
#include <stdio.h> #include <string.h> void printlength (char*s, char*t) { unsigned int c = 0; int len = ((strlen(s)  strlen(t)) > c) ? strlen(s):strlen(t); printf("%d\n", len); } void main () { char*x = "abc"; char*y = "defgh"; printlength(x,y); }
Recall that strlen is defined in string.h as returning a value of type size_t, which is an unsigned int. The output of the program is _________.
3  
4  
5  
6 
{
char*x = "abc";
char*y = "defgh";
printlength(x,y);
}
printlength(char*3, char*t)
{
unsigned int c = 0;
int len = ((strlen(s)  strlen(t))> c) ? strlen(s) : strlen(t);
printf("%d", len);
}
Here strlen(s)  strlen(t) = 3  5 = 2
But in C programming, when we do operations with two unsigned integers, result is also unsigned. (strlen returns size_t which is unsigned in most of the systems).
So this result '2' is treated as unsigned and its value is INT_MAX2.
Now the comparison is in between large number & another unsigned number c, which is 0.
So the comparison returns TRUE here.
Hence (strlen(s)  strlen(t))>0 will return TRUE, on executing, the conditional operator will return strlen(s)
⇒ strlen(abc) = 3
Question 10 
The output of executing the following C program is __________.
#include<stdio.h> int total (int v) { static int count=0; while(v) { count += v&1; v ≫= 1; } return count; } void main() { static int x = 0; int i = 5; for(; 1> 0; i) { x = x + total(i); } printf("%d\n", x); }
23  
24  
25  
26 
Question 11 
Consider the following function implemented in C:
void printxy (int x, int y) { int *ptr; x = 0; ptr = &x; y = *ptr; *ptr = 1; printf("%d,%d",x,y); }
The output of invoking printxy(1, 1) is
0, 0  
0, 1  
1, 0  
1, 1 
{
int *ptr;
x = 0;
ptr = &x;
y = *ptr;
*ptr = 1;
}
printxy (1, 1)
Question 12 
Consider the C program fragment below which is meant to divide x and y using repeated subtractions. The variables x, y, q and r are all unsigned int.
while (r >= y) { r = r  y; q = q + 1; }
Which of the following conditions on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x == (y*q + r)?
(q == r) && (r == 0)  
(x > 0) && (r == x) && (y > 0)  
(q == 0) && (r == x) && (y > 0)  
(q == 0) && (y > 0) 
x, y, q, r are unsigned integers.
while (r >= y)
{
r = r – y;
q = q + 1;
}
Loop terminates in a state satisfying the condition
x == (y * q + r)
y ⇒ Dividend = Divisor * Quotient + Remainder
So, to divide a number with repeated subtractions, the Quotient should be initialized to 0 and it must be incremented for every subtraction.
So initially q=0 which represents
x = 0 + r ⇒ x = r
and y must be a positive value (>0).
Question 13 
Consider the following snippet of a C program. Assume that swap(&x, &y) exchanges the contents of x and y.
int main () { int array[] = {3, 5, 1, 4, 6, 2}; int done = 0; int i; while (done == 0) { done = 1; for (i=0; i<=4; i++) { if (array[i] < array[i+1]) { swap (&array[i], &array[i+1]); done = 0; } } for (i=5; i>=1; i) { if (array[i] > array[i1]) { swap(&array[i], &array[i1]); done=0; } } } printf("%d", array[3]); }
The output of the program is ___________.
3  
4  
5  
6 
(1) ⇒ 1st for i = 0 <= 4
a[0] < a[1] ≃ 3<5 so perform swapping
done =
(1) ⇒ no swap (3, 1)
(2) ⇾ perform swap (1, 4)
(1) ⇒ perform swap (1, 6)
(1) ⇒ perform swap (1, 2)
(1) ⇒ (done is still 0)
for i = 5 >= 1 a[5] > a[4] ≃ 1>2 – false. So, no swapping to be done
(2) ⇾ no swap (6, 2)
(3) ⇾ swap (4, 6)
(1) ⇒ Swap (3, 6)
(1) ⇒ Swap (5, 6)
⇒ Done is still 0. So while loop executes again.
(1) ⇾ no swap (6, 5)
done = 1
(2) ⇾ No swap (5, 3)
(3) ⇾Swap (3, 4)
So, array [3] = 3
Question 14 
Consider the following C program:
#include <stdio.h> int main() { int m = 10; int n, n1; n = ++m; n1 = m++; n; n1; n = n1; printf("%d",n); return 0; }
The output of the program is _______.
0  
1  
2  
3 
Question 15 
Consider the following C program.
#include<stdio.h> #include<string.h> int main () { char* c = "GATECSIT2017"; char* p = c; printf("%d", (int) strlen (c + 2[p]  6[p]  1)); return 0; }
The output of the program is __________.
1  
2  
4  
6 
char * P = C;
(int) strlen (C + 2[P] – 6[P] – 1)
C + 2[P]  6[P] – 1
∵2[P] ≃ P[2]
100 + P[2] – P[6] – 1
100 + T – I – 1
100 + 84 – 73 – 1
ASCII values: T – 84, I – 73
100 + 11 – 1
= 110
(int) strlen (110)
strlen (17) ≃ 2
Question 16 
Consider the following C program.
void f(int, short); void main () { int i = 100; short s = 12; short *p = &s; __________ ; // call to f() }
Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error?
f(s, *s)  
i = f(i, s)  
f(i, *s)  
f(i, *p) 
short s = 12;
short *p = &s;
_______ // call to f ( ) :: (void f(int,short);)
It is clearly mentioned the return type of f is void.
By doing option elimination
(A) & (C) can be eliminated as s is short variable and not a pointer variable.
(B) i = f(i, s) is false because f’s return type is void, but here shown as int.
(D) f(i, *p)
i = 100
*p = 12
Hence TRUE
Question 17 
Consider the following C program.
#include<stdio.h> void mystery(int *ptra, int *ptrb) { int *temp; temp = ptrb; ptrb = ptra; ptra = temp; } int main() { int a=2016, b=0, c=4, d=42; mystery(&a, &b); if (a < c) mystery(&c, &a); mystery(&a, &d); printf("%d\n", a); }
The output of the program is ________.
2016  
2017  
2018  
2019 
For the first mystery (&a, &b);
temp = ptr b
ptr b = ptr a
ptr a = temp
If (a
Hence, a = 2016 will be printed.
Question 18 
The following function computes the maximum value contained in an integer array p[] of size n (n >= 1).
int max(int *p, int n) { int a=0, b=n1; while (__________) { if (p[a] <= p[b]) {a = a+1;} else {b = b1;} } return p[a]; }
The missing loop condition is
a != n  
b != 0  
b > (a + 1)  
b != a 
{
int arr [ ] = {3, 2, 1, 5, 4};
int n = sizeof(arr) / sizeof (arr[0]);
printf (max(arr, 5));
}
int max (int *p, int n)
{
int a = 0, b = n – 1;
(while (a!=b))
{
if (p[a] <= p[b])
{
a = a + 1;
}
else
{
b =b – 1;
}
}
return p[a];
}
The function computes the maximum value contained in an integer array p[ ] of size n (n >= 1).
If a = = b, means both are at same location & comparison ends.
Question 19 
What will be the output of the following C program?
void count(int n) { static int d=1; printf("%d ", n); printf("%d ", d); d++; if(n>1) count(n1); printf("%d ", d); } void main() { count(3); }
3 1 2 2 1 3 4 4 4  
3 1 2 1 1 1 2 2 2  
3 1 2 2 1 3 4  
3 1 2 1 1 1 2 
Count (3)
static int d = 1
It prints 3, 1
d++; //d = 2
n>1, count(2)
prints 2, 2
d++; // d = 3
n>1, count(1)
prints 1, 3 → Here n = 1, so condition failed & printf (last statement) executes thrice & prints d
d++; //d=4 value as 4. For three function calls, static value retains.
∴ 312213444
Question 20 
What will be the output of the following pseudocode when parameters are passed by reference and dynamic scoping is assumed?
a=3; void n(x) {x = x * a; print(x);} void m(y) {a = 1; a = y  a; n(a); print(a);} void main() {m(a);}
6, 2  
6, 6  
4, 2  
4, 4 
First m(a) is implemented, as there are no local variables in main ( ), it takes global a = 3;
m(3) is passed to m(y).
a = 1
a = 3 – 1 = 2
n(2) is passed to n(x).
Since it is dynamic scoping
x = 2 * 2 = 4 (a takes the value of its calling function not the global one).
The local x is now replaced in m(y) also.
Hence, it prints 4,4.
And we know it prints 6, 2 if static scoping is used.
It is by default in C programming.
Question 21 
The value printed by the following program is __________.
void f(int* p, int m) { m = m + 5; *p = *p + m; return; } void main() { int i=5, j=10; f(&i, j); printf("%d", i+j); }
30  
31  
32  
33 
P is a pointer stores the address of i, & m is the formal parameter of j.
Now, m = m + 5;
*p = *p + m;
Hence, i + j will be 20 + 10 = 30.
Question 22 
The following function computes X^{Y} for positive integers X and Y.
int exp (int X, int Y) { int res = 1, a = X, b = Y; while ( b != 0 ){ if ( b%2 == 0) { a = a*a; b = b/2; } else { res = res*a; b = b1; } } return res; }
Which one of the following conditions is TRUE before every iteration of the loop?
X^{Y} = a^{b}  
(res * a)^{Y} = (res * X)^{b}  
X^{Y} = res * a^{b}  
X^{Y} = (res * a)^{b} 
{
int res = 1, a = X, b = Y;
while (b != 0)
{
if (b%2 == 0)
{
a = a*a;
b = b/2;
}
else
{
res = res*a;
b = b – 1;
}
}
return res;
}
From that explanation part you can understand the exponent operation, but to check the conditions, first while iteration is enough.
x = 2, y = 3, res = 2, a = 2, b = 2.
Only (C) satisfies these values.
x^{y} = res * a^{b}
2^{3} = 2 * 2^{2} = 8
Explanation:
Will compute for smaller values.
Let X = 2, Y = 3, res = 1
while (3 != 0)
{
if(3%2 == 0)  False
else
{
res = 1*2 = 2;
b = 3 – 1 = 2;
}
For options elimination, consider
return res = 2 (but it is out of while loop so repeat while)
__________
while (2 != 0)
{
if (2%2 == 0)  True
{
a = 2*2 = 4
b = 2/2 = 1
}
__________
repeat while
while (1 != 0)
{
if (1%2 == 0)  False
else
{
res = 2 * 4 = 8
b = 1 – 1 = 0
}
__________
while (0 != 0)  False
return res = 8 (2^{3})
Question 23 
Consider the following program:
int f(int *p, int n) { if (n <= 1) return 0; else return max (f(p+1,n1),p[0]p[1]); } int main() { int a[] = {3,5,2,6,4}; printf("%d", f(a,5)); }Note: max(x,y) returns the maximum of x and y.
The value printed by this program is __________.
3  
4  
5  
6 
f(a, 5) ⇒ f(100, 5)
Question 24 
The output of the following C program is __________.
void f1 (int a, int b) { int c; c=a; a=b; b=c; } void f2 (int *a, int *b) { int c; c=*a; *a=*b;*b=c; } int main() { int a=4, b=5, c=6; f1(a, b); f2(&b, &c); printf (“%d”, cab); return 0; }
5  
6  
7  
8 
But f_{2} will swap the value of 'b' and 'c' because f_{2} is call by reference. So finally the value of
a=4
b=6
c=5
So, answer will be
c  a  b
5  4  6 = 5
Question 25 
What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory.
int main() { unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3); }
2036, 2036, 2036  
2012, 4, 2204  
2036, 10, 10  
2012, 4, 6 
⇒ x [4] [3] can represents that x is a 2dimensional array.
⇒ x+3 = (Address of x) + 3 * 4 * 3 [3×4×3 is inner dimention]
= 2000 + 36
= 2036
⇒ *(x+3) also returns the address i.e., 2036.
The '*' represents 1  D but x is starting at 2036.
⇒ *(x+3)+3 = *(Address of x + 2 * 3 * 4) + 3
= *(2000 + 24) +3
= *(2024) + 3 ['*' will change from 2D to 1D]
= 2024 + 3 * 4
= 2024 + 12
= 2036
Question 26 
Consider the following pseudo code, where x and y are positive integers.
begin q := 0 r := x while r >= y do begin r := r – y q := q + 1 end end
The post condition that needs to be satisfied after the program terminates is
{r = qx+y ∧ r  
{x = qy+r ∧ r  
{y = qx+r ∧ 0  
{q+1 
⇒ x = qy + r
Question 27 
Consider the following C program segment.
while (first <= last) { if (array [middle] < search) first = middle +1; else if (array [middle] == search) found = True; else last = middle – 1; middle = (first + last)/2; } if (first < last) not Present = True;
The cyclomatic complexity of the program segment is __________.
5  
6  
7  
8 
Question 28 
Consider the following C function.
int fun1 (int n) { int i, j, k, p, q = 0; for (i = 1; i<n; ++i) { p = 0; for (j = n; j > 1; j = j/2) ++p; for (k = 1; k < p; k = k*2) ++q; } return q; }
Which one of the following most closely approximates the return value of the function fun1?
n^{3}  
n(log n)^{2}  
nlog n  
nlog (log n) 
for (j=n; j>1; j=j/2)  p = O(log n) ++p;
for (k=1; k
++q;
}
∴ The return value of the function fun1,
q = n log p
= n log log n
Question 29 
Consider the following function written the C programming language.
void foo(char *a) { if(*a && *a != ' ') { foo(a+1); putchar(*a); } }
The output of the above function on input "ABCD EFGH" is
ABCD EFGH  
ABCD  
HGFE DCBA  
DCBA 
if condition fails
& returns controls
∴ DCBA will be pointed
Question 30 
Consider the following C functions
int fun(int n) { int x = 1, k; if(n == 1) return x; for(k = 1; k < n; ++k) x = x + fun(k) * fun(n  k); return x; }
The return value of fun(5) is _______.
51  
52  
53  
54 
f(n) = 1; if n = 1
Question 31 
Consider the C program below.
#includeint *A, stkTop; int stkFunc (int opcode, int val) { static int size=0, stkTop=0; switch (opcode) { case 1: size = val; break; case 0: if (stkTop < size ) A[stkTop++]=val; break; default: if (stkTop) return A[stkTop]; } return 1; } int main() { int B[20]; A=B; stkTop = 1; stkFunc (1, 10); stkFunc (0, 5); stkFunc (0, 10); printf ("%dn", stkFunc(1, 0)+ stkFunc(1, 0)); }
The value printed by the above program is ___________
2  
2  
1  
15 
When next time stkFunc (0,5) is called then, inside Switch(opcode), the control will go to Case0, where A[0] = 5 and stkTop = 0+1 = 1.
When next time stkFunc (0,10) is called then, inside Switch (opcode), the control will go to Case '0', where A[1] = 10 and stkTop = 1+1 = 2.
When next time stkFunc(1,0) is called from inside the printf statement, then inside Switch(opcode), the control will go to default and stkTop = 21 = 1 and value of A[1] will get returned, i.e., 10.
When next time stkFunc(1,0) is called from inside the printf statement, then inside Switch(opcode), the control will go to default and stkTop = 11 = 0 and value of A[0] will get returned, i.e., 5.
Finally the two values 10 & 5 will be added and printed.
Question 32 
Consider the following C program segment.
# includeint main( ) { char s1[7] = "1234", *p; p = s1 + 2; *p = '0' ; printf ("%s", s1); }
What will be printed by the program?
12  
120400  
1204  
1034 
p now points to third element in s1, i.e., '3'.
*p = '0', will make value of '3' as '0' in s1. And finally s1 will become 1204.
Question 33 
Consider the following recursive C function. If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()?
void get (int n){ if (n < 1) return; get(n1); get(n3); printf("%d", n); }
15  
25  
35  
45 
Question 34 
Consider the following C program.
# include int main( ) { static int a[] = {10, 20, 30, 40, 50}; static int *p[] = {a, a+3, a+4, a+1, a+2}; int **ptr = p; ptr++; printf("%d%d", ptr  p, **ptr}; }
The output of the program is _________.
140  
150  
160  
170 
**ptr = 40
∴ printf (“%d%d”, p + r – p, p + r) will print 140.
Question 35 
Consider the following C program:
# includeint main( ) { int i, j, k = 0; j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5; k = j; for (i = 0; i < 5; i++) { switch(i + k) { case 1: case 2: printf("n%d", i + k); case 3: printf("n%d", i + k); default: printf("n%d", i + k); } } return 0; }
The number of times printf statement is executed is __________.
10  
11  
12  
13 
= 6 / 4+2.0 / 5+1;
= 1 + 0.4 + 1
= 2.4
But since j is integer,
j=2
Now,
k = k  (j)
k = 0  (1) = 1
When i=0, i+k = 1,
printf executed 1 time
When i=1, i+k = 0,
printf executed 1 time
When i=2, i+k = 1,
printf executed 3 times
When i=3, i+k = 2,
printf executed 3 times
When i=4, i+k = 3,
printf executed 2 times
∴ Total no. of times printf executed is,
1 + 1 + 3 + 3 + 2 = 10
Question 36 
Consider the following C program. The output of the program is __________.
# includeint f1(void); int f2(void); int f3(void); int x = 10; int main() { int x = 1; x += f1() + f2() + f3() + f2(); pirntf("%d", x); return 0; } int f1() { int x = 25; x++; return x; } int f2( ) { static int x = 50; x++; return x; } int f3( ) { x *= 10; return x; }
230  
240  
250  
260 
f1( ) = 25 + 1 = 26
f2( ) = 50 + 1 = 51
f3( ) = 10 × 10 = 100
f2( ) = 51 × 1 = 52 (Since here x is static variable so old value retains)
∴ x = 1+26+51+100+52 = 230
Question 37 
Consider the following program in C language:
#includemain() { int i; int *pi = &i; scanf("%d", pi); printf("%dn", i+5); }
Which one of the following statements is TRUE?
Compilation fails.  
Execution results in a runtime error.  
On execution, the value printed is 5 more than the address of variable i.  
On execution, the value printed is 5 more than the integer value entered. 
int *pi = &i; // pi is a pointer which stores the address of i.
scanf (pi); // pi = &i (we rewrite the garbage value with our values) say x = 2
printf (i+5); // i+5 = x+5 = 2+5 = 7
Hence on execution, the value printed is 5 more than the integer value entered.
Question 38 
Consider the function func shown below:
int func(int num) { int count = 0; while (num) { count++; num >>= 1; } return (count); }
The value returned by func(435)is __________.
9  
10  
11  
12 
Shift left of 1, which means the number gets doubled.
In program, shift right of 1 is given, means every time we enter the loop the number will get halved, and the value of count will get incremented by 1. And when the value of num will become zero then the while loop will get terminated. So,
num = 435/2 = 217/2 = 108/2 = 54/2 = 27/2= 13/2 = 6/2 = 3/2 = 1/2 = 0
Count = 9
So, the value count that will get returned is 9.
Question 39 
Suppose n and p are unsigned int variables in a C program. We wish to set p to ^{n}C_{3}. If n is large, which one of the following statements is most likely to set p correctly?
p = n * (n1) * (n2) / 6;  
p = n * (n1) / 2 * (n2) / 3;  
p = n * (n1) / 3 * (n2) / 2;  
p = n * (n1) * (n2) / 6.0; 
From the options n*(n1)*(n2) will go out of range. So eliminate A & D.
n*(n1) is always an even number. So subexpression n(n1)/2 also an even number.
n*(n1)/ 2*(n2), gives a number which is a multiple of 3. So dividing with 3 will not have any loss. Hence B is option.
Question 40 
Consider the following function
double f(double x){ if (abs(x*x  3) < 0.01) return x; else return f(x/2 + 1.5/x); }
Give a value q (to 2 decimals) such that f(q) will return q:_____.
1.73  
1.74  
1.75  
1.76 
x^{2}  3 < 0.01 will become True.
So, x^{2}  3 < 0.01
x^{2}  3 < 0.01
x^{2} < 3.01
x < 1.732
Hence, x = 1.73.
Question 41 
Consider the C function given below.
int f(int j) { static int i = 50; int k; if (i == j) { printf(“something”); k = f(i); return 0; } else return 0; }
Which one of the following is TRUE?
The function returns 0 for all values of j.  
The function prints the string something for all values of j.  
The function returns 0 when j = 50.  
The function will exhaust the runtime stack or run into an infinite loop when j = 50. 
int f(int j)
{
static int i = 50;
int k;
if (i == j) // This will be True.
{
printf ("Something");
k = f(i); // Again called f(i) with value of i as 50. So, the function will run into infinite loop.
return 0;
}
else return 0;
}
Question 42 
Let A be a square matrix of size n x n. Consider the following program. What is the expected output?
C = 100 for i = 1 to n do for j = 1 to n do { Temp = A[i][j] + C A[i][j] = A[j][i] A[j][i] = Temp  C } for i = 1 to n do for j = 1 to n do Output(A[i][j]);
The matrix A itself  
Transpose of the matrix A  
Adding 100 to the upper diagonal elements and subtracting 100 from lower diagonal elements of A  
None of the above 
For first row iteration, it get swapped and becomes
For second row iteration, it comes to the original position
=A
So, it is the same matrix A.
Question 43 
Consider the following function:
int unknown(int n) { int i, j, k = 0; for (i = n/2; i <= n; i++) for (j = 2; j <= n; j = j * 2) k = k + n/2; return k; }
Θ(n^{2})  
Θ(n^{2} log n)  
Θ(n^{3})  
Θ(n^{3} logn) 
So, the total number of times loop runs is (n/2 logn).
So, the final k value will be n/2*(n/2 logn) = O(n^{2}logn)
= (n/2+1).n/2 ∙log n
= (n^{2}log n)
Question 44 
The procedure given below is required to find and replace certain characters inside an input character string supplied in array A. The characters to be replaced are supplied in array oldc, while their respective replacement characters are supplied in array newc. Array A has a fixed length of five characters, while arrays oldc and newc contain three characters each. However, the procedure is flawed
void find_and_replace(char *A, char *oldc, char *newc) { for (int i = 0; i < 5; i++) for (int j = 0; j < 3; j++) if (A[i] == oldc[j]) A[i] = newc[j]; }
The procedure is tested with the following four test cases
(1) oldc = "abc", newc = "dab" (2) oldc = "cde", newc = "bcd" (3) oldc = "bca", newc = "cda" (4) oldc = "abc", newc = "bac"
If array A is made to hold the string “abcde”, which of the above four sest cases will be successful in exposing the flaw in this procedure?
None  
2 only  
3 and 4 only  
4 only 
1, 2 works fine, 3, 4 carries flaw.
Question 45 
Consider the following program
Program P2 var n: int: procedure W(var x: int) begin x=x+1; print x; end procedure D begin var n: int; n=3; W(n); End begin //beginP2 n=10; D; end
If the language has dynamic scoping and parameters are passed by reference, what will be printed by the program?
10  
11  
3  
None of the above 
W(n)=W(3)
Procedure W(var x; int)
begin
x = x+1 = 3+1 = 4
Print x → Print x=4
end
Question 46 
What value would the following function return for the input x=95?
Function fun (x:integer):integer; Begin If x > 100 then fun = x  10 Else fun = fun(fun(x + 11)) End;
89  
90  
91  
92 
fun(95) = fun(fun(106))
= fun(96)
= fun(fun(107))
= fun(97)
= fun(fun(108))
= fun(98)
= fun(fun(109))
= fun(99)
= fun(110)
= fun(100)
= fun(fun(111))
= fun(101)
= 91
Question 47 
What is the result of the following program?
program sideeffect (input, output); var x, result: integer; function f (var x:integer):integer; begin x:x+1;f:=x; end; begin x:=5; result:=f(x)*f(x); writeln(result); end;
5  
25  
36  
42 
If it is call by value then answer is 36.
Question 48 
What is the value of X printed by the following program?
program COMPUTE (input, output); var X:integer; procedure FIND (X:real); begin X:=sqrt(X); end; begin X:=2 Find(X) Writeln(X) end
2  
√2  
Run time error  
None of the above 
X in the procedure FIND is a local variable. No change will be reflected in global variable X.
Question 49 
An unrestricted use of the “goto” statement is harmful because
it makes it more difficult to verify programs  
it increases the running time of the programs  
it increases the memory required for the programs
 
it results in the compiler generating longer machine code 
Question 50 
Program PARAM (input, output); var m, n : integer; procedure P (var, x, y : integer); var m : integer; begin m : = 1; x : = y + 1 end; procedure Q (x:integer; vary : integer); begin x:=y+1; end; begin m:=0; P(m,m); write (m); n:=0; Q(n*1,n); write (n) end
The value of m, output by the program PARAM is:
1, because m is a local variable in P  
0, because m is the actual parameter that corresponds to the formal parameter in p
 
0, because both x and y are just reference to m, and y has the value 0  
1, because both x and y are just references to m which gets modified in procedure P  
none of the above 
Question 51 
Program PARAM (input, output); var m, n : integer; procedure P (var, x, y : integer); var m : integer; begin m : = 1; x : = y + 1 end; procedure Q (x:integer; vary : integer); begin x:=y+1; end; begin m:=0; P(m,m); write (m); n:=0; Q(n*1,n); write (n) end
The value of n, output by the program PARAM is:
0, because n is the actual parameter corresponding to x in procedure Q.  
0, because n is the actual parameter to y in procedure Q.  
1, because n is the actual parameter corresponding to x in procedure Q.  
1, because n is the actual parameter corresponding to y in procedure Q.  
none of the above 
Question 52 
Program PARAM (input, output); var m, n : integer; procedure P (var, x, y : integer); var m : integer; begin m : = 1; x : = y + 1 end; procedure Q (x:integer; vary : integer); begin x:=y+1; end; begin m:=0; P(m,m); write (m); n:=0; Q(n*1,n); write (n) end
What is the scope of m declared in the main program?
PARAM, P, Q  
PARAM, P  
PARAM, Q  
P, Q  
none of the above 
Question 53 
What does the following code do?
var a, b : integer; begin a:=a+b; b:=ab; a:=ab end;
exchanges a and b  
doubles a and stores in b  
doubles b and stores in a  
leaves a and b unchanged  
none of the above 
Let us consider a=5; b=2
a := a+b = 5+2 = 7
b := ab = 72 = 5
a := ab = 75 = 2
O/P: a=2; b=5
Question 54 
An unrestricted use of the "go to" statement is harmful because of which of the following reason(s):
It makes it more difficult to verify programs.  
It makes programs more inefficient.  
It makes it more difficult to modify existing programs.  
It results in the compiler generating longer machine code. 
Question 55 
Study the following program written in a blockstructured language:
Var x, y:interger; procedure P(n:interger); begin x:=(n+2)/(n3); end; procedure Q Var x, y:interger; begin x:=3; y:=4; P(y); Write(x) __(1) end; begin x:=7; y:=8; Q; Write(x); __(2) end.
What will be printed by the write statements marked (1) and (2) in the program if the variables are statically scoped?
3, 6  
6, 7  
3, 7  
None of the above. 
Question 56 
For the program given below what will be printed by the write statements marked (1) and (2) in the program if the variables are dynamically scoped?
Var x, y:interger; procedure P(n:interger); begin x := (n+2)/(n3); end; procedure Q Var x, y:interger; begin x:=3; y:=4; P(y); Write(x); __(1) end; begin x:=7; y:=8; Q; Write(x); __(2) end.
3, 6  
6, 7  
3, 7  
None of the above 
Question 57 
The function f is defined as follows:
int f (int n) { if (n <= 1) return 1; else if (n % 2 == 0) return f(n/2); else return f(3n  1); }Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.
(i) The function f terminates for finitely many different values of n ≥ 1.
(ii) The function f terminates for infinitely many different values of n ≥ 1.
(iii) The function f does not terminate for finitely many different values of n ≥ 1.
(iv) The function f does not terminate for infinitely many different values of n ≥ 1.
Which one of the following options is true of the above?
(i) and (iii)  
(i) and (iv)  
(ii) and (iii)  
(ii) and (iv) 
→ Let n=3, then it is terminated in 2^{nd} iteration.
→ Let n=5, then sequence is 5→14→7→20→10 and it will repeat.
→ Any number with factor 5 and 2 leads to infinite recursion.
So, (iv) is True and (iii) is False.
Question 58 
Which one of the choices given below would be printed when the following program is executed?
#includestruct test { int i; char *c; }st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"}; main () { struct test *p = st; p += 1; ++p > c; printf("%s,", p++ > c); printf("%c,", *++p > c); printf("%d,", p[0].i); printf("%s n", p > c); }
jungle, n, 8, ncestor  
etter, u, 6, ungle  
cetter, k, 6, jungle  
etter, u, 8, ncestor 
Line 1  main ( )
Line 2  {
Line 3  struct test *p = st;
Line 4  p += 1;
Line 5  ++p → c;
Line 6  printf("%s", p++→ c);
Line 7  printf("%c", +++p → c);
Line 8  printf("%d", p[0].i);
Line 9  printf("%s\n", p → c);
Line 10  }
Now,
Line 3: Initially p is pointing to st, i.e., first element of st which is {5, "become"}
Line 4: Now p is pointing to {4, "better"}
Line 5: ++(p → c), since → has higher precedence, so p → c points to 'e' of "better".
Line 6: prints 'enter' and p now points to {6, "jungle"}
Line 7: ***(p → c), since → has higher precedence. So, prints 'u'.
Line 8: p → i, which is 6 so prints '6'.
Line 9: prints 'ungle' since p is pointing to 'u'.
So, output is "enter, u, 6, ungle".
Question 59 
#includevoid swap (int *x, int *y) { static int *temp; temp = x; x = y; y = temp; } void printab () { static int i, a = 3, b = 6; i = 0; while (i <= 4) { if ((i++)%2 == 1) continue; a = a + i; b = b + i; } swap (&a, &b); printf("a = %d, b = %d\n", a, b); } main() { printab(); printab(); }
a = 0, b = 3 a = 0, b = 3  
a = 3, b = 0 a = 12, b = 9  
a = 3, b = 6 a = 3, b = 6  
a = 6, b = 3 a = 15, b = 12 
Inside print 'a' and 'b' are added to odd integers from 1 to 5, i.e., 1+3+5=9. So, in first call to print ab,
a = 3+9 = 6
b = 6+9 = 3
Static variable have one memory throughout the program run (initialized during program start) and they keep their values across function calls. So during second call to print ab,
a = 6+9 = 15
b = 3+9 = 12
Question 60 
Which one of the choices given below would be printed when the following program is executed?
#includeint a1[] = {6, 7, 8, 18, 34, 67}; int a2[] = {23, 56, 28, 29}; int a3[] = {12, 27, 31}; int *x[] = {a1, a2, a3}; void print(int *a[]) { printf("%d,", a[0][2]); printf("%d,", *a[2]); printf("%d,", *++a[0]); printf("%d,", *(++a)[0]); printf("%d/n", a[1][+1]); } main() { print(x); }
8, 12, 7, 23, 8  
8, 8, 7, 23, 7  
12, 12, 27, 31, 23  
12, 12, 27, 31, 56 
It returns the value of 3^{rd} element in a1.
First printf print 8.
2) *a[2] = *(*(a+2))
It returns the value of 1^{st} element in a3.
Second printf print 12.
3) *++a[0] = *(++(*(a+0)))
a[0] is pointing to 1^{st} element in a1.
++a[0]  after preincrement performed, now a[0] is pointing to 2^{nd} element in a1.
*++a[0] return the value of 2^{nd} element in a1.
Third printf print 7.
4) *(++a)[0]
++a  after preincrement is performed 'a' is pointing to a2.
(++a)[0] is pointing to 1^{st} element in a2.
*(++a)[0] returns the value of 1^{st} element in a2.
Fourth printf print 23.
5) a[1][+1] = *(*(a1)+1)
(a1) is pointing to a1.
*(a1) is pointing to the 2^{nd} element in a1, because in 3^{rd} printf already a1 was incremented by 1.
*(a1)+1 is pointing 3^{rd} element in a1.
*(*(a1)+1) returns the value of 3^{rd} element in a1, i.e., 8.
Question 61 
The following function computes the value of ^{m}C_{n} correctly for all legal values m and n (m≥1,n≥0 and m>n)
int func(int m, int n) { if (E) return 1; else return(func(m  1, n) + func(m  1, n  1)); }
In the above function, which of the following is the correct expression for E?
(n == 0)  (m == 1)  
(n == 0) && (m == 1)  
(n == 0)  (m == n)  
(n == 0) && (m == n) 
^{m}C_{0} = 1
^{n}C_{n} = 1
Question 62 
The following C function takes two ASCII strings and determines whether one is an anagram of the other. An anagram of a string s is a string obtained by permuting the letters in s.
int anagram (char *a, char *b) { int count [128], j; for (j = 0; j < 128; j++) count[j] = 0; j = 0; while (a[j] && b[j]) { A; B; } for (j = 0; j < 128; j++) if (count [j]) return 0; return 1; }
Choose the correct alternative for statements A and B.
A : count [a[j]]++ and B : count[b[j]]–  
A : count [a[j]]++ and B : count[b[j]]++  
A : count [a[j++]]++ and B : count[b[j]]–  
A : count [a[j]]++and B : count[b[j++]]– 
B: Decrements the count by 1 at each index that is equal to the ASCII value of the alphabet it is pointing at. Also it increments the loop counter for next iteration.
If one string is permutation of other, there would have been equal increments and decrements at each index of array, and so count should contain zero at each index, that is what the loop checks at last and if any nonzero elements is found, it returns 0 indicating that strings are not anagram to each other.
Question 63 
The following C function takes a singlylinked list of integers as a parameter and rearranges the elements of the list. The list is represented as pointer to a structure. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the function completes execution?
struct node { int value; struct node *next; ); void rearrange (struct node *list) { struct node *p, *q; int temp; if (!list  !list > next) return; p = list; q = list > next; while (q) { temp = p > value; p > value = q > value; q > value = temp; p = q > next; q = p ? p > next : 0; } }
1, 2, 3, 4, 5, 6, 7  
2, 1, 4, 3, 6, 5, 7  
1, 3, 2, 5, 4, 7, 6  
2, 3, 4, 5, 6, 7, 1 
Question 64 
What is the output printed by the following program?
#includeint f(int n, int k) { if (n == 0) return 0; else if (n % 2) return f(n/2, 2*k) + k; else return f(n/2, 2*k)  k; } int main () { printf("%d", f(20, 1)); return 0; }
5  
8  
9  
20 
Hence, 9 is the answer.
Question 65 
Let a be an array containing n integers in increasing order. The following algorithm determines whether there are two distinct numbers in the array whose difference is a specified number S > 0.
i = 0; j = 1; while (j < n ) { if (E) j++; else if (a[j]  a[i] == S) break; else i++; } if (j < n) printf("yes") else printf ("no");
Choose the correct expression for E.
a[j] – a[i] > S  
a[j] – a[i] < S  
a[i] – a[j] < S  
a[i] – a[j] > S 
If at times difference becomes greater than S we know that it won't reduce further for same 'i' and so we increment the 'i'.
Question 66 
Let x be an integer which can take a value of 0 or 1. The statement if(x = =0) x = 1; else x = 0; is equivalent to which one of the following?
x = 1 + x;  
x = 1  x;  
x = x  1;  
x = 1 % x; 
For x = 0, it gives 1.
For x = 1, it gives 0.
Question 67 
A program attempts to generate as many permutations as possible of the string, 'abcd' by pushing the characters a, b, c, d in the same order onto a stack, but it may pop off the top character at any time. Which one of the following strings CANNOT be generated using this program?
abcd  
dcba  
abad  
cabd 
B) First push abcd, and after that pop one by one. Sequence of popped elements will come to dcba.
C) push abc, and after that pop one by one. Sequence of popped elements will come to cba. Now push 'd' and pop 'd', final sequence comes to cbad.
D) This sequence is not possible because 'a' cannot be popped before 'b' anyhow.
Question 68 
What is the output of the following program?
#include <stdio.h> int funcf (int x); int funcg (int y); main() { int x = 5, y = 10, count; for (count = 1; count <= 2; ++count) { y += funcf(x) + funcg(x); printf ("%d ", y); } } funcf(int x) { int y; y = funcg(x); return (y); } funcg(int x) { static int y = 10; y += 1; return (y+x); }
43 80  
42 74  
33 37  
32 32 
In first case of funcf, which in turn calls funcg, y becomes 11 and it returns 5+11 = 16.
In second call of funcg, y becomes 12 and it returns 5+12 = 17.
So, in main y is incremented by 16+17 = 33 to become 10+33 = 43.
In second iteration:
y will be incremented by 18+19 = 37 to give 43+37 = 80.
Question 69 
Consider the following C program which is supposed to compute the transpose of a given 4 x 4 matrix M. Note that, there is an X in the program which indicates some missing statements. Choose the correct option to replace X in the program.
#include<stdio.h> #define ROW 4 #define COL 4 int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; main() { int i, j, t; for (i = 0; i < 4; ++i) { X } for (1 = 0; i < 4; ++i) for (j = 0; j < 4; ++j) printf ("%d", M[i][j]); }
for(j = 0; j < 4; ++j){ t = M[i][j]; M[i][j] = M[j][i]; M[j][i] = t; }  
for(j = 0; j < 4; ++j){ M[i][j] = t; t = M[j][i]; M[j][i] = M[i][j]; }  
for(j = i; j < 4; ++j){ t = M[i][j]; M[i][j] = M[j][i]; M[j][i] = t; }  
for(j = i; j < 4; ++j){ M[i][j] = t; t = M[j][i]; M[j][i] = M[i][j]; } 
In (D) , given statements is wrong as temporary variable needs to be assigned some value and not viceversa.
Question 70 
Choose the correct option to fill the ?1 and ?2 so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line character.
#includevoid wrt_it (void); int main (void) { printf("Enter Text"); printf ("n"); wrt_ it(); printf ("n"); return 0; } void wrt_it (void) { int c; if (?1) wrt_it(); ?2 }
?1 is getchar() ! = ‘\n’ ?2 is getchar(c);  
?1 is (c = getchar()); ! = ‘\n’ ?2 is getchar(c);  
?1 is c! = ‘\n’ ?2 is putchar(c);  
?1 is (c = getchar()) ! = ‘\n’ ?2 is putchar(c); 
putchar( ) = writes a character specified by the argument to stdout.
As getchar( ) and putchar( ), both are needed to read the string and prints its reverse and only option (D) contains both the function. (D) is the answer.
Now coming to the code, wrt_id(void) is calling itself recursively. When \n is encountered, putchar( ) gets executed and prints the last character and then the function returns to its previous call and prints last 2^{nd} character and so on.
Question 71 
Consider the following C program:
#include <stdio.h> typedef struct { char *a; char *b; } t; void f1(t s); void f2(t *p); main() { static t s = {"A", "B"}; printf ("%s %sn", s.a, s.b); f1(s); printf ("%s %sn", s.a, s.b); f2(&s); } void f1(t s) { s.a = "U"; s.b = "V"; printf ("%s %sn", s.a, s.b); return; } void f2(t *p) { p > a = "V"; p > b = "W"; printf("%s %sn", p > a, p > b); return; }What is the output generated by the program?
AB UV VW VW  
AB UV AB VW  
AB UV UV VW  
AB UV VW UV 
→ f1 is call by value. The changes applicable only for local from f1. UV is printed.
→ Back in main( ), AB is printed.
→ Then in f2, VW is printed.
Hence, answer is (B).
Question 72 
for (int k=0; k<20; k=k+2)
{
if (k % 3 == 1)
system.out.print(k+ " ")
}
What is printed as a result of executing the code segment?
4 16  
4 10 16  
0 6 12 18  
1 4 7 10 13 16 19 
k = 0 % 3 = 0
k = 2 % 3 = 2
k = 4 % 3 = 1 // prints 4
k = 6 % 3 = 0
k = 8 % 3 = 2
k = 10 % 3 = 1 // prints 10
k = 12 % 3 = 0
k = 14 % 3 = 2
k = 16 % 3 = 1 // prints 16
k = 18 % 3 = 0
So, Output is 4 10 16
Question 73 
function F(K : integer)
integer;
begin
if (k<3) then F:=k
else F:=F(k1)*F(k2)+F(k3)
end;
5  
6  
7  
8 
F(4) = F(3)*F(2)+F(1) = 5
F(3) = F(2)*F(1)+F(0) = 2
F(2) = 2
F(1) = 1
F(0) = 0
Question 74 
the values are displayed right justified  
the values are displayed centered  
the values are displayed left justified  
the values are displayed as negative numbers 
Controlling integer width with printf
The %3d specifier is used with integers, and means a minimum width of three spaces, which, by default, will be rightjustified:
Leftjustifying printf integer output
To leftjustify integer output with printf, just add a minus sign (  ) after the % symbol, like this:
The printf integer zerofill option
To zerofill your printf integer output, just add a zero (0) after the % symbol, like this:
Question 75 
subroutine swap(ix,iy)
it = ix
L1 : ix = iy
L2 : iy = it
end
ia = 3
ib = 8
call swap (ia, ib+5)
print *, ia, ib
end
S1: The compiler will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell to swap
S2: On execution the code will generate a runtime error on line L1
S3: On execution the code will generate a runtime error on line L2
S4: The program will print 13 and 8
S5: The program will print 13 and 2
Exactly the following set of statement(s) is correct:
S1 and S2  
S1 and S4  
S3  
S1 and S5 
Swap (8, 13)
⇒ ia will returns value with 13.
⇒ ib is unchanged, because here we using pass by reference value.
➝ Temporary nameless is initialized to 13.
➝ There is No runtime error.
Question 76 
7  
8  
7.4  
7.0 
But here the argument is negative value.
Floor of 7.4 will return the lower limit, i.e. 8.
Question 77 
x:=1;
i:=1;
while (x ≤ 500)
begin
x:=2x ;
i:=i+1;
end
What is the value of i at the end of the pseudocode?
4  
5  
6  
7 
After completion of second iteration x and i values are : x = 4 and i = 3
After completion of third iteration x and i values are : x = 16 and i = 4
After completion of fourth iteration x and i values are : x =256 and i = 5
After completion of fifth iteration x and i values are : x = 65536 and i = 6(Condition is false)
Then the value of “i” is 5
Question 78 
What is the output of the following C code?
#include
int main()
{
int index;
for(index=1; index<=5; index++)
{
printf("%d", index);
if (index==3)
continue;
}
}
1245  
12345  
12245  
12354 
In the given code, there are no statements after continue statement , So it won’t effect on the output.
The loop will executes for five iterations,For each iteration it will print corresponding value i.e; 12345.
Question 79 
p = getnode()
info (p) = 10
next (p) = list
list = p
result in which type of operation?
pop operation in stack  
removal of a node  
inserting a node  
modifying an existing node 
info (p) = 10 // Storing the value of 10 into the info field of new node
next (p) = list // adding new node to the existing list.
list=p // the starting address of the list will
point to the new node
Question 80 
#include
#include
void main()
{
double pi = 3.1415926535;
int a = 1;
int i;
for(i=0; i < 3; i++)
if(a = cos(pi * i/2) )
printf("%d ",1);
else printf("%d ", 0);
}
What would the program print?
000  
010  
101  
111 
→For a given i = 0:
a = cos(pi * 0/2) [ PI*0 is 0]
a = cos(0) = 1, if condition true and it will print 1
→For a given i = 1
a = cos (pi/2) [pi*1 is pi]
a=cos(1.57075) whose value approximately equal to zero
a = 0, Here the condition is false then else part will execute and it will print 0
→For i = 2
a = cos(pi) [pi*2/2 is nothing but pi]
a = 1, ,Here also condition is true and it will execute if part and it will print “1”
→Finally it will print 101.
Question 81 
Class Test
{
public static void main (String [] args)
{
int x = 0;
int y = 0;
for (int z = 0; z < 5; z++)
{
if((++x > 2)  (++y > 2))
{
x++;
}
}
System.out.println( x + " " + y);
}
}
8 2  
8 5  
8 3  
5 3 
For the given code, the loop will execute 5 times.
Step1: For z = 0,
The following condition will execute
if((++x > 2)  (++y > 2))
Here preincrement operation will be performed on x and y. Then x and y values are 1 and the condition false
Similar operation will be performed for z values 1,2,3 and 4
Step2: For z = 1, Then x and y values become 2 and the condition false.
Step3: For z = 2, “” operator is present in the expression, if first operand is true then no need to check the second operand.x value is 3, if condition true and ++y is not evaluated,again “x” value is incremented then x value is “4”.
Perform the step3 procedure for z value 3 and 4 . Here x value is incremented “4” times. Then the final value of “x” is 8 and “y” is 2.
Question 82 
2  
3  
4  
5 
The “current” binding for a given name is the one encountered most recently during execution
Dynamic scoping :
The scope of bindings is determined at run time not at Compile time .
→ For deep binding, the referencing environment is bundled with the subroutine as a closure and passed as an argument. A subroutine closure contains
– A pointer to the subroutine code
– The current set of nametoobject bindings
→ By considering dynamic scoping with deep binding when add is passed into second the environment is x = 1, y = 3 and the x is the global x so it writes 4 into the global x, which is the one picked up by the write_integer.
Question 83 
Consider the following pseudocode fragment, where m is a nonnegative integer that has been initialized:
p = 0; k = 0; while(k < m) p = p + 2^{k}; k = k + 1; end while
Which of the following is a loop invariant for the while statement?
(Note: a loop variant for a while statement is an assertion that is true each time guard is evaluated during the execution of the while statement).
Options:p = 2^{k}  1 and 0≤k  
p = 2^{k+1}  1 and 0≤k  
p = 2^{k}  1 and 0≤k≤m  
p = 2^{k+1}  1 and 0≤k≤m 
k=1, P = 1+21 = 3
k=2, P = 3+22 = 7
k=3, P = 7+23 = 15
Only the option3 satisfied to the loop variant.
Ex: m=2, The loop will execute for 3 times for k value is 0,1 and 2.
Then options3 gives P=1, P=3 and P=7 for the k values 0,1 and respectively.
Question 84 
Consider the C/C++ function f() given below:
void f(char w []) { int x = strlen(w); //length of a string char c; For (int i = 0; i < x; i++) { c = w[i]; w[i] = w[x  i  1]; w[x  i  1] = c; } }
Which of the following is the purpose of f() ?
It output the content of the array with the characters rearranged so they are no longer recognized a the words in the original phrase.
 
It output the contents of the array with the characters shifted over by one position.
 
It outputs the contents of the array in the original order.
 
It outputs the contents of the array in the reverse order.

Question 85 
Consider the following two C++ programs P_{1} and P_{2} and two statements S_{1} and S_{2} about the programs :
 S_{1}: P_{1} prints out 3
S_{2}: P_{2} prints out 4:2
What can you say about the statement S_{1} and S_{2} ?
Code:Neither S_{1} nor S_{2} is true
 
Only S_{1} is true
 
Only S_{2} is true
 
Both S_{1} and S_{2} are true 
The definition of the function first modifies “i” value by 2 and later by “3” and that modification of the value is automatically reflects in the main program as “c” is reference variable.
The program code P_{2} will gives the output of 4:5 as the function will take reference variable as parameter and return type also reference object.
Question 86 
What does the following java function perform ? (Assume int occupies four bytes of storage)
Public static int f(int a) { //Preconditions : a>0 and no overflow/underflow occurs int b=0; for(int i=0; i<32; i++) { b=b <<1; b= b  (a & 1); a = a >>> 1; // This is a logical shift } Return b; }
Return the int that represents the number of 0’s in the binary representation of integer a.  
Return the int that represents the number of 1’s in the binary representation of integer a.  
Return the int that has the reversed binary representation of integer a.
 
Return the int that has the binary representation of integer a.

The initial value of b is 0 and b<<1 means b will get value of 2
b=b(a&1) which means 2(5&1) then b will get value “2”
a=a<<1 means value a will reduce to 3
→ Repeat the above procedure for 31 iterations.
→ Iteration2: b=b<<1 then b is 4 and b=b(a&1)= 4(3&1) then b is 5 and a becomes 1
→ Iteration3: b=b<<1 then b is 10 and b=b(1&1) = 4(3&1) then b is 11 and a becomes 0
→ And this procedure will repeat until “i” value is 32.
→ The final value is 11 (Number of 1’s in the result is 2) and the number of 1’s in the input 5 are 2.
Question 87 
Consider the following recursive Java function f that takes two long arguments and returns a float value :
public static float f(long m, long n) { float result = (float) m / (float) n; if (m < 0  n < 0) return 0⋅0f; else result += f(m*2, n*3); result result; }
Which of the following integers best approximates the value of f(2,3) ?
0  
3  
1  
2 
Question 88 
Consider the following method :
int f(int m, int n, boolean x, boolean y) { int res=0; if(m<0) {res=nm;} else if(x  y) { res= 1; if( n==m) { res =1; } } else {res=n; } return res; } /*end of f */
If P is the minimum number of tests to achieve full statement coverage for f() and Q is the minimum number of tests to achieve full branch coverage for f(), then (P,Q) =
(3,4)  
(3,2)
 
(2,3)  
(4,3)

→ Statement coverage is a white box test design technique which involves execution of all the executable statements in the source code at least once. It is used to calculate and measure the number of statements in the source code which can be executed given the requirements.
→ Branch coverage is a testing method, which aims to ensure that each one of the possible branch from each decision point is executed at least once and thereby ensuring that all reachable code is executed.
→ In the branch coverage, every outcome from a code module is tested. For example, if the outcomes are binary, you need to test both True and False outcomes.
Question 89 
main( )
{
int x=128;
printf (“\n%d”, 1 + x++);
}
128  
129  
130  
131 
printf(“\n%d”, 1 + x++); /* x=128 */
Here, 1+128=129
Question 90 
A function returning a pointer to an array of integers.  
Array of functions returning pointers to integers.  
A function returning an array of pointers to integers.  
An illegal statement. 
Question 91 
x * y < i + j  k
1  
0  
1  
2 
Step1: Evaluate x * y because multiplication has more priority than remaining operators
x * y→ 0
Step2: i + j is 1
Step3: (x*y) < (i+j) is 1. Because relational operators only return 1(TRUE) or 0(FALSE).
Step4: ((x*y) < (i+j))  k is logical OR operator.
1  1 will returns 1.
Note: The precedence is ((x*y) < (i+j))  k
Question 92 
int f (int n)
{
if (n = = 0) then return n;
else return n + f(n2);
}
2550  
2556  
5220  
5520 
Step1: int f(100)
{ if (100 = = 0) then return 100; /*false*/
else return 100 + f(1002); /* return 198 */
}
Step2: int f(98)
{ if (98 = = 0) then return 98; /*false*/
else return 98 + f(96); /* return 194 */
}
Step3: int f(96)
{ if (96 = = 0) then return 96; /*false*/
else return 96 + f(94); /* return 190 */
}
;
;
Stepfinal:
int f(0)
{ if (0 = = 0) then return 0; /*TRUE*/
else return 0 + f(0); /* FALSE */
}
Actual process:
The above series is nothing but sum of Arithmetic Progression(A.P)
= 2+4+6+8+ ... +98+100
= n(n+1)
/* It is nothing but sum of even numbers upto 'n' */
= 50*51
= 2550
Question 93 
#include
main( )
{
int i, inp;
float x, term=1, sum=0;
scanf(“%d %f ”, &inp, &x);
for(i=1; i<=inp; i++)
{
term = term * x/i;
sum = sum + term ;
}
printf(“Result = %f\n”, sum);
}
The program computes the sum of which of the following series?
x + x ^{2} /2 + x ^{3} /3 + x^{ 4} /4 +...  
x + x ^{2} /2! + x^{ 3} /3! + x^{ 4} /4! +...  
1 + x ^{2} /2 + x^{ 3} /3 + x^{ 4} /4 +...  
1 + x ^{2} /2! + x^{ 3} /3! + x^{ 4} /4! +... 
Let x=5, inp=5
Iteration1:
for(i=1; i<=inp; i++)
/* condition true 1<=5 */
{
term = term * x/i;
/* (1*5)/1=5. Here, 5 nothing but ‘x’ */
sum = sum + term ; /* 0+5=5 */
}
Iteration2: Here, ‘i’ becomes 2.
for(i=2; i<=inp; i++)
/* condition true 2<=5 */
{
term = term * x/i;
/* (5*5)/2 is nothing but x 2 /i */
sum = sum + term ; /* 5+12=17 */
}
;;;
;;;
Iteration5: Here, ‘i’ becomes 5.
for(i=5; i<=inp; i++)
/* condition true 5<=5 */
{
term = term * x/i;
/* (625*5)/5! is nothing but x ^{5} /i! */
sum = sum + term ;
}
So, OptionB is correct answer.
Question 94 
a pointer to a function returning array of n pointers to function returning character pointers.  
a function return array of N pointers to functions returning pointers to characters  
an array of n pointers to function returning pointers to characters  
an array of n pointers to function returning pointers to functions returning pointers to characters. 
Question 95 
Public static int f(int a)
{
//Preconditions : a>0 and no overflow/underflow occurs
int b=0;
for(int i=0; i<32; i++)
{
b=b <<1;
b= b  (a & 1);
a = a >>> 1; // This is a logical shift
}
Return b;
}
Return the int that represents the number of 0’s in the binary representation of integer a.  
Return the int that represents the number of 1’s in the binary representation of integer a.  
Return the int that has the reversed binary representation of integer a.  
Return the int that has the binary representation of integer a. 
The initial value of b is 0 and b<<1 means b will get value of 2
b=b(a&1) which means 2(5&1) then b will get value “2”
a=a<<1 means value a will reduce to 3
→ Repeat the above procedure for 31 iterations .
→ Iteration2: b=b<<1 then b is 4 and b=b(a&1)= 4(3&1) then b is 5 and a becomes 1
→ Iteration3: b=b<<1 then b is 10 and b=b(1&1)= 4(3&1) then b is 11 and a becomes 0
→ And this procedure will repeat until “i” value is 32.
→ The final value is 11 (Number of 1’s in the result is 2) and the number of 1’s in the input 5 are 2.
Question 96 
public static float f(long m, long n)
{
float result = (float) m / (float) n;
if (m < 0  n < 0)
return 0⋅0f;
else
result += f(m*2, n*3);
result result;
}
Which of the following integers best approximates the value of f(2,3) ?
0  
3  
1  
2 
{
float result = (float) m / (float) n;
if (m < 0  n < 0)
return 0⋅0f;
else
result += f(m*2, n*3);
result result;
}
function call→ f(2,3)
→ result=2.0/3.0
=0.666
if condition is false, else will be executed result = 0.666+f(4,9) function call→ f(4,9)
→ result=4.0/9.0
=0.444
result=0.4444+f(8,27)
function call→ f(8,27)
→ result=8.0/27.0
=0.2962
result=0.2962+f(16,54)
function call→ f(16,81)
→ result=16.0/81.0
=0.1975
result=0.1975+f(32,243)
function call→ f(32,243)
→ result= 32.0/243.0
= 0.13168
result=0.13168+f(64,729)
and so on
f(128,2187),f(256,6561),f(512,19683),f(1024,59049),f(2048,177147)
result=0.6666+0.4444+0.2962+0.1975+0.13168+0.08779+0.0390+0.026+0.0173+0.0115
The best approximate value is sum of all the function call result values which is equal 2.
Question 97 
int f(int m, int n, boolean x, boolean y)
{
int res=0;
if(m<0) {res=nm;}
else if(x  y) {
res= 1;
if( n==m) { res =1; }
}
else {res=n; }
return res;
} /*end of f */
If P is the minimum number of tests to achieve full statement coverage for f() and Q is the minimum number of tests to achieve full branch coverage for f(), then (P,Q) =
(3,4)  
(3,2)  
(2,3)  
(4,3) 
→ Statement coverage is a white box test design technique which involves execution of all the executable statements in the source code at least once. It is used to calculate and measure the number of statements in the source code which can be executed given the requirements.
→ Branch coverage is a testing method, which aims to ensure that each one of the possible branch from each decision point is executed at least once and thereby ensuring that all reachable code is executed.
→ In the branch coverage, every outcome from a code module is tested. For example, if the outcomes are binary, you need to test both True and False outcomes.
Question 98 
What is the output of the following program:
#include
class sample
{
private int a,b’
public: void test ( )
{
a = 100;
b = 200;
}
friend int compute (sample e1);
};
int compute (sample e1)
{
return int (e1.a + e1.b)  5;
}
Main ( )
{
Sample e;
e.test( );
Cout < < compute(e);
}
205  
300  
295  
error 
Question 99 
Topdown  
Bottomup  
Rightleft  
Leftright 