wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Conditional Constructs

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

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?

a)

It is too hot

b)

It is pleasant

c)

It is hot but not too much

d)

runtime error

2.

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");

}

a)

Error

b)

Incorrect code

c)

x is greater than y

d)

Error

Incorrect code

3.

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

}

a)

Hello World

b)

Hello World

Error

c)

Error

d)

Hello world

4.

What will be the output of above statement-

a)

5

b)

No output

c)

6

d)

Error

5.

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

a)

Equilateral triangle

b)

Scalene triangle

c)

Isosceles triangle

d)

All of the above

6.

In a switch case, when the switch value does not respond to any case then the execution transfers to:

a)

break

b)

default

c)

loop

d)

none

7.

Condition is essentially formed using:

a)

Arithmetic operators

b)

Relational operators

c)

Logical operators

d)

None

8.

Which of the following is a conditional statement?

a)

if

b)

if else

c)

if else if

d)

all of the above

9.

Which of the following statement accomplishes ‘fall through’?

a)

if else

b)

switch

c)

for

d)

nested if

10.

A Java program executes but doesn’t give the desired output. It is due to:

a)

logical error

b)

syntax error

c)

run time error

d)

none

11.

Re-write above statement using ternary operator-

if(X<=Y)

X=Y;

else

Y=X;

a)

if(X<=Y) then X=Y else Y=X;

b)

if(X<=Y) ? X=Y : Y=X;

c)

(X<=Y) ? X=Y : Y=X;

12.

What will be the output of following statement-

System.out.println('A'=='a');

a)

true

b)

false

c)

A==a

d)

A=97

13.

What will be the output of following snippet-

a)

Both are not equal

b)

Both are equal

c)

Syntax error

d)

runtime error

14.

Select correct output-

a)

y is 0

b)

y is 2

c)

y is 1

d)

y is 10

15.

Which of the following statements is true.

a)

for every 'if' there should be an 'else'.

b)

for every 'else' there should be an 'if'.

c)

'if' block can exist without an 'else'

d)

'if' statement can be used inside another 'if' statement

16.

int marks = 80;

if( marks > 70 )

System.out.println("Distinction");

System.out.println("Congratulations");

else if( marks > 35 )

System.out.println("Pass");

a)

Distinction

Congratulations

b)

Pass

c)

Distinction

d)

Error ('else' without 'if')

17.

If there is Only one statement to be executed in if else block, giving braces are

a)

must

b)

optional

18.

The if -else statement executes a block of code if a specified condition is

a)

true

b)

false

c)

both

d)

none

19.

System.exit(0); is a method used to......

a)

terminate the program

b)

terminate the loop

c)

none

20.

Nested ifs can be replaced with which logical operator?

a)

||

b)

&&

c)

!

d)

none