wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

conditional constructs in Java

Total questions: 10

Worksheet time: 6mins

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

c)

Error

d)

Hello World

Error

4.

Re-write above statement using ternary operator-

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;

5.

int marks = 50;

if( marks > 70 )

System.out.println("Distinction");

else if( marks > 35 )

System.out.println("Pass");

else

System.out.println("Fail");

a)

Pass

b)

Distinction

c)

Fail

d)

Pass

Fail

6.

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

7.

"if" is a conditional construct?

a)

True

b)

False

8.

What will be the output of following statement-

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

a)

A==a

b)

65==97

c)

true

d)

false

9.

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

a)

true

b)

false

c)

both of these

d)

None of these

10.

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

a)

must

b)

optional