• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

1-on-1 Online Java Training by a Senior Software Engineer

  • Home
  • AP CSA
    • FRQ Practice
      • Arrays
      • ArrayList
      • Strings
      • 2D Arrays
    • MCQ Practice
      • Booleans
      • Iteration
      • Arrays
      • View all Units
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

AP CSA Unit 1.2 MCQ Practice: Variables & Data Types

Home » AP CSA MCQ Practice » Unit 1: Using Objects and Methods » Variables and Data Types

AP CSA Unit 1.2 MCQ Practice: Variables and Data Types

📚 Topic 1.2 ⏱️ 15 Min Practice ✅ Current AP CSA Framework

Variables and data types are fundamental building blocks of Java programs. In the current AP Computer Science A framework, students work with three primitive data types: int, double, and boolean. The course also introduces reference types, which are used for objects.

In this interactive practice set, we cover Topic 1.2: Variables and Data Types. These 10 multiple-choice questions test your ability to declare and use variables, identify data types, recognize uninitialized local variables, understand type compatibility, and distinguish primitive values from reference types.

Key Concepts to Remember Before You Start:

  • Primitive Data Types: The three primitive data types used in AP Computer Science A are int, double, and boolean.
  • Variables: Every variable has a name and an associated data type. A primitive variable stores a value of its primitive type.
  • Uninitialized Local Variables: Local variables declared inside a method do not automatically receive default values. A local variable must be assigned a value before it is used.
  • Type Compatibility: An int value can be assigned to a double, because Java can perform the widening conversion automatically. A double cannot be assigned to an int without an explicit cast.
  • Reference Types: Reference types are used for objects rather than primitive values. For example, String is a reference type.

AP CSA 1.2: Variables and Data Types Practice

Question 1 of 10 | Score: 0
1. Consider the following code segment. What is the result of compiling and executing this code?
int score;
score = score + 5;
System.out.println(score);
Correct Answer: CIn Java, a local variable must be assigned a value before it is used. Because score is declared but has not been initialized before being used in score + 5, the code produces a compilation error.
2. Consider the following code segment. What is the result of compiling this code?
int temperature = 98.6;
System.out.println(temperature);
Correct Answer: CThe literal 98.6 is a double. Java does not automatically convert a double to an int because that conversion can lose the fractional part. An explicit cast would be required, such as int temperature = (int) 98.6;.
3. Consider the following code segment. What is printed?
double price = 50;
System.out.println(price);
Correct Answer: BAssigning an int value to a double variable is allowed. Java performs a widening conversion, so the value 50 is represented as the double value 50.0.
4. Which of the following is a valid variable declaration in Java?
Correct Answer: CA Java variable name cannot begin with a number, contain spaces, or use a reserved keyword. Therefore, A, B, and D are invalid declarations. The identifier is_Winner is valid.
5. Consider the following code segment. What is the result of compiling this code?
final int MAX_LIVES = 3;
int currentLives = MAX_LIVES;
MAX_LIVES = MAX_LIVES – 1;
System.out.println(currentLives);
Correct Answer: CA variable declared with final cannot be assigned a new value after it has been initialized. The statement MAX_LIVES = MAX_LIVES - 1; attempts to assign a new value to a final variable, causing a compilation error.
6. Which of the following correctly describes the data type of name?
String name = “Alex”;
Correct Answer: CString is a reference type, not one of the three primitive data types used in AP Computer Science A. A variable of a reference type refers to an object. The primitive types used in the course are int, double, and boolean.
7. Consider the following sequential updates. What gets printed?
int a = 5;
int b = a;
a = 10;
System.out.println(b);
Correct Answer: BWhen int b = a; executes, the current value of a, which is 5, is assigned to b. Changing a later does not change the value stored in the separate primitive variable b.
8. Consider the following code segment. What is the result of compiling this code?
int x, y = 5;
System.out.println(x + y);
Correct Answer: CThe statement int x, y = 5; declares both variables as integers, but only y is initialized. The local variable x has not been assigned a value, so using it in x + y causes a compilation error.
9. Consider the following code segment designed to swap two variables. What is printed?
int p = 2;
int q = 8;
int temp = p;
p = q;
q = temp;
System.out.println(p + ” ” + q);
Correct Answer: BFirst, temp stores the value of p, which is 2. Then p receives the value of q, which is 8. Finally, q receives the value stored in temp. Therefore, the output is 8 2.
10. Which of the following lines of code will result in a compilation error?
Correct Answer: CA boolean variable can hold only the Boolean values true or false. In option C, "true" is enclosed in quotation marks, so it is a String literal rather than the Boolean value true. A String cannot be assigned to a primitive boolean variable.

Unit 1.2 Variables and Data Types Practice Complete!

0 / 10

Why Variables and Data Types Matter for AP CSA

Variables and data types are fundamental to reading and writing Java code on the AP Computer Science A exam. Understanding what type of value a variable can store, when a variable must be initialized, and when Java allows one type of value to be assigned to another will help you analyze code accurately.

Ready for the Next Topic?

Now that you have practiced variables and data types, continue with Topic 1.3: Expressions and Output to practice Java expressions, operators, evaluation, and output.

Continue to Expressions & Output Practice →

← Back to Unit 1: Using Objects and Methods ↑ AP CSA Multiple Choice Practice Hub
Next: Expressions & Output →

Primary Sidebar

Mr Chinmay

Chinmay Patel
Book a Demo Class

Phone & Whatsapp +919853166385
javatution@gmail.com

Recent Posts

  • Learn Java in One Day: 10-Hour 1-on-1 Crash Course | JavaTutorOnline
  • Constructor in Java: Examples, Types & Constructor Overloading
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services React Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse

Additional Resources

  • AP Computer Science A Summer Prep
Copyright © 2026 JavaTutorOnline