## PRogramming

Question 1 |

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

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

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 vice-versa.

Question 4 |

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 iteration__:

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

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

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

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

What is the result of the following program?

program side-effect (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 9 |

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

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

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

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

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

What does the following code do?

var a, b : integer; begin a:=a+b; b:=a-b; a:=a-b 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 := a-b = 7-2 = 5

a := a-b = 7-5 = 2

O/P: a=2; b=5

Question 15 |

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 non-zero elements is found, it returns 0 indicating that strings are not anagram to each other.

Question 16 |

The following C function takes a singly-linked 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 17 |

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

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

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

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

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

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] = *(*(a-1)+1)

(a-1) is pointing to a1.

*(a-1) is pointing to the 2

^{nd}element in a1, because in 3

^{rd}printf already a1 was incremented by 1.

*(a-1)+1 is pointing 3

^{rd}element in a1.

*(*(a-1)+1) returns the value of 3

^{rd}element in a1, i.e., 8.

Question 23 |

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

Study the following program written in a block-structured language:

Var x, y:interger; procedure P(n:interger); begin x:=(n+2)/(n-3); 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. |