Monday, June 14, 2010

C - I

Here is a list of 50 interesting C interview questions that can be discussed upon...

1. In order to assign attributes to the pointer itself, rather than to the pointed-to object, you put the attributes after the asterisk. like ' char *const p = &c; ' - True/False

2. What is the output of the below code ?
char **p ="Hello";
return 0;

3. There is a char * pointer that points to some ints, and what should be done to step over it ?

4. What changes has to be done to get the ouput as "9 8 7 6 5" by the same recursive call method ?
int main(void) {
int i=9;
printf("%d \t",i--);
return 0;

5. What is the output in the below program ?
void main() {
int z = 12;

6. You can't 'shift' an array - True/False

7. Is it possible to do like this in C ?
char s[8];

8. bit-fields do not provide a way to select bits by index value - True/False

9. Does 'char *a' allocate some memory for the pointer to point to ?

10. A null pointer is does not to point to any object or function - True/False
An uninitialized pointer might point anywhere - True/False

11. The address of operator will never yield a null pointer - True/False
malloc returns a null pointer when it fails - True/False

12. char y[] is not identical to char *y - True / False . Explain the reason for either true/false.

13. What would be output of the below code ?
char x[] = "abcde";
char *y= "abcde";
printf("%c \n", x[3]);
printf("%c \n", y[3]);

14. Is it possible to have char x[5] in one file a declaration extern char * in other file ?

15. What is dangling pointer ?

16. Why does the below code cause segmentation fault ?
int* z = NULL;
*z = 1;

17. What are the two problems in the below code ?
char *s1 = "Hello, ";
char *s2 = "world!";
char *s3 = strcat(s1, s2);

18. What is the problem with the below code ?
i) char a[] = "Hello";
char *p = "Hello";
My program crashes if I try to assign a new value to p[i].

ii) char *a = "abcdef";
*a = 'X';

19. Some compilers have a switch to control if string literals are writable or not - True/False

20. Three attributes can be assign to a pointer: const / volatile / restrict - True/False

21. What are the problems in below code. How will you fix it ?
char *check(int n)
char box[20];
sprintf(box, "%d", n);
return box;

22. What is malloc's internal bookkeeping information and how does it play a role in mis-directing the location of the actual bug ?

23. Where would you use 'const int' instead of #define and where should you use #define instead of 'const int' ?

24. Keyword 'const' refers to read-only -> True/False

25. What is the output of the below code
#define MY_CALCULATOR 2+5+6
printf("%d \n" 3 * MY_CALCULATOR);

26. How does declaring function parameters as 'const' help in better,safer code ?

27. Which of the following is correct . Why does point no 'i' gives output sometime & sometimes it does not give output ?
i. char *a = malloc(strlen(str));
strcpy(a, str);
ii. char *a = malloc(strlen(str) + 1);
strcpy(a, str);

28. How do the below get expanded ? Which one is the preferred method & Why ?
#define mydef struct s *
typedef struct s * mytype;
mydef d1,d2;
mytype t1,t2;

29. i. const values cannot be used in initializers - True/False
ii. const values cannot be used for array dimensions - True/False

30. Is char x[3] = "wow"; legal ?
Why does it work sometimes ?

31. How will the below code corrupt stack ?
void checkstackcorruption (char *var)
char z[12];
memcpy(z, var, strlen(var));
int main (int argc, char **argv)
checkstackcorruption (argv[1]);

32. Is the below method of usage of realloc() correct ?
void *z = malloc(10);
z = realloc(z, 20);

33. What does the below mean ?
int (*)[*];

34. The rank of any unsigned integer type shall equal the rank of the corresponding
signed integer type, if any - True/False

35. The rank of long long int shall be greater than the rank of long int which shall be greater than int - True/False

36.No two signed integer types shall have the same rank, even if they have the same
representation - True/False

37. sprintf function is equivalent to fprintf, except that the output is written into an array - True/False

38. Incase of both sprintf and fprintf , A null character is written at the end of the characters written and that is not counted as part of the returned value - True/False

39. Arithmetic underflow on a negative number results in negative zero - True/False

40.Negative zero and positive zero compare equal under default numerical comparison - True/False

41. 'type cast' should not be used to override a const or volatile declaration - True/False

42. 'sizeof' yields the size of an array in bytes - True / False

43. How will you determine the number of elements in an array using sizeof operator ?

44. What is l-value & r-value ?

45. What is the output of the below code ?
char (*x)[50];
printf("%u, %u,%d\n",x,x+1,sizeof(*x));

46.How will you declare & initialize pointer to a register located at phys addrs 600000h ?

47. What is NPC ?

48. Can we compare a pointer and integer ?

49. Why does *ps.i do not work in the below code ?
struct rec
int i;
float f;
char c;
int main()
struct rec *ps;
ps=(struct rec *) malloc (sizeof(struct rec));
free ps;
return 0;

50. The term 'Pointer to pointer' is different from the term 'double pointer' - True/False


  1. 4. What changes has to be done to get the ouput as "9 8 7 6 5" by the same recursive call method ?
    int main(void) {
    int i=9;
    printf("%d \t",i--);
    return 0;

    ANS Declare int as static int