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

NOTES: To oblain full marks, clearty show all your works leading to the final an

ID: 3906662 • Letter: N

Question

NOTES: To oblain full marks, clearty show all your works leading to the final answer Line numbers are used for lustrations purpose only and does not form part of the codes 1.1. Assuming variable b to be of boolean type, determine the value that will be assigned to b for each of he following cases (a)b (8 Math.ceit(7.2)) 1.2. Consider the following Java codes, assume that each code segment appears in the main method. Analyse and determine the output of each program? Clearly show all relevant steps leading to the final answer 4) ie while (x

Explanation / Answer

1.1

(a) b = false since 8<10 is true and true && false = false

(b) b = true since !true = false and !false =true, true&&true = true and thus false||true is true.

(c) b = false since Math.pow(2.0,3.0) = 8 and Math.ceil(7.2) = 8 and 8>8 is false.

1.2

output:

1 is odd.
2 is even.
3 is odd.
4 is even.
5 is odd.
6 is even.

int x=0; // x is 0

int xLimit = 5;

while(x<=xLimit) //0<=5 true

{

x++; // x becomes 1

if(x%2==0)

System.out.println(x+" is even.");

else

System.out.println(x+" is odd."); //1 is odd therefore print 1 is odd.

}

//while loop will run again

while(x<=xLimit) //1<=5 true

{

x++; // x becomes 2

if(x%2==0)

System.out.println(x+" is even."); //2 is even therefore print 2 is even.

else

System.out.println(x+" is odd.");  

}

while(x<=xLimit) //2<=5 true

{

x++; // x becomes 3

if(x%2==0)

System.out.println(x+" is even.");

else

System.out.println(x+" is odd.");  //3 is odd therefore print 3 is odd.

}

while(x<=xLimit) //3<=5 true

{

x++; // x becomes 4

if(x%2==0)

System.out.println(x+" is even.");  //4 is even therefore print 4 is even.

else

System.out.println(x+" is odd.");

}

while(x<=xLimit) //4<=5 true

{

x++; // x becomes 5

if(x%2==0)

System.out.println(x+" is even.");

else

System.out.println(x+" is odd."); //5 is odd therefore print 5 is odd.

}

while(x<=xLimit) //5<=5 true

{

x++; // x becomes 6

if(x%2==0)

System.out.println(x+" is even."); //6 is even therefore print 6 is even.

else

System.out.println(x+" is odd.");  

}

while(x<=xLimit) //6<=5 false. therefore, loop stops here.