Tuesday, September 26, 2017

Frequently asked C Programming Questions

Below are the list of some of the questions:

CFAQ:
http://c-faq.com



Some of the good links for Bit Manipulations:
1).  Check if the number is power of two.

If we subtract 1 from a power of 2 what we get is 1s till the last unset bit and if we apply Bitwise AND operator we should get only zeros.

/* Function to check if x is power of 2*/
bool isPowerOfTwo (int x)
{
  /* First x in the below expression is for the case when x is 0 */
  return x && (!(x&(x-1)));
}


2). Check if he number is even of Add using Bitwise operator:
The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even. 

A). bitwise XOR Operation of the Number by 1 increment the value of the number by 1 if the number is even otherwise it decrements the value of the number by 1 if the value is odd.

// Returns true if n is even, else odd
bool isEven(int n)
{
 
    // n^1 is n+1, then even, else odd
    if (n ^ 1 == n + 1)
        return true;
    else
        return false;
}

B).  Biwise And
// Returns true if n is even, else odd
bool isEven(int n)
{
    // n&1 is 1, then odd, else even
    return (!(n & 1));
}
  
c). W know bitwise OR Operation of the Number by 1 increment
the value of the number by 1 if the number is even otherwise
it will remain unchanged.

// Returns true if n is even, else odd
bool isEven(int n)
{
 
    // n|1 is greater than n, then even, else odd
    if ((n | 1) > n)
        return true;
    else
        return false;
} 


3). // C program to check if k-th bit
// of a given number is set or not
#include <stdio.h>
 
void isKthBitSet(int n, int k)
{
    if (n & (1 << (k - 1)))
        printf("SET");
    else
        printf("NOT SET");
}

1).Bit Twiddling Hacks
2).Bitwise Operators from GeekforGeeks
3). 



Strings


Pointers

  1. Implementing system calls by yourself (sizeof, strcpy, strcmp etc)
  2. Implement your own sizeof and explain




Function Pointers


Structures/Unions/Bit fields/ Structure padding


Memory allocations and APIs (Malloc/calloc/realloc/heap)
 

  1. What is the point of using malloc(0)?

Various Keyword (Static, Volatile etc)
  1. Can static variables be declared in a header file?
  2. Can a variable be both const and volatile?


Make Files


Libraries (Static/Dynamic)


GCC Flags




Some  popular questions:
  1. Difference between malloc and new?
  2. How will you design interface between two modules?
  3. Where variables sits In memory
  4. Diff between Vector and list .
  5. Volatile keyword
  6. Diff between Structure and union 
  7. How to get size for union and structure
  8. Memory allocation in c
  9. Heap or variable which memory is fast
  10. Diff between link list and and vector
  11. Bit field, how these stored in memory
  12. Big endian and little endian
  13. If 1000 of integer linklist and array is present which would be faster and why
  14. Synchronisation in thread
  15. Diff in mutex and binary semaphore
  16. C memory management
  17. C Storage class
  18. Gdb
  19. Binary search
  20. Multi dimensional array
  21. IPC mechanism
  22. Which storage class is fast
  23. Write your own strtok function where each call return one token separated by delimiter  passed in the string passed. 
  24. Reverse string in place. 
  25. What are different gcc compiler options to secure your code. 
  26. How malloc works behind the scene.
  27. How can you protect the same. 
  28. What is memory corruption. Different types. 
  29. How printf works and write your own printf which can print all basic data type of c. 
  30. What is the security issue with printf. 
  31. Difference between strlcpy and strncpy. What is the security issue with this. 
  32. Check for Balanced parenthesis.
     - using Stack method how you will implement stack and what are different ways. 
     -above algo using lot of memory. now implement same using minimal use of memory. 









No comments:

Post a Comment