C MCQ Set-9 : Multiple Choice Questions in C Set-9

Set-26 C MCQ

1. What does the above program print?

void foo(int n, int sum)
{
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf (“%d,”, k);
}

int main ()
{
int a = 2048, sum = 0;
foo (a, sum);
printf (“%dn”, sum);

getchar();
}

A)8, 4, 0, 2, 14
B)8, 4, 0, 2, 0
C)2, 0, 4, 8, 14
D)2, 0, 4, 8, 0

Ans:D


2. Consider the following C function

void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}

In order to exchange the values of two variables x and y.

A)Call swap (x, y)
B)Call swap (&x, &y)
C)swap(x,y) cannot be used as it does not return any value
D)swap(x,y) cannot be used as the parameters are passed by value

Ans:D


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

Ans:A


4. What does the above program print?

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;
}
Run on IDE

A)-5
B)-4
C)5
D)3

Ans:A


5. Consider the following C function:

int f(int n)
{
static int i = 1;
if(n >= 5) return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is

A)5
B)6
C)7
D)8

Ans:C


6. What would the program print?

#include <stdio.h>
void main()
{
double pi = 3.1415926535;
int a = 1;
int i;
for(i=0; i < 3; i++)
if(a = cos(pi * i/2) )
printf(“%d “,1);
else printf(“%d “, 0);
}

A)000
B)010
C)101
D)111

Ans:C


7. How many times the while loop will get executed if a short int is 2 byte wide?

#include <stdio.h>
int main()
{
short int j=1;
while(j <= 255)
{
printf(“%c %d\n”, j, j);
j++;
}
return 0;
}

A)Infinite times
B)255 times
C)256 times
D)254 times

Ans: B
C MCQ Set-9 Explanation


8. Point out the error, if any in the for loop.

#include <stdio.h>
int main()
{
int i=1;
for(;;)
{
printf(“%d\n”, i++);
if(i>10)
break;
}
return 0;
}

A)There should be a condition in the for loop
B)The two semicolons should be dropped
C)The for loop should be replaced with while loop.
D)No error

Ans: D


9. Point out the error, if any in the while loop.

#include <stdio.h>
int main()
{
int i=1;
while()
{
printf(“%d\n”, i++);
if(i>10)
break;
}
return 0;
}

A)There should be a condition in the while loop
B)There should be at least a semicolon in the while
C)The while loop should be replaced with for loop.
D)No error

Ans: A


10. What will be output of following code?

#include<stdio.h>
int main()
{
while ()
printf(“In while loop “);
printf(“After loop\n”);
}

A) In while loop after loop
B) After loop
C) Compile time error
D) Infinite loop

Ans: C
C MCQ Set-9 Explanation


11. What will be output of following code?

#include<stdio.h>
int main()
{
do
printf(“In while loop “);
while (0);
printf(“After loop\n”);
}

A) In while loop
B) In while loop after loop
C) After loop
D) Infinite loop

Ans: B
C MCQ Set-9 Explanation


12. What will be output of following code?

#include<stdio.h>
void main()
{
int i = 2;
do
{
printf(“Hi”);
} while (i < 2)
}

A) Compile time error
B) Hi Hi
C) Hi
D) Varies

Ans: A


13. What will be output of following code?

# include<stdio.h>
void main()
{
double k = 0;
for (k = 0.0; k < 3.0; k++);
printf(“%lf”, k);
}

a) 2.000000
b) 4.000000
c) 3.000000
d) Run time error

Ans: C


14. Which of the following sentences are correct about a for loop in a C program?

1: for loop works faster than a while loop.
2: All things that can be done using a for loop can also be done using a while loop.
3: for(;;); implements an infinite loop.
4: for loop can be used if we want statements in a loop get executed at least once.

A. 1
B. 1, 2
C. 2, 3
D. 2, 3, 4

Ans: D


15. What is printed on console?

int main()
{
int a = 0;
while(a=1)
{
printf(“Hello”);
}
return 0;
}

(A) Hello is printed Infinite times
(B) Hello is printed 1 time only
(C) Nothing is printed
(D) Program gives error

Ans: A


Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *