Decision Making in JAVA
Highlights
r
t
1,, -~o~~~~ti~;~ ~ ·~ ~-~t~mentwhich ~closes a number of statements within
a ,e~ of:qp~g·_~d cl<?s4lg..~Iy ~r~ce~. _ •
2. A set of statements enclosed within a pair bf braces is also called a block.
ff3,)ji_~~() ~':'~ iS{~, kzi~#,?f;~ch is ~r defined but automatically'
~ ·tiJ_fuvoked.'Yhen·~ co~~a.~~'r•~g-~~p!og r~_i5 ~s~ed.
4. Computer maintaining execution of statements one by one from beginning to the
end is called normal flow of control or simply flow of control.
~i~@o~tio~
~ j :\~ . . Ao;~· th;J~1;~;~~ti~~
~
p; ~ p:n-~~ df progr~g statements.
........,._.,, :.,...., :..:.:::.---- ... ~.:.M:1:_ J..
6. if, if-else and if-else-if are the statements used for bi-directional flow of control.
8. if-else-if is also nested if structure. .
- - - ,+.~,. ~ ... z < ~ ~ - - - - ; - .... ~ - --·· - - ........ - .
9... Switch ~tatetnerit js_,~d {9~ ~~~Y~~~_eq_tiop_of programming blocks.
10. When no case is available for a given switch value, default case is automatically
selected for execution.
n. A.switch c~ wi~ ~~_!hjr" switcll case is_"called-~ested switch case statement.
- ---
12. A variable declared within a block remains alive only upto the end of the existing
block. It is called scope of a variable.
[~_;:::.~ dry run of the progr~ ~here~;-~~bugging is removal of errors from a
14. Syntax error is grammatical error or punctuation mistake in a program. It is also
called execution error.
(iJ[~I mon are the errors in the ppS!am log!c. :, ZJ
rt Answer Type Questions (1 to 2 marks)
t. at is compound statement? Give an example.
. A compound statement is a statement which encloses a· b f
• • bl k f • - num er o statements
Within a oc o operung and closing curly braces.
e.g., if (condition)
{
2. Why is mainO function so special?
24
Ans. mainO function is invoked in the system by default. Hence, as soon as the
command for execution of the program is issued, control directly reaches the
mainO function.
3. What do you mean by the normal flow of control?
Ans. When a command to execute a program is issued, control keeps on moving
sequentially from one statement to other and from beginning to the end in order
to execute them. It is known as normal flow of control.
4. What is conditional flow of control? Show it with the help of an example.
Ans. Control executes a block of program based on certain condition which is called
conditional flow of control.
e.g., if(condition)
Statementl
else
S ement2
ve example, if the condition is true it executes statementl otherwise
exe tes statement2.
5. ifferentiate between if and if-else statements.
. If statement executes a block of statements when a given condition is true. In
case the condition is false it skips the block.
If-else statement executes either of two blocks of statements alternatively
depending upon a given condition is either true or false.
6. What is if-else-if ladder?
Ans. if-else-if ladder is a conditional control structure which executes another if
statement when the given statement is false. It is also called nested if structure.
e.g., if (condition) •
{
}
else
if (conditionl)
{
}
else
{
}
. What is switch case statement? Give an example.
Ans. Switch case statement is used for selective execution of th bl k
to eliminate the use of multiple if statement. e oc s. It can be used
e.g.; switch (control variable)
{
easel:
break;
25
r
case2:
break;
default:
}
at is a control variable in a switch case?
s. A control variable in a switch case is one which guides the control to jump on
a specified case.
e.g.: switch (p)
H , p is the control variable.
• Why is it must to use break after each case?
ns. Break statement terminates the block. If the break is not used after a case then
~ontro l will keep on executing other cases.
ft What is the case in switch case statement?
Ans. When no case is executed for a given value of control variable, default case is
~owed. •
Jl<Wha t do you mean by nested switch case?
Ans. A switch case statement applied within another switch case statement is called
nested switch statement.
e.g. : switch (variable) .
{
easel:
break;
case2: switch (variable)
{
easel:
break;
case2:
break;
}
/}
~ - ~!_Herentiate between testing and debugging.
Ans. Testing is the process of checking program logic manually to ensure whether: it
c tains any error or not.
ebugging is the process of removing errors from a program.
involved in debugging.
3. Me on the steps
Ans. or detection and error correction are the steps involved in debugging.
mean ~y syntax error?
1 . What do you . . ter • t
tax error or execution error is the grammatical mistake like m1ssmg
s. Syn
• sing parenthes
.is etc. mma or,
15 at o you mean by logical error?
. Lo ·cal error is the error due to wrong logic in a program.
• ?
at is run time error.
16_
Ans" rror occurring due to any fault in programming statement .
components do not respond properly. e.g., division by zero or machiner y
i/ 0 .,error etc. ' memory full, device
26
snip pets sho wn below:
17. Give the out put of the following
(a) inta=10; b=2;
if (a>20)
a++;
b++;
Sys tem .ou lpri ntln (a+ ','+b);
(b) intx=5,y=8,z=10;
if(x>y)
if(x>z)
System.oulprintln(x);
else
System.out.println(y);
If(y>z)
System.out.println(y);
else
System.out.println(z);
(c) inta=1,b=2,c=3;
switch(i)
{
easel: a++;
case2: b++;
case3: c++;
}
System.out.println(a+' '+b +' '+c);
if (iii) i =3
(i) i =1 (ii) i =2
(d) if(a<b)
{
switch(b)
{
casel:c=a+b;
break;
case2:c=a-b;
break;
}
else
c=a+b;
System.out.println(c);
}
(ii) a=l0, b=S
if (i) a=l , b=2
(e) switch(i)
{
case1:switch(j)
{
casel:System.out.println(i+j);
case2:System.out.println(i-j);
}
break;
27
case2:System.out.print ln(i• j);
}
if (i) i =1, j=l (ii1 i =2, j=2
Ans. (a) 10, 3 (b) 10
(c) (1) 2 3 4 (ii) 1 3 4 (iii) 1 2 4
(d) (1) -1 (ii) 18
(e) (1) 2, 0 (ii) 4
18. Find the error s in the follo wing prog ram snip
pets:
(a) int a=S,b=3;
if(a< =b)
Syst em.o ut.pr intln (a);
else
System.out.println(c=a+b);
(b) inti= l0
switc h(i)
{
easel:. j=i*2;
brea k;
case2: j =i+2;
brea k;
case3: j=i-2;
brea k;
}
(c) int a=10,b=20;
if (a>S)
if (b>l0 )
c=a+b;
else
c=a-b;
else
c=a*b;
else
c=a/b;
(d) if (i= =5)
else
Syst em.o ulpri ntln( i*S);
1 else
System.out.println(i*j);
(e) switch(i)
{
easel: { case A:
System.out.println(i);
caseB:
System.out.println(j);
}
}
28
(f) int i;
switc h(i)
{
case 'a': Syste m.ou t.pri ntln( (int) i);br eak;
case 'b': System.out.println((char)i);break;
}
print state ment .
Ans. (a) Assi gnm ent state men t cann ot be used with
(b) (z) No, case is exec uted as i =10. (ii) varia ble j is not decla red.
ment .
(c) Num ber of else is less than num ber of if state
(d) (i) Mult iple else for singl e if. (ii) No, if cond ition is true.
(e) (i) Inter nal switc h state men t is not prov ided .
(ii) i & j are unde clare d.
(f) (i) Swit ch varia ble i mus t be char type.
(ii) No, expli cit data conv ersio n of i is requ ired
in prin t state ment .
ruct:
19. Use ternary operator for the follo wing if const
(a) if(s>5000)
p=s*S/100;
else
p=s*t0/100;
(b) if(a>b)
if(a>c)
max=a;
else
max=c;
else
if(b>c)
max=b;
else
max=c;
Ans. (a) p= (s>5000)? s*S/1 00 : s*l0/ 100;
(b) max= (a>b )?(a> c) ? a : c : (b>c) ? b : c;
tor:
20. Use if construct to resol ve the give n ternary opera
(a) c= (val+500>2000) ? 0 : 1;
itive even " : "Neg ative
(b) Strin g c=(a>0) ? (a%2==0) ? "Pos itive odd" : "Pos
number";
Ans. (a) if(val+500>2000)
c=0;
else
c=l;
(b) Strin g c; int a;
if(a>0)
{
if(a%2==0)
c= "Pos itive Odd ";
else
c= "Pos itive Even ";
}
else
c= "Neg ative Num ber";
29
21. Use swit ch case statement for the give n prog
ram snip pet
if(i==100)
c=i"'2
if(i==200)
c=i"'4
if(i==600)
c=i"'l0;
Ans. switc h(i)
{
case 100: c=i*2;
break ;
case 200: c=i*4;
break ;
case 600: c=i*l0;
}
22. Use if-els e-if statement for the given switch
case statement:
switch(x)
{
case 'a':System.out.println("Passed with distinction
");
break;
case 'b': System.out.println("Passed with first class
");
break;
case 'c':System.out.println("Passed with second class
");
break;
case 'd':System.out.println("Passed class");
break;
default:System.out.primtln("Detained");
}
Ans. if(x== 'a')
System.out.println("Passed with distinction");
else
if(x== 'b')
System.out.println("Passed with first class");
else
if(x== 'c')
System.out.println("Passed with second class");
else
if(x== 'd')
System.out.println("Passed class");
else
System.out.println("Detained");
30