wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Strings and lops

Total questions: 10

Worksheet time: 4mins

Name
Class
Date
1.

What is the output of the following?

int x = 9;

if (x>9)

System.out.println("Hello");

else

System.out.println("World");

a)

Hello

b)

World

c)

Syntax Error

d)

Compilation Error

2.

What is the output of the following:

for(int x=1 ; x<5 ; x++){

System.out.print(x);

}

a)

1234

b)

01234

c)

012345

d)

12345

3.

What is the output of the following:

String myWord = "FUN";

String anotherWord = myWord.toLowerCase();

System.out.println(myWord);

a)

fun

b)

Fun

c)

FUN

d)

"fun"

4.

What is the output of the following:

int y = 5;

do{

System.out.println("This is not the output!");

y++;

}while(y>5);

a)

6

b)

Compilation error

c)

This is not the output!

(printed 5 times)

d)

This is not the output!

5.

int x = 25;

if(5*5)

System.out.println("The answer is 25!");

else

Sytem.out.println("The answer is zero");

a)

true

b)

The answer is 25!

c)

The answer is zero

d)

Compilation Error

6.

What is the output of the following?

boolean yes=true, no=false;

if (!yes||no)

System.out.println("maybe");

else

System.out.println("yes!");

a)

no

b)

maybe

c)

Compilation Error

d)

yes!

7.

What is the output of the following?

String a = "I will pass the exams.";

String b = " I will pass the exams. ";

if (a.equals(b))

System.out.println("fail");

else

System.out.println(b.substring(9,12));

a)

pass

b)

fail

c)

Compilation Error

d)

I will pass the exams.

8.

What is the output of the following?

String a = "Are you ready kids?";

String b = "Aye! Aye! Captain!";

System.out.println(a.concat(b));

a)

Are you ready kids? Aye! Aye! Captain!

b)

Compilation Error

c)

Are you ready kids?Aye! Aye! Captain!

d)

Aye! Aye! Captain!

9.

What is the output of the following code?

String a = "19";

String b = "75";

System.out.println(a+" "+b);

a)

19 75

b)

94

c)

Compilation Error

d)

1975

10.

What method of class String is used to remove white spaces at the beginning and end of String objects?

a)

remove()

b)

trim()

c)

concat()

d)

Substring()