CS 1401, Quiz #2

Date: Monday, September 10, 2007
Class section (9:30 am or 11: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.

September10 yes

10September no: does not start with a letter

September 10 no: has a blank space inside

September_10 yes

priceInUS$ 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.

double priceInDollars;

declare a new variable of type double; its
name is "priceInDollars", its value is undefined
   --------------
  |      ?       |
   --------------
   priceInDollars
priceInDollars = 3.0;
to the variable "priceInDollars", we assign a value 3.0
   --------------
  |     3.0      |
   --------------
   priceInDollars
priceInDollars = priceInDollars * 0.5;
we multiply the previous value of the variable "priceInDollars"
by 0.5, and assign the result to the same variable
   --------------
  |     1.5      |
   --------------
   priceInDollars
3. What is the result of the following Java operations:

1 / 2 the result is 0

1.0 / 2.0 the result is 0.5

1 % 2 the result is 1

1.0 % 2.0 the result is 1.0

-1 % -2 the result is -1