Operators in C language and MCQs

                        Operators in C 


An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators.we will descuss all type of operators in this slide.

1.Arithmetic Operator

2.Relational Operator

3.Logical Operator

4.Bitwise Operator

5.Assignment Operator


1. Arithmetic Operators

An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).

Operator                                       Meaning

+                                              addition or unary plus

-                                             subtraction or unary minus

*                                             multiplication

/                                            division

%                                            remainder after division


2. Relational operators

table of all the relational operators supported by C language. Assume variable A holds 5 and variable B holds 2 then.

operator                meaning  

  ==                                  equal to    
  >                                  greater than
  <                                   less than
 !=                                  not  equal to
 >=                                  greater equal to  
 <=                                   less than equal to                 


3. Logical operators

There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).

Operators                          Example                     Description

&& (logical AND)      (x>5)&&(y<5)           It returns true when both conditions are true

|| (logical OR)            (x>=10)||(y>=10)      It returns true when at-least one of the condition                                                                                                                                             is true

! (logical NOT)         !((x>5)&&(y<5))          It reverses the state of the operand “((x>5) &&                                                                                                                                          (y<5))


4. bitwise operator

To perform bit-level operation in c, bitwise operators are used here i am mentioning following bitwise operators.

1.      &                    bitwise AND operator
2.       |                      bitwise OR operator
3.       ^                     bitwise XOR operator
4.       ~                     bitwise compliment 
5.       <<                   shift left
6.       >>                   shift right


5. Assignment operator

Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value.i am mentioning following assignment operator.

operator           example        equvalent


=                                     a=b                    a=b
+=                                  a+=b                  a=a+b
-=                                    a-=b                  a=a-b
*=                                  a*=b                   a=a*b
/=                                   a/=b                   a=a/b
%=                                 a%=b                 a=a%b



MCQs based on operators:-

1) Which is not a bitwise operator?
a).&                                b).  |
c).<<                              d).  &&
Answer- d

2. What will be the output of the following C code?

    #include <stdio.h>
    int main()
    {
        unsigned int a = 10;
        a = ~a;
        printf("%d\n", a);
    }
a) -9                                 b) -10
c) -11                               d) 10
Answer- c

3. What will be the output of the following C code?

    #include <stdio.h>
    int main()
    {
        if (7 & 8)
        printf("Honesty");
            if ((~7 & 0x000f) == 8)
                printf("is the best policy\n");
    }
a) Honesty is the best policy                b) Honesty
c) is the best policy                                d)error
answer- c


4. Comment on the output of the following C code.

    #include <stdio.h>
    int main()
    {
        int i, n, a = 4;
        scanf("%d", &n);
        for (i = 0; i < n; i++)
            a = a * 2;
    }
a) Logical Shift left                    b) No output
c) Arithmetic Shift right             d) Bitwise exclusive OR
answer -b

5.What is the output of this C code?

    int main()
    {
        int i = -5;
        int k = i %4;
        printf("%d\n", k);
    }
a). Compile time error               b). -1
c). 1                                           d). None
answer-b


6. What is the output of this C code?

    int main()
    {
        int i = 7;
        i = i / 4;
        printf("%d\n", i);
       return 0;
    }
a). Run time error                        b). 1
c). 3                                             d). Compile time error
answer-b

7.What is the output of this C code?

    void main()
    {
        int a = 5;
        int b = ++a + a++ + --a;
        printf("Value of b is %d", b);
    }

a). Value of x is 16                  b). Value of x is 21
c). Value of x is 15                  d). Undefined behaviour
answer-d

8. What is the output of this C code?

    void main()
    {
        int x = 4.3 % 2;
        printf("Value of x is %d", x);
    }
a). Value of x is 1.3                    b). Value of x is 2
c). Value of x is 0.3                    d). Compile time error
answer-d

9. The precedence of arithmetic operators is (from highest to lowest)?
a). %, *, /, +, -                              b). %, +, /, *, -
c). +, -, %, *, /                              d). %, +, -, *, /
answer -a

10. Which of the following is not an arithmetic operation?
a). a *= 20;                                  b). a /= 30;
c). a %= 40;                                d). a != 50;
answer-d






















Comments

Post a Comment

Popular posts from this blog

Decision making with if statement and else..if

Switch statement in C language

Call by value and by reference in function