CECS 174                                        QUIZ #2                           NAME:
                                                         

Answers at the end of the quiz.

1. Write a complete code to draw a line in 50 pixel horizontally using Title class.
Answer

2.  a. Declare a variable that represents a test score.
Answer:  

b. Declare a variable that represents whether an answer has been true or false, and declare and initialize a accumulated variable that
represents and initialize the total test score to 0.

Answer:

3.  What is the output of the coding below?
a.
int x;
x = 15/2 + 4.7;
System.out.println(x);
System.out.println(21%3);
x = 8-3*2+4;
System.out.println(x);
Answer

b. 
int x;
x =  static_cast<float>(5)/2  +4.7;
System.out.println(x);

Answer  
 

4. Convert the following math expression to the Java expression.

  
                  A 4   +  BC
      D  =  --------------------
                  C  +  (D – A)

where A, B,C  and D are variables.

Answer


5. If x = 5, y = 2 and z = 10, what is the value of z for each of the following expressions?

a.  z = x*y%z + y;
Answer

b. z = (x+y)%4*2 - 10/3+4;
Answer

Solution
1.
World w1 = new World();
Turtle t1 = new Turtle(w1);
t1.forward(50);

2.
a.  int testScore;
b.  boolean  answer, int totalscore = 0;

3.
a.
 11
0
6
b.

 

4.
D =  (Math.pow(A, 4.0) + B*C)/(C + (D - A));

5.
a. 
z = 5*2%10 + 2 = 10%10 + 2 = 0 + 2 = 2
b. z = (5+2)%4*2 -10/3+4 = 7%4*2-10/3+4 = 3*2-3+4 = 6-3+4 = 7