Exercise: Questions on C Quiz 1


Questions on C Quiz 1 : Question 1 :
Suppose that in a C program snippet, followings statements are used.
i) sizeof(int);
ii) sizeof(int*);
iii) sizeof(int**);
Assuming size of pointer is 4 bytes and size of int is also 4 bytes, pick the most correct answer from the given options.

Only i) would compile successfully and it would return size as 4.
i), ii) and iii) would compile successfully and size of each would be same i.e. 4
i), ii) and iii) would compile successfully but the size of each would be different and would be decided at run time.
ii) and iii) would result in compile error but i) would compile and result in size as 4.
Show Answer
Questions on C Quiz 1 : Question 2 :
Assume int is 4 bytes, char is 1 byte and float is 4 bytes. Also, assume that pointer size is 4 bytes (i.e. typical case)
char *pChar;
int *pInt;
float *pFloat;

sizeof(pChar);
sizeof(pInt);
sizeof(pFloat);
What’s the size returned for each of sizeof() operator?

4 4 4
1 4 4
1 4 8
None of the above
Show Answer
Questions on C Quiz 1 : Question 3 :
#include "stdlib.h"
int main()
{
 int *pInt;
 int **ppInt1;
 int **ppInt2;

 pInt = (int*)malloc(sizeof(int));
 ppInt1 = (int**)malloc(10*sizeof(int*));
 ppInt2 = (int**)malloc(10*sizeof(int*));

 free(pInt);
 free(ppInt1);
 free(*ppInt2);
 return 0;
}
Choose the correct statement w.r.t. above C program.

malloc() for ppInt1 and ppInt2 isn’t correct. It’ll give compile time error.
free(*ppInt2) is not correct. It’ll give compile time error.
free(*ppInt2) is not correct. It’ll give run time error.
No issue with any of the malloc() and free() i.e. no compile/run time error.
Show Answer
Questions on C Quiz 1 : Question 4 :
#include "stdio.h" 
int main()
{
 void *pVoid;
 pVoid = (void*)0;
 printf("%lu",sizeof(pVoid));
 return 0;
}
Pick the best statement for the above C program snippet.

Assigning (void *)0 to pVoid isn’t correct because memory hasn’t been allocated. That’s why no compile error but it'll result in run time error.
Assigning (void *)0 to pVoid isn’t correct because a hard coded value (here zero i.e. 0) can’t assigned to any pointer. That’s why it'll result in compile error.
No compile issue and no run time issue. And the size of the void pointer i.e. pVoid would equal to size of int.
sizeof() operator isn’t defined for a pointer of void type.
Show Answer
Questions on C Quiz 1 : Question 5 :
Consider the following variable declarations and definitions in C
i) int var_9 = 1;
ii) int 9_var = 2;
iii) int _ = 3;
Choose the correct statement w.r.t. above variables.

Both i) and iii) are valid.
Only i) is valid.
Both i) and ii) are valid.
All are valid.
Show Answer
Questions on C Quiz 1 : Question 6 :
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;
Show Answer
Questions on C Quiz 1 : Question 7 :
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
cbad
cabd
Show Answer
Questions on C Quiz 1 : Question 8 :
Consider following two C - program :
P1 :
 int main()
 {
     int (*ptr)(int ) = fun;
     (*ptr)(3);
     return 0;
 }
 
 int fun(int n)
 {
   for(;n > 0; n--)
     printf("PROcoders ");
   return 0;
 }
 
P2 :
 int main()
 {
     void demo();
     void (*fun)();
     fun = demo;
     (*fun)();
     fun();
     return 0;
 }
 
 void demo()
 {
     printf("PROcoders ");
 }
 
Which of the following option is correct?

P1 printed "PROcoders PROcoders" and P2 printed "PROcoders PROcoders"
P1 printed "PROcoders PROcoders" and P2 gives compiler error
P1 gives compiler error and P2 printed "PROcoders PROcoders"
None of the above
Show Answer
Questions on C Quiz 1 : Question 9 :
Choose the best statement with respect to following three program snippets.
/*Program Snippet 1 with for loop*/
for (i = 0; i < 10; i++)
{
   /*statement1*/
   continue;
   /*statement2*/
}

/*Program Snippet 2 with while loop*/
i = 0;
while (i < 10)
{
   /*statement1*/
   continue;
   /*statement2*/
   i++;
}

/*Program Snippet 3 with do-while loop*/
i = 0;
do
{
   /*statement1*/
   continue;
   /*statement2*/
   i++;
}while (i < 10);

All the loops are equivalent i.e. any of the three can be chosen and they all will perform exactly same.
continue can't be used with all the three loops in C.
After hitting the continue; statement in all the loops, the next expression to be executed would be controlling expression (i.e. i < 10) in all the 3 loops.
None of the above is correct.
Show Answer