CS110 JAVA Practice Quiz #3

More on the Elements of Java




  Choose the best alternative:
  1. An error created by violating the required syntax, or grammar, of a programming
    language is called a _______.

    NOTES:
    Two primitive data type for numbers in Java are int and double.


  2. 250 is an primitive data type.

  3. -250.00 is a(n) primitive data type.

  4. -3.5e-4 is primitive data type.

  5. 4980000 a byte primitive data type.

  6. What output is produced by the segment of code shown below:
    double tax = .177777;
    System.out.printf("%.2f", tax);

  7. When displaying to the console, the printf is required for formatting numeric values.

    Java Expressions
    Evaluate the following expressions.

  8. 6 - 3 - 3
  9. 6 - (3 - 3)
  10. 3 + 5 / 2
  11. 5 / 2.0
  12. 5.0 / (6 - 4 % 6)
  13. 2 - 10 % 4
  14. 100 / 5 / 2 % 7
  15. ( 1 + 5) % 3 * 2 - 2

    Java Assignment Statements

  16. What output is produced by the segment of code shown below:
    int x = -22;
    x = -x;
    System.out.println(x);

  17. What output is produced by the segment of code shown below:
    int x = 12;
    x += 12;
    System.out.println(x);

  18. What output is produced by the segment of code shown below:
    final int MAX = 5;
    int x = 12;
    x -= MAX;
    System.out.println(x);

  19. What output is produced by the segment of code shown below:
    TIP: You will need your ascii table for this one.
    final char LETTER = 'A'
    int x = LETTER;
    System.out.println(x);

  20. What output is produced by the segment of code shown below:
    char letter = 'c';
    int x = 2;
    letter = letter + x;
    System.out.println(letter);

  21. What output is produced by the segment of code shown below:
    double fx = 3.999999;
    int x = (int) fx;
    System.out.println(x);;

  22. The remainder operator (%) can operate on real numbers.

  23. The basic assignment operator is

  24. The escape sequence that produces a newline is


Answers

  1. syntax error. Syntax errors are misspelled words, missing punctuation, etc.
  2. int only. Remember that 250 is an integer and 250.0 is a double.
  3. double. -250.00 is clearly a real number.
  4. a double. -3.5e-4 is a real number represented in scientific notation.
  5. is not. 4980000 cannot be represented by a byte because it exceeds byte limitations.
  6. .18. Round off occurs at the second decimal place.
  7. true
  8. 0 because 6 - 3 - 3 = 3 - 3 = 0
  9. 6 because 6 - (3-3) = 6 - 0 = 6
  10. 5 because because 3 + 5/2 = 3 + 2 = 5 Remember: integer division yields an integer.
  11. 2.5 because 5 / 2.0 = 2.5 .... Real division yields a real number!
  12. 2.5 because 5.0 / (6-4%6) = 5.0 / (6 - 4) = 5.0 / 2 = 2.5 ----Remember: 4 % 6 = 4
  13. 0 because 2 - 10 % 4 = 2 - 2 = 0 -----Remember: 10 % 4 = 2
  14. 3 because 100 / 5 / 2 % 7 = 20 / 2 % 7 = 10%7 = 3 ----Remember: Calculate from left to right.
  15. -2 because ( 1 + 5) % 3 * 2 - 2 = 6 % 3 * 2 - 2 = 0 * 2 - 2 = 0 - 2 = -2
  16. 22 Remember, x = -x; simply negates x.
  17. 24 because x += 12; can be rewritten as x = x + 12.
  18. 7 because x -= MAX; can be rewritten as x = x - MAX;
  19. 65 because 'A' can also be represented as the integer 65.
  20. e because 'c' + 2 = 'e' Remember, characters can be evaluated as integers.
  21. 3 because fx is a double, x is an integer. 3.99999 can be coerced into an integer by truncating the fraction.
  22. TRUE: The % operator can operate on integers and real numbers.
  23. = The basic assignment operator is =
  24. '\n' because endl is called a manipulator.