primitives Flashcards
What are the 3 primitive types tested on the AP CSA exam?
int, double, boolean
What keyword is used to declare a variable in Java?
No keyword is required. Just write the type:
Example: int x = 5;
What does the = operator do?
Assigns the value on the right to the variable on the left.
What is the default value of an uninitialized primitive variable?
Primitive variables must be initialized before use in local scope. (No default.)
What is the result of 7 / 2 in Java?
3 — Integer division truncates decimal.
What is the result of 7.0 / 2 in Java?
3.5 — If either operand is double, result is double.
What does the % operator do?
Modulo — returns the remainder.
Example: 7 % 2 = 1
What is a compound assignment operator?
Shortcuts like x += 1; which means x = x + 1;
What does x++ do?
Increments x by 1 (post-increment).
How do you cast a double to an int?
(int) 3.8 results in 3 — truncates the decimal.
What is the difference between int and double in Java?
int = whole numbers; double = decimal values.
What does boolean represent?
Either true or false.
What happens if you try to divide by 0 in Java?
For integers: runtime error (ArithmeticException). For doubles: Infinity or NaN.
What is an object in Java?
An instance of a class.
How do you create an object?
ClassName obj = new ClassName();
What is a constructor?
A special method that initializes a new object.
Can constructors have parameters?
Yes!
Example: new Dog(“Fido”);
What keyword is used to refer to the current object?
this
Can a class have more than one constructor?
Yes — this is called overloading.
What is a method?
A function defined in a class, which can be called on objects.
How do you call a method that returns no value?
obj.methodName(); — must match parameters.
What is the difference between a void method and a non-void method?
Void = no return value; non-void = returns something (like int, String, etc.)
How do you store the result of a method call that returns a value?
int result = obj.getValue();
Are strings primitives in Java?
No — String is a reference type (class).