AP Computer Science A • Unit 1.2

Variables and Data Types

Detailed answer explanations for the Unit 1.2 multiple-choice practice questions.

Q1A
Q2B
Q3C
Q4A
Q5B
Q6C
Q7A
Q8B
Q9B
Q10B
Q11A
Q12A
Q13B
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the declarations inside main. Which statement is correct immediately after these declarations?

public static void main(String[] args) {
    int attempts;
    double temperature;
    boolean connected;
    String teamName;
}
AAll four names identify local variables declared inside main.
BOnly teamName is local because String is a class.
CEach declaration gives its variable the value 0.
DEach variable already contains a usable value.
Correct answer: A. A variable declared inside a method is a local variable, and all four declarations appear inside main.
Explanation

A declaration introduces a variable by writing its type followed by its name. Each of the four lines does exactly that, and each one is written inside the braces of main.

  • B is not correct: a variable is local because of where it is declared, not because of which type it has. String is a reference type, but teamName is declared inside main just like the other three.
  • C is not correct: these declarations do not assign values to the local variables, so none of them is given the value 0.
  • D is not correct: Java does not allow a local variable's value to be read until a value has been assigned to it, so none of the four contains a usable value yet.
Q2

Consider the declarations inside the same method. Which statement about the declarations is correct?

int level;
int Level;
AThey declare the same variable twice.
BThey declare two different variables because Java is case-sensitive.
COnly Level is legal because variable names must begin with uppercase letters.
DNeither declaration is legal because a variable name cannot contain letters.
Correct answer: B. Java is case-sensitive, so level and Level are two different names.
Explanation

A variable name is an identifier, so it must be written exactly the same way every time it is used. Changing the case of a letter produces a different identifier, and therefore a different variable.

  • A is not correct: the two names are not written the same way, so they do not declare one variable twice.
  • C is not correct: a name beginning with an uppercase letter is legal. Variable names normally begin with a lowercase letter in Java style, but that is a style convention rather than a rule that makes level illegal.
  • D is not correct: a variable name may contain letters. The rules are that it cannot contain spaces, cannot begin with a digit, and cannot be a Java keyword.
Q3

Which numbered line contains a valid and appropriately styled declaration? Select the correct line.

1  int public;
2  double ticket price;
3  boolean hasAccess;
4  String 3city;
ALine 1
BLine 2
CLine 3
DLine 4
Correct answer: C. boolean hasAccess; is the only line whose name follows the identifier rules and lower camel case.
Explanation

A declaration is a type followed by a name. The name cannot contain spaces, cannot begin with a digit, and cannot be a Java keyword. Line 3 satisfies all three rules and describes the value it stores.

  • A is not correct: public is a Java keyword, and a variable name cannot be a Java keyword.
  • B is not correct: ticket price contains a space, and a variable name cannot contain spaces.
  • D is not correct: 3city begins with a digit, and a variable name cannot begin with a digit.
Q4

Which statement correctly identifies the types used in this topic?

Aint, double, and boolean are primitive; String is a reference type.
BAll four types are primitive types.
COnly String and boolean are primitive types.
DA type is primitive only when its name begins with a lowercase letter.
Correct answer: A. int, double, and boolean are primitive types, and String is a reference type used for text.
Explanation

A variable of a primitive type holds a primitive value. String is a class rather than a primitive type, and this topic uses it to store text.

  • B is not correct: String is a reference type, so the four types are not all primitive.
  • C is not correct: String is not primitive, and this choice leaves out int and double, which are.
  • D is not correct: int, double, and boolean are the primitive types in this topic and String is the reference type; the first letter of a type's name is not what decides this.
Q5

Which declaration is most appropriate for a decimal average?

Aint average;
Bdouble average;
Cboolean average;
DString 2average;
Correct answer: B. A decimal average needs double, the primitive type for floating-point values.
Explanation

Choose a type by asking what kind of value the variable needs to store. An average that can contain a fractional part is a floating-point value.

  • A is not correct: int is the primitive type for whole numbers, so it cannot represent the fractional part of an average.
  • C is not correct: boolean stores exactly true or false, not a numeric average.
  • D is not correct: a variable name cannot begin with a digit, so 2average is not a valid name.
Q6

Which type is most appropriate for the number of students in a class?

Adouble
Bboolean
Cint
DString
Correct answer: C. A count of students is a whole number, and int is the primitive type for whole numbers.
Explanation

Choose a type by asking what kind of value the variable needs to store. The number of students in a class is counted in whole numbers.

  • A is not correct: double is the primitive type for floating-point values, and a count does not need a fractional part.
  • B is not correct: boolean stores exactly true or false, not a count.
  • D is not correct: String is the reference type used for text, not for a counted number.
Q7

Which type is used to store text such as "North Hall"?

AString
Bint
Cdouble
Dboolean
Correct answer: A. String is the reference type used to store text.
Explanation

String is a class rather than a primitive type. This topic uses it in the same way as the other types, to store the kind of value it is meant for.

  • B is not correct: int is a primitive type for whole numbers, so it does not hold the characters of a name.
  • C is not correct: double is a primitive type for floating-point values, so it does not hold text.
  • D is not correct: boolean stores exactly true or false, which are logical values rather than text.
Q8

Where may a local variable declared inside main be used?

AAnywhere in every class in the program
BAfter its declaration and inside the braces that contain it
COnly on the line where it is declared
DOnly inside comments
Correct answer: B. A local variable's scope is the part of the source code where its name may be used.
Explanation

For a variable declared inside main, the name may be used after its declaration and before the closing brace of main.

  • A is not correct: the scope of a local variable is the block that contains its declaration, not every class in the program.
  • C is not correct: the name may also be used on later lines, up to the closing brace of the block that contains it.
  • D is not correct: scope is the region of source code in which the name may be used by the program's statements.
Q9

Which statement about boolean is correct?

AIt is a reference type for text.
BIts values are exactly true and false.
CIts values are the numbers 0 and 1.
DIt can store any numeric value.
Correct answer: B. boolean stores exactly true or false.
Explanation

boolean is one of the primitive types introduced in this topic, and it represents logical values.

  • A is not correct: String is the reference type used for text; boolean is a primitive type.
  • C is not correct: the values of a boolean are true and false, which are not numbers.
  • D is not correct: int and double are the numeric types in this topic; boolean does not store numeric values.
Q10

Which variable name best follows lower camel case and describes its role?

ACompleted Tasks
BcompletedTasks
C3completedTasks
Dpublic
Correct answer: B. completedTasks contains no spaces, does not begin with a digit, is not a keyword, and follows lower camel case.
Explanation

In Java style, variable names usually use lower camel case: the first word begins with a lowercase letter, and later words begin with uppercase letters. A variable name should also clearly describe the value it stores.

  • A is not correct: Completed Tasks contains a space, and a variable name cannot contain spaces.
  • C is not correct: 3completedTasks begins with a digit, and a variable name cannot begin with a digit.
  • D is not correct: public is a Java keyword, and a variable name cannot be a Java keyword.
Q11

What does the statement int retries; do?

AIt declares an int variable named retries without assigning it a value.
BIt gives retries the value 0.
CIt prints the word retries.
DIt creates a String variable.
Correct answer: A. A declaration introduces a variable by writing its type followed by its name.
Explanation

Read the declaration from left to right: int is the type and retries is the variable name. A declaration of this form does not assign a value to the local variable.

  • B is not correct: declarations of this form do not assign values, so retries is not given the value 0.
  • C is not correct: a declaration introduces a variable; it does not display anything.
  • D is not correct: the type written in this declaration is int, not String.
Q12

Which declaration is invalid because the variable name is a Java keyword?

Aint public;
Bint publicCount;
Cdouble temperature;
Dboolean active;
Correct answer: A. A variable name cannot be a Java keyword, and public is a Java keyword.
Explanation

A variable name is an identifier and must follow Java's identifier rules. The name class is invalid for the same reason.

  • B is not correct: publicCount is not a keyword. It contains no spaces and does not begin with a digit.
  • C is not correct: temperature is a valid name written in lower camel case.
  • D is not correct: active is a valid name written in lower camel case.
Q13

Why must Java know a variable's data type before the variable can be used?

AThe type tells Java which method will run next.
BThe type determines which values the variable can represent and which uses are permitted.
CThe type guarantees that the variable already contains a usable value.
DThe type converts every stored value into text.
Correct answer: B. A data type tells Java which kind of value a variable may store and which operations may use that value.
Explanation

The label alone is not enough. The name identifies the variable, and the type tells Java what kind of value belongs in the container.

  • A is not correct: the type describes the kind of value the variable may store; it does not select which method runs next.
  • C is not correct: a declaration does not assign a value, so knowing the type does not mean the variable already holds a usable value.
  • D is not correct: String is the reference type used for text; the other types do not convert their stored values into text.