[go: up one dir, main page]

0% found this document useful (0 votes)
14 views6 pages

Le-8 conditional constructs practice questions

Kakjdjndnd SS

Uploaded by

samrudhmakam6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Le-8 conditional constructs practice questions

Kakjdjndnd SS

Uploaded by

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

Lesson 8-Conditional constructs in java

I Multiple Choice Questions

Question 1

Which operator cannot be used with if-else statement?

<=

||

&&

?:✓

Question 2

Predict the output of the following code snippet:

int a = 1;

int b = 2;

if (a == b)

System.out.println ("Both values are equal");

else

System.out.println ("Values are not equal");

Both values are equal

Incorrect use of the == operator

Values are not equal ✓

No output

Q4) if ((a > b) && (a > c)), then which of the following statements is true?

a is the largest number. ✓

b is the largest number.

c is the largest number.


b is the smallest number.

Q5)A sequence of statements enclosed between a pair of curly brackets is called

a compound statement ✓

an empty statement

a null statement

a void statement

Q6) Format the following if statements with indentation:

i. if (x == y) if (x == z) x = 1; else y = 1; else z = 1;

Answer

if (x == y)

if (x == z)

x = 1;

else

y = 1;

else

z = 1;

ii. if (x == y) {if (y == z) x = 1; y = 2; } else z = 1;

Answer

if (x == y)

{
if (y == z)

x = 1;

y = 2;

else

z = 1;

Q7) if (num1 != num2) {

if (num2 >= num3) x = 1; y = 2; }

else {x = 1; if (num1 == num2) z = 3;}

Answer

if (num1 != num2)

if (num2 >= num3)

x = 1;

y = 2;

else

x = 1;

if (num1 == num2)

z = 3;

}
Q 8)

Write an if statement to find the smallest of the three given integers using the min() method of the
Math class.

Answer

if (a < Math.min(b, c))

System.out.println(a);

else

System.out.println(Math.min(b, c));

Q9) 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

Q10) Which of the following is a conditional statement?

a)if b) goto c) for d) none

Q11) Explain the following statements with their constructs:

(a) nested if
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
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
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;

You might also like