C Programming MCQs: 12 solved multi-topic questions with explanations

Attempt 12 mixed C questions before checking the answers. Each short solution shows the rule, calculation or definition that decides the result.

KnowledgeGate Team

Exam prep & CS education

Updated 16 Aug 20267 min read

C output-prediction and preprocessor questions appear across GATE, UGC NET, teaching-recruitment papers and company placement screens. The same traps keep costing marks: missing macro parentheses, operator precedence, default enum values and memory functions that look similar.

Attempt each question before reading its explanation, and note which block a miss falls in: macros and the preprocessor, then operators, precedence and enum values, then types, memory allocation and file handling, then errors and type checking. Where you miss, the C Programming Course rebuilds that part of C from the ground up.

Macros and the C preprocessor

The preprocessor performs text substitution before the compiler processes the result. Most macro mistakes are therefore caused by precedence, missing parentheses or token pasting, not by the intended logic. C Preprocessor and Macros: #define Traps works through those expansion rules line by line if the next three questions go badly.

Q1. What is the output of the following program? (UGC NET 2023)

#include<stdio.h>
#define SQR(x) (x*x)
int main(){ int a, b = 3; a = SQR(b + 2); printf("%d", a); return 0; }
  • (a) 25

  • (b) 11

  • (c) Garbage value

  • (d) 24

Answer: (b) 11.

Text substitution expands SQR(b + 2) to (b + 2*b + 2), not ((b+2)*(b+2)), because the macro does not put parentheses around each use of its parameter. With b = 3, the result is 3 + 2*3 + 2 = 3 + 6 + 2 = 11. The safer definition #define SQR(x) ((x)*(x)) would produce (3+2)*(3+2) = 25.

Q2. Predict the output.

#include<stdio.h>
#define f(g,g2) g##g2
int main(){ int var12 = 100; printf("%d", f(var,12)); return 0; }
  • (a) 100

  • (b) 12

  • (c) error

  • (d) 0

Answer: (a) 100.

The ## token-pasting operator joins the two arguments into one identifier. Thus, f(var,12) becomes var12. That variable stores 100, so the program prints 100.

Q3. Consider #define hypotenuse (a, b) sqrt (a*a+b*b); The macro call hypotenuse(a+2, b+3); (ISRO 2015)

  • (a) Finds the hypotenuse of a triangle with sides a+2 and b+3

  • (b) Finds the square root of (a+2)^2 and (b+3)^2

  • (c) Is invalid

  • (d) Find the square root of 3*a + 4*b + 5

Answer: (d).

Without parentheses around the parameters, substituting a+2 into a*a produces a + 2*a + 2 = 3a + 2. Similarly, substituting b+3 into b*b produces b + 3*b + 3 = 4b + 3. Their sum is 3a + 4b + 5, so the resulting expression is sqrt(3a + 4b + 5). One caution about the definition as printed: the space between hypotenuse and (a, b) makes it an object-like macro, which would not compile as a call at all. The question intends the function-like reading, and that is the lesson worth keeping: a parameter that is not parenthesised inside the macro body is pasted in as raw text.

Side-by-side macro expansion: without parentheses the call expands so that b = 3 gives 3 + 6 + 2 = 11, while the fully parenthesised macro gives 5 times 5 = 25.

Output prediction: operators, precedence and enum values

Many output questions are precedence questions in disguise. Identify the operator that binds first, add parentheses to show that order, and only then calculate.

Q4. What does this print? (NAT question, type the number)

#include <stdio.h>
int main(){ int a = 4, b = 2, c = 3; printf("%d", a << b + c); return 0; }

Answer: 128.

Addition has higher precedence than the left-shift operator, so the expression is a << (b + c) = 4 << 5. A left shift by n multiplies by 2^n, giving 4 * 2^5 = 4 * 32 = 128. Shifting first would wrongly give (4 << 2) + 3 = 16 + 3 = 19. This is a NAT item, so there are no options to rescue a guessed precedence rule.

Q5. What is the output?

int main(){ enum status { pass, fail, atkt }; enum status stud1, stud2, stud3;
  stud1 = pass; stud2 = atkt; stud3 = fail;
  printf("%d, %d, %d\n", stud1, stud2, stud3); return 0; }
  • (a) 0, 1, 2

  • (b) 1, 2, 3

  • (c) 0, 2, 1

  • (d) 1, 3, 2

Answer: (c) 0, 2, 1.

Enum constants start at 0 by default, so pass = 0, fail = 1 and atkt = 2. Follow the assignments in the printf order: stud1 is 0, stud2 is 2 and stud3 is 1.

Q6. Predict the output.

#include "stdio.h"
int main(){ int x, y = 5, z = 5; x = y == z; printf("%d", x); return 0; }
  • (a) 0

  • (b) 1

  • (c) 5

  • (d) Compiler Error

Answer: (b) 1.

y == z compares two values. Since both are 5, the comparison is true, which C represents as integer 1, and that value is assigned to x. Do not confuse the comparison operator == with the assignment operator =.

Three-step flow for a << b + c with a = 4, b = 2, c = 3: b + c gives 5, shifting 4 left by five places gives 128, and shifting first would wrongly give 19.

Data types, memory allocation and file handling

These questions reward exact definitions. Learn what each type or library function guarantees instead of choosing the option that merely sounds familiar.

Q7. Which of the following is a user-defined data type?

  • (a) long int

  • (b) double

  • (c) unsigned long int

  • (d) enum

Answer: (d) enum.

long int, double and unsigned long int are built-in types. An enum lets the programmer define a type whose named constants have integer values, so it is the user-defined type in this list.

Q8. Which function reserves memory and sets all allocated bytes to zero? (UPLT 2026)

  • (a) realloc()

  • (b) free()

  • (c) calloc()

  • (d) malloc()

Answer: (c) calloc().

calloc() allocates a block and sets all its bytes to zero. malloc() allocates without that zero-initialisation, realloc() changes the size of an existing allocation, and free() releases allocated memory.

Q9. Arrange the steps of file handling in C in the correct order. (UGC NET 2024)

(A) Close the file (B) Read from or write to the file (C) Open the file (D) Check for error

  • (a) (A), (C), (D), (B)

  • (b) (D), (B), (C), (A)

  • (c) (C), (D), (B), (A)

  • (d) (B), (D), (A), (C)

Answer: (c) (C), (D), (B), (A).

First open the file (C), then check whether the operation succeeded (D). Only after that should the program read or write (B). Finally, close the file (A) to flush buffered output and release the handle.

Errors, type checking and terminology

In this group, one precise term decides each answer. Read the full definition and watch for a stem that asks for the incorrect statement.

Q10. ______ errors occur when the code executes without errors but the output is not what the programmer intended. (RPSC 2024)

  • (a) Compilation

  • (b) Logical

  • (c) Syntax

  • (d) Semantic

Answer: (b) Logical.

A logical error allows the program to compile and run, but its reasoning produces the wrong result. The compiler usually stays silent because the code is syntactically valid.

Q11. What is the term for a condition during program execution that disrupts the normal flow of instructions? (Bihar STET 2025)

  • (a) Abstraction

  • (b) Inheritance

  • (c) Exception

  • (d) Encapsulation

Answer: (c) Exception.

An exception is a runtime condition that interrupts normal instruction flow. Abstraction, inheritance and encapsulation are design concepts, not names for such a runtime event.

Q12. Which comparison between static and dynamic type checking is INCORRECT? (ISRO 2018)

  • (a) Dynamic type checking slows down the execution

  • (b) Dynamic type checking offers more flexibility to the programmers

  • (c) In contrast to static type checking, dynamic type checking may cause failure in runtime due to type errors

  • (d) Unlike static type checking, dynamic type checking is done during compilation

Answer: (d).

Dynamic type checking happens at runtime, not during compilation, so statement (d) is incorrect. The other statements correctly describe its runtime cost, flexibility and possibility of runtime type failures. When a stem asks for the incorrect option, mark that reversal before evaluating the choices.

The short version and where to go next

Map your misses before solving another random set. Q1 to Q3 test macros and the preprocessor, Q4 to Q6 cover shift precedence, default enum values and the comparison operator, Q7 to Q9 cover types, memory and files, and Q10 to Q12 test errors and type checking. A score below 10 usually points to a concept gap in one of those blocks, so rebuild that concept and attempt this same set again after a week.

For full placement preparation, the Coding for Placements course develops the same C, C++, Java and Python patterns used in screening rounds. Solved sets on operating systems, DBMS, networks, algorithms and digital electronics sit alongside this one in MCQ Practice.

Q4 carried no options because it was a numerical answer type. MCQ, MSQ or NAT? GATE question types explained sets out how each format is marked and where a guess still pays. The goal is not to remember these 12 answers. It is to make the reasoning automatic.