Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Assertions. You will identify various assertions as being either always true,

ID: 658559 • Letter: 1

Question

1. Assertions. You will identify various assertions as being either always true, never true or sometimes true/sometimes false at various points in program execution. The comments in the method below indicate the points of interest public static int mystery(int x) int y 1; int z 0; // Point A while (x > y) { // Point B x=x/2; // Point C y=y*2; // Point D /I Point E return z; Copy the table below onto a sheet of paper and fill it in with the words ALWAYS, NEVER or SOMETIMES. x > y Point A Point B Point C Point D Point E

Explanation / Answer

x>y

z>0

Y%2==0

Point A

Sometimes true / Sometimes false

Never true

Never true

Point B

Always true

Never true

Never true

Point C

Sometimes true / Sometimes false

Always true

Never true

Point D

Sometimes true / Sometimes false

Always true

Always true

Point E

Never true

Sometimes true / Sometimes false

Sometimes true / Sometimes false

Explanation:

Point A:

1)      X>y: It depends on value of x, sometimes value could be less than y and sometimes it could be more. Thus, it is sometimes true / sometimes false.

2)      Z>0: Here z=0. Means it will never be greater than 0. Thus, it is never true.

3)      Y%2==0: It is never true because at this position, y=1 means its mod can’t be 0. Thus, it is never true.

Point B:

1)      X>y: If compiler moves to point B, means at that point value of x is always greater than y. Thus, it is always true.

2)      Z>0: Here z=0. Means it will never be greater than 0. Thus, it is never true.

3)      Y%2==0: It is never because at this position, y=1 means its mod can’t be 0.

Point C:

1)      X>y: Here, x = x / 2. It depends on value of x, sometimes value could be less than y and sometimes it could be more. Thus, it is sometimes true / sometimes false.

2)      Z>0: Here z=z+x-y. Means it will always be greater than 0. Thus, it is always true.

3)      Y%2==0: It is never because at this position, y=1 means its mod can’t be 0.

Point D:

1)      X>y: It depends on value of x, sometimes value could be less than y and sometimes it could be more.

5)      Z>0: Here z=z+x-y. Means it will always be greater than 0.

2)      Y%2==0: It is always because at this position, y=y*2 means its mod will be 0. Thus, it is always true.

Point E:

1)      X>y: It means value of x will be never greater than y.  Thus, it is Never true.

2)      Z>0: Here z=0 or z=z+x-y. Means sometimes it can be greater than 0. Thus, it is sometimes true / sometimes false.

3)      Y%2==0: It is sometimes because at this position, if compiler enters into the loop then y % 2 ==0 otherwise y=1 means its not true. Thus, it is sometimes true / sometimes false.

x>y

z>0

Y%2==0

Point A

Sometimes true / Sometimes false

Never true

Never true

Point B

Always true

Never true

Never true

Point C

Sometimes true / Sometimes false

Always true

Never true

Point D

Sometimes true / Sometimes false

Always true

Always true

Point E

Never true

Sometimes true / Sometimes false

Sometimes true / Sometimes false