CS 1401, Quiz #2, Solutions

Date: Tuesday, January 30, 2007
Class section (9:00 am or 10:30 am): _______________________________

1. For each of the following strings, write "yes" or "no" depending on whether the string is a legal names of a Java variable.; if your answer is "no", briefly explain why.

January30 yes

30January no: does not start with a letter

January 30 no: has a blank space inside

January_30 yes

January30$ yes (but not recommended)

2. Translate the following sequence of Java statements into English; show, step by step, what will happen after each of these statements.

int today;

declare a new variable of type int; its name is
"today", its value is undefined
   --------------
  |      ?       |
   --------------
       today
today = 30;
to the variable "today", we assign a value 30
   --------------
  |     30       |
   --------------
       today
today = today + 1;
we add 1 to the previous value of the variable "today", and assign the
result to the same variable
   --------------
  |     31       |
   --------------
       today
3. What is the result of the following Java operations:

11 / 5 the result is 2

11.0 / 5.0 the result is 2.2

11 % 5 the result is 1

11.0 % 5.2 the result is 11.0 - 2 * 5.2 = 11.0 - 10.4 = 0.6

-11 % -5 the result is -1