BPJ 04-06 Quiz

  1. What is the value of b in the Java program below?

     int b = 5 + 1 / 6 + 2 * (5 / 6);
    
  2. What is the output of the code segment below?

     int c = 60;
     int d = 8;
     System.out.println(c % (++d));
    
  3. What is the value of g in the segment below?

     double g = -1;
     g = (int)(3.0 + 4)/(1 + 4.0) * 2 - 3;
    
  4. Is the code below allowed?

     int x = 7;
     x += 1.93;
    
    1. Yes, the answer is 9.
    2. Yes, the answer is 8.
    3. No, you cannot add an int plus a double.
    4. No, you cannot store a double in an int.
  5. Is double h6 = 83; legal?

  6. What is the result?

     double r = (double) (30/4);
     System.out.println(r);
    
  7. What is the result?

     int s = 31 % 4;
     int t = 17 / 5;
     double ans = s + t;
     System.out.println(ans);
    
  8. Is this legal? Include a fix if not. Answer: “yes” or “no, (fix here)”.

     int u = Math.abs(-19.7);
    
  9. Is this legal? Include a fix if not. Answer: “yes” or “no, (fix here)”.

    double v = Math.cos(0.125);
    
  10. Write a short program to get the computer to print out the mathematical computation and its integer result.

     50 + 30 / 60 + 8 * 13 / 7 + 1234 * (-4321)
    
  11. Write a segment of code that declares an appropriate variable w and initializes it with the following value. Have the computer do the math.

    $$ 3 \pi \cos(60^\circ) + 1.75^4 $$

    Using $\cos(60^\circ) = 0.5$, this evaluates to 14.091…