Name: 
 

Final Exam Review #1



Multiple Choice
Identify the choice that best completes the statement or answers the question.
 

 1. 

Which statement correctly generates a random number between 1 and 100?
a.
number = Math.random() * 100 + 1;
b.
number = Math.random() * 100 - 1;
c.
number = Math.random() * 99 + 1;
d.
number = Math.random(100);
 

 2. 

Given the following declarations, which statement is an explicit cast?
int n;
double pi = 3.1415;
a.
n = int(pi);
c.
n = (int) pi;
b.
pi = int(n);
d.
n = pi;
 

 3. 

Which of the following variable names is NOT valid?
a.
pShape
c.
3rdClass
b.
ThirdClass
d.
discount
 

 4. 

What will these lines output?
    char letter = ‘A’;
    if (letter == ‘a’) {
        System.out.print (“Hello “);
    }
    System.out.println(“Welcome to Java World”);
       
a.
Welcome to Java World
c.
Hello Welcome to Java World
b.
Welcome to Hello World
d.
Hello
Welcome to Java World
 

 5. 

Which of the following is one of the Java primitive types?
a.
boolean
c.
int
b.
double
d.
all of the above
 

 6. 

Which of the following is not a primitive data type?
a.
float
c.
char
b.
double
d.
String
 

 7. 

Which of the following is the correct statement given
int x = 5;
a.
if (x = 5)
c.
if (x is 5)
b.
If (x == 5)
d.
if (x == 5)
 

 8. 

Block comments in Java are marked with
a.
//
c.
/*  */
b.
\\
d.
%
 
 
Code Segment 4

    if (letter >= ‘A’) && (letter <= ‘Z’) {
        isUpperCase = true;
    } else {
        isUpperCase = false;
    }
     
 

 9. 

In Code Segment 4, what data type is the variable isUpperCase?
a.
double
c.
boolean
b.
char
d.
int
 

 10. 

The Boolean operators in Java are
a.
&&, ||, !
c.
+,  -,  *,  /
b.
<, >, ==, !=, >=, <=
d.
<<, >>, ::
 

 11. 

Which of these statements correctly declares a char variable.
a.
char c2 = “W”;
c.
All of the above
b.
char c1 = ‘W’;
d.
char c3 = W;
 

 12. 

Which value is a floating point data type?
a.
0
c.
‘Q’
b.
true
d.
55.7
 

 13. 

Which statement must be included to use Math.PI in your program?
a.
import java.lang.*;
c.
public class Math
b.
import java.util.*;
d.
public static void main()
 

 14. 

Which one is a Boolean data type:
a.
2.130000+02
c.
‘Z’
b.
131
d.
false
 

Matching
 
 
Match the value with the correct data type.
a.
‘Q’
b.
false
c.
3.14159265
d.
“Jean-Luc”
e.
47
 

 15. 

double
 

 16. 

String
 

 17. 

boolean
 

 18. 

char
 

 19. 

int
 

True/False
Indicate whether the statement is true or false.
 
 
Code Segment 3

if (temperature > maxTemp) {
    message = “Too Hot”;
}
if (temperature < minTemp) {
    message = “Too Cold”;
}
message = “Just right”;
if (temperature > maxTemp) {
    message=”Too Hot”;
} else if(temperature < minTemp) {
    message = “Too Cold”;
} else {
    message = “Just right”;
}
 
 

 20. 

In Code Segment 3 both the example on the left and on the right will give the same result in every case.
 

 21. 

The following lines of code will correctly calculate 220
     double megabyte;
     megabyte = Math.power(2, 20);
 

 22. 

The following program segment will NOT output the message, “Hello Joe”. 
      String name = "Joe";     
      if (name == "Joe"){
          System.out.println ("Hello" + name);
      }
 

 23. 

These statements will cause a syntax error because the brace brackets are missing.
            if (code == ‘Q’)
                  System.out.print(“Program Stopped”);
 
 
Code Segment 4

    if (letter >= ‘A’) && (letter <= ‘Z’) {
        isUpperCase = true;
    } else {
        isUpperCase = false;
    }
     
 

 24. 

In Code Segment 4, if  letter is a‘q’ then isUpperCase will be true.
 
 

Code Segment 1
int number = 5;
if (number % 2 == 0) {
      message = “Remainder is zero”;
} else {
      message = “Remainder is one”;
}
 

 25. 

The statments above in Code Segment 1 will output “Remainder is one”.
 
 
Code Segment 2
int age, minAge;
int income, minIncome;
minAge = 18;
minIncome = 25000;

if ( (age > minAge) && (income > minIncome) ) {
      System.out.println (“Accept”);
} else {
      System.out.println (“Reject”);
}
 

 26. 

Give age = 19 and income = 21000, Code Segment 1 above will print “Reject”
 

 27. 

The statement
      char name = “Billy”;
is the correct way to declare a string in Java.
 

 28. 

A class name can contain spaces.
 

 29. 

A "void" method must not return a value.
 

 30. 

The following statement will repeat 50 times.        
      for (int i = 0; i<50; i++)
 



 
Check Your Work     Start Over