1.

Define an operator and explain operator in detail.

Answer»

An operator is a symbol that performs an operation. The data on which operations are carried out are called operands. 

Following are the operators 

1. lnput(>>) and output(<<):

These operators are used to perform input and output operation.

eg: cin >>n;

cout<<n;

2. Arithmetic operators: 

It is a binary operator. It is used to perform addition(+), subtraction(-), division (/), multiplication (*) and modulus (%- gives the remainder) operations. 

eg: If x = 10 and y = 3 then

x+yx-yx*yx/yx%y
1373031

x/y = 3, because both operands are integer. To get the floating point result one of the operand must be float.

3. Relational operator: 

It is also a binary operator. It is used to perform comparison or relational operation between two values and it gives either true(1) or false(O). The operators are ,>=,== (equality)and !=(not equal to) eg: If x = 10 and y = 3 then

x<yx<=yx>yx>=yx==yx!=y
FalseFalseTrueTrueFalseTrue

4. Logical operators: 

Here AND(&&), OR(||) are binary operators and NOT(!) is a unary operator. It is used to combine relational operations and it gives either true(1) or false(O). If x=True and y=False then

x||xx||yy||xy||y
TrueTrueTrueFalse

Either one of the operands must be true to get a true value in the case of OR(||) operation If x = True and y = False then

!x!y
FalseTrue

5. Conditional operator: 

It is a ternary operator hence it needs three operands. The operator is ?: Syntax: expression ? value if true : value if false. First evaluates the expression if it is true the second part will be executed otherwise the third part will be executed.

  eg: If x = 10 and y = 3 then x>y ? cout<<x : cout<<y;. Here the output is 10

6. sizeof(): 

This operator is used to find the size used by each data type. 

eg. sizeof(int) gives 2.

7. Increment and decrement operator: 

These are unary operators.

(a) Increment operator (++): It is used to increment the value of a variable by one i.e., x++ is equivalent to x = x + 1; 

(b) Decrement operator (–): It is used to decrement the value of a variable by one i.e., x-is equivalent to x = x – 1.

8. Assignment operator (=): 

lt is used to assign the value of a right side to the left side variable. 

eg: x = 5; Here the value 5 is assigned to the variable x.



Discussion

No Comment Found

Related InterviewSolutions