You are on page 1of 2

OPERATORS AND EXPRESSIONS

What do you mean by operators and operands?


The operations being carried out are represented by operators. The objects of the operation are operands.eg) A +B
+ is the operator and A,B are the operands.
2. What are the different types of operators?
1. Unary operator 2. Binary operator 3. Ternary operator
3. Distinguish between a unary, a binary and a ternary operator. Give examples?
Unary operator requires a single operand. Eg) unary +,unary -, ++ ,-- etc
Binary operator requires two operands eg) +,-,*,/,% etc
Ternary operator requires three operands eg) Conditional operator (? :)
4. What are arithmetic operators . Give eg.
Arithmetic operators carry out arithmetic calculations.
Operator
data type
1. Addition
+
integer or float
2. Subtraction
integer or float
3. Multiplication *
integer or float
4. Division
/
integer or float
5. Modulus
%
only integer
5. What does the modulus operator % do? What will be the result of 7.2 % 2.1?
Modulus operator produces the remainder of dividing the first operand by the second operand. 7.2 % 2.1 results to an error
6. How is unary + operator different from + operator ? How is unary operator different from - operator?
Unary +
+
1. Needs one operand
1. Needs two operands
2.Unary operator
2. Binary operator
3. Result is the value of the operand
3. Adds the value of its two operands.
If a = 5 then +a = 5
3+5 =8
If a= -4 then +a =-4
1.

Unary 1. Needs one operand


1. Needs two operands
2.Unary operator
2. Binary operator
3. Result is the negation of its operands value.
3. Subtracts the value of its two operands.
If a = 5 then -a = -5
5-3 =2
If a= -4 then -a =4
7. What is the function of increment and decrement operators? How many varieties do they come in? How are these two varieties
different from one another?
They are unary operators.
The increment operator ++ adds 1 to operands value. The decrement operator - -subtracts 1 from operands value.
They come in two forms 1. Post increment and post decrement 2. Pre increment and Pre decrement
The postfix version first uses the value of the operand and then changes ie, it follows use then- change rule.
The prefix version first changes its operands value and then use it. ie, it follows change- then- use rule.
Post fix have higher precedence over prefix operators.
8. Show the working of prefix and post fix increment and decrement operator with eg
1. sum = sum + (++ count)
2. P = P * - - N;
The initial values of sum and count are 0 and 10 respectively.
The initial values of P=4 and N=8.
sum
sum
count
P
P
N
0
10
(initial values)
4
8 (initial values)
11 (first increment it)
7(first decrement it)
11 =
0
+
11 (Now use it)
28 = 4
*
7 (now use it)
3. sum = sum + ( count ++)
4. P = P * N--;
The initial values of sum and count are 0 and 10 respectively.
The initial values of P=4 and N=8.
sum
sum
count
P
P
N
1
10
(initial values)
4
8 (initial values)
10
=
0
+
10
(first use it)
32 = 4 *
8(first use it)
11 (Now increment it)
7 (now decrement it)
9. What role do the relational operators play?
A relational operator is used to compare two values and the result of the operator is always logical.ie, either true or false.
Relational operator compares numbers and characters and not strings.
10. What are the different relational operators in C++?
(1) > (greater than) (2) >=(greater than or equal to) (3) < (less than) (4) <= (less than or equal to) (5) ==(equal to) (6) != (not equal
to)
11. Why should equality comparisons among floating point numbers be discouraged?
After any calculation involving floating point numbers, there may be a small residue error. ie, why equality comparisons
among floating point numbers are discouraged.
12.Why should comparisons between signed and unsigned values be discouraged?
Because the compiler will treat the signed value as unsigned.

13 What is the use of logical operators?


A logical operator is used to connect two relational expressions or logical expressions. The result of such an operation is
always logical. ie,either true or false.
14 What are the different logical operators in C++?
(1) && ( logical AND) (2) || (logical OR)
(3) ! (logical NOT)
15 What is the rule of logical AND?
The output of a logical AND operation is TRUE if its both the operands are TRUE. All other combinations the result is FALSE.
True && True -> True , True && False -> False, False && True -> False, False && False -> False,
16 What is the role of logical OR?
The output of a logical OR operation is FALSE if its both the operands are FALSE. All other combinations the result is TRUE.
True || True -> True , True || False -> True, False || True -> True, False || False -> False
17 What is the role of logical NOT operator?
Logical NOT operator (!) negates the truth value of the expression. It is a unary operator.
18 Illustrate with an example that unary negation operator ! is useful as a test for zero.
The condition if (check == 0) can be written as if(! check)
19What do you mean by truth value of an expression?
C++ considers 0 as a false value and any non-zero value as a true value. Values 0 and 1 are the truth values of an expression.
20 Explain the usage of the conditional operators ?
Conditional operator stores a value depending upon a condition. This is a ternary operator. It requires three operands. The general
form is
expression1 ? expression2 : expression3
If expression1 evaluates to true then the value of the whole expression is the value of expression2 otherwise, the value of the
whole expression is the value of expression3.
Eg) 6 > 4 ? 9 : 7 evaluates to 9
4 == 9 ? 10 : 25 evaluates to 25
The conditional operators is allowed on the left hand side of an expression.
Eg) int x,y;
float value;
cin>> value;
value > 500 ? x : y =10;
( if value is greater than 500, x is initialized with 10. otherwise y is initialized with 10)
21 What is an expression ? How many types of expression does C++ supports?
An expression is composed of one or more operations. It is a valid combinations of operators, constants and variables.
Expressions can be arithmetic, relational (or logical), compound expression etc.
22 What do you mean by an arithmetic expression?
An arithmetic expression contains variables ,constants and arithmetic operators. Two or more variables or operators should not
occur in continuation. Arithmetic expressions may consists of mathematical functions.
Arithmetic expressions can either be integer expressions or real expressions or mixed mode expressions.
Integer expressions are combination of arithmetic operators, integer constants and integer variables.
Real expressions are combination of arithmetic operators, real constants and real variables.
Mixed mode expressions are combination of arithmetic operators, integer / real constants / variables.
23 What do you mean by logical expressions?
The expressions that result into false or true are called logical expressions. The logical expressions are combination of
constants, variables and logical and relational operators. Two or more variables or operators should not occur in continuation.
24 What is type conversion? What is meant by implicit and explicit type conversion?
The process of converting one predefined data type into another is called type conversion.
Implicit conversion is performed by compiler with out programmers intervension.
Explicit conversion is defined by the user.
25. What is the process of type promotion? What is integral promotion?
In implicit conversion, all operands are converted upto the type of the largest operand, which is called type promotion.
The conversion of integer types (char, short, int) into int(if all values can be represented by int) or unsigned int (if all values cannot
be represented by int)is called integral promotion.
26. What do you mean by type casting ? What is type cast operator?
The explicit conversion of an operand to a specific type is called type casting. It is done using type cast operator.
The general form is ; (type) expression
Where type cast operator is a valid C++ data type to which the conversion is to be done. Type cast is a unary operator.
27 What is an assignment statement?
An assignment statement assigns value to a variable . The value assigned may be a constant ,variable or an expression.
= is an assignment operator. Assignments can be chained together. Eg) int a,b,c,d,e;
a=b=c=d=e=10;
( In an assignment statement the value of the right side of the assignment is converted to the data type of the left )
28. Explain C++ short hands?
C++ short hands simplify the coding of a certain type of assignment statement. The general form is
Var operator = expression
is same as
var = var operator expression.
Eg) a= a+10 can be written as a+= 10;
29. What are the different arithmetic assignment operators?
The operators += , - = , * = , / = and % = are called arithmetic assignment operators.

You might also like