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

Set-26 C MCQ

1. What will be the output of the program ?

#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf(“%d, %d, %d”, i, j, m);
return 0;
}

A.2, 1, 15
B.1, 2, 5
C.3, 2, 15
D.2, 3, 20

Answer : C


2. The number of swapping needed to sort the numbers 8, 22, 7, 9, 31, 5, 13 in ascending order, using bubble sort is

(A) 11
(B) 12
(C) 13
(D) 10

Answer : d


3. Consider the following three C functions :

[PI] int * g (void)
{
int x = 10;
return (&x);
}

[P2] int * g (void)
{
int * px;
*px = 10;
return px;
}

[P3] int *g (void)
{
int *px;
px = (int *) malloc (sizeof(int));
*px = 10;
return px;
}
Which of the above three functions are likely to cause problems with pointers?

(a) Only P3
(b) Only P1 and P3
(c) Only P1 and P2
(d) P1, P2 and P3

Answer : C


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

Answer : A
C MCQ Set-18 Explanation


5. Consider the following C declaration

struct
{
short s [5]
union
{
float y;
long z;
}u;
} t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment
considerations, is

(a) 22 bytes
(b) 14 bytes
(c) 18 bytes
(d) 10 bytes

Answer : C


6. In the C language:

a) At most one activation record exists between the current activation record and the activation record for the main
b) The number of activation records between the current activation record and the activation record for the main depends on the actual function calling sequence.
c) The visibility of global variables depends on the actual function calling sequence.
d) Recursion requires the activation record for the recursive function to be saved on a different stack before the recursive function can be called.

Answer : B
C MCQ Set-18 Explanation


7. Consider the C program shown below.

#include<stdio.h>
define print(x) printf(“%d”,x)
int x;
void Q(int z)
{
z += x;
print(z);
}
void P(int *y)
{
int x = *y+2;
Q(x);
*y = x-1;
print(x);
}

main(void)
{
x=5;
P(&x);
print(x);
getchar();
}
The output of this program is:

a) 12 7 6
b) 22 12 11
c) 14 6 6
d) 766

Answer : A


8. Consider the following declaration of a ‘two-dimensional array in C:

char a[100][100];
Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0,
and size of char is 1 byte. In row major implementation, the address of a[40][50] is :

a) 4040
b) 4050
c) 5040
d) 5050

Answer : B


9. The most appropriate matching for the following pairs:

X: m=malloc(5); m= NULL; 1: using dangling pointers
Y: free(ptr); ptr->value=5; 2: using uninitialized pointers
Z: char *p; *p = ’a’; 3. lost memory is:

(a) X—1 Y—3 Z-2
(b) X—2 Y—1 Z-3
(C) X—3 Y—2 Z-1
(d) X—3 Y—1 Z-2

Answer : D


10. Consider the following C program segment:

#include<stdio.h>
int main()
{
char p[20];
char *s = “string”;
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
{
p[i] = s[length – i-1];
if(s[length-i-1]== ‘a’|| s[length-i-1]== ‘e’ || s[length-i-1]== ‘i’|| s[length-i-1]== ‘o’|| s[length-i-1]== ‘u’)
break;
}
p[i]=’\0′
printf(“%s”,p);
}

The output of the program is:

a) gni
b) gn
c) string
d) no output is printed

Answer : B


Spread the love

Leave a Comment

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