AP CSA Unit 1 Day 2: Primitive Ranges

Unit 1 Foundation (Cycle 1) Day 2 of 28 Foundation

Primitive Type Ranges and Defaults

Section 1.2 — Variables and Data Types

Key Concept

Java has eight primitive types, but the AP CSA exam focuses on int, double, and boolean. When a double variable is assigned an integer value like 2, Java automatically promotes it to 2.0 — this is called implicit widening conversion. The System.out.println method always prints double values with at least one decimal place. Also remember that local variables in Java must be explicitly initialized before use, unlike instance variables which receive default values.

Consider the following variable declarations.

boolean flag; int count = 0; double rate = 2; System.out.println(count + " " + rate);

What is printed as a result of executing the code segment?

Answer: (B) 0 2.0

Trace the variables:

count: Declared as int and initialized to 0. Prints as 0.

rate: Declared as double and assigned 2. The integer 2 is automatically promoted to 2.0 when stored in a double. Java always prints double values with at least one decimal place.

flag: Declared but never printed, so it does not affect output. Note: boolean instance variables default to false, but local variables must be initialized before use.

Why Not the Others?

(A) A double always prints with a decimal point. 2 stored as a double becomes 2.0, not 2.

(C) The variable flag is never printed. Only count and rate appear in the println statement.

(D) Java prints double values with exactly one decimal place for whole numbers: 2.0, not 2.00.

Common Mistake

A common trap is confusing how double values display. Java always shows at least one digit after the decimal point, so 2 stored in a double prints as 2.0.

AP Exam Tip

When tracing output, pay attention to the declared type of each variable. An int prints without decimals; a double always shows a decimal point. This detail catches many students on the exam.

Review this topic: Section 1.2 — Variables and Data Types • Unit 1 Study Guide

More Practice

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.