Chapter No-05(Decision making and branching)
=============================================================================
RQ-5.1: State whether the following are true or false :
(a)When
if statements are nested, the last else gets associated with 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.
(g)Any expression can be used for the if expression. Ans: True.
(h)Each case label can have only one statement. Ans: True.
(i)The default case is required in the switch statement. Ans: True.
(j)The predicate !(
(x>=10) (y==5) ) is equivalent to (x<10) && (y!=5).
Ans: True
RQ-5.2:Fill in the blanks in the following statements:
(a)The logical AND (&&) operator
is true only when both the operands are true. (b)Multiway section can be
accomplished using an else if statement or
the switch statement.
(c)The break statement when executed in a
switch statement causes immediate exit from the structure
(d)The ternary conditional expression using the
operator ?: code be easily coded using if…else statement.
(e)The expression !(x!=y)can be replaced by the expression x==y.
RQ-5.3:Find errors, if any, in each of the
following segments: Solution:
(a) if((x+y=z)
&& (y>0) )
|
(b) if (code >1)
|
||||||
printf(" ");
|
a= b+c
|
||||||
else
|
|||||||
Ans: Error.
|
a=0
|
||||||
Correct ans:
|
Ans:
Error.
|
||||||
if((x+y==z) && (y>0) )
|
Correct ans: if (code >1)
|
||||||
printf(" ");
|
a= b+c;
|
||||||
else
|
|||||||
a=0;
|
|||||||
Ans:
Error.
Correct ans: if((p>0) || (q <0))
printf("Sign is
negative”);
RQ-5.4:The following is a segment of a program:
x=1;
y=1;
if(n>0)
x=x+1; y=y-1;
printf("%d %d",
x,y);
what will be the values of x and y if n assumes a value of (a)
1and (b) 0.
Solution:
(a)The value of x is 2 & y is
0. (b)The value of x & y is
imaginary.
RQ-5.5:Rewrite each of the following without using compound relations:
(a) if(grade<=59&&grade>=50)
|
(b) if ( number>100||number<0)
|
|||
second=second+1;
|
printf(“Out of range”);
|
|||
else
|
||||
Solution:
|
sum=sum+number;
|
|||
if(grade<=59)
|
Solution:
|
|||
second=second+1;
|
if ( number>100)
|
|||
if(grade>=50)
|
printf(“Out of range”);
|
|||
second=second+1;
|
else if(number<0)
|
|||
printf(“Out of range”);
|
||||
else
|
||||
sum=sum+number;
|
||||
(c) if
(M1>60&&M2>60||T>200)
|
||||
printf(“Admitted\n”);
|
||||
else
|
||||
printf (“Not admitted”);
|
||||
Solution:
|
||||
if (M1>60)
|
||||
printf (“Admitted\n”);
|
||||
if (M2>60)
|
||||
printf (“Admitted\n”);
|
||||
else if(T>200)
|
||||
printf (“Admitted\n”);
|
||||
else
|
||||
printf (“Not admitted”);
|
||||
RQ-5.6:Assuming x=10 ,state
whether the following logical expressions are true or false:
(a)x==10 && x>10 && !x
|
False
|
(b)x==10 || x> 10 && !x
|
True
|
(c)x==10 && x>10 ||!x
|
False
|
(d)x==10 ||x>10 || !x
|
True
|

RQ-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.
Solution:
(a)
|
switch(y);
|
(b)
|
case 10;
|
Ans: Error.
|
Ans:
|
Error.
|
|
Correct ans: switch(y)
|
Correct ans: case 10:
|
||
(c)
|
switch(x+y)
|
(d) switch(x) {Case 2:
y= x+y; break};
|
|
Ans: No
error.
|
Ans: Error.
|
||
Correct ans: switch(x) {Case 2:
y= x+y; break;}
|
RQ-5.8:Simplify the following compound logical expressions:
Questions
|
Answer
|
(a) !(x<=10)
|
x>10
|
(b)!(x==10)||!((y==5)||(z<0))
|
x>0
|
(c)!((x+y==z)&&!(z>5))
|
x<z
|
(d)!((x<=5)&&(y==10)&&(z<5))
|
x>5
|
RQ-5.9:Assuming that x=5, y=0,and z=1 initially
,what will be their values after executing the following code segments?
(a)if(x && y)
|
(b)if(x|| y ||z)
|
x=10;
|
y=10;
|
else
|
else
|
y=10;
|
z=0;
|
Output:
|
Output:
|
10
|
1
|
10
|
0
|


(d)if(x
==0 || x && y)
|
|
if(y)
|
if(!y)
|
z=10;
|
z=0;
|
else
|
else
|
z=0;
|
y=1;
|
Output:
|
Output:
|
10
|
0
|
0
|
1
|
RQ-5.10:Assuming that x=2,y=1 and z=0 initially
,what will be their values after executing the following code segments?
(a)
|
(b)
|
||||
switch(x)
|
switch(y)
|
||||
{
|
{
|
||||
case
|
2:
|
case
|
0:
|
||
x=1;
|
x=0;
|
||||
y=x+1;
|
y=0;
|
||||
case
|
1:
|
case
|
2:
|
||
x=0;
|
x=2;
|
||||
break;
|
z=2;
|
||||
default:
|
default:
|
||||
x=1;
|
x=1;
|
||||
y=0;
|
y=2;
|
||||
}
|
}
|
||||
Output:
|
Output:
|
||||
1
|
0
|
0
|
0
|
0
|
|
RQ-5.11:Find the error ,if any,in the following statements:
Solution:
Questions
|
Answer
|
(a)if(x>=10)
|
No error
|
printf("\n");
|
|
(b)if(x>=10)
|
No
error
|
printf("OK");
|
|
(c)if(x==10)
|
No
error
|
printf ("Good");
|
|
(d)if(x=<10)
|
Error
|
printf("Welcome");
|
Correct ans: if(x<=10)
|
Printf(“Welcome”);
|


Output:
|
8
|
RQ-5.12:What is the output of the following program?
Program:

main()
{
int m=5;
if(m<3) 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();
}
RQ-5.13:What is the output of the following program?
Program:
main ()
|
Output:
|
{
|
1
|
int m=1;
|
Delhi
|
if( m==1)
|
2
|
{
|
Chennai
|
printf ("Delhi");
|
3
|
if(m==2)
|
Banglore
|
printf("Chennai");
|
|
else
|
|
printf("Banglore");
|
|
}
|
|
else
|
|
Printf("END");
|
|
getch();
|
|
}
|
RQ-5.14:What is the output of the following program?
Program:
main()
|
Output:
|
{
|
1 4 3 8
|
int m;
|
|
for(m=1; m<5; m++)
|
|
printf("%d\n",(m%2) ? m : m*2);
|
|
getch();
|
|
}
|

Output: a
|
A b B c C
|
RQ-5.15:What is the output of following program?
Program:
main()
|
Output:
|
{
|
0 0 2
|
int m,n,p;
|
|
for(m=0; m<3;m++)
|
|
for(n=0;n<3;n++)
|
|
for(p=0;p<3;p++)
|
|
if(m+n+p==2)
|
|
goto print;
|
|
print:
|
|
printf("%d %d %d",m,n,p);
|
|
getch();
|
|
}
|

RQ-5.16:What will be the value of x when the following segment is
executed?
int x=10,y=15;
|
Solution:
|
x= (x<y)? (y+x) :
(y-x) ;
|
The value of x after execution is :-25.
|
RQ-5.17:What will be the output when the following segment is
executed?
int x=0;
|
Output:
|
if(x>=0)
|
0
|
if(x>0)
|
Number is positive
|
printf("Number is positive");
|
1
|
else
|
Number is negative
|
printf("Number is negative");
|
RQ-5.18: What will be the output when the following segment is
executed?
Program:
char ch = ‘a’ switch(ch)
{
case ‘a’:
printf(“A”);
case ‘b’:
printf(“B”);
case ‘c’:
printf(“C”);
}

RQ-5.19:What will be the
output of the following segment when executed? Program:
main()
|
Output:
|
{
|
10
|
int x=10,y=20;
|
|
if(( x<y)|| (x+5)>10)
|
|
printf("%d",x);
|
|
else
|
|
printf("%d",y);
|
|
getch();
|
|
}
|
RQ-5.20:What will be the output of the following
segment when executed? Program:
main()
|
Output:
|
|
{
|
10
|
|
int
|
a=10, b=5;
|
|
if(a>b)
|
||
{
|
||
if(b>5)
|
||
printf("%d",b);
|
||
}
|
||
else
|
||
printf("%d",a);
|
||
getch();
|
||
}
|
||
Go ahead by honesty--->Rm
No comments:
Post a Comment