C Quiz – 101

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.
Cross
Only i) would compile successfully and it would return size as 4.
Tick
i), ii) and iii) would compile successfully and size of each would be same i.e. 4
Cross
i), ii) and iii) would compile successfully but the size of each would be different and would be decided at run time.
Cross
ii) and iii) would result in compile error but i) would compile and result in size as 4.


Question 1-Explanation: 
Size of all pointer types is same. And whether it is a \'pointer to char\' or \'pointer to int\' or \'pointer to pointer to int\', the size always remain same. That\'s why all i), ii) and iii) would compile successfully and would result in same size value of 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)
char *pChar;
int *pInt;
float *pFloat;

sizeof(pChar);
sizeof(pInt);
sizeof(pFloat);
What’s the size returned for each of sizeof() operator?
Tick
4 4 4
Cross
1 4 4
Cross
1 4 8
Cross
None of the above


Question 2-Explanation: 
Irrespective of the type of pointer, the size for a pointer is always same. So whether it’s pointer to char or pointer to float, the size of any pointer would be same. Even size of a pointer to user defined data type (e.g. struct) is also would be same.
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.
Cross
malloc() for ppInt1 and ppInt2 isn’t correct. It’ll give compile time error.
Cross
free(*ppInt2) is not correct. It’ll give compile time error.
Cross
free(*ppInt2) is not correct. It’ll give run time error.
Tick
No issue with any of the malloc() and free() i.e. no compile/run time error.


Question 3-Explanation: 
ppInt2 is pointer to pointer to int. *ppInt2 is pointer to int. So long as the argument of free() is pointer, there\'s no issue. That\'s why B) and C) both are not correct. Allocation of both ppInt1 and ppInt2 is fine as per their data type. So A) is also not correct. The correct statement is D).
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.
Cross
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.
Cross
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.
Tick
No compile issue and no run time issue. And the size of the void pointer i.e. pVoid would equal to size of int.
Cross
sizeof() operator isn’t defined for a pointer of void type.


Question 4-Explanation: 
(void *)0 is basically NULL pointer which is used for many purposes in C. Please note that no matter what is the type of pointer, each pointer holds some address and the size of every pointer is equal to sizeof(int). So D) isn\'t correct. An absolute address can be assigned to any pointer though it might result in issues at run time if the address is illegal. Since 0 is a legal address, assigning (void *)0 to pVoid is fine. So B) isn\'t correct. We aren\'t doing any illegal operation with pVoid here. So it\'ll not result in any compile/run time error. So A) isn\'t correct. For example, if we perform illegal operation over pVoid e.g. de-referencing of void pointer i.e. *pVoid, it\'ll result in error. The above program will compile/run without any issue. So C) is correct.
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.
Tick
Both i) and iii) are valid.
Cross
Only i) is valid.
Cross
Both i) and ii) are valid.
Cross
All are valid.


Question 5-Explanation: 
In C language, a variable name can consists of letters, digits and underscore i.e. _ . But a variable name has to start with either letter or underscore. It can\'t start with a digit. So valid variables are var_9 and _ from the above question. Even two back to back underscore i.e. __ is also a valid variable name. Even _9 is a valid variable. But 9var and 9_ are invalid variables in C. This will be caught at the time of compilation itself. That\'s why the correct answer is A).  
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?
Cross
x = 1 + x;
Tick
x = 1 - x;
Cross
x = x - 1;
Cross
x = 1 % x;


Question 6-Explanation: 
Consider (B): x=1-x as we need to have x value either 0 or 1. Consider x=0 ; x=1-0=1 => So when x=0 we get x value to be 1. Consider x=1 ; x=1-1=0 => So when x=1 we get x value to be 0.
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?
Cross
abcd
Cross
dcba
Cross
cbad
Tick
cabd


Question 7-Explanation: 
  A. PUSH a- POP a , PUSH b- POP   b , PUSH c- POP   c, PUSH d- POP   d B. PUSH a ,PUSH b, PUSH c,PUSH d , POP d, POP c ,POP b, POP a    C.PUSH a ,PUSH b, PUSH c , POP c ,POP b, POP a   PUSH d- POP   d D. Sequence not feasible Therefore,Answer is D
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("GeeksQuiz ");
  return 0;
}
P2 :
int main()
{
    void demo();
    void (*fun)();
    fun = demo;
    (*fun)();
    fun();
    return 0;
}

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


Question 8-Explanation: 
P1 : The only problem with program is fun is not declared/defined before it is assigned to ptr. The following program works fine and prints \"GeeksQuiz GeeksQuiz GeeksQuiz \"
int fun(int n);

int main()
{
    // ptr is a pointer to function fun()
    int (*ptr)(int ) = fun;

    // fun() called using pointer 
    (*ptr)(3);
    return 0;
}

int fun(int n)
{
  for(;n > 0; n--)
    printf(\"GeeksQuiz \");
}
P2 : This is a simple program with function pointers. fun is assigned to point to demo. So the two statements \"(*fun)();\" and \"fun();\" mean the same thing. Option (C) is correct.
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);
Cross
All the loops are equivalent i.e. any of the three can be chosen and they all will perform exactly same.
Cross
continue can\'t be used with all the three loops in C.
Cross
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.
Tick
None of the above is correct.


Question 9-Explanation: 

First and foremost, continue can be used in any of the 3 loops in C. In case of “for” loop, when continue is hit, the next expression to be executed would be i++ followed by controlling expression (i.e. i < 10). In case of “while” loop, when continue is hit, the next expression to be executed would be controlling expression (i.e. i < 10). In case of “do-while” loop, when continue is hit, the next expression to be executed would be controlling expression (i.e. i < 10). That’s why “while” and “do-while” loops would behave exactly same but not the “for” loop. Just to re-iterate, i++ would be executed in “for” loop when continue is hit.

Option (D) is correct.
There are 9 questions to complete.


  • Last Updated : 26 Sep, 2023

Share your thoughts in the comments
Similar Reads