Free Practice Quiz Question List

Using Objects, Classes, and Java API Methods Online Quiz Questions

Use this free practice quiz with 30 questions to review Using Objects, Classes, and Java API Methods, test your knowledge, and prepare for your next test or exam.

30 questions
01
Choose one
1 point

Which statement best describes the relationship between a Java class and an object?

  1. A

    A class is an object created during execution, while an object is a method definition.

  2. B

    A class describes a type, while an object is an instance of that type.

  3. C

    A class stores only primitive values, while an object stores only references.

  4. D

    A class must be created with new, while an object cannot be created with new.

02
True or false
1 point

True or false: A Java constructor has the same name as its class and does not declare a return type.

  1. A

    True

  2. B

    False

03
Written response
1 point

What integer value is returned by "computer".indexOf("uter")?

04
Fill in the blank
1 point

A method called with the class name rather than with an object reference is generally a method.

05
Choose one
1 point

What happens when this statement executes?

String name = null;
int count = name.length();

  1. A

    It prints an empty string.

  2. B

    It returns 0 because null has no characters.

  3. C

    It throws a NullPointerException.

  4. D

    It creates a new String object automatically.

06
True or false
1 point

True or false: After String word = "cat"; word.toUpperCase();, the variable word still refers to the string "cat".

  1. A

    True

  2. B

    False

07
Choose all
1 point

Select all statements that are true after this code executes:

StringBuilder first = new StringBuilder("Hi");
StringBuilder second = first;
second.append("!");

  1. A

    first prints Hi!.

  2. B

    first prints Hi.

  3. C

    second prints Hi!.

  4. D

    second prints Hi.

08
Choose one
1 point

Two separately created String objects both contain "Java". Which approach correctly tests whether their contents are equal?

  1. A

    Use == because it always compares string characters.

  2. B

    Use equals because it compares the strings' contents.

  3. C

    Use compareTo only when the strings are references to different objects.

  4. D

    Use length because equal-length strings have equal contents.

09
Choose all
1 point

Select all statements that are correct about Java wrapper classes and autoboxing.

  1. A

    Assigning an int to an Integer can use autoboxing.

  2. B

    Assigning an Integer to an int can use unboxing.

  3. C

    A primitive int variable can contain null.

  4. D

    Unboxing an Integer reference that is null causes a NullPointerException.

10
Open ended
1 point

Consider this code:

String first = "AP";
String second = first;
second += " CSA";

What string does each variable refer to afterward, and why?

11
Choose all
1 point

When using an unfamiliar Java API method, select all information that may be needed to use the class or call the method correctly.

  1. A

    The class name and package

  2. B

    The method's parameter types

  3. C

    The method's return type

  4. D

    The programmer's preferred variable names

  5. E

    Whether the method is static or an instance method

  6. F

    Any documented restrictions or exceptions

12
Choose one
1 point

Given the class below, what happens when Point p = new Point(); is compiled?

public class Point {
private int x;
public Point(int value) { x = value; }
}

  1. A

    It creates a Point with x equal to 0.

  2. B

    It calls Point(int) with an implicit argument of 0.

  3. C

    It causes a compile-time error because no no-parameter constructor is declared.

  4. D

    It creates a Point whose x field is null.

13
Choose one
1 point

Which expression correctly calls the Java API method that returns the larger of 12 and 20?

  1. A

    max.Math(12, 20)

  2. B

    Math.max without parentheses

  3. C

    Math.max(12, 20)

  4. D

    12.max(20)

14
True or false
1 point

Is the following statement true?

For ordinary string-content comparison in Java, equals is the appropriate method, while == compares object references.

  1. A

    True

  2. B

    False

15
Written response
1 point

Enter the single character returned by "Java".charAt(1).

16
Written response
1 point

Enter the Java String method name that returns a new version of a string with its letters converted to uppercase.

17
Written response
1 point

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

18
Fill in the blank
1 point

Complete both statements about Java constructors: A constructor must have and .

19
Choose one
1 point

Given a Rectangle class with constructors Rectangle(), Rectangle(int side), and Rectangle(int width, int height), what does new Rectangle(5) do?

  1. A

    It causes a compile-time error because Rectangle has multiple constructors.

  2. B

    It calls the no-parameter constructor and ignores the argument.

  3. C

    It calls the one-parameter constructor, producing a rectangle with width 5 and height 5.

  4. D

    It calls the two-parameter constructor and uses 5 for both parameters automatically.

20
Choose one
1 point

Which pair correctly calls a static method and an instance method, respectively?

  1. A

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

  2. B

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

  3. C

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

  4. D

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

21
Choose one
1 point

What is printed by the following code?

String word = "computer";
System.out.println(word.substring(4));

  1. A

    comp

  2. B

    puter

  3. C

    computer

  4. D

    uter

22
Choose one
1 point

What is printed by the following 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!.

  2. B

    Both a and b print Hi!.

  3. C

    Both a and b print Hi.

  4. D

    The code causes a NullPointerException because b is an alias.

23
Choose one
1 point

What happens when this statement executes?

Integer value = null;
int number = value;

  1. A

    It assigns 0 to number.

  2. B

    It produces a compile-time error because Integer cannot be assigned to int.

  3. C

    It throws a NullPointerException during unboxing.

  4. D

    It creates a new Integer object containing 0.

24
True or false
1 point

True or false: If String a = new String("Java") and String b = new String("Java"), then a.equals(b) is true even though a == b commonly evaluates to false.

  1. A

    True

  2. B

    False

25
Written response
1 point

What Java method converts the string "123" into the primitive value 123? Enter the exact dotted method name, such as ClassName.methodName.

26
Choose one
1 point

Consider these methods:

public int adjust(int value) { return value * 2; }

public void report(int value) { System.out.println(value); }

Which statement compiles successfully and assigns 12 to result?

  1. A

    int result = report(6);

  2. B

    int result = adjust(6);

  3. C

    adjust(6) = 12;

  4. D

    int result = report;

27
Choose one
1 point

A class contains this method:

public String toString() { return "Ticket #" + number; }

If number is 27, what is printed by System.out.println("Current: " + ticket); when ticket is an instance of the class?

  1. A

    Ticket

  2. B

    Ticket@27

  3. C

    Ticket #27

  4. D

    27

28
True or false
1 point

Integer.MAX_VALUE represents the greatest value that can be stored in a primitive int, and a program can access it through the Integer class name without first creating an Integer object.

  1. A

    True

  2. B

    False

29
Written response
1 point

What exact Java method name should a class override to define the textual representation used when one of its objects is concatenated with a String?

30
Fill in the blank
1 point

In the method declaration public int area(int width, int height), the int immediately before area is the method's .