primitives Flashcards

1
Q

What are the 3 primitive types tested on the AP CSA exam?

A

int, double, boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What keyword is used to declare a variable in Java?

A

No keyword is required. Just write the type:

Example: int x = 5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the = operator do?

A

Assigns the value on the right to the variable on the left.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the default value of an uninitialized primitive variable?

A

Primitive variables must be initialized before use in local scope. (No default.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the result of 7 / 2 in Java?

A

3 — Integer division truncates decimal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the result of 7.0 / 2 in Java?

A

3.5 — If either operand is double, result is double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the % operator do?

A

Modulo — returns the remainder.

Example: 7 % 2 = 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a compound assignment operator?

A

Shortcuts like x += 1; which means x = x + 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does x++ do?

A

Increments x by 1 (post-increment).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you cast a double to an int?

A

(int) 3.8 results in 3 — truncates the decimal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between int and double in Java?

A

int = whole numbers; double = decimal values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does boolean represent?

A

Either true or false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens if you try to divide by 0 in Java?

A

For integers: runtime error (ArithmeticException). For doubles: Infinity or NaN.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an object in Java?

A

An instance of a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you create an object?

A

ClassName obj = new ClassName();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a constructor?

A

A special method that initializes a new object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Can constructors have parameters?

A

Yes!

Example: new Dog(“Fido”);

18
Q

What keyword is used to refer to the current object?

19
Q

Can a class have more than one constructor?

A

Yes — this is called overloading.

20
Q

What is a method?

A

A function defined in a class, which can be called on objects.

21
Q

How do you call a method that returns no value?

A

obj.methodName(); — must match parameters.

22
Q

What is the difference between a void method and a non-void method?

A

Void = no return value; non-void = returns something (like int, String, etc.)

23
Q

How do you store the result of a method call that returns a value?

A

int result = obj.getValue();

24
Q

Are strings primitives in Java?

A

No — String is a reference type (class).

25
How do you declare a String variable?
String name = "Java";
26
What does s.length() return?
The number of characters in the string s.
27
What does s.substring(1, 3) return if s = "Hello"?
"el" (start at index 1, up to but not including index 3)
28
How are string characters indexed in Java?
Zero-based indexing: first character is at index 0.
29
How do you compare strings for equality?
Use .equals() — not ==
30
What does "Hello".indexOf("l") return?
2 — index of the first occurrence of "l"
31
What does "Hi" + 5 evaluate to?
"Hi5" — the number is converted to a string.
32
What are wrapper classes for int and double?
Integer and Double
33
What method converts a string to an integer?
Integer.parseInt("123") → 123
34
What method converts a string to a double?
Double.parseDouble("3.14") → 3.14
35
Are wrapper classes needed for arrays or ArrayLists of primitives?
Yes — ArrayLists cannot store primitives directly. Use wrapper classes.
36
What is Math.pow(2, 3)?
8.0 — Raises 2 to the power of 3
37
What is Math.sqrt(16)?
4.0
38
What is Math.abs(-10)?
10
39
What does Math.random() return?
A double from 0.0 (inclusive) to 1.0 (exclusive)
40
How do you generate a random int from 1 to 6?
(int)(Math.random() * 6) + 1