You are on page 1of 2

Name:

Date:
Section:

Variables and Types

1. Which of the following is a proper way to declare and initialize a variable in Java?

A. myInteger = 100;

B. char = 'a';

C. int myNumber = 10;

D. "Variable"

2. Consider the following code snippet.


public static void main(String args[]){

final int z;

z = 20;

z = 30;

System.out.println(z);

What value of z is actually printed?

A. 20
B. 30
C. This code gives an error.
D. Nothing is printed.

3. What are the memory values associated with the variables x, y, and z after the code snippet below executes?
int x = 7;

double y = 2.0;

boolean z = false;

x = x + 3;

z = true;

A. x holds the int value 7, y holds the double value 2.0 and z holds the boolean value false .

B. x holds the int value 10, y holds the double value 2.0 and z holds the boolean value true .

C. This code snippet will result in a compile time error.


D. x holds the int value 10, y holds the double value 2.0 and z holds the boolean value false .

4. Which of the following variable names follows best practices for naming a variable?

A. 5apples
B. someVariable
C. applesnum
D. numApples
5. What does the keyword final do?

A. It’s necessary to declare a variable.


B. Enables the use of println on a variable.

C. It prevents variables from being altered.


D. It indicates that the program has finished executing.

6. Which Java Data Type would be the best suited to represent whether or not a student has completed their homework?

A. int

B. double

C. String

D. boolean

7. Which Java Data Type would be best suited to represent the amount of money in a bank account?

A. double

B. boolean

C. int

D. String

8. Which Java Data Type would be the best suited to represent the number of days left until the AP Computer Science
Exam?

A. double

B. String

C. int

D. boolean

You might also like