0% found this document useful (1 vote)
408 views10 pages

E Balagurusamy CH 5 Solution 2.

This document contains solutions to review questions about decision making and branching in ANSI C. It includes: 1) True/false questions about if/else statements and switch statements. 2) Questions testing understanding of logical operators, if/else statements, switch statements. 3) Code snippets with errors to identify or code to rewrite without compound conditions. 4) Questions evaluating logical expressions and predicting output of code segments.

Uploaded by

Priya Bhavsar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
408 views10 pages

E Balagurusamy CH 5 Solution 2.

This document contains solutions to review questions about decision making and branching in ANSI C. It includes: 1) True/false questions about if/else statements and switch statements. 2) Questions testing understanding of logical operators, if/else statements, switch statements. 3) Code snippets with errors to identify or code to rewrite without compound conditions. 4) Questions evaluating logical expressions and predicting output of code segments.

Uploaded by

Priya Bhavsar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

*****

***********
ANSI C REVIEW QUESTION SOLUTION

Chapter- 05

Decision making and Branching


5.1:State whether the following are true or false:

last else gets associated with


(aWhen if statements are nested, the
the nearest if without an else.
Ans: False.
(b)One if can have more than one else clause.
Ans: False.
(c)A switch statement can always be replaced by a series of if.else
statements.
Ans: False.
(d)A switch expression can be of any type.
Ans: False.
(e)A program stops its execution when a break statement is
encountered.
Ans: False.
(f)Each expression in the else if must test the same variable.
Ans: True. TEVE JOBS
(g)Any expression can be used for the T expressioEaEEW wOR
Ans: True.
(h)Each case label can have only one statement.
Ans: True.
()The default case is required in the switch statement.
Ans: True.
GThe predicate {(x=10) I (y==5) ) is equivalent to (x«10) && (yl=5).
Ans: False

MOHAMMAD ABIR REZA (CSE-5TH BATCH)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
cOMILLA UNIVERSITY
** **** ************ ************* ****** ***** *************** *****
*****************************************************************************************************************************************************************************e******************************************************

Page 48

5.2:Fll in the blanks in the following statements:


(a)The....operatori true only when both the operands are true.
Ans: logical AND (&&).
(b)Multiway section can be accomplished using an else if statement or
the Statement.
Ans: switch.
(c)The.... statement when executed in a switch statement causes.
immediate exit from the structure
Ans: break.

(d)The ternary conditional expression using the operator ?: code be


easily coded using.. statement.

Ans: if...else.
(e)The expression !(xl=y)can be replaced by the expression...
Ans: x==y.

5.3:Find errors, if any, in each of the following segments:


(a)if(x*y=z) && (y>0) )
Printf (" "):
Answer: Error.
Correct answer: if((xty==z) && (y>0))
STEVE JOBS
printf" ")
(b)if (code>1)
a= b+c
else
a=0
Answer: Error.
Correct answer: if (code >1)
a= btC;
else
a=0;
(c) if(p>0) I1 (q <0)
printf("Sign is negative");
Answer: Error
Correct answer: if(p>0) || (q <0))
printf"Sign is negative");

Mohammad Abir Reza [CSE. sTH BATCH]


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

5.4:The following is a segment of a program:


x=1; statement is not
N.B: This
y1; associated with if condition.

if(n>0)
X=X*1;
y=y-1;
printf(%d %d", x.y): 1and
What will be the values of x and y if n assumes a value of (a)
(b) 0.
Solution:

(a)2,0
(b) 1,0

5.5:Rewrite each of the following without using compound relations:

(a)if (grade<=59 && grade>=50)


second=second+1;

Answer:
if (grade<=59) STEVE JOBS
if(grade >=50)
Second=second+1;

(b) if (number>100 || number<0)


Printf("out of range");
else
Sum=sum+ number;

Answer
if (number>100)
Printf("Out of range");
else if (number<0)
Printf('Out of range");
else
Sum=sum+1;

MOHAMMAD ABIR REZA (CSE-5TH BATCH)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
COMILLA UNIVERSITY
(b)if (M1>60 && M2>60) || T>200)
printfi(Admitted In):
else
printf("Not admittedin");
answer

if (M1>60)

if (M2 >60)
Printf("AdmittedIn");
else if (T>200)
Printf" AdmittedIn);
else if (T>200)
Printf("'Admitted In);
else
Printf("Not admittedin);
5.6:Assuming x=10 ,state whether the following logical expressions
are true or false:
(a)x==10 && x>10 && Ix Ans:False.
(b)x==10|| x> 10 && Ix Ans:True.
(c)x==10 && x>10||!lx Ans:False. STEVE JOBS
(d)x==10 ||x>10 || !x Ans:True.

5.7:Find errors,if any, in the following switch related statements.


Assume that the variables x and y are of int type and x=1 and y=2.
(a)switch(y):
Answer Eror. Correct answer: switch(y)

(bcase 10: Semicolon (;)


Answer Eror.Correct answer: case 10:
after break.

(c)switch(x+y)
Answer: No erro.

(d)switch(x) {Case 2: y= x*y; break}


Answer Eror.Correct answer: switch(x) {Case 2: y= x*y; break;}

Mohammad Abir Reza [CSE- 5TH BATCH]


Department of Computer Science & Engineering
Comilla University
5.8:Simplify the following compound logical expressions:
(a)(x=10)
Answer:(x>10)
(b)!x==10)(y==5)||(z<0))
Answer: x!=10|| y!=5 && z>=0

(c(x**y==z)8&!(z>5)
Answer: !(x+y==z) || (z>5) x+y!=z || z>5

(d)(x<=5}&&(y==10)8&(z<5))
Answer: x>5|| y!=10 | z>=5
be their valuees
5.9:Assuming that x=5, y=0,and z=1 initially ,what will
after executing the following code segments?
(a jif(x && y)
x=10;
else
y=10;
Output: x= 5, y= 10,z= 1
(bif(xl|l y ||2)
10 STEVE JOBS
else
z=0;
Output: x5, y= 10, z=1
(c)if(x)
if(y)
10:
else
z=0;
Output: x= 5, y=0, z=0

(d iftx ==0 I| x && y) Here 'else' is


if(ly) related with
Z=0;
else. OC previous if.
y=1;
Output: x= 5, y=0, z=1

MOHAMMAD ABIR REZA (CSE-5TH BATCH)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
COMILLA UNIVERSITY
Page 52

5.10:Assuming that x=2,.y=1 and z=0 initially ,what will be their values
after executing the following code segments?
(a)
switchx)
case 2:
x=1;
y=x+1;
case 1:
x=0;
break;
default:
x=1;
y=0;

Output: x=0,y= 2,z= 0

(b)
switch(y)
case 0: STEVE JOBS
x=0;
y=0;
case 2:
x=2;
z-2;
default:
x=1;
y-2;
Output: x=1,y= 2, z= 0

Mohammad Abir Reza [CSE- 5TH BATCH]


Department of Computer Science & Engineering8
Comilla University
*************************************** *********
*******************m*********************************************************e******************************************************************************************************************************************1
ANSI C REVIEW QUESTION SOLUTION

5.11:Find the error ,if any, in the following


statements:
(aif (x>=10) then AFTER CORRECTION
printf" In") Error (a)if (xo=10)
(b) if x>=10 printf" In ")
Printf(" ok "; Error (b) if (x=10)
(c) if (x=10)
printf("GOOD"); No error Printf ok ");
(d) if (x=<10) (c) if (x=10)[no error]1
Printf(" Welcome "); Error printfCGOOD");
(d) if (x-10)
5.12:What is the output of the following PrintfCWelcome'"):
program?
main()
int m=5;
if(m3) printf("%d", m+1);
else if (m<5) printf("%d", m+2);
else if (m<7) printf("%d", m+3);
else printf("%d", m+4)
getch();
Output: 8 STEVE JOBS
5.13:What is the output of the following program?
DESIGEP Df NIW WORLI
main (

int m=1;
if( m==1)
printf ("Delhi");
if(m==2) Be careful- if there is
printf("Chennai"); semicolon (:) after else, it
else takes as a blank statement. So
printf("Banglore"); here printf¢END"); 'statement
else is not working with 'else.
Printf("END");
getch():
Output: Delhi Banglore END

MOHAMMAD ABIR REZA (CSE-5TH BATCH)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
COMILLA UNIVERSITY

e********. ********** ****


******************************************a**********
*****

Page 54

5.14:What is the output of the following program?


Program:
main)
int m
or(m=1; m<5; m++)
printf("%din",(m%2) ? m: m 2);
getch();
Output: 1
4
3
8

5.15:What is the output of following


program?
main()
int m,n,P
for(m=0; m<3;m++)
for(n=0;n<3;n++) If there is 'goto'
for(p=0;p<3;p++) statement, it comes out
ifm +n *p==2),
goto print; oO from the loop.
print:
printf("%d %d %d", m, n, p};
getch);

Output: 0 0 2

5.16:What will be the value of x when the following segment is


executed?
int x=10,y=15;
x=
(xsy)?(y+x) : (y-x);
Solution:
The value of x after execution is: 25
Mohammad Abir Reza [CSE- 5TH BATCH]
Department of Computer Science & Engineering
Comilla University
ANSI C REVIEW QUESTION SOLUTION

5.17:What will be the output when the following segment is executed?


int x=0;
ifx>=0)
if(x>0)
printf("Number is positive");
else
printf"Number is negative");
Output:
Number is negative

5.18: What will be the output when the following segment is


executed?
Programn:
char ch = 'a'

switch(ch)
case 'a':
printf("A"):
case 'b':
printf("B");
case 'c': STEVE JOBS
printf("C"):
Output: ABC

5.19:What will be the output of the following segment when executed?


Program:
main()

int x=10,y=20;
if(( x<y)|l (x*5)>10)
printf(%d",x);
else
printf("%d",y);
getch(:
Output: 10

MOHAMMAD ABIR REZA (CSE-5TH BATCH)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
COMILLA UNIVERSITY
Page 56

5.20:What will be the output of the following segment when executed?

main()
int a=10, b=5;
if(a>b)
if(b>5)
printf(%d",b);
else
printf(%d",a);
getch();

Answer: blank.
That means there will be no output.

STEVE JOBS
DFSIGAER D N[W WORLD

Mohammad Abir Reza [CSE- 5H BATCH]


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

*********** ********

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy