Flashcard library · Computer Science

AP Computer Science: Java Basics

This flashcard deck covers the fundamental Java concepts essential for success in AP Computer Science A. It focuses on the most frequently tested material, including basic syntax, data types, control structures, methods, and core object-oriented programming principles. Use these cards to reinforce your understanding and prepare thoroughly for the exam.

Want to actually learn these?

Create a free NoteFren account to study with spaced repetition, or turn your own notes into cards like these.

What are the two main categories of data types in Java?

Java data types are broadly categorized into primitive types (like int, double, boolean) and reference types (like String, arrays, and objects of user-defined classes).

List four common primitive data types in Java and what they store.

`int` stores whole numbers, `double` stores floating-point numbers, `boolean` stores true/false values, and `char` stores single characters.

Explain the difference between `==` and `.equals()` when comparing objects in Java.

`==` compares memory addresses to check if two object references point to the same object, while `.equals()` (if overridden) compares the actual content or state of two objects.

What is the purpose of the `main` method in a Java program?

The `main` method is the entry point for any Java application. When a Java program runs, the Java Virtual Machine (JVM) looks for and executes this method first.

How do you declare and initialize an integer variable named `age` with the value `30` in Java?

You declare and initialize it as `int age = 30;`.

What is an `if-else if-else` statement used for?

An `if-else if-else` statement allows a program to execute different blocks of code based on multiple conditional expressions being true or false, providing branching logic.

Describe the purpose of a `for` loop.

A `for` loop is used for iterating a known number of times, typically when you have a counter variable that needs to be initialized, checked against a condition, and updated.

When would you use a `while` loop instead of a `for` loop?

A `while` loop is typically used when the number of iterations is unknown and depends on a condition being met, continuing as long as the condition evaluates to `true`.

What is a Java class?

A Java class is a blueprint or a template for creating objects, defining their attributes (instance variables) and behaviors (methods).

What is an object in Java?

An object is an instance of a class, representing a real-world entity with specific state (data) and behavior (methods) defined by its class.

How do you create a new object of a class named `Dog`?

You create a new object using the `new` keyword, like this: `Dog myDog = new Dog();`.

What is a constructor in Java?

A constructor is a special method used to initialize new objects of a class. It has the same name as the class and does not have a return type.

How do you declare and initialize an array of 5 integers named `numbers`?

You declare and initialize it as `int[] numbers = new int[5];` or `int[] numbers = {1, 2, 3, 4, 5};`.

What is the purpose of the `System.out.println()` method?

`System.out.println()` is used to print output to the console, followed by a new line character, making it easy to display information or debug.

Explain what a method signature consists of.

A method signature consists of the method's name and the number, type, and order of its parameters. The return type is NOT part of the method signature.