NEW
Font size
WorksheetsJava Predefined Classes
Total questions: 15
Worksheet time: 8mins
1. What is a predefined class in Java?
A class created by the user
A class provided by the Java API/library
A class that cannot be imported
A class without any methods
2. Which package is automatically imported into all Java programs?
What is the correct import statement to use the Scanner class?
import java.lang.Scanner;
import java.util.Scanner;
import java.io.Scanner;
import java.math.Scanner;
Which of the following statements correctly creates a String object?
String name = new String("Ali");
String name = String("Ali");
String("Ali") = new name;
new String = "Ali";
What does a reference variable of an object class type store?
The object's actual value (data)
The object's class definition
The address (reference) of the object in memory
The default value
The Math class belongs to which package?
java.lang
java.util
java.math
java.api
Which method of the String class returns the number of characters in a string?
size()
length()
count()
index()
Which Random class method generates a random integer?
nextNumber()
nextInt()
randomInt()
getInt()
Which method of the String class is used to compare the contents of two String objects?
length()
equals()
indexOf()
charAt()
Which method of the String class is used to get the index of a specified character of a String object?
length()
equals()
indexOf()
charAt()
Which method of the String class is used to get the character at a specified index of a String object?
length()
equals()
indexOf()
charAt()
10. What is the output range for random.nextInt(10);?
1 to 10
0 to 9
1 to 9
0 to 10
Given String word = "Hello";, what is the result of word.substring(1,4)?
Hell
llo
ell
ello
Given String word1 = "I love Java";
System.out.println(word1.substring(word1.indexOf('o'), 6));
What is the output?
love
ove
I love
e Ja
Given String word1 = "OOP";
System.out.println(word1.charAt(3));
What is the output?
Error
P
O
OO
