Interview Questions

Tricky Interview QnA in C.

Interview questions are important for evaluating a candidate's proficiency in the C programming language and assessing their coding and problem-solving skills.

Interview QnA

Here are some tricky interview questions in C that can help you prepare for technical interviews:

  1. 1 • What is C?

C is a general-purpose, procedural programming language that was developed in the early 1970s by Dennis Ritchie at Bell Labs. It is known for its efficiency and low-level system programming capabilities.

  1. 2 • Explain the difference between '++i' and 'i++':

Both '++i' and 'i++' increment the value of 'i' by 1, but '++i' is the pre-increment operator, which increments the value first and then uses it, while i++ is the post-increment operator, which uses the value first and then increments it.

  1. 3 • What is the difference between 'malloc' and 'calloc' functions?

In C, 'malloc' allocates a specified number of bytes of memory but doesn't initialize the memory content, while 'calloc' allocates a specified number of bytes and initializes them to zero.

  1. 4 • What is the difference between '==' and '=' in C?

In C,'==' is a comparison operator used to check if two values are equal, whereas '=' is an assignment operator used to assign a value to a variable.

  1. 5 • '(a = (a + 1, a + 2))' evaluate to if 'a' is initially 3?

It evaluates to 5 because the comma operator returns the result of the last expression.

  1. 6 • What is the output of 'printf("%c", 65)'?

The output is 'A' because the ASCII value 65 corresponds to the character 'A'.

  1. 7 • Swap the values of two variables 'x' and 'y' without using a temporary variable?

'x = x ^ y ^ (y = x);'

  1. 8 • Check if a number is even or odd in a single line without using modulus operator '%'?

if (num & 1) { /* num is odd */ } else { /* num is even */ }

  1. 9 • What is the output of 'printf("%d", -1u > 1)'?

It prints 1 because '-1u' is a large positive value due to unsigned integer conversion, and it's not greater than 1.

  1. 10 • Explain the difference between 'malloc' and 'calloc':

The 'malloc' allocates a block of memory without initializing its contents, while the 'calloc' allocates a block of memory and initializes all its bits to zero.

  1. 11 • What is the 'const' keyword in C used for?

The 'const' keyword is used to define constant values or to specify that a variable's value cannot be changed after initialization.

  1. 12 • What is a 'function pointer,' and how is it useful?

int (*operation)(int, int); // Declare a function pointer

A function pointer is a pointer that points to a function instead of a data object. It allows dynamic selection of functions to call at runtime, which is useful for callbacks and building flexible data structures.

  1. 13 • Explain the difference between 'stack' and 'heap' memory:

Stack memory is used for function call management and local variables, and it operates in a last-in, first-out (LIFO) manner. Heap memory is used for dynamic memory allocation and is more flexible but requires manual memory management.

  1. 14 • What is a volatile variable in C?

volatile int* hr = (int*)0x1000; // Declare a volatile pointer
int value = *hr; // Read the hardware register

A 'volatile' variable tells the compiler that its value may change at any time, and the compiler should not optimize or cache its access. It's typically used for hardware registers or variables that can change asynchronously, such as in interrupt service routines.

  1. 15 • Explain the purpose of 'NULL' in C:

The 'NULL' is a constant that represents a null pointer, which points to no memory location. It is commonly used to initialize pointers or check for pointer validity.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.