Skip to content
GATEiit

GATE CSE 2024 Session 1 Q40: Possible final values of a and b for two interleaved threads

Question

Consider the following two threads T1 and T2 that update two shared variables a and b. Assume that initially a = b = 1. Though context switching between threads can happen at any time, each statement of T1 or T2 is executed atomically without interruption.

T1T2
a = a + 1;b = 2 * b;
b = b + 1;a = 2 * a;

Which one of the following options lists all the possible combinations of values of a and b after both T1 and T2 finish execution?

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 2/3 mark in GATE.
Show answer and explanation

Answer: A (official GATE 2024 final answer key)

Concept used: Enumerate interleavings that respect each thread's own statement order.

First step: a ends as 4 if a + 1 runs before 2 * a, else 3; do the same for b, then check which pairs an interleaving allows.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2024 Session 1 CS question paper and final answer key, organised by IISc Bangalore. Question paper · Answer key