GATE / CS ARCHIVE

SUBJECT COLLECTION

Programming in C · Expressions, Operators & Types

9 questions, newest first

GATE CSE has asked 9 questions on Expressions, Operators & Types (Programming in C) from 2008 to 2024, in 9 of the 46 papers; the most recent is in GATE CSE 2024 Set 1. In the last five years it carried 0.4 marks per 100-mark paper on average, against 0.4 since 2003. Since 2014 the questions have been 4 NAT and 4 MCQ.

Browse topics
1 mark · NAT
Report issue

Parse arithmetic with unusual precedence

Expressions, Operators & Types

Question 33, GATE 2024: Evaluate 3+1+5*2/7+2−4−7−6/2 using these rules: + has highest precedence and associates left; − is next and associates right; * is next and associates right; / is lowest and associates right. All arithmetic is integer arithmetic.
Tap to enlarge
Answer & solution

Official answer: 6

Approach: Build the grouping from the stated table, not from ordinary mathematical or C precedence. Associativity decides grouping only among operators at the same precedence.
1. Highest-precedence additions group (3+1+5)=9 and (7+2)=9. The expression becomes 9*2/9−4−7−6/2.
2. Subtraction has higher precedence than multiplication and division and is right-associative. Its group is 9−(4−(7−6)). Evaluate inward: 7−6=1, 4−1=3, 9−3=6.
3. The expression is now 9*2/6/2. Multiplication is next, giving 18/6/2.
4. Division is right-associative, so this means 18/(6/2), not (18/6)/2. The inner quotient is 3 and the result is 18/3=6.
Answer: 6. A fully parenthesized form is ((3+1)+5)*2 / (((7+2)−(4−(7−6)))/2). Both actual divisions are exact, so no integer-rounding issue arises.

Original paper, page 20 · Answer key

2 marks · MCQ
Report issue

Expressions, Operators & Types

Question 44, GATE 2022: ANSI C uses char a='P',b='x'; char c=(a&b)+'*',d=(a|b)-'-',e=(a^b)+'+'; printf("%c %c %c\n",c,d,e). ASCII values P=80,x=120,*=42,−=45,+=43,z=122,K=75,S=83. Choose output: A z K S; B 122 75 83; C * − +; D P x +.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Using the provided ASCII values, a=P=80 and b=x=120. a&b=80, so adding *=42 gives 122, character z. a|b=120, so subtracting -=45 gives 75, character K. ab=40, so adding +=43 gives 83, character S. Thus the output is z K S, option A.

Binary P=01010000 and x=01111000. AND keeps their shared 1 bits (01010000); OR combines them (01111000); XOR keeps differing bits (00101000). The arithmetic uses integer promotions, and the resulting 122,75,83 fit char. %c converts the codes to characters; %d would have printed numbers instead.

Original paper, page 29 · Answer key

2 marks · MCQ
Report issue

Expressions, Operators & Types

Question 37, GATE 2021: What happens in this ANSI C program? #include <stdio.h>
int main(void){ 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; }
A fails compilation; B compiles and prints 10; C compiles and prints 8; D compiles and prints 13.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (35 steps)

For j=−3,−2,−1 the left operand of && is false, so i++ is not evaluated. At j=0 the old i=0 makes the condition false, but increments i to 1. At j=1, 2, 3 the old i is nonzero, adding 1+2+3=6 while i reaches 4. The final addition gives count=6+4=10. Thus B. Short-circuiting and postincrement order are defined by C.

A compact trace (j, i after condition, count after body) is (−3,0,0),(−2,0,0),(−1,0,0),(0,1,0),(1,2,1),(2,3,3),(3,4,6). Postincrement yields the old value as the expression result, then changes i. The logical AND operator sequences and short-circuits its operands, so the first three iterations do not increment i at all. The final statement adds the final i=4 once, obtaining 10 without undefined unsequenced modifications.

Original paper, page 31 · Answer key

2 marks · NAT
Report issue

Expressions, Operators & Types

Question 53, GATE 2017: Source includes stdio.h and string.h. void printlength(char *s,char *t) { unsigned int c=0; int len=((strlen(s)-strlen(t))>c)?strlen(s):strlen(t); printf("%d\n",len); } void main() { char *x="abc"; char *y="defgh"; printlength(x,y); }
The question explicitly says size_t, returned by strlen, is unsigned int. What is printed?
Tap to enlarge
Answer & solution

Official answer: 3.0

Our answer: 3

1. strlen("abc")=3 and strlen("defgh")=5. The stipulated return type is unsigned int, so subtraction is unsigned rather than signed.

2. Let the unsigned modulus be U=UINT_MAX+1. Then 3−5 wraps to U−2, a large positive value, not signed −2. The comparison is against unsigned c=0 and is therefore true.

3. The conditional selects strlen(s)=3. Conversion of 3 to int is representable and printf prints 3. The surprising result comes from unsigned subtraction; the printed void main is a separate source portability issue.

void main is a nonstandard hosted-C entry point; the exam assumes an implementation accepting it.

Original paper, page 23 · Answer key

2 marks · NAT
Report issue

Expressions, Operators & Types

Question 54, GATE 2017: C program fragment: int m=10; int n,n1; n=++m; n1=m++; n--; --n1; n-=n1; printf("%d",n);. What is printed?
Tap to enlarge
Answer & solution

Official answer: 0.0

Our answer: 0

Step through it (12 steps)

1. Prefix increment changes the operand before producing its value. Thus n=++m first makes m=11 and then assigns n=11.

2. Postfix increment produces the old value and increments the operand by the end of the full expression. Hence n1=m++ assigns n1=11 and leaves m=12.

3. The separate statements n-- and --n1 reduce n and n1 to 10. Their prefix/postfix distinction does not matter when their expression values are discarded. Finally n-=n1 computes n=10-10=0, which is printed. These modifications occur in separate full expressions, so there is no unsequenced-increment ambiguity.

Original paper, page 22 · Answer key

1 mark · MCQ
Report issue

Expressions, Operators & Types

Question 12, GATE 2016: Given void f(int,short); and a main body declaring int i=100; short s=12; short *p=&s;, which expression can fill a call-site blank without a type-checking error? A: f(s,*s). B: i=f(i,s). C: f(i,*s). D: f(i,*p). The printed wrapper uses void main(); judge the candidate expressions.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

The function expects int and short and returns void. In D, i is int and *p is short, so f(i,*p) is well typed. A and C incorrectly dereference the nonpointer s; B tries assigning a void result to i. Thus D, evaluating the blank expression independently of the nonstandard void main wrapper.

The unary * operator requires a pointer operand. Since s is a short integer, *s is invalid regardless of the value 12 stored in it. By contrast p has type short*, so *p is a short lvalue compatible with f's second parameter. In B the arguments themselves are compatible, but f returns no value, so its call cannot supply an integer assignment value. Separate argument-type checking from return-value checking.

Original paper, page 6 · Answer key

1 mark · MCQ
Report issue

Expressions, Operators & Types

Question 11, GATE 2014: Unsigned int variables n and p are used in C to compute p=n choose 3. For large n, which statement is most likely to work correctly? A p=n*(n-1)*(n-2)/6; B p=n*(n-1)/2*(n-2)/3; C p=n*(n-1)/3*(n-2)/2; D p=n*(n-1)*(n-2)/6.0;
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. Set up the reasoning. Consider two separate hazards: overflow of intermediate unsigned products and truncation by integer division. Adjacent integers n and n-1 always have an even product, permitting an exact early division by two.

2. Derive the result. B divides the always-even product n(n−1) by 2 before multiplying by n−2, reducing intermediate magnitude without truncation. The final division by 3 is exact for the integer binomial product. A and D form the larger full product before division, while C may divide n(n−1) by 3 prematurely, truncating when n≡2 mod3. B is less overflow-prone, not guaranteed correct for arbitrarily large unsigned n; even its intermediate can wrap.

3. Check the interpretation. Multiplication and division associate left to right. The final floating divisor in D does not retroactively convert preceding integer products to floating point. B mitigates risk but cannot guarantee correctness for every representable n or for results too large to fit p.

Original paper, page 31 · Answer key

1 mark · NAT
Report issue

Expressions, Operators & Types

Question 11, GATE 2014: For a supplied X, find the minimum number of arithmetic operations needed to evaluate P(X)=X⁵+4X³+6X+5 using only one temporary variable. This is a numerical-answer question.
Tap to enlarge
Answer & solution

Official answer: 7

Our answer: At most 6; official 7 is not minimal under algebraic refactoring

Step 1: Sparse Horner form is X((X²+4)X²+6)+5. An evaluation recomputing X² with one temporary can use seven additions/multiplications, explaining the official key 7. Step 2: However, expand X[(X²+2)²+2]+5: the square is X⁴+4X²+4, so multiplying X(X⁴+4X²+6)+5 gives the same polynomial. Step 3: Execute t=X*X; t=t+2; t=t*t; t=t+2; t=t*X; t=t+5. This uses only X and one temporary t, with three multiplications and three additions. Step 4: Six operations suffice, so 7 cannot be the unrestricted minimum. This is an upper-bound counterexample, not a proof that 6 is globally minimal. Keep the official answer and the open discrepancy separate; no unstated ban on algebraic refactoring has been supplied.

Original paper, page 55 · Answer key

1 mark · MCQ
Report issue

Nested conditional computes the maximum

Expressions, Operators & Types

Question 18, GATE 2008: Which (x,y,z) makes a=4 in the C expression a=(x>y)?((x>z)?x:z):((y>z)?y:z)?

(A) (3,4,2)
(B) (6,5,3)
(C) (6,3,5)
(D) (5,4,5)
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. If x>y, the expression selects the larger of x and z. Otherwise it selects the larger of y and z. Thus a=max(x,y,z).
2. The maxima for A,B,C,D are respectively 4,6,6,5.
3. Only A yields 4. Equality in a comparison does not change the selected maximum value.

Original paper, page 4