1

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.
A
Only i) would compile successfully and it would return size as 4.
B
i), ii) and iii) would compile successfully and size of each would be same i.e. 4
C
i), ii) and iii) would compile successfully but the size of each would be different and would be decided at run time.
D
ii) and iii) would result in compile error but i) would compile and result in size as 4.
Pointer Basics    C Quiz - 101    
Discuss it


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?
A
4 4 4
B
1 4 4
C
1 4 8
D
None of the above
Pointer Basics    C Quiz - 101    
Discuss it


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.
A
malloc() for ppInt1 and ppInt2 isn’t correct. It’ll give compile time error.
B
free(*ppInt2) is not correct. It’ll give compile time error.
C
free(*ppInt2) is not correct. It’ll give run time error.
D
No issue with any of the malloc() and free() i.e. no compile/run time error.
Advanced Pointer    C Quiz - 101    
Discuss it


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.
A
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.
B
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.
C
No compile issue and no run time issue. And the size of the void pointer i.e. pVoid would equal to size of int.
D
sizeof() operator isn’t defined for a pointer of void type.
Advanced Pointer    C Quiz - 101    
Discuss it


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.
A
Both i) and iii) are valid.
B
Only i) is valid.
C
Both i) and ii) are valid.
D
All are valid.
Variable Declaration and Scope    C Quiz - 101    
Discuss it


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?
A
x=1+x;
B
x=1—x;
C
x=x—1;
D
x=1%x;
C Quiz - 101    GATE-IT-2004    
Discuss it


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?
A
abcd
B
dcba
C
cbad
D
cabd
C Quiz - 101    GATE-IT-2004    
Discuss it


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
There are 7 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice