edited by
725 views
1 votes
1 votes

Consider the following functions $f()$ and $g().$

f(){
w = 3;
w = 4;
}

g(){
z = w;
z = z + 2*w;
print(z);
}

We start with $w$ set to $0$ and execute $f()$ and $g()$ in parallel-that is, at each step we either execute one statement from $f()$ or one statement from $g()$. What is the set of possible values printed by $g()?$

  1. $0,9,12$
  2. $0,8,9,12$
  3. $0,6,8,9,11,12$
  4. $0,4,6,9,10,12$
edited by

3 Answers

1 votes
1 votes

FOR 0= THE COMPLETE EXECUTION OF FUNCTION g WITH W=0 PRINTS 0.

FOR 6 :       a. z=w executed in g() and 0 stored in Z then preempted   

                  b .w=3 executed in f() then g() gives 6.  

FOR 8:       

                  a. z=w executed in g() and 0 stored in Z then preempted  

                   b   here w=3 then w=4 executed in f() then g()gives 8

FROM HERE OPTION C IS CLEAR HOW EVER LETS DERIVE ..

FOR 9:        w=3 in f() thne stored in z in g()

                     then 2nd statement of g() gives 9

for 11:         w=3 in f() thne stored in z in g()

              then w=4 in f() executed.

              then 2nd statement of g() gives 11

for 12:        w= 3 then w=4 in f() executed

                 then g() give 12

hence C is the ans

              

1 votes
1 votes
option c

when we execute whole funtion g() first  print z=0

when we execute f() upto statement w=3  first then whole function g() print z=9

when we execute  f() upto statement w=4  first then whole function g() print z=12

when we execute  g() upto statement z=w first then function f() upto w=3 then whole function g() print z=6

when we execute  g() upto statement z=w first then function f() upto w=4 then whole function g() print z=8

when we execute  f() upto statement w=3  first then g() upto statement z=w then statement w=4 of funtion f() then whole function g() print z=11

<0,6,8,9,11,12> option C
1 votes
1 votes

Let’s provide the number to the statement present inside the function $f()$ & $g()$.

f()                             g()
{                               {
 A: W=3;                        C: Z=W;
 B: W=4;                        D: Z=Z+2*W;
}                               E: print(Z);
                                }

In above code variable $W$ is common for both the functions whose initial value is $0$.

Following orders can be possible for desired output:

Possible order  print(z)
$A,B,C,D,E$ $12$
$A,C,D,E,B$ $9$
$A,C,B,D,E$ $11$
$A,C,D,B,E$ $9$
$C,A,B,D,E$ $8$
$C,D,E,A,B$ $0$
$C,A,D,E,B$ $6$
$C,A,D,B,E$ $6$

$\therefore 0,6,8,9,11,12$ option $C$ is correct here.

$\text{Note: We can preempt above statements at any time as per requirements.}$

  1. Gate 2015
  2. CMI 2011
  3. CMI 2012
  4. Race Around Condition

Related questions

3 votes
3 votes
2 answers
3
go_editor asked May 19, 2016
844 views
For integer values of $n$, the expression $\frac{n(5n + 1)(10n + 1)}{6}$Is always divisible by $5$.Is always divisible by $3$.Is always an integer.None of the above