Java Operators Flashcards

1
Q

Arithmetic Operators in Java are used for what purpose?

A

Arithmetic Operators in Java are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.

Example: 10 % 3 = 1

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

What is the result when dividing two integers in Java?

A

When dividing two integers in Java, the result is an integer (fractional part is discarded).

Example: 10 / 3 = 3

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

What is the purpose of the Modulus operator in Java?

A

The Modulus operator in Java returns the remainder of a division.

Example: 10 % 3 = 1

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

What do Increment/Decrement Operators do in Java?

A

Increment/Decrement Operators in Java are used to increase or decrease a value by 1.

Example: int x = 10; int y = x++; // y = 10, x = 11

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

What are Relational Operators used for in Java?

A

Relational Operators in Java are used to compare two values or expressions and return a boolean result (true or false).

Example: boolean result1 = (a == b); // false

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

What is the main purpose of Logical Operators in Java?

A

The main purpose of Logical Operators in Java is to perform logical operations, primarily for combining multiple conditions.

Example: if (a > 0 && b > 0) { System.out.println("Both a and b are positive."); }

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

What does the Logical AND operator (&&) do?

A

Returns true if both conditions are true.

The condition evaluates to true only if both a > 0 and b > 0 are true.

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

What does the Logical OR operator (||) do?

A

Returns true if at least one condition is true.

The condition evaluates to true if either a > 0 or b > 0 is true.

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

What does the Logical NOT operator (!) do?

A

Reverses the logical state of its operand. If a condition is true, ! makes it false, and vice versa.

If a > 0 is true, then !(a > 0) will be false.

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

What is Short-circuit Evaluation in Java?

A

Java supports short-circuiting with logical operators. This means that evaluation of a logical expression stops as soon as the outcome is determined:
- For &&, if the first condition is false, the whole expression is false, so the second condition is not evaluated.
- For ||, if the first condition is true, the whole expression is true, so the second condition is not evaluated.

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

How do Relational and Logical Operators work together in Java?

A

Relational and logical operators often work together to form complex conditional statements in programs.

In this case:
- a < 15 is true.
- b > 15 is true.
- The && operator ensures both conditions are true, so the statement inside the if block will execute.

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

What is Operator Precedence in Java?

A

Java evaluates operators based on a predefined operator precedence. Operators with higher precedence are evaluated first. For example, arithmetic operators generally have higher precedence than relational operators, which in turn have higher precedence than logical operators.

In this case, multiplication * is performed before addition +.

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