Perfect Developer basic tutorial 2 | This page last modified 2011-10-29 (JAC) |
1. Which of the following example strings are valid integer literals?
123
123,456
123.456
0x4a5h
-123
0b123
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
?
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.