MCQs
Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.
Step 2: m = ++i || ++j && ++k; here (++j && ++k;) this code will not get executed because ++i has non-zero value.
becomes m = -2 || ++j && ++k;
becomes m = TRUE || ++j && ++k; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.
Step 3: printf("%d, %d, %d, %d`setminus`n", i, j, k, m); In the previous step the value of variable 'i' only increemented by '1'(one). The variable j,k are not increemented.
Hence the output is "-2, 2, 0, 1".
The integer value 2 is represented as 00000000 00000010 in binary system.
Negative numbers are represented in 2's complement method.
1's complement of 00000000 00000010 is 11111111 11111101 (Change all 0s to 1 and 1s to 0).
2's complement of 00000000 00000010 is 11111111 11111110 (Add 1 to 1's complement to obtain the 2's complement value).
Therefore, in binary we represent -2 as: 11111111 11111110.
After left shifting it by 2 bits we obtain: 11111111 11111000, and it is equal to "fff8" in hexadecimal system.
Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.
Step 2: m = ++i && ++j && ++k;
becomes m = -2 && 3 && 1;
becomes m = TRUE && TRUE; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.
Step 3: printf("%d, %d, %d, %d`setminus`n", i, j, k, m); In the previous step the value of i,j,k are increemented by '1'(one).
Hence the output is "-2, 3, 1, 1".
2. Arithmetic operators: *, /, %, +, -
1. Relational operators: >, <, >=, <=, ==, !=
3. Logical operators : !, &&, ||
4. Assignment operators: =
An operation with only one operand is called unary operation.
Unary operators:
! Logical NOT operator.
~ bitwise NOT operator.
sizeof Size-of operator.
&& Logical AND is a logical operator.
Therefore, 1, 2, 3 are unary operators.
Here, Multiplication will happen before the addition, but in which order the functions would
be called is undefined. In an arithmetic expression the parenthesis tell the compiler which
operands go with which operators but do not force the compiler to evaluate everything within
the parenthesis first.
Option A: assignment statements are always return in paranthesis in the case of conditional
operator. It should be a>b? (c=30):(c=40);
Option B: it is syntatically wrong.
Option D: syntatically wrong, it should be return(a>b ? a:b);
Option C: it uses nested conditional operator, this is logic for finding greatest number out of
three numbers.
Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction).
How Do I Remember ? BODMAS !
Here the multiplication will happen before the addition, but in which order the functions would be called is undefined
C uses left associativity for evaluating expressions to break a tie between two operators
having same precedence.