Sie sind auf Seite 1von 8

ASSIGNMENT OF OPERATORS AND EXPRESSIONS

Class XI
Subject Computer Science

1. Write equivalent c++ expressions for the following.


i) cos x ii) a4 +b4-c + | d |
tan x +xe

iii) val=2(yz)-xe2x +4cos x iv) ax8+bx6-tan-1b


Answer:
i. cos(x)/ (tan(x) + x*e)
ii. pow(a,4)+pow(b,4)-c +fabs(d)
iii. val=2*(y*z)-x*pow(e, 2*x)+ 4*cos(x)
iv. a*pow(x,8)+b*pow(x,6)- tanh(b)
2. Evaluate the following expressions

i) x *(++ y) y % 10 +3
ii) (x+y > z) ? cout<<( ++ x y) :cout<< (- - z + ++y) where x=8, y=10, z=8
Answer :
i. 90
ii. -1
3. Construct logical expressions to represent the following conditions
i) salary is in the range 8000 to 10000
ii) ch is an uppercase letter
iii) weight is greater than or equal to 30 but less than 50
iv) a is an odd number
Answer:
i. salary>=8000 && salary<=10000
ii. ch>=65 && ch<=90
iii. weight>=30 && weight<50
iv. a%2!=0? cout<<Odd:cout<<Even;

4. . . Evaluate the following


a) x-y<z && y+z >x||x-z<=y-x+z if x=4, y=8,z=9
b) y> z && (x-z<y) || !(2y<z-x) if x=10 ,y=11, z=10
Answer:
a. T or 1
b. T or 1
5. Convert the following if-else to a single conditional statement using conditional
operator.
if(qty>=20)
order = max +5;
else
order = max;

answer : order= qty>=20 ? max+5: max


6. Write equivalent c++ expressions for the following.
i) cos 2x + 1 ii) (1-y3)0.5
sin z - | 1+a | (1+x )
Answer :
i. (cos(2*x) + 1)/(sin(z)-fabs(1+a))
ii. pow(1-pow(y,3), 0.5))/(1+x)
7. Evaluate the following expressions
i) y = ++x + ++x where x=10
ii) !(a<b) && c!= d || b+c < a where a=7, b=3, c=5, d=6
Answer:
i. 23
ii. T or 1
8.. Give the output
#include<iostream.h>
void main( )
{ int a=2,b=5,c=3,m;
m=(a>b) ? c : (( b>c) ? b : c ) ;
cout<<m; }

Answer: 5
9. Give the output
#include<iostream.h>
void main()
{ int U=10, V=20;
cout<<[1]=<<U++<<&<<V-5<<endl;
cout<<[2]=<<++V<<&<<U+2<<endl; }
Answer: [1]=10&15
[2]=21&13
10. Differentiate between the following.

i) Unary, Binary and Ternary operators

ii) = and = =
iii) / and %

Unary: They operate on single operands eg. increment decrement operators(++,--), unary
Binary: operate on two operands eg arithmetic, relational, logical operators
Ternary: operate on three operands, eg conditional operator ?:

Give output:
#include<iostream.h>
void main( )
{
int a=2,b=5,c=3,m;
m=(a>b) ? c : (( b>c) ? b : c ) ;
cout<<m;
}
Answer : 5

14. What is the output of the following?


i) # include<iostream.h>
void main ( )
{
int ch=20;
cout << ++ch <<\n<< ch<<\n;
}
What is the effect of replacing ++ ch with ch+1?

Answer: 21
21
After replacing: 21
20

15 What output will be the following code fragment produce?


void main( )
{
int val, res, n=1000;
cin>>val;
res = n+val >1750 ? 400:200;
cout<<res; }
(i) if val=2000 (ii) if val=1000 (iii) if val=500
Answer :
i. 400
ii. 400
iii. 200
16 Find the error from the following code segment and rewrite the corrected code
underlining the correction made.
# include(iostream.h)
void mains ( )
int X,Y;
cin>>X;
cout>>y;
if x= =y
cout<<Y+X;
else
cout>>Y; }
Answer:
# include <iostream.h>_
void main_ ( )
{ int X,Y;
cin>>X;
cout<<y;
if ( x= =y)
cout<<Y+X;
else
cout>>Y; }

17 What is the output of the following?


i) # include<iostream.h>
void main ( )
{ int i=0;
cout<<i++<< <<i++<< <<i++<<endl;
cout<<++i<< <<++i<< <<++i<<endl ;}
Answer :
210
654

18 Evaluate the following conditional statement given values of a=9, b=8.


N=((a<b)?a:b);
Answer: N=8

20. What will be the size of the following constants


i) 100 ii) 98.967 iii) 8 iv) *
Answer:
i. 2 bytes(int)
ii. 4 bytes(float)
iii. 1 byte(character)
iv. 2 bytes(string one byte for * one for null character)

21 What will be the output of the following?


#include<iostream.h>
void main()
{ int a=5, b=5, c=3, ans;
ans = (a < ++b ? b/2 : c/2);
cout<<ans;
}
Answer :
3
22. What will be the output of the following?
#include<iostream.h> (2)
void main()
{ int A=5, B=10;
cout<< Line1 =<<A++<<&<<B-2<<endl;
cout<< Line2=<<++B<<&<<A+3<<endl;
}
Answer :
Line 1 = 5 & 8
Line 2 = 11 & 9
23 . Predict the output of following code segment

int n=7;
cout<<n++=<<n++<<,n=<<n<<\n;
Answer :
n++=7,n=8
24. What will be the size of following constants : \a , A\a , Reema\s , / \ .

25. What will be the size of following constants?


Computer, \
26 Write the corresponding C++ expressions for the following mathematical expressions:

i) ut+ ft2 (ii) v-w/(a+b)9


Answer:
i. 1/(u*t+2*f*t*t)
ii. v- w/pow((a+b),9)
27 Using the given values of x, y and z, evaluate the following( answer in True/ False)
(X<=Y) | | ( ! (Z >= Y) &&(Z = =X))

(i) X=10, Y=2, Z=11 (ii) X=11,Y=11,Z=11 (iii) X=9,Y=10,Z=5


Answer :
i. False or 0
ii. True or 1
iii. True or 1

29. Answer the following questions.


(a) What is the purpose of comments and indentation in a program ?
(b) What do you mean by cascading of I/O operators?
(c) What are the predefined stream objects in I/O Library ?
(d) The modulus operator works only with integers True or False.

(e) What do you mean by runtime error and logical errors ?


Answer : Refer to blog
30 What is the output of following program?
int result = 4 + 5 * 6 +2;
cout<<result;

int a = 5 + 7 % 2;
cout<<a;

Answer :

366

31 Evaluate the following :


(i) (a>=b)||(!c==b) &&(c<a) [where a=10,b=5,c=11]
(ii) c=(a++)*d+a [where a=5,b=3,d=1.5]

(iii)(x<y)&&(z==x) || y [where x=3,y=5,z=9]

Answer :
i. True or 1
ii. c=13.5
iii. False or 0

32 What is the output of following program?

int x = 10,y; int x = 10; cout<<++x; int x = 10;


y = x++; cout<<x++;
cout<<y<<x;

Output: Output: Output:


1011 11 10

34. What is the output of following program?


int i=9,z=20; int a=25; int x=a/3; int x=9;
cout<<i++<<++i<<z+ i++; cout<<a++; int z=x++ + ++x;
cout<<++a; cout<<z;
cout<<a<<++x<<a++<<x--;
Output:
Output: Output: 20
111129

int x=17; int z=9,x=17; int x=17; int y=x++;


int y=x--; int y; cout<<++y; x++;
cout<<++y; y=x%z; cout<<++x + y++<<y++;
--x; cout<<x/y<<y++<<-- z * x;
cout<<--x + y++<<y++;

Output: Output: Output:


183318 18136 183918

35 Write the equivalent C++ expressions for the following :

(i) T=(z+3) *(y-4) (ii) W=log(v) (iii) A=tan-1 x

Answer:

(i) T=(z+3)*(y-4)
(ii) W=log(v)
(iii) A=tanh(x)
36 Explain the following functions with example

clrscr() (ii) pow() (iii) sqrt (iv) ceil() (v) setw() (vi) floor

Refer to math,h header file and blog.

38. Given the following expressions


a) v = 5 b) v==5
i) How are these two different
ii) What will be the result of the two if the value of val is 10 initially?

Answer:
i. in the first statement the value 5 is assigned to 5 whereas in the second statement it is
checked whether the value of v is 5 .
ii. first statement : v= 5, in the second statement 0 will be output as v =10 thus statement
v= =5 will be false or 0.
39. Correct the error(s) if any in the following code segment
int i, float k; i = 25;
k = 75.90;
cout >> \n Enter The new values of I and k ;
cin << i<< k;
cout<< The new values are \n << i <<\n << k;
Answer :
int i, float k;
i = 25;
k = 75.90;
cout << \n Enter The new values of I and k ;
cin >> i>> k;
cout<< The new values are \n << i <<\n << k;

40.State true or false


(i) A variable of type char can hold value 310.
(ii) A short int and int variable may have equal sizes in C++
(iii) The char is treated as one of the integer type
(iv) An identifier declared as char type cannot hold numbers
(v) Data type long float is a valid data type

Answer:
i. True
ii. True
iii. True
iv. False
v. False

Das könnte Ihnen auch gefallen