Chapter 7 Flashcards

(28 cards)

1
Q

Define operator precedence and associativity.

A

Operator Precedence: The rules that define the order in which operators of different precedence levels are evaluated

Operator Associativity: The rules that determine the order of evaluation when an expression contains two adjacent operators with the same level of precedence

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

What is a ternary operator?

A

A ternary operator is an operator that has three operands.

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

What is a prefix operator?

A

A prefix operator is one that precedes its operands.

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

What operator usually has right associativity?

A

The exponentiation operator (when provided) usually has right associativity. In C-based languages, unary operators like ++, –, and unary +/- are also right associative.

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

What is a nonassociative operator?

A

A nonassociative operator is one that cannot be used in a chain of the same operator without parentheses. For example, in Ada, the exponentiation operator is nonassociative, meaning A ** B ** C is illegal and must be parenthesized.

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

What associativity rules are used by APL?

A

In APL, all operators have the same precedence, and the associativity for all operators is right to left.

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

What is the difference between the way operators are implemented in C++ and Ruby?

A

In C-based languages (like C++), operators are distinct syntax constructs. In Ruby, all arithmetic, relational, and assignment operators are implemented as methods; for example, a + b is actually a call to the + method of object a with b as a parameter.

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

Define functional side effect.

A

A functional side effect occurs when a function changes one of its parameters or a global variable (a variable declared outside the function).

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

What is a coercion?

A

A coercion is an implicit type conversion that is initiated by the compiler (as opposed to an explicit cast requested by the programmer).

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

What is a conditional expression?

A

A conditional expression is a ternary operation (often using ? :) that evaluates to one of two values based on a boolean condition. It has the form expression_1 ? expression_2 : expression_3, where expression_1 determines which of the other two expressions is the resulting value.

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

What is an overloaded operator?

A

An overloaded operator is an operator that is used for more than one purpose, such as using + for both integer and floating-point addition, or for string concatenation.

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

Define narrowing and widening conversions.

A

Narrowing Conversion: Converts a value to a type that cannot store all values of the original type (e.g., double to float), potentially losing magnitude or precision.
Widening Conversion: Converts a value to a type that can include at least approximations of all values of the original type (e.g., int to float), which is usually safe regarding magnitude.

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

In JavaScript, what is the difference between == and ===?

A

==: This operator allows coercion, so operands of different types (like a string and a number) can be considered equal (e.g., "7" == 7 is true).
===: This operator prevents coercion, meaning operands must be of the same type to be equal (e.g., "7" === 7 is false).

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

What is a mixed-mode expression?

A

A mixed-mode expression is an expression in which an operator is allowed to have operands of different types.

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

What is referential transparency?

A

A program is referentially transparent if any two expressions that have the same value can be substituted for one another anywhere in the program without affecting the program’s action.

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

What are the advantages of referential transparency?

A

The semantics of referentially transparent programs are much easier to understand because the meaning of a function depends entirely on its parameters (context is irrelevant), making them equivalent to mathematical functions.

17
Q

How does operand evaluation order interact with functional side effects?

A

If an operand evaluation causes a side effect (like changing a global variable that is also used in the expression), the final result of the expression depends on the order in which the operands are evaluated.

18
Q

What is short-circuit evaluation?

A

Short-circuit evaluation is when the result of an expression is determined without evaluating all of the operands and/or operators.

19
Q

Name a language that always does short-circuit evaluation of Boolean expressions. Name one that never does it. Name one in which the programmer is allowed to choose.

A

Always: Ruby, Perl, ML, F#, and Python.
Never: The text implies early versions of languages or specific operators might not, but specifically notes that bitwise operators in C-based languages (& and |) are not short-circuit.
Allows Choice: Ada allows the programmer to choose (using and then / or else for short-circuit, and and / or for non-short-circuit).

20
Q

How does C support relational and Boolean expressions?

A

Versions of C prior to C99 do not have a Boolean type; they use numeric values where zero is false and any nonzero value is true. The result of a relational expression is an integer (0 or 1).

21
Q

What is the purpose of a compound assignment operator?

A

It provides a shorthand method for specifying a common form of assignment where the destination variable also appears as the first operand on the right side (e.g., a += b instead of a = a + b).

22
Q

What is the associativity of C’s unary arithmetic operators?

A

C’s unary arithmetic operators (like ++, –, unary -) are right associative.

23
Q

What is one possible disadvantage of treating the assignment operator as if it were an arithmetic operator?

A

It creates expression side effects, which can make expressions difficult to read and understand. It can also lead to errors where an assignment (=) is accidentally used instead of an equality check (==) in a condition, which the compiler may not catch.

24
Q

What two languages include multiple assignments?

A

Perl, Ruby, and Lua are mentioned as languages that provide multiple-target, multiple-source assignment statements.

25
What mixed-mode assignments are allowed in Ada?
Ada does not allow mixed-mode assignment.
26
What mixed-mode assignments are allowed in Java?
Java allows mixed-mode assignment only if the required coercion is widening (e.g., assigning an int to a float, but not vice versa).
27
What mixed-mode assignments are allowed in ML?
In functional languages like ML, assignments are used to name values and variables never change, so there is no such thing as a mixed-mode assignment.
28
What is a cast?
A cast is an explicit type conversion requested by the programmer