Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Chapter: 03

Operators and Expressions

3.3 Given the statement

Int a=10, b=20,c;


Determine whether each of the following statements are true or
false.

a. The statement a =+10, is valid.

Answer: False.

b. The expression a+ 4/6 "6/2 evaluates to 11.

Answer: False.

c. The expression b+ 3/2* 2/3 evaluates to 20.


STEVE JOBS
0ESIGUER OF NEW IVORL
Answer: True .

d. The statement a+=b; gives the values 30 to a and 20 to b.

Answer True.

e. The statement ++at+; gives the value 12 to a

Answer: False.

f. The statement a=1/b; assigns the value 0.5 to a

Answer: False.

MOHAMMAD ABIR REZA (CSE-5TH BATCH)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
COMIL.LA UNIVERSITY
Page 26

3.4 Declared a as int and b as float, state whether the following


statements are true or false.
a. The statement a=1/3 + 1/3 + 1/3; assigns the value 1 to a.

Answer: False.

b.Thestatement b=1.0/3.0 +1.0/3.0 +1.0/3.0; assigns a value 1.0 to


b.

Answer: True.

c. The statement b=1.0/3.0 *3.0 gives a value 1.0 to b.

Answer: True.

d. The statement b=1.0/3.0+ 2.0/3.0 assigns a value 1.0 to b.

Answer: True.

e. Thestatement a=15/10.0+3/2; assigns a value 3 to a.


Answer: False.
STEVE JOBS
3.5 Which of the following arithmetic expressions are true ?
a. I(5+5 >=10)

Answer: False.

b. 5+5- 10 | 1+3 ==5


Answer true .

C. 5>10 || 10<20 && 3<5

Answer : true.
d. 10 15 && (10<20) || 15>30
Answer: False.
Mohammad Abir Reza [CSE- 5TH BATCH]
Department of Computer Science & Engineering
Comilla IInivers
3.6 Which of the following arithmetic expressions are valid ? If
valid, give the value ofthe expression; otherwise give reason.
(a )25/3 %2

Answer: 0

Because the expression 25/3' evaluates to 8. Because the number 3 is int


type. and the expression '8%2' evaluates to 0.

(b) +9/4+5
Answer: 7

(c)7.5%3
Answer: Not valid.
Invalid operands to binary % (have "loat and 'int' type)
(d) 14%3+ 7%2
STEVE JOBS
Answer: 3 ESIGNER UF NEW WORLD

(e)-14%3
Answer: -2

(15.25+5.0
Answer 10.250000
g5/3) 3 +5%3

Answer 5

(h) 21%(int)4.5
Answer: 1

MOHAMMAD ABIR REZA (CSE-5TH BATCH)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
COMILLA UNIVERSITY
3.8 Identify unnecessary parentheses in the following arithmetic
expressions.(red color is unnecessary)

(a) ((x-(y/5)+z)%8)+25

(b) ((x*y)°p)+q
(c)(mn) +(xly)
(d) x/(3y)
3.9 Find errors,if any, in the following assignment statements
and rectify them.
a. x=y=z = 0.5,2.0,-5.75;

Answer: Error.

Correct answer : x=0.5, y=2.0, z=-5.75;


b. m=t+a "5; STEVE JOBSS
Answer: No Error.
C. y=sqrt (100)
Answer: No error.

d. p =xly:
Answer Error.(no space between * and )

e. s=/5;

Answer Error.(T should be used before =")


f. a b++ - c*2

Answer: Error.(semicolon)
Mohammad Abir Reza [cSE- 5TH BATCH]
Department of Computer Science & Engineering
Comilla Uníversity
ANSI C REVIEW QUESTION SOLUTION

3.10 Determine the value of each of the following logical


Expressions if a=5, b=10 and c=-6
a. ab && ac

Answer: false.
b. acb && a>c

Answer: True.

C. a=c|| b>a

Answer: True.

d. b>15 && c<0 || a>0

Answer: True.

e. (a/2.0-=0.0 && b/2.0 I= 0.0) || c<0.0 STEVE JOBS


0ESIGAER OF NEW VORID
Answer: True.

MOHAMMAD ABIR REZA (CSE-5TH BATCH)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Page 30

3.11 What is the output of the following program ?


main()

charX,

int y

x=100;

y=125;

printf("%cn".x);
printf("%cn".y):
printf("%din",x);

Output
d

100 STEVE JOBS


3.12 Find the output of the following program ?ER OREO NOR
main)

intx =100;

printf("%din ",10+ x++);

printf("%d/n",10+ ++x}

Output: 110/n112/n

Mohammad Abir Reza [CSE- 5TH BATCH]


Department of Computer Science & Engineering
Comilla University
***********************************************************************************************************************************************************************************************************************
******
********************

ANSI C REVIEW QUESTION SOLUTION

3.13 What is the output of the following program?


main()

z=10:
int x=5, y=10,
x= y==z;

printf("%d"x):

Output:

program ?
the following
the output of
3.14 What is

STEVE JOBS
NEW IWORID
main() OESIGHER OF

tr5100.Y=200;
3.15 What is the output of the following program ?
main()

unsigned x=1;

signed y=-1;

if(x>y)
printf("x>y'):
else

printf('x=y");

Did you expect this output ? Explain

Output: x<=y
Yes Iexpected this output. STEVE JOBS
In this program to evaluate if expression the signed char y=-1 is
converted to unsig ned int type. So the "if' condition becomes false
and 'else' part in executed. So the output is x<=y.

3.16 What is the output of the following program ? Explain the


output.
main()

int x=10;

if(x=20)
printf("true");
Mohammad Abir Reza [cSE-5H BATCH]
Department of Computer Science & Engineering
Comilla University
***
ANSI C
REVIEw QUESTION SOLUT

else

printf("fulse");

Output: true
Computer evaluates the expression first and then
whether the value of the depending on
non zero) or
expression (relation or condition) is 'true' (or
false'(zero), it transfers the control to a particular
statement. In this case the
expression x=20 evaluates to 20(non
zero).So the expression is true and the statement after it is
So the is TRUE
executed.
output

3.17 What is the error in each of the


following statement ?
(a) if(m==1& nl=0)
printf("ok");
Answer: Error STEVE JOBS
CA: if(m==1&& nl=0)

printf("'ok")

(b) if(x=<5)
printf(jump"):
Answer: Error

CA: if(x<=5)

printf(jump"):

MOHAMMAD ABIR REZA (CSE-5TH BATCH)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
cOMILLA UNIVERSITY
Page 3

3.18 What is the error, if any, in the following segment ?


int x=10;

float y=4.25;

X= y% X;
Answer:There is an error in the segment. The modulo division operator
% cannot be used on floating point data. Because real operator can't use
%

3.19 What is printed when the following is executed ?

for(m=0;m<3;++m)
printf("%din".(m%2) ? m:.m+2);

Output: 2

3.20 What is the output of the following segment when executed ?


DESIGER OF W WORLD
int m=-14,n=3;
printf("%din",m/n* 10)
n=-n
printf("%d n",m/n*10);

Output:40

40

Mohammad Abir Reza [CSE- 5TH BATCH]


Department of Computer Science & Engineering
Comilla University

You might also like