Comp Sci 2S03 Midterm exercises McMaster University

28
CS/SE 2S03 Comp Sci 2S03 Midterm exercises McMaster University Note: Some of these exercise questions are slightly harder than what can comfortably be asked in a 50 minute midterm (but not a 3 hour final). And there are, of course, way more questions here than can fit on a midterm. There will not be any multiple choice questions on this year’s midterm, but the topics are still relevant. So similar questions will be asked. Question 1 [1 mark] The first program run on a mechanical device was: A. on the Jacquard loom. B. on the Bouchon loom. C. Lady Lovelace’s code to be run on Babbage’s Analytical Engine. D. Assembler on ENIAC I. Question 2 [1 mark] Which one of these is not a programming language: A. APL B. Java C. HTML D. Whitespace Question 3 [1 mark] Operational Semantics is the specification: A. Of what programs should do. B. Of what the parts of a programming language mean (mathematically). C. Of how to operate a computer. D. Of how to execute the parts of a programming language. continued on page 2

Transcript of Comp Sci 2S03 Midterm exercises McMaster University

CS/SE 2S03

Comp Sci 2S03 Midterm exercises

McMaster University

Note: Some of these exercise questions are slightly harder than what can comfortably be asked in a 50 minute midterm(but not a 3 hour final). And there are, of course, way more questions here than can fit on a midterm. There will not beany multiple choice questions on this year’s midterm, but the topics are still relevant. So similar questions will be asked.

Question 1 [1 mark]The first program run on a mechanical device was:

A. on the Jacquard loom.

B. on the Bouchon loom.

C. Lady Lovelace’s code to be run on Babbage’s Analytical Engine.

D. Assembler on ENIAC I.

Question 2 [1 mark]Which one of these is not a programming language:

A. APL

B. Java

C. HTML

D. Whitespace

Question 3 [1 mark]Operational Semantics is the specification:

A. Of what programs should do.

B. Of what the parts of a programming language mean (mathematically).

C. Of how to operate a computer.

D. Of how to execute the parts of a programming language.

continued on page 2

CS/SE 2S03 2/28

Question 4 [1 mark]What are the 2 principal syntactic categories of an imperative programming language?

A. expressions and statements.

B. declarations and expressions.

C. assignments and statements.

D. statements and declarations.

Question 5 [1 mark]What is Test-Driven Development?

A. A software development approach that relies on the repetition of a very short development cycle involving manyunit tests.

B. A sequential design process, used in software development processes, in which progress is seen as flowing steadilydownwards (like a waterfall).

C. The activity of creating prototypes of software applications.

D. All of the above

Question 6 [1 mark]In Java, declarations always allocate memory.

A. True.

B. False.

Question 7 [1 mark]Why is Θ partial?

A. Because you might look up an undefined variable in the environment.

B. Some operations are partial.

C. Some statements loop.

D. All of the above.

continued on page 3

CS/SE 2S03 3/28

Question 8 [1 mark]What is the value of

Θ (x + y− 3, [x = r1, y = r3, z = r2] , [r1 = 5, r2 = 17, r3 = 8])

A. 10

B. 13

C. 19

D. 22

Question 9 [1 mark]What is printed by

stat ic int x = 5 ;stat ic int f ( f ina l int y ) { x = x + 1 ; return y∗y ; }public stat ic void main ( St r ing [ ] a rgs ) {

System . out . p r i n t f ( ”\%d” , f ( x )+x ) ; }

A. 30

B. 42

C. 31

D. an error.

Question 10 [1 mark]In the following code, what is the result of calling f (5.0)?

stat ic double f ( f ina l double y ) {f loat x , z , w;x = Math . s q r t ( y∗y ) ;z = x∗x∗x + 3.0∗ x∗x − 7 .0∗ x + 1 2 . 0 ;return y ;x = z∗z∗z + Math . s q r t ( x∗x ) ;z = x−x ;return z ; }

A. 0.0

B. 1.0e-37

C. 42.0

D. 5.0

continued on page 4

CS/SE 2S03 4/28

Question 11 [1 mark]Why are globals bad?

A. Because Dyjkstra said so.

B. Because they destroy local reasoning.

C. Because I said so.

D. Because they allow you to have a single database connection.

Question 12 [1 mark]Which of the following best describes shadowing :

A. local variables hide global variables

B. local variables hide parameters

C. global variables hide local variables

D. parameters hide local variables

Question 13 [1 mark]What is stored in the global environment?

A. The value of global variables.

B. The association of global names to values/memory and also the association of global names to functions.

C. The association of global names to functions.

D. The global memory.

continued on page 5

CS/SE 2S03 5/28

Question 14 [1 mark]Consider the following function:

stat ic int f ( int x , int y ) { return (x−1) ∗ (x−y ) ; }

What is the memory state after the execution of the statement u = f(a, b) in the environment e = [a = r1, b = r2, u = r3],the memory state m = [r1 = −2, r2 = 5, r3 = 0] and the global environment G composed of the environment e and thefunction declaration f .

A. [r1 = 2, r2 = 3, r3 = 15, r4 = 2, r5 = 3].

B. [r1 = −2, r2 = 5, r3 = 0, r4 = −2, r5 = 5, r6 = 21].

C. [r1 = −2, r2 = 5, r3 = 21, r4 = −2, r5 = 5].

D. [r1 = −2, r2 = 5, r3 = 21].

Question 15 [1 mark]In the following code, which of the following could be the result of Σ(f (9.0) , e,mG) where e = {},m = {}, and G = {(f =(final double y). . .)} ?

stat ic double f ( f ina l double y ) {f loat x , z , w;x = Math . s q r t ( y∗y ) ;z = x∗x∗x + 3.0∗ x∗x − 7 .0∗ x + 1 2 . 0 ;return y ;x = z∗z∗z + Math . s q r t ( x∗x ) ;z = x−x ;return z ; }

A. 9.0

B. (return, 0.0, {})

C. (normal, {})

D. (normal, {r1 = 9.0, r2 = 3.0, r3 = 921.0})

continued on page 6

CS/SE 2S03 6/28

Question 16 [3 marks]The code below does not compile - fix it. The change needed to make it compile does not modify the statements definingthe function f .

class Foo {f ina l stat ic int x = 5 ;

f ina l stat ic int f ( f ina l int n) {i f (n<=0) { return x ;} else i f (n==1) { return 1 ;} else {

x = x − 1 ;return 2∗ f (n−1) ;

}}

}

Question 17 [1 mark](This will not be on the midterm, as we have not covered the answer to this in class yet).

public class MidtermPractise0 {public f ina l int X = 0 ;private f ina l int Y = 1 ;f ina l int Z = 4 ;

public stat ic void main ( St r ing [ ] a rgs ) {System . out . p r i n t l n (X) ;System . out . p r i n t l n (Y) ;System . out . p r i n t l n (Z) ;}}

Which one of the following lines causes this code to not compile:

A. System.out.println(X);

B. System.out.println(Y);

C. System.out.println(Z);

D. This code compiles.

E. All 3 lines.

continued on page 7

CS/SE 2S03 7/28

Question 18 [5 marks]Write (in the space below) an iterative program which does the same as the following:

f ina l stat ic int f ( f ina l int n) {i f (n<=0) { return 2 ;} else i f (n==1) { return 1 ;} else {

return 2∗ f (n−1) − f (n−2) ;}

}

continued on page 8

CS/SE 2S03 8/28

The following code will be used for the next several questions. (this and ++ will be covered on Monday; they will notappear on the midterm – but this is still good practice)

public class MidtermPractise1 {public f ina l int X = 3 ;private f ina l int Y = 1 ;int Z = 4 ;

public int addOne( int x ) {Z++;return x++;}

public int addTwo( int x ) {x = this .X + x++;return x++;}

public int addThree ( int x ) {Z = this .X + this .Y + x++;return Z ;}}

Also assume a global environment G = {(X = 3), (Y = 1), (Z = 4), . . .}.

Question 19 [1 mark] What does calling addOne(addThree(3)) return?

A. 3

B. 4

C. 5

D. 6

E. 7

Question 20 [1 mark] What does calling addOne(3) return?

A. 1

B. 2

C. 3

D. 4

continued on page 9

CS/SE 2S03 9/28

Question 21 [1 mark] What does calling addTwo(3) return?

A. 3

B. 4

C. 5

D. 6

E. 7

Question 22 [1 mark] What does calling addThree(3) return?

A. 3

B. 4

C. 5

D. 6

E. 7

Question 23 [1 mark] What does Σ(addThree(3), {}, {}, G) return?

A. 3

B. (return, 7)

C. (normal, y)

D. (return, {r1 = 7})

E. (normal, {r1 = 7})

continued on page 10

CS/SE 2S03 10/28

The following code will be used for the next several questions.

public class MidtermPractise2 {public f ina l int X = 3 ;private f ina l int Y = 1 ;int Z = 4 ;

public int addToX( int x ) {x = this .X + x ;return x ;}

public int addToX ( ) {int x = this .X + addToX( this .X) ;return x ;}

public int addToY( f ina l int Y) {this . Z++;return this .Y + Y;}

public int addToZ( f ina l int Z) {return this . Z + Z ;}}

Question 24 [1 mark] What does calling addToX(5) return?

A. 7

B. 8

C. 9

D. 10

E. 11

Question 25 [1 mark] What does calling addToX() return?

A. 7

B. 8

C. 9

D. 10

E. 11

continued on page 11

CS/SE 2S03 11/28

Question 26 [1 mark] What does calling addToY(4) return?

A. 4

B. 5

C. 6

D. 7

E. 8

Question 27 [1 mark] What does calling addToZ(addToY(2)) return?

A. 4

B. 5

C. 6

D. 7

E. 8

Question 28 [1 mark] Which values of I , II , III satisfy the following equalities: f(4, 4) = 1; f(−8, 11) = 2;f(10, 0) = 3; f(19, 9) = 4?

stat ic int f ( int v , int u) {i f ( v <= I ) {

i f (u <= I I ) {return 1 ;

} else {return 2 ;

}} else {

i f (u <= I I I ) {return 3 ;

} else {return 4 ;

}}

}

A. -3 8 2

B. 12 7 3

C. 9 10 4

D. 5 0 11

continued on page 12

CS/SE 2S03 12/28

Question 29 [3 marks]Write the mathematical function computed by the following Java function:

stat ic int f ( int x ) {i f ( x <= 0) return 0 ;int a = 1 ;while ( x > 1 ) {

a = 2∗a + 1 ;x−−;

}return a ;

}

Question 30 [3 marks]Describe three different code transformations which can be applied to the following code in order to make it better. Briefyjustify each transformation.

public class Bad {int a = 3 ;int b = 9 ;int c = −31;

public int f ( ) {int c = 8 ;int d = 0 ;int e = 22 ;

a = 10 + e ;c = a ∗ b ;while (33 > e ) e += e ;i f ( e < c ) d = 33 ;this . a = c + d ∗ ( a − b + this . c + a ) ;c = 3030 ∗ e++;c = this . a ;return c ;

}}

Question 31 [1 marks]What is the value of

Θ((x1 + x2)× x3, [x1 = r4, x2 = r2, x3 = r1], [r1 = 4, r2 = 6, r3 = 9, r4 = 7, r5 = 10])

A. 90

B. 52

C. 31

D. None of the above.

continued on page 13

CS/SE 2S03 13/28

Question 32 [1 marks] What is the value ofΘ(x1 + x2 − y, [x1 = z, y = a, x2 = c], [x3 = 14, z = 5, c = 6, q = 7, a = 5])

A. -3

B. 4

C. 15

D. None of the above.

Question 33 [1 marks]What is the value ofΘ(w + x− y + z, [w = a, x = b, y = c, z = d], [d = 5, c = 3, b = 17, a = 9])

A. 0

B. 24

C. 28

D. None of the above.

Question 34 [1 marks]What is printed

stat ic f ina l int x = 4 ;stat ic int y = 5 ;public stat ic void main ( St r ing [ ] a rgs ) {

System . out . p r i n t f ( ”%d” , −−x ∗ y ) ;}

A. 20

B. 15

C. 9

D. Error.

continued on page 14

CS/SE 2S03 14/28

Question 35 [1 marks]What is printed

stat ic int x = 4 ;stat ic int y = 5 ;public stat ic void main ( St r ing [ ] a rgs ) {

System . out . p r i n t f ( ”%d” , −−x ∗ y ) ;}

A. 20

B. 15

C. 9

D. None of the above.

Question 36 [1 marks]What is printed

stat ic int f = 3 ;stat ic int g = 5 ;public stat ic void main ( St r ing [ ] a rgs ) {

f oo ( g ) ; foo ( ) ; foo ( ) ; foo ( g ) ;System . out . p r i n t f ( ”%d” , f + g ) ;}stat ic void f oo ( int g ) { g++; }stat ic void f oo ( ) { f ++; }

A. 8

B. 10

C. 12

D. None of the above.

continued on page 15

CS/SE 2S03 15/28

Question 37 [1 marks]What is returned by calling foo(4.0)

stat ic double f oo (double z ) {double x = 1 . 5 ; double y = 4 . 6 ; double w = 9 . 2 ;x += z ; x += w;z += Math . s q r t ( z ) ;y −= x − w;return z ;x −= z ; x −= w;return x ;}

A. 1.5

B. 6

C. 4

D. None of the above.

Question 38 [1 marks]What is printed by calling foo()

stat ic void f oo ( ) {int x = 5 ; int y = 2 ; int z = 6 ;do {

x++;} while ( x > 10) ;i f ( x < z ) { x −= z ; }else { z += x ; }System . out . p r i n t f ( ”%d %d %d” , x , −−y , z++) ;}

A. -1 1 6

B. 5 1 11

C. 6 2 12

D. None of the above.

Question 39 [2 marks]Here is a simple piece of C code:

int q1 ( int num) {i f (num%2 == 0) {return 0 ;} else {return 1 ;}}

Which of the following Java code returns the same value?

continued on page 16

CS/SE 2S03 16/28

A. stat ic int q1a ( int num) {i f ( (num/2) ∗2 == 1) {return 0 ;} else {return 1 ;}}

B. stat ic int q1b ( int nm) {int num;i f ( (nm/2) ∗2 == nm) {return 0 ;} else {return 1 ;}}

C. stat ic int q1c ( int nm) {i f (nm%2 == 0) {return 1 ;} else {return 0 ;}}

Question 40 [2 marks]Here is a simple piece of C code, that computes a factorial:

int q2 ( int n) {int i = 1 ;int f a c t = 1 ;

for ( i = 1 ; i <= n ; i++) {f a c t = f a c t ∗ i ;}

return f a c t ;}

Which of the following returns the same value?

A. stat ic void q2a ( int n) {int i = 1 ;int f a c t = 1 ;

for ( i = 1 ; i <= n ; i++) {f a c t = f a c t ∗ i ;}}

continued on page 17

CS/SE 2S03 17/28

B. stat ic int q2b ( int n) {int f a c t = 1 ;for ( int i = 0 ; i < n ; i++) {

f a c t = f a c t ∗ i ;}return f a c t ;}

C. stat ic int q2c ( int n) {int f a c t = 1 ;

for ( int i = 0 ; i < n ; i++) {f a c t ∗= n − i ;}

return f a c t ;}

Question 41 [2 marks]Here is a simple piece of C code, that computes num1 to the power of num2:

int q3 ( int num1 , int num2) {int var ;int s = 1 ;for ( var = 0 ; var < num2 ; ++var ) {

s = s ∗ num1 ;}return s ;}

Which of the following Java code does the same?

A. stat ic int q3a ( int num1 , int num2) {int var ;int s = 0 ;for ( var = 0 ; var < num2 ; ++var ) {

s = s ∗ num1 ;}return s ;}

B. stat ic int q3b ( int num1 , int num2) {int var = 0 ;int s = 1 ;

while ( var != num2) {s ∗= num1 ;++var ;}

return s ;}

continued on page 18

CS/SE 2S03 18/28

C. stat ic int q3c ( int num1 , int num2) {int var = 0 ;int s = 1 ;

while ( var != num2) {s ∗= num1 ;var++;}

return var ;}

D. stat ic int q3d ( int n , int m) {int var ;int s = 2 ;for ( var = 0 ; var < m; ++var ) {

s = s ∗ n ;}return ( s − 1) ;}

Question 42 [2 marks]Here is a simple piece of C code:

s t r i n g q4 ( int so , int fancy ) {int im ;

im = so / fancy ;i f ( im∗ fancy == so ) {return ” very fancy ” ;} else {return ” not that fancy ” ;}}

Which of the following Java code returns the same value?

A. stat ic St r ing q4a ( int so , int fancy ) {int im ;S t r ing howFancy = ”” ;

im = so / fancy ;im ∗= fancy ;i f ( im == so ) {howFancy = ” very fancy ” ;} else {

howFancy = ” not that fancy ” ;}

return howFancy ;}

continued on page 19

CS/SE 2S03 19/28

B. stat ic int q4b ( int so , int fancy ) {int im ;S t r ing howFancy = ”” ;

im = so / fancy ;im ∗= fancy ;i f ( im == so ) {howFancy = ” very fancy ” ;} else {

howFancy = ” not that fancy ” ;}

return fancy ;}

C. stat ic St r ing q4c ( int so , int fancy ) {int im ;S t r ing howFancy = ” very ” ;

im = so / fancy ;im ∗= fancy ;i f ( im == so ) {howFancy += ” fancy ” ;} else {

howFancy += ” that fancy ” ;}

return howFancy ;}

D. stat ic St r ing q4d ( int so , int fancy ) {int im ;S t r ing howFancy = ” ” ;

im = so / fancy ;im ∗= fancy ;i f ( im == so ) {howFancy += ” very fancy ” ;} else {

howFancy += ” that fancy ” ;}

return howFancy ;}

Question 43 [2 marks]Here is a simple piece of C code, that returns the Greatest Common Denominator of its two inputs:

int q5 ( int a , int b) {int t ;

continued on page 20

CS/SE 2S03 20/28

while (b != 0) {t = b ;b = a % b ;a = t ;}

return a ;}

Which of the following Java code returns the same value?

A. stat ic int q5a ( int a , int b) {int t ;

while (b != 0) {t = a ;b = a % b ;a = t ;}

return a ;}

B. stat ic int q5b ( int a , int b) {int t = 0 ;

while (b != 0) {t = b ;b = a / b ;a = t ;}

return a ;}

C. stat ic int q5c ( int apple , int banana ) {int t ;

while ( banana != 2) {t = banana ;banana = apple % banana ;apple = t ;}

return apple ;}

D. stat ic int q5d ( int c , int d) {int t ;

i f ( c >= d | | c <= d) {while (d != 0) {

continued on page 21

CS/SE 2S03 21/28

t = d ;d = c % d ;c = t ;}}

return c ;}

Question 44 [3 marks]Here is a simple piece of C code, that returns the number of fingers frodo has at the end of the Lord Of the Rings

Trilogy:

int q6 ( int f rodo , int mountDoom) {int s t ep s = 0 ;int f i n g e r s = 10 ;while ( f i n g e r s == 10) {i f ( s t ep s == mountDoom) {

f i n g e r s −−;} else {

s t ep s++;}}return f i n g e r s ;}

Which of the following Java code returns the same number of fingers?

A. stat ic int q6a ( int bi lbo , int mountDoom) {int s t ep s = 0 ;int f i n g e r s = 10 ;while ( f i n g e r s == 10) {i f ( s t ep s == mountDoom) {

f i n g e r s −= 1 ;} else {

s t ep s++;}}return ++f i n g e r s ;}

B. stat ic int q6b ( int gener icHobbit , int mountDoom) {int s t ep s = 0 ;int f i n g e r s = 10 ;

for ( s t ep s = 1 ; s t ep s < mountDoom ; s t ep s++) {f i n g e r s = mountDoom / s t ep s ;}

return f i n g e r s ;}

continued on page 22

CS/SE 2S03 22/28

C. stat ic int q6c ( int f rodo , int mountDoom) {int s t ep s = 0 ;int f i n g e r s = 10 ;

for ( s t ep s = 0 ; s t ep s <= mountDoom ; s t ep s++) {i f ( s t ep s == mountDoom) {

f i n g e r s −−;}}

return f rodo ;}

D. stat ic int q6d ( int f rodo , int mountDoom) {int s t ep s = 0 ;int f i n g e r s = 10 ;int golem = −1;

for ( s t ep s = 0 ; s t ep s <= mountDoom ; s t ep s++) {i f ( s t ep s == mountDoom) {

f i n g e r s += golem ;}}

return f i n g e r s ;}

Question 45 [2 marks]Here is a simple piece of python code, that tells you the state of groot:

de f q1 ( shipCrash ) :i f ( shipCrash == True ) :groot = ”dead”

else :g root = ” a l i v e ”

return groot

Which of the following Java code returns the same state?

A. public stat ic St r ing q1a (boolean shipCrash ) {St r ing groot = ”” ;

i f ( shipCrash == true ) {groot = ” a l i v e ” ;} else {

groot = ”dead” ;}

return groot ;}

continued on page 23

CS/SE 2S03 23/28

B. public stat ic int q1b (boolean shipCrash ) {int groot = 0 ;

i f ( shipCrash == true ) {groot = 1 ;} else {

groot = 0 ;}

i f ( groot > 2) {groot = 1 ;}

return groot ;}

C. public stat ic St r ing q1c (boolean shipCrash ) {St r ing groot = ”” ;

i f ( shipCrash == true ) {groot = ”dead” ;} else i f ( shipCrash == fa l se ) {

groot = ” a l i v e ” ;}

return groot ;}

D. public stat ic St r ing q1d (boolean shipCrash ) {St r ing groot = ”” ;

i f ( shipCrash != true ) {groot = ”dead” ;} else i f ( shipCrash == fa l se ) {

groot = ” a l i v e ” ;}

return groot ;}

Question 46 [2 marks]Here is a simple piece of python code, that takes in how many experimental cycles you will run on a racoon:

de f q2 ( c y c l e s ) :s u b j e c t = ””

for x in xrange (1 , c y c l e s ) :i f x < 10 :

s u b j e c t = ”sadPanda”e l i f 10 < x < 100 :

continued on page 24

CS/SE 2S03 24/28

s u b j e c t = ”Nothin ’ but a lab ra t ”e l i f x >= 100 :s u b j e c t = ”Rocket Racoon”

return s u b j e c t

Which of the following Java code returns the same result?

A. public stat ic St r ing q2a ( int c y c l e s ) {St r ing s u b j e c t = ”” ;

for ( int i = 0 ; i < c y c l e s ; i++) {i f ( i < 10) {

s u b j e c t = ”sadPanda” ;} else i f ( i > 10 && i < 100) {

s u b j e c t = ”Nothin ’ but a lab ra t ” ;} else i f ( i >= 100) {

s u b j e c t = ”Rocket Racoon” ;}}return s u b j e c t ;}

B. public stat ic St r ing q2b ( int c y c l e s ) {St r ing s u b j e c t = ”” ;

for ( int i = 0 ; i < c y c l e s ; i++) {i f ( i < 10) {

s u b j e c t = ”sadPanda” ;} else i f ( i > 10 && i <= 100) {

s u b j e c t = ”Nothin ’ but a lab ra t ” ;} else i f ( i >= 100) {

s u b j e c t = ”Rocket Racoon” ;}}return s u b j e c t ;}

C. public stat ic St r ing q2c ( int c y c l e s ) {St r ing s u b j e c t = ”” ;

for ( int i = 0 ; i < c y c l e s ; i++) {i f ( i < 10) {

s u b j e c t = ”sadPanda” ;} else i f ( i > 10 && i <= 100) {

s u b j e c t = ”Nothing ’ but a lab ra t ” ;} else i f ( i >= 100) {

s u b j e c t = ”Rocket Racoon” ;}}return s u b j e c t ;}

continued on page 25

CS/SE 2S03 25/28

public stat ic St r ing q2d ( int c y c l e s ) {St r ing s u b j e c t = ”” ;int i = 0 ;while ( i < c y c l e s ) {i f ( i < 10) {

s u b j e c t = ”sadPanda” ;} else i f ( i > 10) {

s u b j e c t = ”Nothing ’ but a lab ra t ” ;} else i f ( i >= 100) {

s u b j e c t = ”Rocket Racoon” ;}i ++;}return s u b j e c t ;}

Question 47 [3 marks]Here is a simple piece of python code, that tells you how far Peter Quill is in plot development, as the movie goes on:

de f q3 ( timeWatched ) :whoAmI = ” Peter Qu i l l ”movieMinutes = 0 ;i f timeWatched > 122 :timeWatched = 122

while movieMinutes <= timeWatched :i f movieMinutes < 30 :whoAmI = ” s t i l l Q u i l l ”i f 30 < movieMinutes <= 60 :whoAmI = ” almostThere ”i f 60 < movieMinutes <= 100 :whoAmI = ” hope l e s s ”i f movieMinutes > 100 :whoAmI = ” StarLord ”

movieMinutes += 1return whoAmI

Which of the following Java code returns the same result?

D.A. public stat ic void q3a ( int timeWatched ) {St r ing whoAmI = ” Peter Qu i l l ” ;int movieMinutes = 0 ;i f ( timeWatched > 122) {timeWatched = 122 ;}

while ( movieMinutes <= timeWatched ) {i f ( movieMinutes < 30) {whoAmI = ” s t i l l Q u i l l ” ;} else i f (30 < movieMinutes && movieMinutes <= 60) {

continued on page 26

CS/SE 2S03 26/28

whoAmI = ” almostThere ” ;} else i f (60 < movieMinutes && movieMinutes <= 145) {whoAmI = ” hope l e s s ” ;} else i f ( movieMinutes > 145) {whoAmI = ” StarLord ” ;}movieMinutes++;}}

B. public stat ic int q3b ( int timeWatched ) {St r ing whoAmI = ” Peter Qu i l l ” ;int movieMinutes = 0 ;i f ( timeWatched > 122) {timeWatched = 122 ;}

while ( movieMinutes <= timeWatched ) {i f ( movieMinutes < 30) {whoAmI = ” s t i l l Q u i l l ” ;} else i f (30 < movieMinutes && movieMinutes <= 60) {whoAmI = ” almostThere ” ;} else i f (60 < movieMinutes && movieMinutes <= 145) {whoAmI = ” hope l e s s ” ;}movieMinutes += 1 ;}

return movieMinutes ;}

C. public stat ic St r ing q3c ( int timeWatched ) {St r ing whoAmI = ” Peter Qu i l l ” ;int movieMinutes = 0 ;i f ( timeWatched > 122) {timeWatched = 122 ;}

while ( movieMinutes <= timeWatched ) {i f ( movieMinutes < 30) {whoAmI = ” s t i l l Q u i l l ” ;} else i f (30 < movieMinutes && movieMinutes <= 60) {whoAmI = ” almostThere ” ;} else i f (60 < movieMinutes && movieMinutes <= 100) {whoAmI = ” hope l e s s ” ;} else {whoAmI = ” StarLord ” ;}movieMinutes++;}

return whoAmI ;}

continued on page 27

CS/SE 2S03 27/28

public stat ic St r ing q3d ( int timeWatched ) {St r ing whoAmI = ” Peter Qu i l l ” ;int movieMinutes = 0 ;i f ( timeWatched > 122) {timeWatched = 122 ;}

while ( movieMinutes <= timeWatched ) {i f ( movieMinutes > 30) {whoAmI = ” s t i l l Q u i l l ” ;} else i f (30 < movieMinutes && movieMinutes <= 60) {whoAmI = ” almostThere ” ;} else i f (60 < movieMinutes && movieMinutes <= 145) {whoAmI = ” hope l e s s ” ;} else i f ( movieMinutes > 145) {whoAmI = ” StarLord ” ;}

movieMinutes += 1 ;}

return whoAmI ;}}

continued on page 28

CS/SE 2S03 28/28

For your reference:

D.• Θ(x, e,m,G)) = (m(e(x)),m) if k(x) = mutable

• Θ(x, e,m,G)) = (e(x),m) if k(x) = constant

• Θ(c, e,m,G)) = (c,m) if c is a literal

• Θ(x op y, e,m,G) = (v op w,m′′) where(v,m′) = Θ(x, e,m,G)(w,m′′) = Θ(y, e,m′, G)

• Θ((b) ? t : u) = Let (v,m′) = Θ(b, e,m,G) in

{Θ(t, e,m′, G) if v = true

Θ(u, e,m′, G) if v = false

• Θ(f(t1, · · · , tn), e,m,G) = X whereLet x1, · · · , xn be formal parameters and p the body of f .Let (v1,m1) = Θ(t1, e,m,G)(v2,m2) = Θ(t2, e,m1, G)...(vn,mn) = Θ(tn, e,mn−1, G)Let e′ ⊆ G be the declaration of (global) variablesLet ri (1 ≤ i ≤ n) be fresh references

Let ai =

{(x(i) = vi) if K(xi) = constant

(x(i) = ri) if K(x) = mutable

Let e′′ = e′ ⊕ a1 ⊕ · · · ⊕ anLet J = {J1, · · · , Jk} in ascending order, k ≤ n, J ⊆ {1, · · · , n}m′′ = mn ⊕ (rj1 = vj1)⊕ · · · ⊕ (rjk = vjk)

X =

{(v,m′′′) if Σ(p, e′′,m′′, G) = (return, v,m′′′)

undefined otherwise

And

• Σ({T x = t; s}, e,m,G)) =Let (v,m′) = Θ(t, e,m,G) in Σ(s, e⊕ (x = r),m′ ⊕ (r = v), G) where r fresh e,m

• Σ({final T x = t; s}, e,m,G)) = Let (v,m′) = Θ(t, e,m,G) in Σ(s, e⊕ (x = v),m′, G)

• Σ(x = t; , e,m,G) = Let (v,m′) = Θ(t, e,m,G) in (normal,m′ ⊕ (e(x) = v))

• Σ({s1 s2}, e,m,G) = Let X = Σ(s1, e,m,G) in{Σ(s2, e,m

′, G) if X = (normal,m′)

(return, b,m′) if X = (return, v,m′)

• Σ(if (b) s1 else s2, e,m,G) = Let (v,m′) = Θ(b, e,m,G) in

{Σ(t, e,m′, G) if v = true

Σ(u, e,m′, G) if v = false

• (The definition of loop is unchanged)Σ(while (b) s, e,m,G) = limn Σ(pn, e,m,G) where · · ·

• Σ(return t; , e,m,G) = Let (v,m′) = Θ(t, e,m,G) in (return, v,m′)

• Σ(f(t1, · · · , tn); , e,m,G) =case Σ(p, e′′,m′′, G) of(normal,m′′′) −→ (normal,m′′′))(return,m′′′) −→ (normal,m′′′)

THE END.