Tuesday, November 23, 2010

QUESTIONS ON OPERATORS


1. 
Which of the following is the correct order of evaluation for the below expression?z = x + y * z / 4 % 2 - 1
A.
* / % + - =
B.= * / % + -
C./ * % - + =D.* % / - + =
Answer: Option A
Explanation:
C uses left associativity for evaluating expressions to break a tie between two operators having same precedence.


2. 
Which of the following correctly shows the hierarchy of arithmetic operations in C?
A./ + * -B.* - / +
C.+ - / *D.
/ * + -
Answer: Option D
Explanation:
Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction).
How Do I Remember ? BODMAS !

  • B - Brackets first




  • O - Orders (ie Powers and Square Roots, etc.)




  • DM - Division and Multiplication (left-to-right)




  • AS - Addition and Subtraction (left-to-right)





  • 3. 
    Which of the following is the correct usage of conditional operators used in C?
    A.a>b ? c=30 : c=40;B.a>b ? c=30;
    C.
    max = a>b ? a>c?a:c:b>c?b:c
    D.return (a>b)?(a:b)
    Answer: Option C
    Explanation:
    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.


    4. 
    Which of the following is the correct order if calling functions in the below code?a = f1(23, 14) * f2(12/4) + f3();
    A.f1, f2, f3
    B.f3, f2, f1
    C.
    Order may vary from compiler to compiler
    D.None of above
    Answer: Option C
    Explanation:
    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.


    5. 
    Which of the following are unary operators in C?
    1.!
    2.sizeof
    3.~
    4.&&
    A.1, 2B.1, 3
    C.2, 4D.
    1, 2, 3
    Answer: Option D
    Explanation:
    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.


    6. 
    In which order do the following gets evaluated
    1.Relational
    2.Arithmetic
    3.Logical
    4.Assignment
    A.
    2134
    B.1234
    C.4321D.3214
    Answer: Option A
    Explanation:
    1. Arithmetic operators: *, /, %, +, - 
    2. Relational operators: >, <, >=, <=, ==, !=
    3. Logical operators : !, &&, ||
    4. Assignment operators: =

    TRUE OR FALSE



    1. 
    Are the following two statement same?
    1.a <= 20 ? b = 30: c = 30;
    2.(a <=20) ? b : c = 30;
    A.YesB.
    No
    Answer: Option B
    Explanation:
    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;
    }
    


    2. 
    Two different operators would always have different Associativity.
    A.YesB.
    No
    Answer: Option B
    Explanation:
    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.


    3. 
    Will the expression *p = c be disallowed by the compiler?
    A.YesB.
    No
    Answer: Option B
    Explanation:
    Because, here even though the value of p is accessed twice it is used to modify two different objects p and *p


    4. 
    Every operator has an Associativity
    A.
    Yes
    B.No
    Answer: Option A
    Explanation:
    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.


    1. 
    Associativity has no role to play unless the precedence of operator is same.
    A.
    True
    B.False
    Answer: Option A
    Explanation:
    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.


    2. 
    The expression of the right hand side of || operators doesn't get evaluated if the left hand side determines the outcome.
    A.
    True
    B.False
    Answer: Option A
    Explanation:
    Because, if a is non-zero then b will not be evaluated in the expression (a || b)


    3. 
    In the expression a=b=5 the order of Assignment is NOT decided by Associativity of operators
    A.TrueB.
    False
    Answer: Option B
    Explanation:
    The equal to = operator has Right-to-Left Associativity. So it assigns b=5 then a=b.


    4. 
    Associativity of an operator is either Left to Right or Right to Left.
    A.
    True
    B.False
    Answer: Option A
    Explanation:
    Yes, the associativity of an operator is either Left to Right or Right to Left.


    No comments:

    Post a Comment