Practice Set 1
Functions, Storage classes
1. What is the output printed by the following C code?
#include <stdio.h>
int main()
{
char a[6] = "world";
int i, j;
for(i = 0, j = 5; i < j; a[i++]=a[j--]);
printf("%s\n", a);
return 0;
}
A. dlrow
B. Null string
C. dlrld
D. worow
2. The value of j at the end of the execution of the following C program:
int incr (int i)
{
static int count = 0;
count = count + i;
return (count);
}
main () {
int i, j;
for (i = 0; i <= 4; i++)
j = incr (i);
}
A. 10
B. 4
C. 6
D. 7
Practice Set 1 1
3. In the following C program:
double foo (double); /* Line 1 */
int main() {
double da, db;
//input da
db = foo(da);
}
double foo (double a) {
return a;
}
The above code compiled without any error or warning. If Line 1 is deleted, the
above code will show:
A. no compile warning or error
B. some compiler-warnings not leading to unintended results
C. some compiler-warnings due to type-mismatch eventually leading to unintended
results
D. compiler errors
4. Which combination of the integer variables x, y and z makes the variable a get
the value 4 in the following expression?
a = (x > y)?((x > z)?x : z) : ((y > z)?y : z)
A. x = 3, y = 4, z = 2
B. x = 6, y = 5, z = 3
C. x = 6, y = 3, z = 5
D. x = 5, y = 4, z = 5
5. Consider the following C code segment:
int a, b, c = 0;
void prtFun(void);
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 */
Practice Set 1 2
int b = 1;
a += ++b;
printf(“ \n %d %d ”, a, b);
}
What output will be generated by the given code segment?
A. 3 1
41
42
B. 4 2
61
61
C. 4 2
62
20
D. 3 1
52
52
6. Referring to Q5, 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. 3 1
41
42
B. 4 2
61
61
C. 4 2
62
20
D. 4 2
42
20
7. Consider the C program given below:
Practice Set 1 3
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.
8. Consider the following C program:
#include<stdio.h>
int 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 _______.
9. Consider the following C program:
#include<stdio.h>
int f1(void);
int f2(void);
int f3(void);
Practice Set 1 4
int x=10;
int main()
{
int x=1;
x += f1() + f2 () + f3() + f2();
printf("%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;}
The output of the program is ______.
10. Consider the following C program:
#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(; i>0; i--) {
x = x + total(i);
}
printf("%d\n", x);
}
The output of the program is ______.
11. 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;
}
Practice Set 1 5
The output of the program is ______.
12. Consider the following C program:
#include <stdio.h>
int r() {
static int num=7;
return num--;
}
int main() {
for (r();r();r())
printf(“%d”,r());
return 0;
}
Which one of the following values will be displayed on execution of the programs?
A. 41
B. 52
C. 63
D. 630
13. Consider the following C program:
#include <stdio.h>
int main() {
float sum = 0.0, j=1.0, i=2.0;
while (i/j > 0.0625) {
j=j+j;
sum=sum+i/j;
printf("%f\n", sum);
}
return 0;
}
The number of times the variable sum will be printed, when the above program is
executed, is _________
14. Consider the following C program:
#include <stdio.h>
int main()
{
int i, j, count;
count=0;
i=0;
for (j=-3; j<=3; j++)
Practice Set 1 6
{
if (( j >= 0) && (i++))
count = count + j;
}
count = count +i;
printf("%d", count);
return 0;
}
Which one of the following options is correct?
A. The program will not compile successfully
B. The program will compile successfully and output 10 when executed
C. The program will compile successfully and output 8 when executed
D. The program will compile successfully and output 13 when executed
15. What is the output of the following C 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);
}
A. 43 80
B. 42 74
C. 33 37
D. 32 32
16. Consider the C program given below
#include <stdio.h>
int main () {
Practice Set 1 7
int sum = 0, maxsum = 0, i, n = 6;
int a [] = {2, -2, -1, 3, 4, 2};
for (i = 0; i < n; i++) {
if (i == 0 || a [i] < 0 || a [i] < a [i - 1]) {
if (sum > maxsum) maxsum = sum;
sum = (a [i] > 0) ? a [i] : 0;
}
else sum += a [i];
}
if (sum > maxsum) maxsum = sum ;
printf ("%d\n", maxsum);
}
What is the value printed out when this program is executed?
A. 9
B. 8
C. 7
D. 6
Answers
1. B 5. C 9. 230 13. 5
2. A 6. D 10. 23 14. B
3. D 7. D 11. 0 15. A
4. A 8. 10 12. B 16. C
Practice Set 1 8