Java Chapter 2 Flashcards
(280 cards)
questions
answers
What are Java operators?
Special symbols that operate on variables, values, or literals and return a result.
What are operands?
The values or variables being operated on by an operator.
What is the result of an operation?
The value produced after applying an operator to operands.
What are the three types of Java operators?
Unary, binary, and ternary.
How many operands does a unary operator work on? Give an example.
One operand (e.g., -a, ++a).
How many operands does a binary operator work on? Give an example.
Two operands (e.g., a + b, x > y).
How many operands does the ternary operator work on? Write its syntax.
Three operands (e.g., condition ? trueVal : falseVal).
Is ++x a unary or binary operator?
Unary (only operates on x).
Is a * b a unary or binary operator?
Binary (operates on a and b).
What symbol represents the ternary operator in Java?
? : (e.g., x > 5 ? “Yes” : “No”).
Let me know if you’d like more questions or refinements!
What is the output of this code?
```java
int cookies = 4;
double reward = 3 + 2 * –cookies;
System.out.println(“Zoo animal receives: “ + reward + “ reward points”);
~~~
“Zoo animal receives: 9.0 reward points”(Explanation: –cookies decrements first (3), then 2 * 3 = 6, then 3 + 6 = 9.)
What determines the order of evaluation in Java expressions?
Operator precedence (higher precedence operators are evaluated first).
How does Java handle operators with the same precedence?
They are evaluated left to right (except for assignment operators, which are right-associative).
How can you override default operator precedence?
Use parentheses () to force evaluation order (e.g., (3 + 2) * cookies).
What does the bitwise negation operator (~) do?
Flips all bits of an integer (e.g., ~70 becomes -71).
What is the output of this code?
```java
int number = 70;
System.out.println(~number);
~~~
-71
Can ~ be used on boolean values?
❌ No, it only works on integers.
Why does this code fail?
```java
int pelican = !5;
~~~
! is a logical operator and cannot be applied to numbers (only booleans).
Why does this code fail?
```java
boolean penguin = -true;
~~~
The - operator cannot be used on booleans (only numbers).
Why does this code fail?
```java
boolean peacock = 10;
~~~
Java does not allow assigning integers (10) to booleans (true/false).
Q: What is the difference between ++x and x++?A:
Pre-increment (++x): Updates x before using its value.
Post-increment (x++): Uses the current value of x, then increments it.
Q: What is the value of x and y after this code?
```java
int x = 5;
int y = x++ * 2;
~~~
A:
x = 6 (post-increment)
y = 10 (original x value 5 * 2)
Let me know if you’d like more questions or clarifications!
What are binary operators in Java?
Operators that work with two operands (values/variables) to perform math, logic, or assignments.
Give an example of a binary operator.
3 + 5 (+ is the binary operator, 3 and 5 are operands).
What three tasks do binary operators perform?
Binary operators typically perform three types of tasks:
1. Arithmetic operations
• Examples: +, -, *, /, %
• Task: Perform mathematical calculations.
2. Relational/comparison operations
• Examples: ==, !=, <, >, <=, >=
• Task: Compare two values and return a boolean.
3. Logical operations
• Examples: &&, ||, ^
• Task: Combine boolean expressions.