C Programming Quiz: 50 Questions

Q1. Which file extension is used for C source files?
C source files typically use a single-letter extension.
Answer: .c — C source code files conventionally use the .c extension.
Q2. Which header is required for printf and scanf?
Standard I/O functions are declared here.
Answer: stdio.h — printf, scanf, getchar, putchar are declared in stdio.h.
Q3. Which command compiles program.c into an executable named program using GCC?
Use GCC with -o to name the output.
Answer: gcc program.c -o program — gcc compiles C source to an executable; -o sets the output filename.
Q4. Which of these is a valid single-line comment in modern C?
C99 and later accept this style.
Answer: // This is a comment — C99 and later accept // single-line comments; /* */ is multi-line comment syntax.
Q5. What is the correct signature of main that accepts command-line arguments?
argc counts arguments; argv holds them.
Answer: int main(int argc, char *argv[]) — provides access to command-line arguments; returning int is standard.
Q6. Which format specifier prints an integer with printf?
Integer specifier is a single letter d.
Answer: %d — %d formats int values; %f for float/double, %c for char, %s for strings.
Q7. Which header provides malloc and free?
Memory allocation functions are here.
Answer: stdlib.h — malloc, calloc, realloc, free are declared in stdlib.h.
Q8. Which type is used to store a single character?
Single-byte character type in C.
Answer: char — char stores character data; often 1 byte in typical implementations.
Q9. What does sizeof(int) commonly return on modern systems?
Commonly 4 bytes on modern platforms.
Answer: 4 — On most modern platforms sizeof(int) is 4 bytes, though it is implementation-defined.
Q10. Which operator gives the address of a variable?
Used to get a variable's memory address.
Answer: & — & returns the address of its operand; * is the dereference operator.
Q11. Which operator dereferences a pointer?
Used to access the value pointed to.
Answer: * — *p yields the value stored at the address in p.
Q12. How do you declare a pointer to int?
p;
Type followed by * then variable name.
Answer: int * — int *p; declares p as a pointer to int.
Q13. Which function copies strings (unsafe if destination too small)?
Copies until null terminator without bounds check.
Answer: strcpy() — strcpy(dest, src) copies the string including the terminating null; it can overflow if dest is too small.
Q14. Which function returns the length of a C string?
Counts characters before the null terminator.
Answer: strlen() — strlen(s) returns the number of characters before '\0'.
Q15. Which header declares string functions like strcpy, strlen?
String manipulation functions are grouped here.
Answer: string.h — string.h declares functions for C string handling.
Q16. What is the result of integer division 7 / 2 in C?
Integer division truncates toward zero.
Answer: 3 — 7 and 2 are ints, so 7/2 yields 3 (fraction discarded).
Q17. Which function allocates memory at runtime?
Function from stdlib.h returns void*.
Answer: malloc() — malloc(size) allocates memory on the heap; free releases it.
Q18. Which function releases dynamically allocated memory?
C uses free to deallocate heap memory.
Answer: free() — free(ptr) returns heap memory to the system; failing to free causes leaks.
Q19. Which operator has higher precedence: * (multiplication) or + (addition)?
Multiplication evaluated before addition.
Answer: * — Multiplicative operators have higher precedence than additive operators.
Q20. Which statement declares a typed constant integer PI with value 3?
Use const keyword for typed constants.
Answer: const int PI = 3; — const declares a typed constant; #define is a preprocessor macro (untyped).
Q21. Which preprocessor directive includes a header file?
Used to bring in declarations from headers.
Answer: #include — #include inserts header contents during preprocessing.
Q22. Which loop executes its body at least once?
Body runs before condition check.
Answer: do-while — do { ... } while(cond); executes the body once before evaluating cond.
Q23. Which statement skips the remainder of the loop body and continues with next iteration?
Skips remaining statements in the loop body.
Answer: continue — continue jumps to the next iteration; break exits the loop entirely.
Q24. Which prototype correctly declares a function add that returns int and takes two ints?
;
Return type, name, parameter types.
Answer: int add(int, int) — Prototype declares return type and parameter types: int add(int a, int b);
Q25. Which causes undefined behavior?
Accessing memory at address 0 is invalid.
Answer: Dereferencing a NULL pointer — leads to undefined behavior, often a crash (segmentation fault).
Q26. Which keyword at file scope gives internal linkage (visible only in that source file)?
Restricts visibility to the current translation unit.
Answer: static — static at file scope limits the symbol to that source file (internal linkage).
Q27. Which printf specifier prints a pointer value?
Pointer addresses use a dedicated specifier.
Answer: %p — %p prints a void* pointer in an implementation-defined format (usually hexadecimal).
Q28. Which function reads a line safely into buffer buf of size n?
Avoid gets due to buffer overflow risk.
Answer: fgets(buf, n, stdin) — reads at most n-1 chars and null-terminates; gets is unsafe and removed from modern standards.
Q29. Which header defines NULL macro (standard)?
NULL is defined in several headers; stddef.h is standard.
Answer: stddef.h — stddef.h defines NULL and other standard types/macros; stdio.h may also include it indirectly.
Q30. Which is a valid struct declaration?
Use struct keyword with braces.
Answer: struct Point { int x; int y; }; — defines a struct type named Point with two int members.
Q31. How do you access member y of struct Point p?
Dot for struct variables, arrow for pointers.
Answer: p.y — For a struct variable p, use p.member; p->member is for pointers to struct.
Q32. Which expression gives the size in bytes of type double?
sizeof is an operator in C.
Answer: sizeof(double) — sizeof yields the size in bytes of its operand type or expression.
Q33. Which function compares two strings lexicographically?
Returns negative, zero, or positive.
Answer: strcmp() — strcmp(a,b) returns <0, 0, >0 depending on lexicographic order.
Q34. Which is the correct way to declare an enum of colors RED, GREEN, BLUE?
Use enum keyword with braces and commas.
Answer: enum Color { RED, GREEN, BLUE }; — defines an enumeration type Color with three constants.
Q35. Which function converts a string to a double (with error reporting)?
strtod provides error reporting via endptr.
Answer: strtod() — strtod converts string to double; atof exists but strtod is preferred for error handling.
Q36. Which can cause a segmentation fault?
Out-of-bounds memory access is dangerous.
Answer: Accessing memory beyond allocated buffer — Buffer overflows and invalid pointer dereferences often cause segmentation faults.
Q37. Which fopen mode opens a file for reading?
FILE *f = fopen("data.txt", );
Mode "r" opens for reading.
Answer: "r" — fopen(filename, "r") opens a file for reading; returns NULL on failure.
Q38. Which function writes formatted output to a file stream?
Like printf but with a FILE* parameter.
Answer: fprintf() — fprintf(stream, format, ...) writes formatted data to the given file stream.
Q39. Which declaration defines a function pointer taking int and returning int?
fp;
Function pointer syntax uses (*name).
Answer: int (*)(int) — int (*fp)(int); declares fp as pointer to function taking int and returning int.
Q40. Which keyword declares an external variable defined in another file?
Used to reference variables defined elsewhere.
Answer: extern — extern int x; declares x exists in another translation unit.
Q41. Which function converts a string to an integer?
atoi returns int from numeric string.
Answer: atoi() — atoi("123") returns integer 123; strtol is preferred for error checking.
Q42. Which is true about unions?
Union size equals size of largest member.
Answer: All members share the same memory location — Union members overlap in memory; only one member is valid at a time.
Q43. Which function resizes previously allocated memory?
Adjusts size of an existing allocation.
Answer: realloc() — realloc(ptr, newsize) resizes the memory block, possibly moving it and returning new pointer.
Q44. Which function reads the next character from stdin as an int?
Returns EOF or unsigned char cast to int.
Answer: getchar() — getchar() reads a single character and returns it as an int (or EOF).
Q45. Which declares a 2D int array with 3 rows and 4 columns?
Rows first, then columns.
Answer: int a[3][4] — Declares an array with 3 rows and 4 columns of ints.
Q46. Which function concatenates at most n characters from src to dest?
Bounded version to avoid overflow (still requires care).
Answer: strncat() — strncat(dest, src, n) appends at most n chars from src; careful size calculations still needed.
Q47. Which is true about integer promotion in expressions?
Integer promotions occur in expressions.
Answer: Smaller integer types are promoted to int — Types like char and short are promoted to int (or unsigned int) in arithmetic expressions.
Q48. Which function compares memory blocks byte-by-byte?
Compares raw bytes for given length.
Answer: memcmp() — memcmp compares n bytes of two memory areas; strcmp is for null-terminated strings.
Q49. Which function copies n bytes from source to destination (undefined if overlap)?
Use memmove when regions may overlap.
Answer: memcpy() — memcpy copies n bytes from source to destination; memmove handles overlapping regions safely.
Q50. Which practice helps avoid memory leaks in C?
Match each malloc/calloc/realloc with a free when appropriate.
Answer: Always free dynamically allocated memory when no longer needed — Properly freeing memory prevents leaks; tools like valgrind help detect leaks.
Your Score: 0 / 50

Post a Comment

0 Comments