• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Quiz - 101

Question 1

Suppose that in a C program snippet, followings statements are used. C
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.

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) C
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

Question 3

C
#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.

Question 4

C
#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.

Question 5

Consider the following variable declarations and definitions in C 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.

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;

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

Question 8

Consider following two C - program :
P1 : C
int main()
{
    int (*ptr)(int ) = fun;
    (*ptr)(3);
    return 0;
}

int fun(int n)
{
  for(;n > 0; n--)
    printf(\"GeeksQuiz \");
  return 0;
}
P2 : C
int main()
{
    void demo();
    void (*fun)();
    fun = demo;
    (*fun)();
    fun();
    return 0;
}

void demo()
{
    printf(\"GeeksQuiz \");
}
Which of the following option is correct?
  • P1 printed "GeeksQuiz GeeksQuiz" and P2 printed "GeeksQuiz GeeksQuiz"
  • P1 printed "GeeksQuiz GeeksQuiz" and P2 gives compiler error
  • P1 gives compiler error and P2 printed "GeeksQuiz GeeksQuiz"
  • None of the above

Question 9

Choose the best statement with respect to following three program snippets.
C
/*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.

There are 9 questions to complete.

Last Updated :
Take a part in the ongoing discussion