Free Practice Quiz Question List

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.

20 questions
01
Choose one
1 point

Which statement correctly distinguishes a Java class from a Java object?

  1. A

    A class is an object created at runtime, and an object is its source-code definition.

  2. B

    A class is a blueprint for a type, and an object is an instance of that class.

  3. C

    A class is a reference variable, and an object is the value null.

  4. D

    A class is a method, and an object is the method's return value.

02
True or false
1 point

True or false: Declaring String text; creates a new String object.

  1. A

    True

  2. B

    False

03
Choose one
1 point

What is printed by this code?

StringBuilder a = new StringBuilder("Hi");
StringBuilder b = a;
b.append("!");
System.out.println(a);
System.out.println(b);

  1. A

    a prints Hi and b prints Hi! because assignment copies the object.

  2. B

    a prints Hi! and b prints Hi because append changes only the calling variable.

  3. C

    Both a and b print Hi! because they are aliases for the same mutable object.

  4. D

    The code does not compile because two variables cannot refer to one StringBuilder.

04
Choose one
1 point

What value is produced by "computer".substring(1, 5)?

  1. A

    ompu

  2. B

    ompur

  3. C

    comp

  4. D

    puter

05
Choose one
1 point

Which pair correctly calls the two operations below?

String text = "hello";

  1. Obtain the number of characters in text.

  2. Find the larger of 12 and 20.

  1. A

    String.length() and Math.max(12, 20)

  2. B

    length("hello") and 20.max(12)

  3. C

    Math.length("hello") and max.Math(12, 20)

  4. D

    text.length() and Math.max(12, 20)

06
Choose all
1 point

Select all statements that are true after this code runs:

String word = "cat";
word.toUpperCase();
String upper = word.toUpperCase();

  1. A

    After the code runs, word still refers to the string cat.

  2. B

    Calling toUpperCase() changes the existing String object in place.

  3. C

    upper refers to a string whose characters are uppercase.

  4. D

    word and upper must refer to the same object because both came from word.

07
Choose all
1 point

Select all statements that are true about these Java conversions and methods.

  1. A

    Integer boxed = 7; uses autoboxing.

  2. B

    int primitive = boxed; uses unboxing.

  3. C

    int n = Integer.valueOf("7"); necessarily returns an int directly.

  4. D

    int n = Integer.parseInt("7"); stores a primitive int.

08
True or false
1 point

True or false: For two String variables, the expression a == b is the reliable way to determine whether they contain the same characters.

  1. A

    True

  2. B

    False

09
True or false
1 point

True or false: If a class declares a constructor that takes an int, Java automatically supplies a no-parameter constructor as well.

  1. A

    True

  2. B

    False

10
Written response
1 point

What integer does "banana".indexOf("na") return? Enter the zero-based index as a decimal number.

11
Written response
1 point

Enter the Java String method name used to test whether two strings contain the same characters.

12
Fill in the blank
1 point

Complete both statements about Java constructors:

A constructor has the same name as its .
A constructor has no , not even void.

13
Fill in the blank
1 point

Complete both results:

"Java".length() returns .
"computer".substring(4) returns .

14
Open ended
1 point

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);

15
Choose one
1 point

Consider the statement Student learner = new Student("Maya", 10);. What does the variable learner contain after the statement executes?

  1. A

    A class definition named learner

  2. B

    A reference to a Student object initialized by a constructor call

  3. C

    A primitive integer storing the value 10

  4. D

    A static method belonging to Student

16
Choose one
1 point

What value is returned by "computer".substring(3, 7)?

  1. A

    comp

  2. B

    pute

  3. C

    puter

  4. D

    uter

17
Choose one
1 point

Given String a = new String("Java"); and String b = new String("Java");, which result is correct for a == b and a.equals(b)?

  1. A

    Both expressions evaluate to true

  2. B

    Both expressions evaluate to false

  3. C

    a == b is true, but a.equals(b) is false

  4. D

    a == b is false, but a.equals(b) is true

18
Choose one
1 point

Which statement correctly classifies the method calls in the following code?

String word = "hello";
int larger = Math.max(12, 20);
int size = word.length();

  1. A

    Math.max(12, 20) is a static call, and word.length() is an instance call

  2. B

    Both calls are static because both return values

  3. C

    Both calls are instance calls because both use the dot operator

  4. D

    Math.max(12, 20) is an instance call, and word.length() is a static call

19
Written response
1 point

What integer value is returned by Integer.parseInt("2026")?

20
Written response
1 point

What exact Java String method name returns the first index where a specified substring occurs, or -1 when the substring is absent?