Nearby lessons

122 of 124

C - Questions and Answers

Frequently asked C questions and answers — every MCQ from all 12 chapters with explanations, plus the common mistakes beginners make in each topic.

Chapter 1 — Quick Questions

Q: Who created the C language?

A: Dennis Ritchie — Dennis Ritchie created C at Bell Labs in the 1970s.

Q: Where does a C program start execution?

A: main() — Execution always starts from main().

Q: Which header file gives printf and scanf?

A: stdio.h — stdio.h (standard input-output) provides printf and scanf.

Q: Which symbol ends every C statement?

A: Semicolon — Every C statement ends with a semicolon ;

Q: The compiler converts your .c file into ___?

A: .obj/.o object file — The compiler produces object code (.obj or .o).

Q: What does \n inside printf do?

A: New line — \n moves the cursor to a new line.

Q: What does %d do in printf?

A: Prints an integer — %d is the format specifier for an integer.

Q: C is mainly used for which type of software?

A: System + application software — C is used for both system and application software.

Chapter 1 — Common Mistakes

Common Mistakes Beginners Make:
  • Forgetting the ; at the end of a statement — the most common C error.
  • Writing Main or MAIN instead of main — C is case-sensitive.
  • Missing #include and then wondering why printf is unknown.
  • Forgetting that every opening { needs a matching closing }.

Chapter 2 — Quick Questions

Q: Which of these is a valid variable name in C?

A: _total — _total is valid; the others start with a digit, have a space, or are keywords.

Q: Which data type stores one single character?

A: char — char stores one character and takes 1 byte.

Q: 'A' (in single quotes) is what kind of constant?

A: Character — Single quotes = character constant.

Q: Which keyword CANNOT be used as a variable name?

A: if — if is a keyword of C.

Q: What does declaration mean?

A: Telling the type and name — Declaration states the type and name of a variable.

Q: Which data type is best for very accurate decimal calculations?

A: double — double has more precision (8 bytes) than float.

Q: Which operator tells the memory size of a type?

A: sizeof — sizeof returns the size in bytes.

Q: C is case sensitive, so sum and SUM are ___?

A: Different names — C treats them as two different identifiers.

Chapter 2 — Common Mistakes

Common Mistakes Beginners Make:
  • Starting a name with a digit — 9marks is invalid; marks9 is valid.
  • Putting a space or a symbol like # inside a name — use _ instead.
  • Using a keyword as a name — int, if, for are reserved by C.
  • Writing a character constant with double quotes ("A") or a string with single quotes ('Hello') — single quotes for one character, double quotes for text.

Chapter 3 — Quick Questions

Q: What is the result of 7 % 3?

A: 1 — 7 % 3 = 1 (the remainder of 7 divided by 3).

Q: What is the result of 7 / 2 in C?

A: 3 — Integer division gives 3 — the decimal part is dropped.

Q: Which operator checks equality?

A: == — == compares two values; = assigns.

Q: a += 5; is the same as ___?

A: a = a + 5; — += adds the value and assigns: a = a + 5.

Q: int a=10; printf("%d", a++); prints ___?

A: 10 — Post-increment uses the old value (10) first.

Q: Which operator returns the remainder?

A: % — % gives the remainder of division.

Q: The ternary operator `c ? a : b` picks ___?

A: a if c is true — It picks a when the condition is true, b otherwise.

Q: In `2 + 3 * 4`, which runs first?

A: Multiplication — Multiplication has higher precedence than addition.

Chapter 3 — Common Mistakes

Common Mistakes Beginners Make:
  • Using = instead of == in a condition — if (a = 10) assigns 10 instead of comparing.
  • Forgetting that 5 / 2 is 2, not 2.5 — integer division drops the decimal.
  • Writing && as & or || as | in conditions — one symbol means *and* (logical), two is correct, one is bitwise.
  • Thinking a++ and ++a are always the same — they differ when the value is used in the same statement.
  • Forgetting the brackets around a condition: a > b ? a : b must be written with a space clearly, or as (a > b) ? a : b.

Chapter 4 — Quick Questions

Q: Which format specifier prints an integer?

A: %d — %d prints an integer.

Q: Which specifier prints a float with exactly 2 decimals?

A: %.2f — %.2f rounds the float to 2 decimal places.

Q: What does \n do?

A: New line — \n moves to a new line.

Q: In scanf("%d", &age), what does & do?

A: Gives the address of age — & is the address-of operator, telling scanf where to store the value.

Q: Which function reads one character from the keyboard?

A: getchar — getchar() reads a single character.

Q: Which escape sequence prints a backslash?

A: \\ — \\ prints one backslash.

Q: Which of these is a Windows-only input function?

A: getch — getch/getche are from conio.h and work only on Windows.

Q: %s is used to print ___?

A: A string — %s prints a string (text).

Chapter 4 — Common Mistakes

Common Mistakes Beginners Make:
  • Forgetting the & before a variable in scanf — the program compiles but gives a garbage value or crashes.
  • Putting & before a string in scanf — %s does NOT need it.
  • Mismatching specifier and value: printf("%d", 3.14) prints a garbage integer.
  • Typing \n as /n or forgetting the backslash — the new line will not work.
  • Forgetting #include — printf and scanf become unknown names.

Chapter 5 — Quick Questions

Q: Which statement runs a block when a condition is true?

A: if — if executes a block based on a condition.

Q: In if-else, which block runs when the condition is false?

A: else block — The else block runs when the condition is false.

Q: Which statement is used for a menu with many fixed choices?

A: switch — switch is best for fixed-value menus.

Q: What does break do in a switch?

A: Stops falling into the next case — break prevents fall-through to the next case.

Q: switch works with which data types?

A: int and char — switch supports integer and character expressions.

Q: An if inside another if is called ___?

A: Nested if — An if inside an if is a nested if.

Q: The default case in switch runs when ___?

A: When no case matches — default runs when no case matches.

Q: Which is a one-line replacement for a simple if-else?

A: ? : (ternary) — The conditional operator ? : is a one-line if-else.

Chapter 5 — Common Mistakes

Common Mistakes Beginners Make:
  • Forgetting break in switch — the program falls through and runs the next case too.
  • Using = instead of == inside a condition — if (marks = 40) always assigns and is always true.
  • Writing a semi-colon right after if (condition); — the block then runs *unconditionally*.
  • Using switch with a float or a string — switch accepts only int and char.
  • Forgetting the { } when a block has more than one statement — only the first line belongs to the if.

Chapter 6 — Quick Questions

Q: Which loop checks the condition BEFORE running the body?

A: while — while checks first; do-while checks after.

Q: Which loop's body runs at least once even if the condition is false?

A: do-while — do-while runs the body first, so it runs at least once.

Q: In `for (i=1; i<=5; i++)`, how many times does the body run?

A: 5 — i runs from 1 to 5 → five times.

Q: Which statement immediately stops a loop?

A: break — break stops the loop completely.

Q: Which statement skips the current round and moves to the next?

A: continue — continue skips the rest of the current iteration.

Q: In a nested loop, for each outer round the inner loop ___?

A: Runs completely — The inner loop runs fully for every outer round.

Q: What happens if you forget the update statement (i++) in a while loop?

A: Infinite loop — Without an update, the condition never becomes false → infinite loop.

Q: Which loop is best when you know exactly how many times to repeat?

A: for — for is designed for a known number of iterations.

Chapter 6 — Common Mistakes

Common Mistakes Beginners Make:
  • Forgetting the update i++ — the condition never becomes false and the loop runs forever (infinite loop).
  • Using ; right after while (condition); — the body then never runs as part of the loop.
  • Writing = instead of == inside the loop condition.
  • Using break when you meant continue (or vice versa) — one leaves, the other skips.
  • In a nested loop, forgetting which loop to break — break leaves only the *inner* loop.

Chapter 7 — Quick Questions

Q: What is the first index of an array?

A: 0 — Array indexes start from 0.

Q: int a[5]; has indexes from ___?

A: 0 to 4 — Size 5 → indexes 0,1,2,3,4.

Q: int a[5] = {1, 2}; leaves the remaining elements as ___?

A: 0 — Remaining elements are initialized to 0.

Q: Which is the correct declaration of a 2D array?

A: int a[2][3] — Two sets of brackets: int a[2][3].

Q: To process a 2D array you use ___?

A: Nested loops — Nested loops go row by row, column by column.

Q: matrix[1][2] means ___?

A: Row 1, column 2 — The first index is the row, the second is the column.

Q: In a linear search, which value is used as a 'not found' marker?

A: -1 — -1 can never be a valid index, so it marks not-found.

Q: Which is the correct way to find the maximum in an array?

A: Compare each element with a running max — Keep a running max and compare every element.

Chapter 7 — Common Mistakes

Common Mistakes Beginners Make:
  • Starting the index from 1 — for (i = 1; i <= 5; i++) skips a[0] and reads past a[4].
  • Using a[5] in a 5-element array — valid indexes are 0 to 4 only.
  • Forgetting that int a[5] = {1, 2}; fills the rest with 0 — not garbage.
  • Forgetting & inside scanf when reading an array element: scanf("%d", &marks[i]).
  • Declaring marks[5] but looping i < 5 with i starting at 1 — you lose the first element.

Chapter 8 — Quick Questions

Q: How does C know where a string ends?

A: The null character '\0' — The '\0' null character marks the end of a string.

Q: char s[] = "Rahul"; needs at least how many bytes?

A: 6 — 5 characters + 1 for '\0' = 6.

Q: Which function reads a whole line including spaces safely?

A: fgets — fgets(name, size, stdin) is the safe line reader.

Q: strlen("Hello") returns ___?

A: 5 — strlen counts characters before '\0' → 5.

Q: Which function copies one string into another?

A: strcpy — strcpy(dest, src) copies.

Q: How do you compare two strings for equality?

A: strcmp(a, b) == 0 — strcmp returns 0 when strings are equal.

Q: Which character is '\0' called?

A: Null character — '\0' is the null character that ends strings.

Q: scanf("%s", s) with input 'Rahul Kumar' stores ___?

A: Rahul — %s stops at the first space, so it stores only 'Rahul'.

Chapter 8 — Common Mistakes

Common Mistakes Beginners Make:
  • Forgetting the null character — char name[5] = "Rahul"; overflows, because 5 chars + \0 needs 6.
  • Mixing 'A' (character) and "A" (string with \0) — they are stored differently.
  • Using == or = on strings — a == b compares addresses, b = a does not copy text.
  • Reading names with scanf("%s") — spaces break the input; use fgets.
  • Forgetting #include — then strlen, strcpy are unknown.

Chapter 9 — Quick Questions

Q: Which keyword ends a function and sends a value back?

A: return — return sends a value back and ends the function.

Q: A function with no arguments and no return value has return type ___?

A: void — void means the function returns nothing.

Q: In call by value, the function receives ___?

A: A copy of the value — C passes a copy, so the original is unchanged.

Q: What is the base case in recursion?

A: The condition that stops the recursion — The base case stops the recursion.

Q: factorial(5) returns ___?

A: 120 — 5! = 5×4×3×2×1 = 120.

Q: Which part of a function comes BEFORE main()?

A: The declaration — The declaration (prototype) tells C about the function before main.

Q: Where is the actual code of a function written?

A: In the definition — The definition holds the body of the function.

Q: Fibonacci series starts with ___?

A: 0, 1 — Fibonacci starts 0, 1, 1, 2, 3...

Chapter 9 — Common Mistakes

Common Mistakes Beginners Make:
  • Forgetting the declaration before main() — C assumes an int return and warns or errors.
  • Writing return; in an int function — the function must return a value.
  • Passing the wrong type or count of arguments — add(5) when add needs two values.
  • Forgetting the base case in recursion — the function calls itself forever and the program crashes (stack overflow).
  • Calling a void function and expecting a value — int x = welcome(); is wrong.

Chapter 10 — Quick Questions

Q: Which operator accesses a structure member?

A: . — The dot operator (.) accesses members.

Q: What does `struct student s[10];` create?

A: An array of 10 student structures — It declares an array of 10 student records.

Q: The main difference between structure and union is ___?

A: Union shares one memory for all members — Union members share the same memory space.

Q: sizeof(union) equals ___?

A: The largest member — The union takes the size of its largest member.

Q: How do you reach the 'pin' of a nested address inside a student?

A: s.addr.pin — Go level by level: s.addr.pin.

Q: A structure definition ends with ___?

A: A semicolon — The struct definition ends with a semicolon.

Q: Which of these is valid member access?

A: s.rollNo — The dot operator: s.rollNo.

Q: Why do we use structures?

A: To group different types under one name — Structures group mixed-type fields into one record.

Chapter 10 — Common Mistakes

Common Mistakes Beginners Make:
  • Forgetting the ; after the struct definition closing brace — a very common error.
  • Using -> when there is no pointer — the dot (.) is for normal variables, -> is for pointers (Chapter 11).
  • Writing scanf("%d", s.rollNo) — forgetting & before a member that is not a string.
  • Reading a name with %s when it has spaces — use a loop with fgets for full names.
  • Declaring struct student s; but then writing student s; — the keyword struct is still needed in C.

Chapter 11 — Quick Questions

Q: A pointer is a variable that stores ___?

A: An address — A pointer stores the memory address of another variable.

Q: Which operator gives the address of a variable?

A: & — & is the address-of operator.

Q: If p points to a and *p = 50 runs, then a becomes ___?

A: 50 — *p changes the value at a's address, so a becomes 50.

Q: The name of an array (int a[5]) is the address of ___?

A: The first element — The array name is the address of its first element.

Q: In a swap function using call by reference, you pass ___?

A: Addresses (&) — You pass &a, &b so the function can change the originals.

Q: *(p + 2) on an int pointer reaches ___?

A: The element at index 2 — *(p+2) reaches the third element (index 2).

Q: What should a pointer hold to be safe before use?

A: NULL — NULL means the pointer points nowhere; check before dereferencing.

Q: Why can't call-by-value change the original variable?

A: It receives only a copy — The function works on a copy, leaving the original untouched.

Chapter 11 — Common Mistakes

Common Mistakes Beginners Make:
  • Forgetting the * in the declaration — int p; is a plain integer, not a pointer.
  • Using *p without ever giving p an address — dereferencing an uninitialised pointer is a crash.
  • Mixing up p = &a (store the address) with *p = a (store a value into the pointed-to location).
  • Declaring int *p; but writing int* p, q; — only p is a pointer, q is a plain int.
  • Forgetting the & when passing to a swap function: swap(a, b) passes copies — nothing changes.

Chapter 12 — Quick Questions

Q: Which function opens a file?

A: fopen() — fopen(name, mode) opens a file.

Q: Which mode opens a file for writing and ERASES old content?

A: w — "w" creates new or erases the old content.

Q: Which mode adds new content at the end without erasing?

A: a — "a" (append) keeps old content and adds at the end.

Q: Which function writes formatted data to a file?

A: fprintf — fprintf works like printf but writes to the file.

Q: What does fscanf return at the end of the file?

A: EOF (-1) — fscanf returns EOF (-1) when the file ends.

Q: Which function reads a whole line from a file safely?

A: fgets — fgets(line, size, fp) reads a whole line safely.

Q: Which function closes a file?

A: fclose() — fclose(fp) closes the file.

Q: Before using a file, what should you always check?

A: fp == NULL — Always check that fopen did not return NULL.

Chapter 12 — Common Mistakes

Common Mistakes Beginners Make:
  • Forgetting to check fp == NULL after fopen — then reading/writing a NULL file pointer crashes.
  • Opening with "w" when you meant "a" — "w" silently erases all old content.
  • Forgetting fclose(fp) — changes may not reach the disk and the file stays locked.
  • Reading with fscanf %s when the data has spaces — %s stops at the first space.
  • Writing fopen("data.txt", w) — the mode is a string, so it needs quotes: "w".
📝 Key Takeaways
  • Each Q&A comes straight from the chapter material
  • Common mistakes are the exact errors beginners make
  • Practise the MCQs, then try the full quiz page