wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Basics Recap

Total questions: 25

Worksheet time: 37mins

Name
Class
Date
1.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

2.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

3.

Which data type gets returned from length() method in String class?

a)

double

b)

String

c)

int

d)

number

4.

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


What is the correct way to find the length of "txt" string?

a)

int len = length(txt);

b)

float len = txt.length();

c)

int len = txt.length();

d)

double len = length(txt);

5.

Which is correct method to convert String into uppercase

a)

changeUpperCase()

b)

convertUpperCase()

c)

toUpper()

d)

toUpperCase()

6.

Choose correct purpose of indexOf() in String class.

a)

returns the index (the position) of the last occurrence of a specified text in a string

b)

returns the character of the first occurrence of a specified character in a string

c)

returns the index (the position) of the first occurrence of a specified text in a string (excluding whitespace)

d)

returns the index (the position) of the first occurrence of a specified text in a string (including whitespace)

7.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

8.

What is the most appropriate way to check if two Strings are identical?

a)

string1 == string2

b)

string1.equals(string2)

c)

string1.same(string2)

d)

string1.equalsIgnorecase(string2)

9.

What is the correct relation between length and last-index of array?

a)

last-index is always 1 more than the length

b)

last-index is 1 less than the length (but not always)

c)

last-index is always 1 less than the length

d)

last-index is always equal to the length

10.

What will get stored in variable word after the evaluation of the below given code snippet:

String txt,word;

txt="bookmark";

word=txt.substring(txt.indexOf('k')+1);

(a)  

11.

Select any two correct statements-

a)

length returns total number of characters of a string

b)

length() is a method

c)

length is a property

d)

length() counts total number of elements of an array

12.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

13.

String word = "A few good men";

What is word.indexOf(“f”)?

a)

2

b)

0

c)

1

d)

3

14.

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

15.

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.

16.

Evaluate the following Java expression, if x=3, y=5, and z=10:

++z + y - y + z + x++

a)

24

b)

23

c)

20

d)

25

17.

What will the output of the following code segment be?


int a = 1, b = 2;

System.out.println(++a+”,”+b++);

a)

1,2

b)

2,2

c)

2,3

d)

3,3

18.

What would be output by the following program segment?


String n1 = "Samuel";

String n2 = "Samantha";


System.out.print(n1.compareTo(n2);

a)

false

b)

true

c)

a positive number, 20

d)

a negative number, -20

19.
What will be the output of the following for() loop:
String target = "millisecond";
for (int i=0; i<target.length(); i++)
{
if (target.charAt(i) == 'l')
break;
else
System.out.print(target.charAt(i));
}
 
a)
m
b)
mi
c)
millisecond
d)
miisecond
20.

Suppose we have defined

int m = 18;

int n = 4;

What is the value of a in the expression?

int a = m / n + m % n;

a)

6.5

b)

6

c)

6.0

d)

100

e)

none of the above

21.

What is the most appropriate way to check if two Strings are equal?

a)

string1 == string2

b)

string1.equals(string2)

c)

string1.same(string2)

d)

string1.equalsIgnorecase(string2)

22.

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

23.

System.out.println((int)Math.random()* 7) + 1);

4 lines
24.

The following code segment will print (a)  

System.out.println(6 % 9);

25.

double x = (int) (1.0 / 2) is (a)