Font size
WorksheetsJava Conditional Constructs
Total questions: 20
Worksheet time: 10mins
public void main()
{
double temp = 56.0;
if (temp <=54.0 && temp>40)
System.out.println(“It is too hot”);
else if ( temp<40 || temp>30)
System.out.println(“It is hot but not too much”);
else
System.out.println(“It is pleasant”);
}
What will be the output?
It is too hot
It is pleasant
It is hot but not too much
runtime error
PREDICT THE OUTPUT:
int x = 20;
int y = 18;
if(x>y)
System.out.println("x is greater than y");
else
{
System.out.println("Error");
System.out.println("Incorrect code");
}
Error
Incorrect code
x is greater than y
Error
Incorrect code
PREDICT THE OUTPUT:
int x = 60;
int y = 90;
if (y%x == 0 && y-x==30)
{
System.out.println("Hello World");
}
else
{
System.out.println("Error")
}
Hello World
Hello World
Error
Error
Hello world
What will be the output of above statement-
5
No output
6
Error
5. 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
In a switch case, when the switch value does not respond to any case then the execution transfers to:
break
default
loop
none
Condition is essentially formed using:
Arithmetic operators
Relational operators
Logical operators
None
Which of the following is a conditional statement?
if
if else
if else if
all of the above
Which of the following statement accomplishes ‘fall through’?
if else
switch
for
nested if
A Java program executes but doesn’t give the desired output. It is due to:
logical error
syntax error
run time error
none
Re-write above statement using ternary operator-
if(X<=Y)
X=Y;
else
Y=X;
if(X<=Y) then X=Y else Y=X;
if(X<=Y) ? X=Y : Y=X;
(X<=Y) ? X=Y : Y=X;
What will be the output of following statement-
System.out.println('A'=='a');
true
false
A==a
A=97
What will be the output of following snippet-
Both are not equal
Both are equal
Syntax error
runtime error
Select correct output-
y is 0
y is 2
y is 1
y is 10
Which of the following statements is true.
for every 'if' there should be an 'else'.
for every 'else' there should be an 'if'.
'if' block can exist without an 'else'
'if' statement can be used inside another 'if' statement
int marks = 80;
if( marks > 70 )
System.out.println("Distinction");
System.out.println("Congratulations");
else if( marks > 35 )
System.out.println("Pass");
Distinction
Congratulations
Pass
Distinction
Error ('else' without 'if')
If there is Only one statement to be executed in if else block, giving braces are
must
optional
The if -else statement executes a block of code if a specified condition is
true
false
both
none
System.exit(0); is a method used to......
terminate the program
terminate the loop
none
Nested ifs can be replaced with which logical operator?
||
&&
!
none
