wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Predefined Classes

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

1. What is a predefined class in Java?

a)

A class created by the user

b)

A class provided by the Java API/library

c)

A class that cannot be imported

d)

A class without any methods

2.

2. Which package is automatically imported into all Java programs?

a)

java.util

c)

java.lang

d)

java.math

3.

What is the correct import statement to use the Scanner class?

a)

import java.lang.Scanner;

b)

import java.util.Scanner;

c)

import java.io.Scanner;

d)

import java.math.Scanner;

4.

Which of the following statements correctly creates a String object?

a)

String name = new String("Ali");

b)

String name = String("Ali");

c)

String("Ali") = new name;

d)

new String = "Ali";

5.

What does a reference variable of an object class type store?

a)

The object's actual value (data)

b)

The object's class definition

c)

The address (reference) of the object in memory

d)

The default value

6.

The Math class belongs to which package?

a)

java.lang

b)

java.util

c)

java.math

d)

java.api

7.

Which method of the String class returns the number of characters in a string?

a)

size()

b)

length()

c)

count()

d)

index()

8.

Which Random class method generates a random integer?

a)

nextNumber()

b)

nextInt()

c)

randomInt()

d)

getInt()

9.

Which method of the String class is used to compare the contents of two String objects?

a)

length()

b)

equals()

c)

indexOf()

d)

charAt()

10.

Which method of the String class is used to get the index of a specified character of a String object?

a)

length()

b)

equals()

c)

indexOf()

d)

charAt()

11.

Which method of the String class is used to get the character at a specified index of a String object?

a)

length()

b)

equals()

c)

indexOf()

d)

charAt()

12.

10. What is the output range for random.nextInt(10);?

a)

1 to 10

b)

0 to 9

c)

1 to 9

d)

0 to 10

13.

Given String word = "Hello";, what is the result of word.substring(1,4)?

a)

Hell

b)

llo

c)

ell

d)

ello

14.

Given String word1 = "I love Java";

System.out.println(word1.substring(word1.indexOf('o'), 6));

What is the output?

a)

love

b)

ove

c)

I love

d)

e Ja

15.

Given String word1 = "OOP";

System.out.println(word1.charAt(3));

What is the output?

a)

Error

b)

P

c)

O

d)

OO