Exercise: Expressions

Questions for: Expressions

Every operator has an Associativity
A:
Yes
B:
No
C:
D:
Answer: A

Yes, Each and every operator has an associativity.

The associativity (or fixity) of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. Operators may be left-associative, right-associative or non-associative.

Will the expression *p = p be disallowed by the compiler?
A:
Yes
B:
No
C:
D:
Answer: B
Because, here even though the value of p is accessed twice it is used to modify two different objects p and *p
Two different operators would always have different Associativity.
A:
Yes
B:
No
C:
D:
Answer: B

No, Two different operators may have same associativity.

Example:
Arithmetic operators like ++, -- having Right-to-Left associativity.
Relational operators like >, >= also have Left-to-Right associativity.

Are the following two statement same?
1. a <= 20 ? (b = 30): (c = 30);
2. (a <=20) ? b : (c = 30);
A:
Yes
B:
No
C:
D:
Answer: B

No, the expressions 1 and 2 are not same.

1. a <= 20 ? (b = 30) : (c = 30); This statement can be rewritten as,


if(a <= 20)
{
    b = 30;
}
else
{
    c = 30;
}

2. (a <=20) ? b : (c = 30); This statement can be rewritten as,


if(a <= 20)
{
    //Nothing here
}
else
{
    c = 30;
}

Associativity of an operator is either Left to Right or Right to Left.
A:
True
B:
False
C:
D:
Answer: A

Yes, the associativity of an operator is either Left to Right or Right to Left.

Ad Slot (Above Pagination)
Quiz