## 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 ______.

A | 4 |

B | 5 |

C | 6 |

D | 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

A | 0, c |

B | 0, a+2 |

C | ‘0’, ‘a+2’ |

D | ‘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

A | Hi Bye Bye Hi |

B | Hi Bye Hi Bye |

C | Bye Hi Hi Bye |

D | 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

A | 4 |

B | 5 |

C | 6 |

D | 40 |

^{40}is the input, so first loop will make j = 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 pseudo-code. 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 ______.

A | 10230 |

B | 10231 |

C | 10232 |

D | 10233 |

int count=0;

Count(x,y){

if(y!=1){

if(x!=1){

printf(“*”);

count = count +1;

Count(x/2,y);

}

else{

y=y-1;

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:

A | compiler error as the return of malloc is not typecast approximately |

B | compiler error because the comparison should be made as x==NULL and not as shown |

C | compiles successfully but execution may result in dangling pointer |

D | 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

A | 53423122233445 |

B | 53423120112233 |

C | 53423122132435 |

D | 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(val-1); } return val; }

Invocations of foo(3) and bar(3) will result in:

A | Return of 6 and 6 respectively. |

B | Infinite loop and abnormal termination respectively. |

C | Abnormal termination and infinite loop respectively. |

D | 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(val-1);

}

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 _________.

A | 3 |

B | 4 |

C | 5 |

D | 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_MAX-2.

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); }

A | 23 |

B | 24 |

C | 25 |

D | 26 |

Question 11 |

Match the following:

A | P→(ii), Q→(iv), R→(i), S→(iii) |

B | P→(ii), Q→(i), R→(iv), S→(iii) |

C | P→(ii), Q→(iv), R→(iii), S→(i) |

D | P→(iii), Q→(iv), R→(i), S→(ii) |

⇾ A variable located in Data Section of memory

P→(ii), Q→(iv), R→(i), S→(iii)

Question 12 |

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

A | 0, 0 |

B | 0, 1 |

C | 1, 0 |

D | 1, 1 |

{

int *ptr;

x = 0;

ptr = &x;

y = *ptr;

*ptr = 1;

}

printxy (1, 1)

Question 13 |

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)?

A | (q == r) && (r == 0) |

B | (x > 0) && (r == x) && (y > 0) |

C | (q == 0) && (r == x) && (y > 0) |

D | (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 14 |

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[i-1]) { swap(&array[i], &array[i-1]); done=0; } } } printf("%d", array[3]); }

The output of the program is ___________.

A | 3 |

B | 4 |

C | 5 |

D | 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 15 |

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 _______.

A | 0 |

B | 1 |

C | 2 |

D | 3 |

Question 16 |

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 __________.

A | 1 |

B | 2 |

C | 4 |

D | 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 17 |

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?

A | f(s, *s) |

B | i = f(i, s) |

C | f(i, *s) |

D | 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 18 |

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 ________.

A | 2016 |

B | 2017 |

C | 2018 |

D | 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 19 |

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=n-1; while (__________) { if (p[a] <= p[b]) {a = a+1;} else {b = b-1;} } return p[a]; }

The missing loop condition is

A | a != n |

B | b != 0 |

C | b > (a + 1) |

D | 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 20 |

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(n-1); printf("%d ", d); } void main() { count(3); }

A | 3 1 2 2 1 3 4 4 4 |

B | 3 1 2 1 1 1 2 2 2 |

C | 3 1 2 2 1 3 4 |

D | 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 21 |

What will be the output of the following pseudo-code 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);}

A | 6, 2 |

B | 6, 6 |

C | 4, 2 |

D | 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 22 |

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); }

A | 30 |

B | 31 |

C | 32 |

D | 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 23 |

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 = b-1; } } return res; }

Which one of the following conditions is **TRUE** before every iteration of the loop?

A | X ^{Y} = a^{b} |

B | (res * a) ^{Y} = (res * X)^{b} |

C | X ^{Y} = res * a^{b} |

D | 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 24 |

Consider the following program:

int f(int *p, int n) { if (n <= 1) return 0; else return max (f(p+1,n-1),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 __________.

A | 3 |

B | 4 |

C | 5 |

D | 6 |

f(a, 5) ⇒ f(100, 5)

Question 25 |

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”, c-a-b); return 0; }

A | -5 |

B | 6 |

C | 7 |

D | 8 |

_{1}will not swap the value of ‘a’ and ‘b’ because f

_{1}is call by value.

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 26 |

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); }

A | 2036, 2036, 2036 |

B | 2012, 4, 2204 |

C | 2036, 10, 10 |

D | 2012, 4, 6 |

⇒ x [4] [3] can represents that x is a 2-dimensional 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 27 |

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

A | {r = qx+y ∧ r |

B | {x = qy+r ∧ r |

C | {y = qx+r ∧ 0 |

D | {q+1 |

⇒ x = qy + r

Question 28 |

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 __________.

A | 5 |

B | 6 |

C | 7 |

D | 8 |

Question 29 |

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?

A | n ^{3} |

B | n(log n) ^{2} |

C | nlog n |

D | 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 30 |

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

A | ABCD EFGH |

B | ABCD |

C | HGFE DCBA |

D | DCBA |

if condition fails

& returns controls

∴ DCBA will be pointed

Question 31 |

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 _______.

A | 51 |

B | 52 |

C | 53 |

D | 54 |

f(n) = 1; if n = 1

Question 32 |

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 ___________

A | -2 |

B | 2 |

C | -1 |

D | 15 |

When next time stkFunc (0,5) is called then, inside Switch(opcode), the control will go to Case-0, 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 = 2-1 = 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 = 1-1 = 0 and value of A[0] will get returned, i.e., 5.

Finally the two values 10 & 5 will be added and printed.

Question 33 |

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?

A | 12 |

B | 120400 |

C | 1204 |

D | 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 34 |

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(n-1); get(n-3); printf("%d", n); }

A | 15 |

B | 25 |

C | 35 |

D | 45 |

Question 35 |

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 _________.

A | 140 |

B | 150 |

C | 160 |

D | 170 |

**ptr = 40

∴ printf (“%d%d”, p + r – p, p + r) will print 140.

Question 36 |

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 __________.

A | 10 |

B | 11 |

C | 12 |

D | 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 37 |

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; }

A | 230 |

B | 240 |

C | 250 |

D | 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 38 |

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?

A | Compilation fails. |

B | Execution results in a run-time error. |

C | On execution, the value printed is 5 more than the address of variable i. |

D | 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 39 |

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 __________.

A | 9 |

B | 10 |

C | 11 |

D | 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 40 |

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?

A | p = n * (n-1) * (n-2) / 6; |

B | p = n * (n-1) / 2 * (n-2) / 3; |

C | p = n * (n-1) / 3 * (n-2) / 2; |

D | p = n * (n-1) * (n-2) / 6.0; |

From the options n*(n-1)*(n-2) will go out of range. So eliminate A & D.

n*(n-1) is always an even number. So subexpression n(n-1)/2 also an even number.

n*(n-1)/ 2*(n-2), gives a number which is a multiple of 3. So dividing with 3 will not have any loss. Hence B is option.

Question 41 |

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:_____.

A | 1.73 |

B | 1.74 |

C | 1.75 |

D | 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 42 |

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?

A | The function returns 0 for all values of j. |

B | The function prints the string something for all values of j. |

C | The function returns 0 when j = 50. |

D | 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 43 |

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]);

A | The matrix A itself |

B | Transpose of the matrix A |

C | Adding 100 to the upper diagonal elements and subtracting 100 from lower diagonal elements of A |

D | 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 44 |

The minimum number of arithmetic operations required to evaluate the polynomial P(X) = X^{5} + 4X^{3} + 6X + 5 for a given value of X, using only one temporary variable is ________.

A | 7 |

B | 8 |

C | 9 |

D | 10 |

P(X) = x

^{5}+4x

^{3}+6x+5

= x(x

^{4}+4x

^{2}+6)+5

= x(x(x

^{3}+4x)+6)+5

= x(x(x(x

^{2}+4))+6)+5

= x(x(x(x(x)+4))+6)+5

4 multiplications & 3 additions.

4 + 3 = 7

Question 45 |

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; }

A | Θ(n ^{2}) |

B | Θ(n ^{2} log n) |

C | Θ(n ^{3}) |

D | Θ(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 46 |

Consider the following languages.

- L

_{1}= {0

^{p}1

^{q}0

^{r}|p,q,r≥0}

L

_{2}= {0

^{p}1

^{q}0

^{r}|p,q,r≥0, p≠r}

Which one of the following statements is FALSE?

A | L _{2} is context-free. |

B | L _{1}∩ L_{2} is context-free. |

C | Complement of L _{2} is recursive. |

D | Complement of L _{1} is context-free but not regular. |

_{1}= 0*1*0* which is regular language and in L

_{2}we have one comparison (i.e. p ≠ r) so it is CFL.

Intersection of a regular language with a context free language is context free language (by closure properties of regular language) So L

_{1}∩ L

_{2}is context-free.

L

_{2}is CFL and CFL is not closed under complementation so we have to assume L

_{2}as CSL (since every CFL is CSL) and CSL is closed under complement so Complement of L

_{2}must be CSL and every CSL is recursive so Complement of L

_{2}is recursive.

Since L

_{1}is regular and regular language is closed under complement so complement of L

_{1}must be regular and hence the statement Complement of L

_{1}is context-free but not regular is a false statement.

Question 47 |

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?

A | None |

B | 2 only |

C | 3 and 4 only |

D | 4 only |

1, 2 works fine, 3, 4 carries flaw.

Question 48 |

What will be the output of the following C program segment?

char inchar = 'A'; switch (inchar) { case 'A' : printf ("choice A n") ; case 'B' : printf ("choice B ") ; case 'C' : case 'D' : case 'E' : default: printf ("No Choice") ; }

A | No Choice |

B | Choice A |

C | |

D | Program gives no output as it is erroneous |

So,

→ Choice A

→ Choice B. No choice. Is the output.

Question 49 |

Consider the following C program

int a, b, c = 0; void prtFun (void); int main () { static int a = 1; /* line 1 */ prtFun(); a += 1; prtFun(); printf ( "n %d %d " , a, b) ; } void prtFun (void) { static int a = 2; /* line 2 */ int b = 1; a += ++b; printf (" n %d %d " , a, b); }

What output will be generated by the given code segment?

A | |

B | |

C | |

D |

Hence

4 2

6 2

2 0

Question 50 |

Consider the following C program

int a, b, c = 0; void prtFun (void); int main () { static int a = 1; /* line 1 */ prtFun(); a += 1; prtFun(); printf ( "n %d %d " , a, b) ; } void prtFun (void) { static int a = 2; /* line 2 */ int b = 1; a += ++b; printf (" n %d %d " , a, b); }

What output will be generated by the given code segment if:

- Line 1 is replaced by

**auto int a = 1**;

Line 2 is replaced by

**register int a = 2**;

A | |

B | |

C | |

D |

Line 2 replaced by register int a=2;

In main there will be no change if it is static or auto because of a+=1 the auto variable a is updated to 2 from 1.

In prtfun ( ), register makes a difference.

For first print statement a is updated to 4 & prints 4, 2.

But now it is not a static variable to retain the value of a to 4. So it becomes 2, when second function call takes place & prints 4, 2 again. There is no change in b, it acts like a local variable.

Hence,

4 2

4 2

2 0.