Skip to content

C foundation learning review

Lesson 6 of 1316 minPDF notesFree

Trace each program using its current integer values. For a call, keep the caller’s variables separate from the helper’s parameters and locals. For a loop, include the test that finally fails. Write a short state trace alongside your answer so that you can check the reasoning afterward.

This is a free, untimed review of the five C foundation lessons. It has 12 questions worth 1 mark each, for 12 marks in total. A correct answer earns +1; a wrong or unattempted answer earns 0. There are no negative or partial marks and no pass threshold. It covers this bounded C foundation, not the full GATE CS syllabus or a full-length GATE paper.

  • MCQ: choose exactly one option.
  • MSQ: choose every correct option. The selected set must match the answer exactly; a missing correct option or an extra incorrect option earns 0. The order in which you select options does not matter.
  • NAT: enter the requested integer. These three questions have exact integer answers.
  • Check the solutions after attempting the questions. Revisit the relevant state trace and retry without a timer; the score is learning feedback, not a qualification prediction.

Use C11 for all questions. Every displayed code block is a complete, independent program: start again with its own initializers. All computed values here fit the portable signed int range -32767 through 32767. Every evaluated division has a nonzero divisor and a representable quotient. Calls have typed definitions before use, and each helper returns an integer on every reached path. No particular int width is assumed. The values printed by %d are integers, and \n ends the output line.

Questions

Question 1 · MCQ · 1 mark

What pair is printed? Choose exactly one option.

C
#include <stdio.h>

int main(void) {
    int a=5,b=8;
    a=b-a;
    b=b+a;
    printf("%d %d\n",a,b);
    return 0;
}
  • A. 3 11
  • B. 3 8
  • C. 5 13
  • D. 3 3

Question 2 · NAT · 1 mark

Enter the integer printed.

C
#include <stdio.h>

int main(void) {
    int total=58,group=9;
    int leftover=total-group*(total/group);
    printf("%d\n",leftover);
    return 0;
}

Question 3 · MCQ · 1 mark

What triple is printed in the order n, left, right? Choose exactly one option.

C
#include <stdio.h>

int main(void) {
    int n=2;
    int left=n++;
    int right=++n;
    printf("%d %d %d\n",n,left,right);
    return 0;
}
  • A. 4 3 4
  • B. 3 2 3
  • C. 4 2 4
  • D. 4 2 3

Question 4 · MSQ · 1 mark

For int p=-17 and q=5, select every true statement.

C
#include <stdio.h>

int main(void) {
    int p=-17,q=5;
    printf("%d %d %d\n",p/q,p%q,(p/q)*q+p%q);
    return 0;
}
  • A. p/q equals -3
  • B. p%q equals 3
  • C. (p/q)*q+p%q equals p
  • D. p/q equals -4

Question 5 · MCQ · 1 mark

What value of tag is printed? Choose exactly one option.

C
#include <stdio.h>

int main(void) {
    int c=7,tag=0;
    if(c<5){tag=1;}else if(c%2==1){tag=2;}else{tag=3;}
    printf("%d\n",tag);
    return 0;
}
  • A. 1
  • B. 2
  • C. 3
  • D. 0

Question 6 · MSQ · 1 mark

For int a=0 and b=6, which expressions have value 1? Select every correct option.

C
#include <stdio.h>

int main(void) {
    int a=0,b=6;
    printf("%d %d %d %d\n",!a,a||(b>4),a&&(b>0),!(b==6));
    return 0;
}
  • A. !a
  • B. a || (b > 4)
  • C. a && (b > 0)
  • D. !(b == 6)

Question 7 · NAT · 1 mark

Enter the number of body executions.

C
#include <stdio.h>

int main(void) {
    int count=0;
    for(int k=3;k<=12;k+=3){count++;}
    printf("%d\n",count);
    return 0;
}

Question 8 · MCQ · 1 mark

What integer is printed? Trace each division and remainder. Choose exactly one option.

C
#include <stdio.h>

int main(void) {
    int n=19,sum=0;
    while(n>0){sum+=n%3;n/=3;}
    printf("%d\n",sum);
    return 0;
}
  • A. 4
  • B. 6
  • C. 19
  • D. 3

Question 9 · MSQ · 1 mark

Select every true statement about this for loop. Count the final failed test.

C
#include <stdio.h>

int main(void) {
    int sum=0,i;
    for(i=0;i<4;i++){sum+=2*i;}
    printf("%d %d\n",sum,i);
    return 0;
}
  • A. The body runs 4 times
  • B. The condition is tested 4 times
  • C. The final sum is 12
  • D. The update i++ runs 4 times

Question 10 · MCQ · 1 mark

What pair is printed? Choose exactly one option.

C
#include <stdio.h>

int bump(int x){x+=3;return x;}

int main(void) {
    int v=4;
    int result=bump(v);
    printf("%d %d\n",v,result);
    return 0;
}
  • A. 7 7
  • B. 4 7
  • C. 4 4
  • D. 7 4

Question 11 · NAT · 1 mark

Enter the final printed integer.

C
#include <stdio.h>

int accumulate(int n){int s=1;for(int k=1;k<=n;k++){s+=2*k;}return s;}

int main(void) {
    int r=accumulate(3);
    int t=accumulate(1);
    printf("%d\n",r-t);
    return 0;
}

Question 12 · MSQ · 1 mark

Use only inputs -3 through 3. Select every true statement about choose.

C
#include <stdio.h>

int choose(int n){if(n<0){return -n;}return n+1;}

int main(void) {
    int a=choose(-3);
    int b=choose(0);
    int c=choose(2);
    int value=-2;
    int result=choose(value);
    printf("%d %d %d %d %d\n",a,b,c,value,result);
    return 0;
}
  • A. choose(-3) returns 3
  • B. choose(0) returns 0
  • C. choose(2) returns 3
  • D. Calling choose(value) does not change the caller's int variable value

Answers and full solutions

Use the reasoning below to compare states, not just final numbers.

Question 1

Answer: A

  1. Initially a = 5, b = 8.
  2. a = b - a reads the current 8 and 5, computes 3, and stores it in a. State: a = 3, b = 8.
  3. b = b + a now reads 8 and the updated 3, so b = 11.
  4. The output order is a, b, giving 3 11.

Option checks

  • A: Correct. Both assignments are applied in order.
  • B: Incorrect. 3 8 stops after the first assignment and ignores the update to b.
  • C: Incorrect. 5 13 retains the old a = 5 and uses it in the second assignment, although a has already become 3.
  • D: Incorrect. b = b + a adds 8 and 3; it does not copy a into b.

Question 2

Answer: 4

  1. Both operands of total / group are integers: 58 / 9 gives 6 whole groups.
  2. Multiply the group size by that quotient: 9 * 6 = 54.
  3. Subtract the grouped items from the original total: 58 - 54 = 4.
  4. Thus leftover = 4, and the required integer is 4. Check: 6 * 9 + 4 = 58, with both 0 <= 4 and 4 < 9.

6 is the quotient, not the leftover. 54 is the number placed into complete groups. 0 would result from treating the division as an exact real-number quotient; C integer division has already discarded the fractional part before multiplication.

Question 3

Answer: C

  1. Start with n = 2.
  2. left = n++ uses the old value 2 as the expression value, so left = 2; by the end of this statement n = 3.
  3. right = ++n first increases n from 3 to 4, then supplies 4, so right = 4.
  4. The separate statements are complete before printf. The requested order n, left, right gives 4 2 4.

Option checks

  • A: Incorrect. left = 3 treats postfix n++ as if it supplied the incremented value.
  • B: Incorrect. There are two executed increments, so n does not stop at 3; right receives 4.
  • C: Correct. The saved values are 2 and 4, while the final n is 4.
  • D: Incorrect. right = 3 treats prefix ++n as if it supplied the old value.

Question 4

Answer: A, C

  1. The mathematical quotient is -3.4. C11 integer division discards the fractional part toward zero, giving p / q = -3.
  2. Use the reconstruction identity: -17 = (-3) * 5 + r, so r = -2. Thus p % q = -2.
  3. Substituting the two C results gives (p / q) * q + p % q = (-3) * 5 + (-2) = -17, which equals p.
  4. The selected set must therefore be exactly A and C.

Option checks

  • A: True. Truncating -3.4 toward zero gives -3.
  • B: False. A positive remainder 3 would go with quotient -4, but that is not C11’s quotient here. The C remainder is -2.
  • C: True. The divisor is nonzero and the quotient is representable, so the stated identity applies and gives -17.
  • D: False. -4 rounds downward toward negative infinity; C11 integer division truncates toward zero.

Question 5

Answer: B

  1. Initially c = 7, tag = 0.
  2. The first test c < 5 is false, so tag = 1 is skipped.
  3. Next, c % 2 is 1, so c % 2 == 1 is true. Execute tag = 2.
  4. The final else is skipped because the preceding branch was selected. The output is 2.

Option checks

  • A: Incorrect. The assignment of 1 is in a branch whose test is false.
  • B: Correct. The odd-number test is the selected branch.
  • C: Incorrect. The final else does not execute after a true else if branch.
  • D: Incorrect. The initial 0 is replaced by the executed tag = 2 assignment.

Question 6

Answer: A, B

  1. a is 0, so it is false in a logical test. b is 6.
  2. !a is !0, which is 1.
  3. For a || (b > 4), the left operand is false, so the right operand is evaluated. 6 > 4 is true, and logical OR returns 1.
  4. For a && (b > 0), the left operand is false, so the right operand is skipped and logical AND returns 0.
  5. For !(b == 6), the equality is true and has value 1; negating it gives 0. Exactly A and B have value 1.

Option checks

  • A: True. Logical negation turns zero into 1.
  • B: True. At least one operand is true, so || returns 1, not the variable b’s value 6.
  • C: False. A false left operand makes && return 0 without evaluating the right operand.
  • D: False. The equality b == 6 is true, so its negation is 0.

Question 7

Answer: 4

  1. Initialization sets count = 0 and the loop’s k = 3.
  2. At k = 3, the test is true: count becomes 1; update gives k = 6.
  3. At k = 6, the test is true: count becomes 2; update gives k = 9.
  4. At k = 9, the test is true: count becomes 3; update gives k = 12.
  5. At k = 12, the test is still true because the bound is inclusive: count becomes 4; update gives k = 15.
  6. At k = 15, the test is false. Four bodies and four updates have executed, with five condition tests. The requested body count is 4.

3 omits the included endpoint 12. 5 counts the final failed condition as if a body ran there. 10 counts every integer from 3 to 12 instead of the visited values separated by 3.

Question 8

Answer: D

  1. Initially n = 19, sum = 0; 19 > 0 is true.
  2. First body: 19 % 3 = 1, so sum = 1; then n /= 3 stores 19 / 3 = 6.
  3. Second test is 6 > 0, true. 6 % 3 = 0, so sum stays 1; integer division sets n = 2.
  4. Third test is 2 > 0, true. 2 % 3 = 2, so sum = 3; integer division sets n = 0.
  5. The test 0 > 0 is false, so the loop ends. Three bodies and four tests occurred; the printed quantity is the remainder sum 3.

Option checks

  • A: Incorrect. 4 is the number of condition tests, not the sum of the remainders. The final failed test adds nothing.
  • B: Incorrect. 6 is the first updated value of n, not the final value printed from sum.
  • C: Incorrect. The starting 19 is not added to sum; only n % 3 is added in each body.
  • D: Correct. The added remainders are 1, 0 and 2, totaling 3.

Question 9

Answer: A, C, D

  1. Start with sum = 0, then initialize i = 0.
  2. At i = 0, the test 0 < 4 is true. Add 2 * 0 = 0, keeping sum = 0; update to i = 1.
  3. At i = 1, the test 1 < 4 is true. Add 2, so sum = 2; update to i = 2.
  4. At i = 2, the test 2 < 4 is true. Add 4, so sum = 6; update to i = 3.
  5. At i = 3, the test 3 < 4 is true. Add 6, so sum = 12; update to i = 4.
  6. At i = 4, the test 4 < 4 is false. There is no fifth body or fifth update. Body count is 4, update count is 4, test count is 5. The output is 12 4; the true statements are A, C and D.

Option checks

  • A: True. The body runs at i = 0, 1, 2, 3.
  • B: False. There are four successful tests and one final false test, making five.
  • C: True. The accumulator contains 0 + 2 + 4 + 6 = 12.
  • D: True. The update follows each of the four completed bodies, including the one that changes i from 3 to 4.

Question 10

Answer: B

  1. In the caller, v = 4. The argument expression in bump(v) supplies 4.
  2. The helper’s separate parameter starts as x = 4. x += 3 makes the helper’s x = 7.
  3. return x ends the helper and returns 7. The caller stores this in result.
  4. Caller v was never assigned a new value; it stays 4. Output order is v, result, so the pair is 4 7.

Option checks

  • A: Incorrect. 7 7 assumes the helper’s parameter update changes caller v.
  • B: Correct. The argument is copied and the returned 7 is stored in result.
  • C: Incorrect. 4 4 preserves the caller correctly but ignores the helper’s addition before its return.
  • D: Incorrect. It both changes caller v without an assignment and uses the old value as the return result.

Question 11

Answer: 10

  1. First call: parameter n = 3; local s starts at 1, as its initializer says.
  2. For k = 1, 2, 3, add 2, 4, 6. The successive s values are 3, 7, 13. At k = 4 the test fails, so return 13 and store r = 13.
  3. Second call: parameter n = 1; this call’s new local s starts at 1 again.
  4. At k = 1, add 2, so s = 3. Update to k = 2; the test fails. Return 3 and store t = 3.
  5. The caller prints r - t = 13 - 3 = 10. The required integer is 10.

13 stops after the first call and ignores the printed subtraction. -2 would result from incorrectly continuing the second s from 13, obtaining 15 and subtracting it from 13. 12 subtracts the second call’s initial 1 rather than its returned 3. A numeric answer alone does not demonstrate that both local initializations were traced.

Question 12

Answer: A, C, D

  1. For every supplied integer from -3 through 3, check n < 0. A negative input takes return -n; zero and positive inputs take return n + 1. The negative return ends its call, so the later return is not also executed.
  2. choose(-3) takes the negative branch and returns 3.
  3. choose(0) has a false negative test and returns 0 + 1 = 1.
  4. choose(2) also takes the final return and gives 2 + 1 = 3.
  5. For caller value = -2, the helper receives a separate parameter -2 and returns 2. Caller value stays -2; result becomes 2. Thus the displayed program prints 3 1 3 -2 2.
  6. The true statements are exactly A, C and D. Across the stated input range, outputs for -3, -2, -1, 0, 1, 2, 3 are 3, 2, 1, 1, 2, 3, 4; all arithmetic is within the stated small-integer range.

Option checks

  • A: True. For -3, n < 0 is true and -n equals 3.
  • B: False. Zero is not negative; it reaches return n + 1 and returns 1.
  • C: True. Positive 2 follows the nonnegative route and returns 3.
  • D: True. The helper receives the argument’s integer value; returning a calculation does not assign to caller value. This remains true throughout the stated input range.

What to revisit

Question 1–Question 2: current state and integer arithmetic. Question 3–Question 4: expression values and signed division. Question 5–Question 6: chosen branches and logical tests. Question 7–Question 9: exact loop states and counts. Question 10–Question 12: parameters, local initializers and returns. For a missed item, correct the first state where your trace diverged, then work forward again.

Source note

C11 semantics are referenced to the WG14 N1570 committee draft. For assignments, arithmetic, logical operators, loops and functions, see §§6.5, 6.8.4–6.8.6 and 6.9.1.

Analogy

Treat a trace as a ledger with a line for each change. An assignment revises one entry, a branch chooses which lines are used, and a loop repeats a group of lines until its test fails. A function call opens a separate worksheet for its copied parameters and local variables, then sends back one result. Comparing ledgers reveals where two apparently plausible answers start to differ.

Quick reference

  • 12 questions, 12 marks; free and untimed; +1 correct, 0 wrong or blank.
  • MCQ: one choice. MSQ: exact correct set, no partial marks. NAT: exact integer.
  • No negative marks or pass threshold. Use explanations to identify what to practise next.
  • Start every program from its own initializers; previous questions do not change its state.
  • Assignment uses the current state. Separate increment statements complete before the next statement.
  • C11 signed integer division truncates toward zero; remainder reconstructs the dividend.
  • && and || short-circuit. Logical results are 0 or 1.
  • Count successful bodies separately from condition tests and updates.
  • An integer parameter is separate from the caller’s variable. Each shown local initializer runs anew on its call.
  • Read the printf arguments in the stated output order; do not confuse that order with a general evaluation-order guarantee.

Notes for this lesson

Sign in to keep your progress. Sign in