WORKSHEET
Ravi gets a copy of code snippets. Each snippet has options to select from. Help Ravi select the
correct result of each snippet.
(a) if(a > b)
C= a;
else
C= b;
Can also be written as
i) c= (b > a) ? a: b;
ii) c = (a != b)? a: b;
iii) c = (a > b)? a: b;
Iv) C = (b < a)? a: b;
(b) if((a>b)&&(b>c)&&(c>d)) implies
i) a is the smallest number
b is the smallest number
lii) d is the greatest number.
iv) d is the smallest number.
Answers
Answer:
In a switch case, when the switch value does not respond to any case then the execution transfers to:
a break statement
a default case ✓
a loop
none
Question 2
Which of the following is a compound statement?
p=Integer.parseInt(in.readLine());
c=++a;
if(a>b) a++; b- - ; ✓
a=4;
Question 3
Condition is essentially formed by using:
Arithmetic operators
Relational operators
Logical operators
All ✓
Question 4
If(a>b)&&(a>c)), then which of the statement is true?
b is the smallest number
b is the greatest number
a is the greatest number ✓
all of the above
Question 5
if(a>b)
c=a;
else
c=b;
It can be written as:
c= (b>a)?a:b;
c= (a!=b)?a:b;
c= (a>b)?b:a;
None ✓
Question 6
If a, b and c are the sides of a triangle then which of the following statement is true for: if(a!=b && a!=c && b!=c)?
Equilateral triangle
Scalene triangle ✓
Isosceles triangle
All of the above
Question 7
Two arithmetic expressions can be compared with if statement, using:
Arithmetic operator
Relational operator ✓
Ternary operator
None
Question 8
Which of the following is a conditional statement?
if ✓
goto
for
none
Question 9
Which of the following statements accomplishes 'fall through'?
for statement
switch statement ✓
if-else
none
Question 10
A Java program executes but doesn't give the desired output. It is due to:
the logical error in the program ✓
the syntax error in the program
the run time error in the program
none
Answer the Following Questions
Question 1
Name the different ways to manage the flow of control in a program.
Answer
Normal flow of control, Bi-directional flow of control, Multiple branching of control
Question 2
Mention one statement each to achieve:
(a) Bi-directional flow of control
Answer
if-else statement
(b) Multiple branching of control
Answer
switch statement
Question 3
Explain the following statements with their constructs:
(a) nested if
Answer
We can write an if-else statement within another if-else statement. We call this nested if. It has the following syntax:
if (condition 1) {
if (condition 2) {
Statement a;
Statement b;
..
}
else {
Statement c;
Statement d;
..
}
}
else {
if (condition 3) {
Statement e;
Statement f;
..
}
else {
Statement g;
Statement h;
..
}
}
(b) if - else
Answer
if - else statement is used to execute one set of statements when the condition is true and another set of statements when the condition is false. It has the following syntax:
if (condition 1) {
Statement a;
Statement b;
..
}
else {
Statement c;
Statement d;
..
}
(c) if - else - if
Answer
if - else - if ladder construct is used to test multiple conditions and then take a decision. It provides multiple branching of control. It has the following syntax:
if (condition)
statement;
else if (condition)
statement;
else if (condition)
statement;
..
..
else
statement;
Explanation:
pls follow me
pls mark me as BRANLIST