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 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 3 |
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 4 |
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 5 |
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 __________.
3 | |
4 | |
5 | |
6 |

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

Question 6 |
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;
}-5 | |
6 | |
7 | |
8 |
But f2 will swap the value of 'b' and 'c' because f2 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 7 |
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 8 |
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?
n3 | |
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 9 |
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 10 |
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 11 |
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 12 |
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);
}
15 | |
25 | |
35 | |
45 |

Question 13 |
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 14 |
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 15 |
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 run-time 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 16 |
Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3. If n is large, which one of the following statements is most likely to set p correctly?
p = n * (n-1) * (n-2) / 6; | |
p = n * (n-1) / 2 * (n-2) / 3; | |
p = n * (n-1) / 3 * (n-2) / 2; | |
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 17 |
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 |
x2 - 3 < 0.01 will become True.
So, x2 - 3 < 0.01
x2 - 3 < 0.01
x2 < 3.01
x < 1.732
Hence, x = 1.73.
Question 18 |
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 19 |
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 20 |
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 21 |
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 22 |
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;
}
Θ(n2) | |
Θ(n2 log n) | |
Θ(n3) | |
Θ(n3 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(n2logn)
= (n/2+1).n/2 ∙log n
= (n2log n)
Question 23 |
A common property of logic programming languages and functional languages is:
both are procedural languages | |
both are based on λ-calculus | |
both are declarative | |
both use Horn-clauses |
Question 24 |
Which one of the following are essential features of an object-oriented programming language?
- (i) Abstraction and encapsulation
(ii) Strictly-typedness
(iii) Type-safe property coupled with sub-type rule
(iv) Polymorphism in the presence of inheritance
(i) and (ii) only | |
(i) and (iv) only | |
(i), (ii) and (iv) only | |
(i), (iii) and (iv) only |
Question 25 |
The goal of structured programming is to:
have well indented programs
| |
be able to infer the flow of control from the compiled code
| |
be able to infer the flow of control from the program text
| |
avoid the use of GOTO statements |
Question 26 |
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 27 |
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.
