PRogramming
Question 1 |
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 2 |
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 3 |
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 4 |
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 5 |
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 6 |
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 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 |
Given the programming constructs (i) assignment (ii) for loops where the loop parameter cannot be changed within the loop (iii) if-then-else (iv) forward go to (v) arbitrary go to (vi) non-recursive procedure call (vii) recursive procedure/function call (viii) repeat loop, which constructs will you not include in a programming language such that it should be possible to program the terminates (i.e., halting) function in the same programming language.
(ii), (iii), (iv) | |
(v), (vii), (viii) | |
(vi), (vii), (viii) | |
(iii), (vii), (viii) |
Question 10 |
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 11 |
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 ______.
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=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 12 |
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 240 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 13 |
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 14 |
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 15 |
#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 16 |
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 17 |
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 2nd character and so on.
Question 18 |
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 19 |
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 20 |
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 21 |
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 22 |
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 23 |
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 24 |
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 25 |
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 26 |
The following function computes the value of mCn 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) |
mC0 = 1
nCn = 1
Question 27 |
#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