C foundation learning review
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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
#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.
#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
- Initially
a = 5, b = 8. a = b - areads the current 8 and 5, computes 3, and stores it ina. State:a = 3, b = 8.b = b + anow reads 8 and the updated 3, sob = 11.- The output order is
a, b, giving3 11.
Option checks
- A: Correct. Both assignments are applied in order.
- B: Incorrect.
3 8stops after the first assignment and ignores the update tob. - C: Incorrect.
5 13retains the olda = 5and uses it in the second assignment, althoughahas already become 3. - D: Incorrect.
b = b + aadds 8 and 3; it does not copyaintob.
Question 2
Answer: 4
- Both operands of
total / groupare integers:58 / 9gives 6 whole groups. - Multiply the group size by that quotient:
9 * 6 = 54. - Subtract the grouped items from the original total:
58 - 54 = 4. - Thus
leftover = 4, and the required integer is 4. Check:6 * 9 + 4 = 58, with both0 <= 4and4 < 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
- Start with
n = 2. left = n++uses the old value 2 as the expression value, soleft = 2; by the end of this statementn = 3.right = ++nfirst increasesnfrom 3 to 4, then supplies 4, soright = 4.- The separate statements are complete before
printf. The requested ordern, left, rightgives4 2 4.
Option checks
- A: Incorrect.
left = 3treats postfixn++as if it supplied the incremented value. - B: Incorrect. There are two executed increments, so
ndoes not stop at 3;rightreceives 4. - C: Correct. The saved values are 2 and 4, while the final
nis 4. - D: Incorrect.
right = 3treats prefix++nas if it supplied the old value.
Question 4
Answer: A, C
- The mathematical quotient is -3.4. C11 integer division discards the fractional part toward zero, giving
p / q = -3. - Use the reconstruction identity:
-17 = (-3) * 5 + r, sor = -2. Thusp % q = -2. - Substituting the two C results gives
(p / q) * q + p % q = (-3) * 5 + (-2) = -17, which equalsp. - 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
- Initially
c = 7, tag = 0. - The first test
c < 5is false, sotag = 1is skipped. - Next,
c % 2is 1, soc % 2 == 1is true. Executetag = 2. - The final
elseis 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
elsedoes not execute after a trueelse ifbranch. - D: Incorrect. The initial 0 is replaced by the executed
tag = 2assignment.
Question 6
Answer: A, B
ais 0, so it is false in a logical test.bis 6.!ais!0, which is 1.- For
a || (b > 4), the left operand is false, so the right operand is evaluated.6 > 4is true, and logical OR returns 1. - For
a && (b > 0), the left operand is false, so the right operand is skipped and logical AND returns 0. - 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 variableb’s value 6. - C: False. A false left operand makes
&&return 0 without evaluating the right operand. - D: False. The equality
b == 6is true, so its negation is 0.
Question 7
Answer: 4
- Initialization sets
count = 0and the loop’sk = 3. - At
k = 3, the test is true: count becomes 1; update givesk = 6. - At
k = 6, the test is true: count becomes 2; update givesk = 9. - At
k = 9, the test is true: count becomes 3; update givesk = 12. - At
k = 12, the test is still true because the bound is inclusive: count becomes 4; update givesk = 15. - 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
- Initially
n = 19, sum = 0;19 > 0is true. - First body:
19 % 3 = 1, sosum = 1; thenn /= 3stores19 / 3 = 6. - Second test is
6 > 0, true.6 % 3 = 0, sosumstays 1; integer division setsn = 2. - Third test is
2 > 0, true.2 % 3 = 2, sosum = 3; integer division setsn = 0. - The test
0 > 0is 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 fromsum. - C: Incorrect. The starting 19 is not added to
sum; onlyn % 3is added in each body. - D: Correct. The added remainders are 1, 0 and 2, totaling 3.
Question 9
Answer: A, C, D
- Start with
sum = 0, then initializei = 0. - At
i = 0, the test0 < 4is true. Add2 * 0 = 0, keepingsum = 0; update toi = 1. - At
i = 1, the test1 < 4is true. Add 2, sosum = 2; update toi = 2. - At
i = 2, the test2 < 4is true. Add 4, sosum = 6; update toi = 3. - At
i = 3, the test3 < 4is true. Add 6, sosum = 12; update toi = 4. - At
i = 4, the test4 < 4is false. There is no fifth body or fifth update. Body count is 4, update count is 4, test count is 5. The output is12 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
ifrom 3 to 4.
Question 10
Answer: B
- In the caller,
v = 4. The argument expression inbump(v)supplies 4. - The helper’s separate parameter starts as
x = 4.x += 3makes the helper’sx = 7. return xends the helper and returns 7. The caller stores this inresult.- Caller
vwas never assigned a new value; it stays 4. Output order isv, result, so the pair is4 7.
Option checks
- A: Incorrect.
7 7assumes the helper’s parameter update changes callerv. - B: Correct. The argument is copied and the returned 7 is stored in
result. - C: Incorrect.
4 4preserves the caller correctly but ignores the helper’s addition before its return. - D: Incorrect. It both changes caller
vwithout an assignment and uses the old value as the return result.
Question 11
Answer: 10
- First call: parameter
n = 3; localsstarts at 1, as its initializer says. - For
k = 1, 2, 3, add2, 4, 6. The successivesvalues are3, 7, 13. Atk = 4the test fails, so return 13 and storer = 13. - Second call: parameter
n = 1; this call’s new localsstarts at 1 again. - At
k = 1, add 2, sos = 3. Update tok = 2; the test fails. Return 3 and storet = 3. - 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
- For every supplied integer from -3 through 3, check
n < 0. A negative input takesreturn -n; zero and positive inputs takereturn n + 1. The negative return ends its call, so the later return is not also executed. choose(-3)takes the negative branch and returns 3.choose(0)has a false negative test and returns0 + 1 = 1.choose(2)also takes the final return and gives2 + 1 = 3.- For caller
value = -2, the helper receives a separate parameter -2 and returns 2. Callervaluestays -2;resultbecomes 2. Thus the displayed program prints3 1 3 -2 2. - The true statements are exactly A, C and D. Across the stated input range, outputs for
-3, -2, -1, 0, 1, 2, 3are3, 2, 1, 1, 2, 3, 4; all arithmetic is within the stated small-integer range.
Option checks
- A: True. For -3,
n < 0is true and-nequals 3. - B: False. Zero is not negative; it reaches
return n + 1and 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
printfarguments 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