Topic 3.1: Using Operators and Decision - Use Java operators; use parentheses to override operator precedence Flashcards

1
Q

What does the postfix version of the increment operator (value++ / value–) do?

A

The postfix version assigns the original value of the variable to another variable and then increments or decrements the value.

Example:

int value = 5;
int result = value++;
// value is assigned first, then incremented afterward
// value = 6, result = 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of parentheses in expressions?

A

These are used to explicitly group expressions and override the default precedence rules, allowing control over the order of evaluation in complex expressions.

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

What is the purpose of the
equality and relational operators?

A

These operators are used to determine if one operand is greater than, less than, equal to, or not equal to another operand.

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

The precedence order, from highest to lowest, is as follows:

  1. Postfix operators: expr++ and expr--
  2. Unary operators: ++expr, --expr, +expr, -expr, and !
  3. Multiplicative operators: *, /, and %
  4. Additive operators: + and -
  5. Relational operators: <, >, <=, >=, and instanceof
  6. Equality operators: == and !=
  7. Logical AND operator: &&
  8. Logical OR operator: ||
  9. Ternary operator: ? :
  10. Assignment operator: =
A

How are the common Java operators ordered based on their precedence?

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

How does using parentheses contribute to code readability?

A

Carefully using these in expressions improves code readability. It helps clarify the order of operations and avoids confusion, making the code more understandable for others.

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

What are the 6
equality and relational operators?
Provide examples of each operator.

A

Here are the operators along with examples:

  • Equality operator (==): Checks if two values are equal. Example: 5 == 5 evaluates to true.
  • Inequality operator (!=): Checks if two values are not equal. Example: 5 != 3 evaluates to true.
  • Greater than operator (>): Checks if one value is greater than another. Example: 7 > 3 evaluates to true.
  • Greater than or equal to operator (>=): Checks if one value is greater than or equal to another. Example: 7 >= 7 evaluates to true.
  • Less than operator (<): Checks if one value is less than another. Example: 2 < 5 evaluates to true.
  • Less than or equal to operator (<=): Checks if one value is less than or equal to another. Example: 2 <= 2 evaluates to true.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When these are used, the expressions inside them are evaluated first, overriding the default precedence rules. This allows specific parts of an expression to be evaluated before or after other operations.

A

How do parentheses affect the evaluation of expressions?

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

What does the prefix version of the increment operator (++value / –value) do?

A

The prefix version carries out the increment or decrement operation first and then assigns the updated value to the variable.

Example:

int value = 5;
int result = ++value;
// value is incremented first, then assigned to result
// value = 6, result = 6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

These operators are used to determine if one operand is greater than, less than, equal to, or not equal to another operand.

A

What is the purpose of the
equality and relational operators?

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

What are the
arithmetic operators
in Java?

A

These include:

Addition operator: “+”
* Performs addition of two operands.
* Can also be used for String concatenation.

Subtraction operator: “-“
* Subtracts one operand from another.

Multiplication operator: “*”
* Multiplies two operands.

Division operator: “/”
* Divides one operand by another.

Remainder operator: “%”
* Divides one operand by another and returns the remainder.

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

These include:

Addition operator: “+”
* Performs addition of two operands.
* Can also be used for String concatenation.

Subtraction operator: “-“
* Subtracts one operand from another.

Multiplication operator: “*”
* Multiplies two operands.

Division operator: “/”
* Divides one operand by another.

Remainder operator: “%”
* Divides one operand by another and returns the remainder.

A

What are the
arithmetic operators
in Java?

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

How are the common Java operators ordered based on their precedence?

A

The precedence order, from highest to lowest, is as follows:

  1. Postfix operators: expr++ and expr--
  2. Unary operators: ++expr, --expr, +expr, -expr, and !
  3. Multiplicative operators: *, /, and %
  4. Additive operators: + and -
  5. Relational operators: <, >, <=, >=, and instanceof
  6. Equality operators: == and !=
  7. Logical AND operator: &&
  8. Logical OR operator: ||
  9. Ternary operator: ? :
  10. Assignment operator: =
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Here are the operators along with examples:

  • Equality operator (==): Checks if two values are equal. Example: 5 == 5 evaluates to true.
  • Inequality operator (!=): Checks if two values are not equal. Example: 5 != 3 evaluates to true.
  • Greater than operator (>): Checks if one value is greater than another. Example: 7 > 3 evaluates to true.
  • Greater than or equal to operator (>=): Checks if one value is greater than or equal to another. Example: 7 >= 7 evaluates to true.
  • Less than operator (<): Checks if one value is less than another. Example: 2 < 5 evaluates to true.
  • Less than or equal to operator (<=): Checks if one value is less than or equal to another. Example: 2 <= 2 evaluates to true.
A

What are the 6
equality and relational operators?
Provide examples of each operator.

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

These are used to explicitly group expressions and override the default precedence rules, allowing control over the order of evaluation in complex expressions.

A

What is the purpose of parentheses in expressions?

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

What are the
unary operators
in Java and what operations do they perform?

A

these include:

Unary plus operator: “+”
* Indicates a positive value (numbers are positive without this operator).

Unary minus operator: “-“
* Negates an expression by changing its sign.

Increment operator: “++”
* Increases a value by 1.
* It can be applied as a prefix (++value) or a postfix (value++).
* The prefix version evaluates to the incremented value, while the postfix version evaluates to the original value.

Decrement operator: “–”
* Decreases a value by 1.
* Similar to the increment operator, it can be used as a prefix (–value) or a postfix (value–).

Logical complement operator: “!”
* Inverts the value of a boolean. If the operand is true, it becomes false, and if it’s false, it becomes true.

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

these include:

Unary plus operator: “+”
* Indicates a positive value (numbers are positive without this operator).

Unary minus operator: “-“
* Negates an expression by changing its sign.

Increment operator: “++”
* Increases a value by 1.
* It can be applied as a prefix (++value) or a postfix (value++).
* The prefix version evaluates to the incremented value, while the postfix version evaluates to the original value.

Decrement operator: “–”
* Decreases a value by 1.
* Similar to the increment operator, it can be used as a prefix (–value) or a postfix (value–).

Logical complement operator: “!”
* Inverts the value of a boolean. If the operand is true, it becomes false, and if it’s false, it becomes true.

A

What are the
unary operators
in Java and what operations do they perform?

17
Q

Carefully using these in expressions improves code readability. It helps clarify the order of operations and avoids confusion, making the code more understandable for others.

A

How does using parentheses contribute to code readability?

18
Q

How do parentheses affect the evaluation of expressions?

A

When these are used, the expressions inside them are evaluated first, overriding the default precedence rules. This allows specific parts of an expression to be evaluated before or after other operations.