MCQs
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.
Because, here even though the value of p is accessed twice it is used to modify two
different objects p and *p
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.
No, the expressions 1 and 2 are not same.
1. a
Yes, the associativity of an operator is either Left to Right or Right to Left.
The equal to = operator has Right-to-Left Associativity. So it assigns b=5 then a=b.
Associativity is only needed when the operators in an expression have the same
precedence. Usually + and - have the same precedence.
Consider the expression 7 - 4 + 2. The result could be either (7 - 4) + 2 = 5 or 7
- (4 + 2) = 1. The former result corresponds to the case when + and - are left-
associative, the latter to when + and - are right-associative.
Usually the addition, subtraction, multiplication, and division operators are left-associative,
while the exponentiation, assignment and conditional operators are right-associative. To
prevent cases where operands would be associated with two operators, or no operator at
all, operators with the same precedence must have the same associativity.
Because, comma operator used in the expression i (1, 2, 3, 4, 5). The comma
operator has left-right associativity. The left operand is always evaluated first,
and the result of evaluation is discarded before the right operand is evaluated.
In this expression 5 is the right most operand, hence after evaluating expression
(1, 2, 3, 4, 5) the result is 5, which on adding to i results into 7.
Because, if a is non-zero then b will not be evaluated in the expression (a || b)
Step 1: char ch; ch = 'A'; here variable ch is declared as an character
type an initialized to 'A'.
Step 2: printf("The letter is"); It prints "The letter is".
Step 3: printf("%c", ch >= 'A' && ch <= 'Z' ? ch + 'a' - 'A':ch);
The ASCII value of 'A' is 65 and 'a' is 97.
Here
=> ('A' >= 'A' && 'A' <= 'Z') ? (A + 'a' - 'A'):('A')
=> (TRUE && TRUE) ? (65 + 97 - 65) : ('A')
=> (TRUE) ? (97): ('A')
In printf the format specifier is '%c'. Hence prints 97 as 'a'.
Step 4: printf("Now the letter is"); It prints "Now the letter is".
Step 5: printf("%cn", ch >= 'A' && ch <= 'Z' ? ch : ch + 'a' - 'A');
Here => ('A' >= 'A' && 'A' <= 'Z') ? ('A') : (A + 'a' - 'A')
=> (TRUE && TRUE) ? ('A') :(65 + 97 - 65)
=> (TRUE) ? ('A') : (97)
It prints 'A'
Hence the output is
The letter is a
Now the letter is A