GateAI
Programming in C

GATE CSE 2021 Set 1 · Q37

MCQ2 marksMedium

Time

Consider the following ANSI C program.#include <stdio.h> int main() { int i, j, count; count = 0; i = 0; for (j = -3; j <= 3; j++) { if ((j >= 0) && (i++)) count = count + j; } count = count + i; printf("%d", count); return 0; }Which one of the following options is correct?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) The program will compile successfully and output 10 when executed.

Programming in C

We trace the execution step-by-step:

  1. Initialization:

    • count = 0

    • i = 0

  2. Loop Iterations (j from −3-3 to 33):

    • j = -3, -2, -1: The condition (j >= 0) evaluates to false (00). Due to short-circuit evaluation of the logical AND operator (&&), the second operand (i++) is not evaluated. i remains 00 and count remains 00.

    • j = 0: The condition (j >= 0) is true (11). Therefore, (i++) is evaluated. Post-increment returns the current value of i (00, which is false), and then increments i to 11. Since the condition is false, the body does not execute. Now, i = 1, count = 0.

    • j = 1:(j >= 0) is true. (i++) evaluates to 11 (true), and then i becomes 22. The if condition succeeds: count = count + j = 0 + 1 = 1. Now, i = 2, count = 1.

    • j = 2:(j >= 0) is true. (i++) evaluates to 22 (true), and then i becomes 33. The if condition succeeds: count = count + j = 1 + 2 = 3. Now, i = 3, count = 3.

    • j = 3:(j >= 0) is true. (i++) evaluates to 33 (true), and then i becomes 44. The if condition succeeds: count = count + j = 3 + 3 = 6. Now, i = 4, count = 6.

  3. After the Loop:

    • count = count + i = 6 + 4 = 10.

  4. Output:

    • The program prints 10.

Hence, the correct option is (B).

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q37 of the GATE CSE 2021 Set 1 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 19 of them are 2-mark MCQs like this one, and GATE CSE 2021 Set 1 has 2.

How GATE tests Programming in C

More from GATE CSE 2021 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2021 Set 1 · Q37 · MCQ

Consider the following ANSI C program. #include int main() { int i, j, count; count = 0; i = 0; for (j = -3; j = 0) && (i++)) count = count + j; } count = count + i; printf("%d", count); return 0; } Which one of the following options is correct?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.