AP Computer Science A • Unit 1.7

API and Libraries

Detailed answer explanations for the Unit 1.7 multiple-choice practice questions.

Q1D
Q2C
Q3C
Q4B
Q5A
Q6A
Q7D
Q8B
Q9D
Q10A
Q11B
Q12B
Q13C
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following code segment. What is printed as a result of executing the code segment?

String word = "methods";
int n = word.length();
double root = Math.sqrt(16);

System.out.println(n + " " + root);
A8 4.0
Bmethods 16
C7 4
D7 4.0
Correct answer: D. The call word.length() returns 7 and Math.sqrt(16) returns the double value 4.0, so both parts print together.
Explanation

An API entry states what kind of result comes back. int length() returns an int, and the word "methods" has 7 letters. The Math entry returns a double, so the square root keeps a decimal part.

  • A is not correct: the word "methods" has 7 letters rather than 8, and length() returns that count, so the first printed value is 7.
  • B is not correct: the program prints the values stored in n and root, not the original text and the argument that was given to sqrt.
  • C is not correct: Math.sqrt(16) returns a double, so the value stored in root prints as 4.0 rather than as a whole number.
Q2

The following entries come from the Java API. Consider the following code segment. What is printed as a result of executing the code segment?

static double sqrt(double x)
int length()
String word = "package";
double value = Math.sqrt(144);
int size = word.length();
System.out.println(value + " " + size);
A144.0 7
B12 6
C12.0 7
D12.0 package
Correct answer: C. Math.sqrt(144) returns the double value 12.0, and length() returns the int value 7 for "package".
Explanation

Read each entry for the type that comes back. static double sqrt(double x) returns a double, so the result keeps a decimal part. int length() returns an int, and that returned value counts the letters of the String.

  • A is not correct: 144 is the argument given to the method, while the value printed is the result the method returns, which is 12.0.
  • B is not correct: the returned double prints as 12.0 with its decimal part, and "package" has 7 letters rather than 6.
  • D is not correct: size holds the int returned by length(), which is a count of letters rather than the text stored in word.
Q3

Consider the following API method entry. Which of the following best describes the method?

int indexOf(String str)
AIt returns a boolean and takes no parameters.
BIt returns no value and takes a String parameter.
CIt returns an int and takes a String parameter.
DIt returns a String and takes an int parameter.
Correct answer: C. The entry names int as the type that comes back and lists one String parameter inside the parentheses.
Explanation

A method entry is read in the same order each time. The type written before the method name states what comes back, and whatever appears inside the parentheses describes the value the method expects.

  • A is not correct: no boolean appears in the entry, the type written before the name is int, and the parentheses are not empty.
  • B is not correct: an entry that returns no value would not begin with int, because the first type in an entry states the kind of result that comes back.
  • D is not correct: this reverses the two parts of the entry: int is the type returned, and String str describes the value the method expects.
Q4

Which class usually requires an import in a standard Java program?

AMath
BScanner
CString
DSystem
Correct answer: B. Scanner belongs to java.util, which is not made available automatically, so the class requires an import statement.
Explanation

Library classes are organized into packages. Java makes java.lang available automatically. A class from another package can be used by its simple name only after an import statement is written above the class declaration.

  • A is not correct: Math is one of the tools in java.lang, which Java makes available automatically, so no import statement is needed for it.
  • C is not correct: String is a frequently used tool in java.lang, so a program may name it directly without writing any import statement.
  • D is not correct: System is also available automatically, so a program may print output without importing anything for that class.
Q5

Which method call takes no arguments?

Acourse.length()
Bcourse.indexOf("CSA")
CMath.pow(2, 3)
Dcourse.substring(0, 2)
Correct answer: A. The empty parentheses in course.length() show that the call provides no values to the method.
Explanation

An API entry shows the values a method expects between its parentheses. The entry int length() has nothing between them, so a matching call provides nothing and simply returns a result.

  • B is not correct: the value "CSA" is written inside the parentheses, so this call provides one argument rather than leaving them empty.
  • C is not correct: two values, 2 and 3, appear inside the parentheses, so this call supplies arguments to the method.
  • D is not correct: the parentheses contain 0 and 2, so this call provides two values instead of providing none.
Q6

Which statement best describes an API?

AA description of how to use classes, methods, parameters, and return values.
BThe bytecode produced after a Java program is compiled.
CA syntax error reported by the compiler.
DA method that always returns an int.
Correct answer: A. An API is the information that tells a programmer what to call, what values to provide, and what result to expect.
Explanation

A library is a collection of reusable classes and methods, and an API explains how to use them. It gives the class, the method name, the values expected, and the kind of value returned, without revealing the internal algorithm.

  • B is not correct: compiled output is not a description of anything, while an API is written information that explains how to turn an entry into a call.
  • C is not correct: a compiler message reports a problem in a program, while an API describes how code that is already written and tested should be used.
  • D is not correct: an API is a description rather than a method, and the entries it lists return many different kinds of values, not only int.
Q7

Which statement best describes a Java package?

AIt is the bytecode produced when a program is compiled.
BIt is a single method call made on an object.
CIt is a primitive type used to store values.
DIt is a named group of related Java classes.
Correct answer: D. A package is a named group of related classes, and its name identifies where a class belongs.
Explanation

Library classes are organized into packages, which helps a programmer find related tools. java.lang includes frequently used tools such as String and Math, while Scanner is in the java.util package.

  • A is not correct: compiled output is produced from a program's own source code, while a package is a way of organizing classes within the library.
  • B is not correct: a single call, such as word.length(), uses one method, while a package groups many related classes under one name.
  • C is not correct: a package is a grouping of classes rather than a type, so it is not something a program uses to store a value.
Q8

Which of the following is an example of a static method call?

Aword.length()
BMath.sqrt(25)
Cword.equals("AP")
Dname.indexOf("a")
Correct answer: B. Math.sqrt(25) begins with a class name, which is the reading clue for a static method call.
Explanation

The name before the dot shows which kind of call is written. The word static indicates that a method belongs to the class, so such a call begins with the class name. An instance call begins with a variable instead.

  • A is not correct: word is a variable rather than a class name, so this call takes the instance form described by the entry int length().
  • C is not correct: the name before the dot is the variable word, so the method is called through a variable rather than through the class.
  • D is not correct: name is a variable holding text, so this call begins with a variable instead of beginning with a class name.
Q9

In the call Math.pow(2, 3), what are the values 2 and 3?

AClass names
BReturn values
CMethod names
DArguments passed to the method
Correct answer: D. The values written inside the parentheses are the arguments the call provides to the method.
Explanation

An API entry lists the values a method expects, and turning that entry into a call means writing those values inside the parentheses. In Math.sqrt(81), Math is the class name, sqrt is the method name, and 81 is the argument.

  • A is not correct: Math is the class name in this call, and it is written before the dot rather than inside the parentheses.
  • B is not correct: a returned value comes back from the call and can then be stored or printed, while these two values are provided to the method instead.
  • C is not correct: pow is the method name and is written before the parentheses, while the values inside the parentheses are what the call provides.
Q10

Which statement correctly compares a library and an API?

AA library contains reusable code, while an API describes how to use that code.
BA library describes syntax errors, while an API compiles the program.
CA library contains only static methods, while an API contains instance methods.
DA library and an API are both compiled Java class files.
Correct answer: A. A library holds reusable classes and methods, and an API is the information that explains how that code is used.
Explanation

The two terms name different things. The Java standard library is code that has already been written and tested. The API tells a programmer what to call, what values to provide, and what kind of result to expect.

  • B is not correct: neither half fits: a library provides reusable code, and an API is information about that code rather than something that compiles a program.
  • C is not correct: both forms of call appear in the library, since Math.sqrt(81) uses a class name and word.length() uses a variable.
  • D is not correct: an API is written information about how to use classes and methods, so it is not the same kind of thing as compiled code.
Q11

Consider the following method header from the Math class: public static int abs(int x) Which statement is correct?

Adouble value = abs.Math(-9);
Bint value = Math.abs(-9);
CMath value = Math.abs(-9);
Dint value = math.abs(-9);
Correct answer: B. The header public static int abs(int x) names int as the return type, so the call stores an int.
Explanation

Read the header in the same order each time. The word static indicates that the method belongs to the class, so the call begins with the class name Math, followed by abs and the value -9 inside the parentheses.

  • A is not correct: the class name Math belongs before the dot and the method name abs after it, so abs.Math(-9) reverses those two parts.
  • C is not correct: the header names int as the type that comes back, and Math is the class name beginning the call rather than the type of the stored value.
  • D is not correct: the class named in the header is written Math, so math is a different name and the call does not begin with that class.
Q12

A program creates a Scanner object for keyboard input. Which statement must appear at the top of the source file?

Apackage java.util.Scanner;
Bimport java.util.Scanner;
Cimport Scanner();
Dnew java.util.Scanner;
Correct answer: B. Using Scanner by its simple name requires the statement import java.util.Scanner; above the class declaration.
Explanation

Scanner is in the java.util package, which Java does not make available automatically. The import statement names the package and the class, and it is written above the class declaration.

  • A is not correct: a package is a named group of related classes, and naming one in this form is not the statement that makes Scanner usable by its simple name.
  • C is not correct: the statement has to name the package as well as the class, as in java.util.Scanner, and no parentheses belong in it.
  • D is not correct: creating an object is a separate step in the program, while the line written above the class declaration is an import statement.
Q13

The Java API lists the entry int length(). Which call is correct for a String variable word?

Aword.length(1)
Blength(word)
Cword.length()
Dword.length("all")
Correct answer: C. The entry int length() has empty parentheses, so the call is written on the variable and provides no values.
Explanation

An instance method call begins with the variable name, followed by a dot and the method name. The empty parentheses in the entry mean that no values are provided, and the value that comes back is an int.

  • A is not correct: the parentheses in the entry are empty, so passing 1 supplies a value that the method does not expect.
  • B is not correct: the call begins with the String variable followed by a dot, so the variable is not written inside the parentheses instead.
  • D is not correct: the entry lists no parameter at all, so providing the String value "all" does not match the call the entry describes.