CLASS XI Operators - and - Expressions
CLASS XI Operators - and - Expressions
2
char ch;
int i, j, k;
float a, b, c;
double p, q;
Which of the following are arithmetic expressions
(i) ch = 40 (ii) i + j - k (iii) j % k (iv) a % b + c - i (v) p + q/b > i
Ans. (i) Not an arithmetic expression
(ii) An arithmetic expression
(iii) An arithmetic expression
(iv) Not an arithmetic expression
(v) Not an arithmetic expression
30. Using the declaration of previous question identify the logical expression in the following expressions:
(i) (a) (ii) (!a) (iii) a + b % c (iv) a && !a (v) a||!a
Ans. (ii), (iv) and (v) are logical expressions.
31. Write an equivalent C++ expressions for the following expressions:
𝟏𝟏 2
(i) ut + 𝟐𝟐 ft (ii) �𝒔 𝒔 𝒔 𝒂𝒂 + 𝒕 𝒂𝒂𝒔𝒔 − 𝟏𝟏 𝒂𝒂 − 𝒆��𝟐𝟐𝟐𝟐 (iii) |a| + b>=|b| + a
𝟑𝟑𝟐��+𝟓𝟓𝟓𝟓
(iv) � 𝟓𝟓𝟐��+𝟑𝟑𝟓𝟓
−𝟖𝟖𝟐𝟐𝟓𝟓 𝟐𝟐𝟓𝟓𝟐𝟐� (v)|2x2
e – 4x|
Ans. (i) ut + ((1/2)(f*t*t))
(ii) sqrt (sin(a) + atan(a) – exp(2*x))
(iii) ((fabs(a) + b) >= (fabs(b) + a)
(iv)( (((3*x) + (5*y))/((5*x) + (3*y))) – ((8*x*y)/(2*y*x)))
(v) exp(fabs((2*x*x) – (4*x)))
32. What is type conversion? What is meant by implicit and explicit type conversion?
Ans. Type conversion: The process of converting one predefined type into another is called Type conversion.
Implicit type conversion: An implicit type conversion is a conversion performed by the compiler without
programmer’s intervention.
Explicit type conversion: An explicit type conversion is user-defined that forces an expression to be of specific type.
33. What is the process of type promotion? What is integral promotion?
Ans. In implicit conversion, all operands are converted up to the type of the largest operand, which is called type
promotion.
The conversion of integer types (i.e., char, short, enumerator, int) into int or unsigned int is called integral
promotion.
34. What do you mean by type casting? What is type cast operator?
Ans. The explicit conversion of an operand to a specific type is called type casting and it is done usig type-cast operator
() that is used as (type) expression
where type is a valid C++ type to which the conversion is to be done.
35. What will be the resultant type of the following expression if ch represents a char variable, i is an int variable, fl
is a float variable and db is a double variable?
ch - i + db / fl - i * fl + db / i.
Ans. The resultant type of the above expression is double as double is the largest data type.
36. What will be the resultant type of the following expression if fl is a float variable and db is a double variable?
(int) (fl + db)
Ans. The resultant type of the above expression is int.
37. Construct logical expression to represent the following conditions:
(i) ch is a lowercase character (The ASCII range for lowercase is 97 – 122)
(ii) a is odd but less than 57.
Ans. (i) ch>=97 && ch<=122
(ii) a%2!=0 && a<57
38. Construct logical expression o represent the following conditions:
(i) Either age is more than 70 or members are more than or equal to 8.
(ii) grade is ‘B’ and exper is more than 3 years.
Ans. (i) age>70 || members>=8
3
(ii) grade==’B’ && exper>3
39. If a = 50 and b = 4 then determine the result of the following:
(i) a += b (ii) a %= b (iii) a -= b (iv) a /= b
(v) a *= b (vi) cout<<100/9; (vii) float pi = 22/7; cout<<pi;
Ans. (i) 54 (ii) 2 (iii) 46 (iv) 12.5 (v) 200 (vi) 11 (vii) 3
40. An arithmetic assignment operator combines the effect of what two operators?
Ans. An arithmetic assignment operator combines the effect of an arithmetic operator and an assignment operator.
41. Write a statement that uses an arithmetic assignment operator to subtract the value of variable ans by 17. Write
the same statement without arithmetic assignment operator.
Ans. Using arithmetic assignment operator:
ans - = 17
Without arithmetic assignment operator:
ans = ans - 17
42. Which header file must be included in the program to make use of C++’s built-in mathematical functions?
Ans. The Which header file math.h must be included in the program to make use of C++’s built-in mathematical
functions
TYPE B : SHORT ANSWER QUESTIONS
1. What are arithmetic operators in C++? Distinguish between unary and binary arithmetic operators. Give
examples for each of them.
Ans. The operators which are performed arithmetic operations are called arithmetic operators.
Unary arithmetic operators Binary arithmetic operators
A unary operator requires a single operand. A binary operators requires two
operands. unary+, unary-, ++, --, sizeof etc. are unary operators. +, -, *, /, % etc. are binary
operators.
For example, if a=2 then +a means 2 For example, 2 + 5 results in 7
2. What is the function of increment/ decrement operator? How many varieties do they come in? How are
these two varieties different from one another?
Ans. The increment and decrement operators (++ and --) add 1 or subtract 1 from operand’s value. They come in
two forms: postfix and prefix.
Postfix version Prefix version
It uses the value of the operand and then changes it. It first changes its operand’s value and then uses it.
For example, a++ or a-- For example, ++a or --a
3. Evaluate a + = a + ++a if a = 20 initially.
Ans. Initially a = 20
a += a + ++a
= 20 + 21
= 63
4. What role do the relational operators play? How is relational operator == different from =?
Ans. Relational operators compare the values of their operands. If the comparison is true, the relational
expression results into the value 1 and to 0, if the comparison is false.
‘==’ Operator ‘=’ Operator
It is a testing operator. It is a assignment operator.
For example, value == 3 For example, value = 3
It returns 1 if the comparison is true otherwise returns It assigns 3 to value.
0.
5. Why should following operations be discouraged?
(i) comparisons between two floating point numbers.
(ii) comparisons between signed and unsigned numbers.
Ans. (i) Floating-point arithmetic is not as exact and accurate as the integer arithmetic is. For instance, 3 * 5 is exactly
15, but 3.25 * 5.25 is nearly equal to 17.06. The exact number resulting from 3.25 * 5.25 is 17.0625. Therefore,
after any calculation involving floating-point numbers, there may be a small residue error. Because of this error, the
equality and inequality comparisons on floating-point number should be avoided.
(ii) It is because if we compare a signed value with an unsigned value, the compiler will treat the signed value as
4
unsigned. If the signed value is negative, it will be treated as an unsigned integer having the same bit pattern as the
signed value, and the result of the comparison will be arbitrary.
6. How are relational operators and logical operators related to one another?
Ans. The relational operator compares the values of their operands and logical operators connect the relationship among
values. For example, to check whether the age is more than 30 and experience is equal to 3 years following
expression is used: (age>30) && (expr=3)
Above code is a combination of relational and logical operators. Because logical AND operator (&&) has lower
precedence than the relational operator, parentheses are not used in above expression.
7. Illustrate with an example that the unary negation operator ! is useful as a test for zero.
Ans. The unary negation operator ! is useful as a test for zero. For example, the condition if (check == 0) can be written as
(!check). As unary negation operator negates the truth value of an expression, it will negates the value of the
expression (!check) and test whether the variable check is equal to 0 or not.
8. Evaluate the following expressions:
(i) x – y < z && y + z ? x || x – z <= y – x + z if x = 4, y = 7 and z = 10?
(ii) (y) && (y – z) || !(2y + z – x) if x = 13, y = 14 and z = 5
Ans. (i) (((x – y) < z) && ((y + z) > x)) || ((x – z) <= (y – (x + z))
(((4 – 7) < 10) && ((7 + 10) > 4)) || (((4 – 10) <= (7 – (4 + 10)))
((1) && (1)) || (0)
(1) || (0)
=1
The result is 1 i.e., true
(ii) (y) && (y – z) || !(2y + z – x)
((14) && (14 – 5)) || (!((2(14) + 5) – 13))
((14) && (9)) || (!(28+5)-13)
(1) || (0)
=0
The result is 0 i.e., false
9. State why are following expressions invalid?
(i) asm = 5100 || val<35
(ii) age > 70 && <90
(iii) income >= 5000 ||&& val <500
(iv) res! > 20 || x > 20.
Ans. (i) ‘asm’ is a keyword which cannot be used as a variable name.
(ii) There should be a variable name after && operator. (iii)
Both operator ‘||’ and ‘&&’ cannot be used together. (iv)
Both operator ‘!’ and ‘>’ cannot be used together.
10. What are unary, binary and ternary operators? Classify the operators you have learnt so far into these three
categories.
Ans. Unary operator: Operators that act on one operand are referred to as unary operators.
Binary operator: Operators that act upon two operands are referred to as binary operators.
Ternary operator: Operators that requires three operands are referred to as ternary operators.
Unary operator Binary operator Ternary operator
unary +, unary -, ++, --, sizeof +, -, *, /, % ?: (the conditional operator)
11. Under what condition do the mathematical function log(), log10() and pow() produce an error?
Ans. The mathematical function log(), log10() produce a domain error if argument is negative and a range error occurs if
the argument is zero. The function pow() produce a domain error if base = 0 and exp <= 0. Also if base < 0 and exp is
not integer.
12. What are the purposes of following mathematical functions in C++?
(i) atan() (ii) atan2() (iii) ceil() (iv) exp()
Ans. No. Function Prototype Description
(i) atan() double atan(double arg) The atan() function returns the arc tangent of arg.
(ii) atan2() double atan2(double b, double a) The atan2() function returns the arc tangent of b/a.
5
(iii) ceil() double ceil(double num) The ceil() function returns the smallest integer
represented as a double not less than num.
(iv) exp() double exp(double arg) The exp() function returns the natural logarithm e raised
to the arg power.
13. Describe the working of these mathematical functions:
(i) fabs() (ii) floor() (iii) fmod()
Ans. No. Function Prototype Description Example
(i) fabs() double fabs(double The fabs() function returns the fabs(1.0) gives 1.0
num) absolute value of num. fabs(-1.0) gives 1.0
(ii) floor() double floor(double The floor() function returns the floor(1.03) gives 1.0
num) largest integer not greater than num. floor(-1.03) gives -2.0
(iii) fmod() double fmod(double The fmod() function returns the fmod(10.0,4.0) returns
x, double y) remainder of the division x/y. 2.0
14. What is meant by type conversion? How is implicit conversion different from explicit conversion?
Ans. The process of converting one predefined type into another is called Type conversion. An implicit conversion is a
conversion performed by the compiler without programmer’s intervention whereas, an explicit type conversion is
user-defined that forces an expression to be of specific type.
15. Write arithmetic type conversion rules for float types and int types.
Ans. 1. If either operand if float then, other is converted to float.
2. Otherwise, the integral promotions are performed on both operands.
3. If one operand is a long int and the other unsigned int, then if a long int can represent all the values of an
unsigned int, the unsigned int is converted to long int; otherwise both operands are converted to unsigned long
int.
4. Otherwise, both operands are int.
16. Determine the data type of the expression
� (𝒑��+𝒓𝒓) � – �(𝒍𝒍 𝒍𝒔𝒔𝒍𝒍)(𝒔��+𝒑��)�
𝟏𝟏𝟏𝟏𝟏��(𝟏𝟏−𝒑𝒑𝒑𝒑) (𝒑��+𝒓𝒓)/𝒔𝒔
7
Ans. The conditional operator: It requires three operands, that is, it is a ternary operator. Its general form is expr1?
expr2: expr3. It gives the value of expr2 if expr1 evaluates to true otherwise it gives the value of the expr3. For
example, 13<20? 1: 2 evaluates to 1.
The sizeof operator: Returns the size of a variable or a data-type. It can be used in two forms: sizeof var (where var
is a declared variable) and sizeof(type) (where type is a C++ data type. For example, sizeof(int) returns 2.
The comma operator: Strings together several expressions which are evaluated from left-to-right and the value of
the rightmost expression becomes the value of the total comma-separated expression. For example, y = (x=2, x-1);
first assign x the value 2 and the assigns y the value x-1 i.e., 1.
26. An expression uses arithmetic, relational and logical operators together. How would you determine the type
(arithmetic, logical) of the expression?
Ans. Type of operators used in an expression determines the expression type. If an expression uses arithmetic, relational
and logical operators together, we have to divide the expression in sub-expression and then decide the type of
expression for sub-expression.
27. Discuss the benefits and loopholes of type casting.
Ans. By type casting we can convert variable from one data type to another data
type, which give us advantage to manipulate variable as per our convenience.
There few loopholes in type casting which are as follow –
1. Assigning a value of larger data type to a smaller data type may result in losing some precision.
2. Conversion of bigger floating-point type to smaller floating-point type may result in loss of precision.
3. Conversion of floating-point type to integer type may result in loss of fractional part.
4. Conversion of bigger integer type to smaller integer type may result in loss of information. Original value may be
out of range for target type.
28. Write a program which will raise any number X to a positive power n. obtain values of x and n from user.
Ans. #include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ clrscr();
int X,p,res;
cout<<"Enter X: ";
cin>>X; cout<<"Enter
p: "; cin>>p;
res=pow(X,p);
cout<<"\t"<<res;
getch();
}
29. Write a C++ program to input principle amount and time. If time is more than 10 years, calculate the simple
interest with rate 8%. Otherwise calculate it with rate 12% per annum.
Ans. #include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float i,p,r,n;
cout<<"Enter Price: ";
cin>>p;
cout<<"Enter Duration: ";
cin>>n;
if(n>10)
i=(p*8*n)/100;
else
i=(p*12*n)/100; cout<<"\
t"<<"Simple Interest: "<<i; getch();
}
8
30. Write a C++ program to input a number. If the number is even, print its square otherwise print its cube.
Ans. #include<iostream.h>
#include<conio.h>
void main()
{ clrscr(); int n,res;
cout<<"Enter n: ";
cin>>n;
if(n%2==0)
res=n*n;
else
res=n*n*n;
cout<<"\t"<<res;
getch();
}
31. Write a C++ program to input a number. If the number n is odd and positive, print its square root otherwise print
5
n.
Ans. #include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ clrscr();
int n,res;
cout<<"Enter n: ";
cin>>n;
if((n%2!=0) && (n>0))
res=sqrt(n);
else
res=n*n*n*n*n;
cout<<"\t"<<res;
getch();
}
32. Write a C++ program to input choice (1 or 2). If choice is 1, print the area of circle otherwise print the perimeter of
circle. Accept the radius of circle from user.
Ans. #include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int ch;
float r,area,peri;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter radius: ";
cin>>r;
area=3.14*r*r;
cout<<"Area of circle: "<<area;
break;
case 2: cout<<"Enter radius: ";
cin>>r;
peri=2*3.14*r;
cout<<"perimeter of circle: "<<peri;
break;
default: cout<<"Wrong choice";
}
9
getch();
33. Write a C++ program to input three integers and print the largest of three.
Ans. #include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a,b,c;
cout<<"Enter a, b, and c: ";
cin>>a>>b>>c;
if((a>b) && (a>c))
cout<<"Largest: "<<a;
else if((b>a) && (b>c))
cout<<"Largest: "<<b;
else
cout<<"Largest: "<<c;
getch();
}
34. Write a C++ program to input a student type (‘A’ or ’B’). if the student type is ‘A’ initialize the college account with
Rs. 200/- otherwise initialize the hostel account with Rs. 200/-.
Ans. #include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int clg_acc,hos_acc;
char type;
cout<<"Enter Student Type ('A' or 'B'): ";
cin>>type;
if(type=='A')
{ clg_acc=200;
cout<<"College account is initialized with Rs."<<clg_acc;
}
else
{ hos_acc=200;
cout<<"Hostel account is initialized with Rs."<<hos_acc;
}
getch();
}
35. Write a program that reads in a character <char> from the keyboard and then displays one of the following
messages:
(i) If <char> is a lower case letter, the message
(The upper case character corresponding to <char> is ….”
(ii) If <char> is a upper case letter, the message
(The lower case character corresponding to <char> is ….”
(iii) If <char> is not a letter, the message <chr> is not a letter”,
Hint. You will need to refer to a table of ASCII characters. Lowercase letters have ASCII value range 97-122.
uppercase letters have ASCII value range 65-90.
Ans. #include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{ clrscr();
char ch;
12