AP CSA - Unit 1

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

30 Terms

1
New cards

How do you convert a number from base 10 to any other base?

Repeatedly divide the base 10 number by the target base, recording the remainder. When the quotient reaches 0, read the remainders in reverse (bottom to top) to get the number in the new base. If the base uses special digits (like A–F for hexadecimal), convert any remainders ≥10 into the correct symbol.

2
New cards
How do you convert a number from any base to base 10?
Multiply each digit by the base raised to the power of its position (counting from right to left, starting at 0), then add all the products together. If the original number uses letters (like A–F in hex), convert them to their decimal equivalents first (A=10, B=11, ..., F=15).
3
New cards
What are the 8 primitive data types in Java?
The 8 primitive data types in Java are: byte, short, int, long, float, double, char, and boolean.
4
New cards
Describe the byte primitive data type.
Stores whole numbers (integers) from -128 to 127. It's the smallest integer type, taking up 1 byte (8 bits) of memory. Use it when memory is critical and the values will always be very small.
5
New cards
Describe the short primitive data type.
Stores whole numbers (integers) from -32,768 to 32,767. It takes up 2 bytes (16 bits) of memory. Use it when byte is too small but you still want to save memory compared to int.
6
New cards
Describe the int primitive data type.
Stores whole numbers (integers) from approximately -2 billion to +2 billion (±2,147,483,647). It's the default and most commonly used integer type, taking up 4 bytes (32 bits) of memory.
7
New cards
Describe the long primitive data type.
Stores very large whole numbers (integers), far exceeding the range of int (from approximately −9×1018 to +9×1018). It takes up 8 bytes (64 bits) of memory. Used for values like timestamps or large financial calculations.
8
New cards
Describe the float primitive data type.
Stores single-precision floating-point numbers (decimal numbers) with about 6-7 decimal digits of precision. It takes up 4 bytes (32 bits) of memory. Less precise than double, often used where memory is a constraint or high precision isn't critical.
9
New cards
Describe the double primitive data type.
Stores double-precision floating-point numbers (decimal numbers) with about 15 decimal digits of precision. It's the default and most commonly used type for decimal numbers, taking up 8 bytes (64 bits) of memory, due to its higher accuracy.
10
New cards
Describe the char primitive data type.
Stores a single Unicode character (e.g., 'A', 'b', '!', '5', '€'). It is represented by a single character enclosed in single quotation marks and takes up 2 bytes (16 bits) of memory.
11
New cards
Describe the boolean primitive data type.
Stores only one of two possible values: true or false. It takes up 1 bit of storage (though its size in memory can vary depending on the JVM). It's fundamental for logical operations and control flow.
12
New cards
Do primitive types in Java have "functions" or "methods" associated with them like objects do?
No, primitive types themselves do not have methods. However, Java provides "wrapper classes" (e.g., Integer, Double, Boolean) that correspond to each primitive type. These wrapper classes are objects and have methods that can perform operations on primitive values (e.g., Integer.parseInt(), Double.toString()).
13
New cards
What is the difference between System.out.print() and System.out.println() in Java?
System.out.print() prints the specified output to the console and keeps the cursor on the same line. System.out.println() (print line) prints the output and then moves the cursor to the next line, effectively adding a newline character at the end.
14
New cards
How do you print a double quotation mark (") within a string literal in Java?
You must "escape" the double quotation mark using a backslash (\). For example: System.out.println("He said, \"Hello!\""); This would output: He said, "Hello!"
15
New cards
How do you print a new line within a string literal in Java?
You use the escape sequence \n. For example: System.out.print("First line.\nSecond line."); This would output:
First line.
Second line.
16
New cards
How do you print a backslash character () within a string literal in Java?
You must escape the backslash itself by using two backslashes (\\). For example: System.out.println("This is a backslash: \\"); This would output: This is a backslash: \
17
New cards
What is the purpose of the semicolon (;) in Java?
The semicolon (;) is used to terminate a statement in Java. Almost every statement (e.g., variable declarations, method calls, expressions) must end with a semicolon. It tells the compiler that the statement is complete.
18
New cards
What is the primary Java class used for reading input from the console?
The Scanner class (java.util.Scanner). It can read various types of input, including numbers, strings, and more, from different sources like the console or files.
19
New cards
How do you set up the Scanner class in your Java code to read input from the standard input (console)?
Import the Scanner class: import java.util.Scanner; (usually at the top of your file).
Create an instance of the Scanner object, typically linked to System.in (the standard input stream): Scanner scanner = new Scanner(System.in);
20
New cards
How do you read different types of data (e.g., integers, decimals, whole lines of text) using the Scanner object?
Use specific methods for each data type:
int: scanner.nextInt();
double: scanner.nextDouble();
String (single word): scanner.next();
String (entire line, including spaces): scanner.nextLine();
boolean: scanner.nextBoolean();
21
New cards
Why is it important to close the Scanner object after you are done taking input, and how do you do it?
It's important to close the Scanner to release system resources (like the input stream System.in) that it's using. Failing to do so can lead to resource leaks in larger applications. You close it by calling the close() method: scanner.close();
22
New cards
What are the five basic arithmetic operators in Java, and what do they do?
+ (Addition): Adds two operands.
- (Subtraction): Subtracts the second operand from the first.
* (Multiplication): Multiplies two operands.
/ (Division): Divides the first operand by the second.1
% (Modulus/Remainder): Returns the remainder of a division.
23
New cards
Explain how integer division (/) works in Java when both operands are integers, and provide an example.
When both operands of the division operator (/) are integers, Java performs integer division, which truncates any decimal part, resulting in an integer.
Example: 10 / 3 results in 3 (not 3.33). 7 / 2 results in 3.
24
New cards
How can you ensure that division in Java results in a floating-point number (with decimals) even if the initial operands are integers?
At least one of the operands must be a floating-point type (float or double). You can achieve this by making one of the numbers a decimal literal or by type casting one of the operands.
Examples: 10.0 / 3 (result: 3.333...), 10 / 3.0 (result: 3.333...), (double) 10 / 3 (result: 3.333...)
25
New cards
What is the purpose of the modulus operator (%) in Java, and what is its result?
The modulus operator (%) returns the remainder when one integer is divided by another. It's often used to check if a number is even/odd, or to extract digits.
Example: 10 % 3 results in 1 (because 10=3×3+1). 7 % 2 results in 1.
26
New cards
What is the "Order of Operations" (operator precedence) in Java for arithmetic expressions?
Java follows standard mathematical order of operations, often remembered by PEMDAS/BODMAS:
Parentheses ()
Exponents (Java doesn't have a direct operator, usually Math.pow())
Multiplication *, Division /, Modulus % (from left to right)
Addition +, Subtraction - (from left to right)
Operators at the same level (e.g., *, /, %) are evaluated from left to right.
27
New cards
What is "type casting" in Java, and why is it used?
Type casting is the process of converting a value from one data type to another. It's used when you need to store a value of one type into a variable of a different type, or when an operation requires operands of a specific type.
28
New cards
Explain widening (implicit) casting in Java and provide an example.
Widening casting (also called implicit or automatic casting) occurs when you convert a smaller data type to a larger one (e.g., int to long, float to double). It is done automatically by Java because there's no risk of data loss.
29
New cards
Explain narrowing (explicit) casting in Java and provide an example.
Narrowing casting (also called explicit or manual casting) occurs when you convert a larger data type to a smaller one (e.g., double to int, long to short). This must be done manually by the programmer using parentheses () because there is a potential risk of data loss.
30
New cards
What is the primary risk or consideration when performing narrowing casting in Java?
The primary risk is data loss. When casting from a larger type to a smaller type (e.g., double to int), the fractional part of a floating-point number is truncated, and if the value is outside the range of the smaller integer type, it can result in incorrect or unexpected values.