Continuing our focus on the C Test, here is a more generic 30-question test that’s challenging and fit for a 1-hour duration and requires a blend of conceptual, syntax, debugging, and problem-solving questions. The questions below are segmented into types and difficulty levels to cover a broad range of essential C programming topics. Most of these questions are intended to be short-answer or multiple-choice questions that test theoretical knowledge, code analysis, and quick coding skills, allowing candidates to work through them within the hour.
1. Basic Language Concepts (5 Questions)
- What is the difference between
malloc
andcalloc
?
(Answer briefly or with a short code snippet.) - How does the
const
keyword affect a pointer in C?
(Describe the difference betweenconst int *p
,int * const p
, andconst int * const p
.) - What is a segmentation fault, and why does it occur?
(Answer briefly.) - Explain the difference between stack and heap memory.
(Briefly describe use cases and differences in memory allocation.) - Why is
sizeof(char)
always 1 in C?
(Provide a brief explanation.)
2. Data Types and Pointers (5 Questions)
- What is the output of the following code?
int x = 10;
int *p = &x;
printf("%d\n", *p + x++);
- Given the following declaration, how would you access the value at the second element of the third row?
int matrix[4][5];
- What will be the output of the following code?
int a = 5, b = 10;
int *ptr = &b;
a = *ptr;
*ptr = 20;
printf("%d %d\n", a, b);
- Write a C function that swaps two integers using pointers without using a temporary variable.
- Explain the output of the following code:
c int a = 10; int *p1 = &a; int **p2 = &p1; printf("%d\n", **p2);
3. Strings and Arrays (5 Questions)
- What is wrong with the following string initialization?
char *str = "Hello, World!"; str[0] = 'h';
- Write a function that reverses a string in place. (This is a quick coding question; keep it short.)
- What will be the output of the following code?
char str[] = "Hello"; printf("%c\n", str[6]);
- How would you dynamically allocate a 2D array of integers in C? (Answer with a code snippet.)
- Explain what’s wrong with this code and how to fix it:
c char str1[10] = "Hello"; char str2[10] = "World"; strcat(str1, str2);
4. Memory Management (5 Questions)
- What happens if you free a pointer twice?
- Explain what is wrong with this code:
int *p = malloc(sizeof(int)); p = NULL; free(p);
- Write a function that allocates memory for an integer array of
n
elements and initializes them to zero. - How would you check for a memory leak in a C program? (Briefly describe a method.)
- What is the difference between
realloc
andmalloc
? (Brief answer.)
5. Structs and Unions (5 Questions)
- Write a
struct
definition to represent a 3D point withx
,y
, andz
coordinates. - What is a union, and how is it different from a struct?
- Explain what’s wrong with the following code:
struct Point { int x, y; } p; printf("%d\n", p.x);
- How would you dynamically allocate memory for an array of 10 structs in C?
- What will be the output of the following code?
c union Data { int i; float f; }; union Data d; d.i = 10; printf("%f\n", d.f);
6. Functions and Recursion (5 Questions)
- What will be the output of the following recursive function call
foo(3)
?int foo(int n) { if (n == 0) return 1; return n * foo(n - 1); }
- Explain why
void main()
is incorrect in C. - Write a function that calculates the nth Fibonacci number using recursion.
- What is the purpose of the
static
keyword in the context of a function? - Describe what’s wrong with the following recursive function and how you’d fix it:
c void recurse() { recurse(); }
Summary
- Total Questions: 30
- Estimated Completion Time: 1 hour
This format mixes theoretical questions, debugging, and small coding tasks to comprehensively assess the candidate’s knowledge in C.