CORE JOBS
Programming in C interview questions
Short answers on the fundamentals, for the technical round of core-job interviews. 24 questions for CSE/IT students; the numericals show the working.
Companies do not publish their interview questions; these are ours.
C fundamentals
C basics notesWhat are integer promotions in C?
Before arithmetic, operands of type char or short are converted to int, or to unsigned int if int cannot hold all their values. So adding two chars is done in int. This is why the sum of two signed chars can exceed 127 inside the expression; it is converted, and may change, only when stored back in a char.
What is the difference between signed and unsigned integer overflow in C?
Signed overflow is undefined behaviour, so the compiler may assume it never happens and the result is not guaranteed. Arithmetic in unsigned int or a wider unsigned type is defined to wrap modulo for an N-bit type. So UINT_MAX + 1 gives 0, while INT_MAX + 1 has no defined result.
Why does the test
p != NULL && p->x > 0not crash when p is NULL?The && operator evaluates its left operand first and skips the right operand if the left is 0. There is a sequence point between them, so the dereference never runs for a null pointer. The operator || works the same way, stopping when its left operand is nonzero.
Why is an expression such as
i = i++ + ++iundefined in C?The object i is modified more than once with no sequence point between the changes, and the + operator does not fix the order in which its operands are evaluated. The standard gives no meaning to such code, so different compilers may print different results, and all are correct.
On a machine with an 8-bit unsigned char,
unsigned char c = 250;is followed byc = c + 10;. What value does c hold afterwards?The variable c holds 4, because the sum 260 is computed in int and then wraps modulo 256 when stored back into the 8-bit unsigned char.
WORKING
- c is promoted to int, so c + 10 is computed as 260.
- Storing 260 in an unsigned char reduces it modulo .
- .
What happens in a switch statement when a case ends without a break?
Execution falls through into the statements of the next case label, whatever its value, and continues until a break or the end of the switch. Case labels are only jump targets, not separate blocks. A default label placed in the middle falls through in the same way.
Pointers, arrays and strings in C
Pointers & arrays notesWhat is array-to-pointer decay?
In most expressions an array name is converted to a pointer to its first element. The exceptions are the operand of sizeof, the operand of unary &, and a string literal that initialises an array. That is why passing an array to a function passes a pointer. A parameter declared as an array is adjusted to a pointer, so sizeof on it gives the pointer size.
What is the difference between
char *s = "hi";andchar s[] = "hi";?The first makes a pointer to a string literal, which usually sits in read-only storage, so modifying it is undefined behaviour. The second makes an array holding its own modifiable copy of the 3 bytes. Also, sizeof gives the pointer size for the first and 3 for the second.
A two-dimensional array
int a[10][20]starts at address 2000. An int takes 4 bytes and the array is stored row by row. What is the address ofa[3][5]?The address of a[3][5] is 2260. In row-major order 3 full rows of 20 elements come before it, plus 5 elements in its own row, so 65 elements of 4 bytes each are added to the base address 2000.
WORKING
- Element offset = elements.
- Byte offset = .
- Address = .
On a machine where an int is 4 bytes and must sit at an address divisible by 4, what is the sizeof of
struct { char a; int b; char c; }?The structure occupies 12 bytes: 1 for a, 3 of padding, 4 for b, 1 for c and 3 of tail padding so that arrays of it stay aligned.
WORKING
- a sits at offset 0, then 3 padding bytes so that b starts at offset 4.
- b occupies offsets 4 to 7 and c sits at offset 8, so the members end at 9 bytes.
- The size is rounded up to a multiple of the largest alignment, 4, giving 12.
How does pointer arithmetic work in C?
Adding n to a pointer of type T* moves it by n times sizeof(T) bytes, so p + 1 points to the next element. Subtracting two pointers into the same array gives the number of elements between them. Arithmetic is valid only inside the array or one position past its end.
What is the difference between malloc and calloc?
The call malloc(n) takes the total number of bytes and leaves the memory uninitialised. The call calloc(count, size) takes an element count and an element size, and sets every byte to zero. Both return NULL on failure, and both blocks must be released with free.
Why is it good practice to set a pointer to NULL after calling free on it?
After free the pointer still holds the old address, which is no longer valid, so using it is undefined behaviour. Setting it to NULL lets a later null check catch the misuse, and free(NULL) is defined to do nothing, so a second free through that pointer is harmless. Other copies of the old address still dangle.
What is the difference between
const int *pandint *const p?In
const int *pthe pointer can be changed to point elsewhere, but the int it points to cannot be modified through p. Inint *const pthe pointer itself is fixed after initialisation, but the int it points to can be changed. Read the declaration from the name outwards.
Functions, parameter passing, scope and storage classes
Functions & scope notesWhat does call by value mean in C, and how is a swap function written?
C passes a copy of each argument, so changing a parameter inside the function does not change the caller's variable. To swap two ints, pass their addresses and exchange the values through the pointers, for example with a temporary: t = *a; *a = *b; *b = t. The pointers themselves are still passed by value.
What is the difference between an auto local variable and a static local variable?
An auto local is created on each entry to its block and destroyed on exit, and holds garbage unless initialised. A static local lives for the whole program, is initialised once (to zero by default) and keeps its value between calls. Its name is still visible only inside its block.
What is the difference between static scoping and dynamic scoping, and which does C use?
With static (lexical) scoping, a name refers to the declaration found in the enclosing text where the function is written. With dynamic scoping, it refers to the most recent declaration in the chain of callers at run time. C uses static scoping, so a function never sees its caller's locals.
With
#define SQ(x) x*x, the statementint y = SQ(a+1);runs with a = 3. What value does y get?The variable y becomes 7, not 16, because the macro pastes the text a+1*a+1 and multiplication has higher precedence than addition. Defining the macro as ((x)*(x)) makes it give 16.
WORKING
- The preprocessor substitutes text, so SQ(a+1) becomes
a+1*a+1. - Multiplication binds tighter than addition, so this is .
- With a = 3 this is .
- The preprocessor substitutes text, so SQ(a+1) becomes
What does the extern keyword do, and how does it differ from static at file scope?
An extern declaration without an initialiser declares an object that is defined elsewhere, so it allocates no storage and refers to that same object. A file-scope variable has external linkage by default. Marking it static gives it internal linkage, so other files cannot refer to it by name.
Recursion
Recursion notesWhat happens on the call stack when a recursive function runs, and why does it need a base case?
Each call gets its own activation record holding its parameters, locals and return address, stacked on the previous one. A base case returns without calling again, so the stack stops growing and then unwinds. Without it the calls never end and the stack overflows.
Why does a printf placed before the recursive call print values in the opposite order to one placed after it?
A statement before the call runs on the way in, as each call starts, so values appear in call order. A statement after the call runs on the way out, as calls return, which happens in reverse order because the last call finishes first. This is the last-in, first-out behaviour of the stack.
What is the difference between tail recursion and non-tail recursion?
In tail recursion the recursive call is the last action, so nothing remains to do when it returns, and a compiler may reuse the frame, though C does not guarantee this. In non-tail recursion work is pending after the call, as in n * f(n-1), so the stack grows with the depth.
How many moves does the standard recursive Tower of Hanoi solution make for 5 disks?
The recursion makes 31 moves for 5 disks. Moving n disks takes two moves of n-1 disks plus one move of the largest disk, so , which solves to .
WORKING
- Moving n disks needs two moves of n-1 disks and one move of the largest: .
- With the solution is .
- .
A naive recursive function has fib(0) = 0 and fib(1) = 1, and otherwise returns fib(n-1) + fib(n-2). How many calls in total does fib(5) make, counting the first call?
The call fib(5) makes 15 calls in total. Each call counts itself plus the calls made by its two sub-calls, so with , which gives 15.
WORKING
- Let be the number of calls: , with .
- , , .
- , matching .
More interview questions: Digital Electronics · Data Structures and Algorithms · Computer Organization and Architecture · Database Management and Warehousing · Operating Systems · Computer Networks
Who hires and how they select: IT jobs for CSE/IT
Programming in C: notes, formula sheet and past questions