Perfect Developer basic tutorial 2 This page last modified 2011-10-29 (JAC)

Class int Quiz

1. Which of the following example strings are valid integer literals?

  1. 123
  2. 123,456
  3. 123.456
  4. 0x4a5h
  5. -123
  6. 0b123
  7. 0x123

Examples 1 and 7 are valid integer literals.
Example 2 is not valid because of the embedded comma.
Example 3 is not valid because of the embedded full-stop (but it is a valid real literal).
Example 4 is not valid because 'h' is not a valid hex digit.
Example 5 is not an integer literal, but can be parsed as the integer literal 123 preceded by the unary-minus operator.
Example 6 is not valid because 2 and 3 are not valid binary digits.


2. In Perfect, what are (respectively) the values of (-10)/3 and (-10)%3 ?

  1. -3 and -1
  2. -3 and 1
  3. -3 and 2
  4. -4 and -1
  5. -4 and 1
  6. -4 and 2

Answer 6 is correct.
Evaluating -10/3 yields -4 because integer division rounds down towards minus infinity.
Evaluating -10%3 must give a result between 0 and (3 - 1); the actual answer is 2 because that is what is left over from -10 when you subtract 3 * -4.