Which statement correctly distinguishes a Java class from a Java object?
Using Java Objects, Classes, and Strings Online Quiz Questions
Use this free practice quiz with 20 questions to review Using Java Objects, Classes, and Strings, test your knowledge, and prepare for your next test or exam.
True or false: Declaring String text; creates a new String object.
- A
True
- B
False
What is printed by this code?
StringBuilder a = new StringBuilder("Hi");StringBuilder b = a;b.append("!");System.out.println(a);System.out.println(b);
- A
aprintsHiandbprintsHi!because assignment copies the object. - B
aprintsHi!andbprintsHibecauseappendchanges only the calling variable. - C
Both
aandbprintHi!because they are aliases for the same mutable object. - D
The code does not compile because two variables cannot refer to one
StringBuilder.
What value is produced by "computer".substring(1, 5)?
- A
ompu - B
ompur - C
comp - D
puter
Which pair correctly calls the two operations below?
String text = "hello";
Obtain the number of characters in
text.Find the larger of 12 and 20.
- A
String.length()andMath.max(12, 20) - B
length("hello")and20.max(12) - C
Math.length("hello")andmax.Math(12, 20) - D
text.length()andMath.max(12, 20)
Select all statements that are true after this code runs:
String word = "cat";word.toUpperCase();String upper = word.toUpperCase();
- A
After the code runs,
wordstill refers to the stringcat. - B
Calling
toUpperCase()changes the existingStringobject in place. - C
upperrefers to a string whose characters are uppercase. - D
wordanduppermust refer to the same object because both came fromword.
Select all statements that are true about these Java conversions and methods.
- A
Integer boxed = 7;uses autoboxing. - B
int primitive = boxed;uses unboxing. - C
int n = Integer.valueOf("7");necessarily returns anintdirectly. - D
int n = Integer.parseInt("7");stores a primitiveint.
True or false: For two String variables, the expression a == b is the reliable way to determine whether they contain the same characters.
- A
True
- B
False
True or false: If a class declares a constructor that takes an int, Java automatically supplies a no-parameter constructor as well.
- A
True
- B
False
What integer does "banana".indexOf("na") return? Enter the zero-based index as a decimal number.
Enter the Java String method name used to test whether two strings contain the same characters.
Complete both statements about Java constructors:
A constructor has the same name as its .
A constructor has no , not even void.
Complete both results:
"Java".length() returns ."computer".substring(4) returns .
Determine the exact two lines printed by this code and explain why the two variables have different final values:
String first = "AP";String second = first;second += " CSA";System.out.println(first);System.out.println(second);
Consider the statement Student learner = new Student("Maya", 10);. What does the variable learner contain after the statement executes?
- A
A class definition named learner
- B
A reference to a Student object initialized by a constructor call
- C
A primitive integer storing the value 10
- D
A static method belonging to Student
What value is returned by "computer".substring(3, 7)?
- A
comp - B
pute - C
puter - D
uter
Given String a = new String("Java"); and String b = new String("Java");, which result is correct for a == b and a.equals(b)?
- A
Both expressions evaluate to
true - B
Both expressions evaluate to
false - C
a == bis true, buta.equals(b)is false - D
a == bis false, buta.equals(b)is true
Which statement correctly classifies the method calls in the following code?
String word = "hello";int larger = Math.max(12, 20);int size = word.length();
- A
Math.max(12, 20)is a static call, andword.length()is an instance call - B
Both calls are static because both return values
- C
Both calls are instance calls because both use the dot operator
- D
Math.max(12, 20)is an instance call, andword.length()is a static call
What integer value is returned by Integer.parseInt("2026")?
What exact Java String method name returns the first index where a specified substring occurs, or -1 when the substring is absent?