Which statement best describes the relationship between a Java class and an object?
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.
True or false: A Java constructor has the same name as its class and does not declare a return type.
- A
True
- B
False
What integer value is returned by "computer".indexOf("uter")?
A method called with the class name rather than with an object reference is generally a method.
What happens when this statement executes?
String name = null;int count = name.length();
- A
It prints an empty string.
- B
It returns 0 because null has no characters.
- C
It throws a NullPointerException.
- D
It creates a new String object automatically.
True or false: After String word = "cat"; word.toUpperCase();, the variable word still refers to the string "cat".
- A
True
- B
False
Select all statements that are true after this code executes:
StringBuilder first = new StringBuilder("Hi");StringBuilder second = first;second.append("!");
- A
firstprintsHi!. - B
firstprintsHi. - C
secondprintsHi!. - D
secondprintsHi.
Two separately created String objects both contain "Java". Which approach correctly tests whether their contents are equal?
- A
Use
==because it always compares string characters. - B
Use
equalsbecause it compares the strings' contents. - C
Use
compareToonly when the strings are references to different objects. - D
Use
lengthbecause equal-length strings have equal contents.
Select all statements that are correct about Java wrapper classes and autoboxing.
- A
Assigning an
intto anIntegercan use autoboxing. - B
Assigning an
Integerto anintcan use unboxing. - C
A primitive
intvariable can containnull. - D
Unboxing an
Integerreference that isnullcauses a NullPointerException.
Consider this code:
String first = "AP";String second = first;second += " CSA";
What string does each variable refer to afterward, and why?
When using an unfamiliar Java API method, select all information that may be needed to use the class or call the method correctly.
- A
The class name and package
- B
The method's parameter types
- C
The method's return type
- D
The programmer's preferred variable names
- E
Whether the method is static or an instance method
- F
Any documented restrictions or exceptions
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; }}
- A
It creates a Point with
xequal to 0. - B
It calls
Point(int)with an implicit argument of 0. - C
It causes a compile-time error because no no-parameter constructor is declared.
- D
It creates a Point whose
xfield is null.
Which expression correctly calls the Java API method that returns the larger of 12 and 20?
- A
max.Math(12, 20) - B
Math.maxwithout parentheses - C
Math.max(12, 20) - D
12.max(20)
Is the following statement true?
For ordinary string-content comparison in Java, equals is the appropriate method, while == compares object references.
- A
True
- B
False
Enter the single character returned by "Java".charAt(1).
Enter the Java String method name that returns a new version of a string with its letters converted to uppercase.
What integer value is returned by Integer.parseInt("123")?
Complete both statements about Java constructors: A constructor must have and .
Given a Rectangle class with constructors Rectangle(), Rectangle(int side), and Rectangle(int width, int height), what does new Rectangle(5) do?
- A
It causes a compile-time error because Rectangle has multiple constructors.
- B
It calls the no-parameter constructor and ignores the argument.
- C
It calls the one-parameter constructor, producing a rectangle with width 5 and height 5.
- D
It calls the two-parameter constructor and uses 5 for both parameters automatically.
Which pair correctly calls a static method and an instance method, respectively?
- A
Math.max(12, 20)andword.length() - B
word.max(12, 20)andString.length() - C
Math.max.length(12, 20)andword.String.length() - D
max.Math(12, 20)andlength.word()
What is printed by the following code?
String word = "computer";System.out.println(word.substring(4));
- A
comp - B
puter - C
computer - D
uter
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);
- A
aprintsHiandbprintsHi!. - B
Both
aandbprintHi!. - C
Both
aandbprintHi. - D
The code causes a NullPointerException because
bis an alias.
What happens when this statement executes?
Integer value = null;int number = value;
- A
It assigns 0 to
number. - B
It produces a compile-time error because Integer cannot be assigned to int.
- C
It throws a NullPointerException during unboxing.
- D
It creates a new Integer object containing 0.
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.
- A
True
- B
False
What Java method converts the string "123" into the primitive value 123? Enter the exact dotted method name, such as ClassName.methodName.
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?
- A
int result = report(6); - B
int result = adjust(6); - C
adjust(6) = 12; - D
int result = report;
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?
- A
Ticket - B
Ticket@27 - C
Ticket #27 - D
27
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.
- A
True
- B
False
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?
In the method declaration public int area(int width, int height), the int immediately before area is the method's .