FINAL EXAM
QUESTIONS AND CORRECT DETAILED
ANSWERS LATEST EXAM UPDATE
Which of the following are valid identifiers?
a) My name
b) my_Name
c) number1
d) 1number
e) $value
f) First&LastName - ✔✔✔ Correct Answer > Which are valid or
not valid and why:
a) My name - no spaces
b) my_Name - underscores are valid
Page 1 of 22
,c) number1 - valid as long as numbers are not the first character
d) 1number - cannot start with a number
e) $value - $ is okay
f) First&LastName - & not a valid character
What is stored in num after the following program segment?
int num = 10; num ++; num *= 2; num --; - ✔✔✔ Correct Answer >
21
int num = 10; //initializes int num to be 10 num ++; //increases num
by 1 - 11 num *= 2; // multiplies num by 2 - 22 num --; // decreases
num by 1 - 21
What output is produced by the following segment? Or is there a
syntax error?
System.out.println("\"One\"");
System.out.print(2 + "Two");
Page 2 of 22
, System.out.println("\"Three\"" + 3); - ✔✔✔ Correct Answer > "One"
2Two"Three"3
\" prints a ". The first statement has System (the object) call println as
the method, so it finishes the line.
2 concatenates to Two and the second statement concatenates to
2Two because System calls print, not println.
3 concatenates to "Three" and finishes the line because System
calls the method println.
Using Math.random, how do you generate a random number within
the range -50 to 50? - ✔✔✔ Correct Answer > (int)(Math.random() *
101) - 50
When working with Math.random(), you first have to make the range
0 to the desired number, so you subtract the negative 50 to the
positive 50, so you get a resulting range of 0 to 100.
The Math.random() range without any operators and values generates
a range between 0 and .999999, so if you were to just multiply the
method by 100, the returned range would be 0 to 99.9999, so 1 must
be added to get the correct range (100.99999999).
Page 3 of 22