Application Program Interface (API) and Libraries MCQ Practice

Question 1 of 10

A marina wants to reuse existing Java code written by others instead of inventing every class from scratch. Which term best names a collection of such reusable classes?

  1. A. A compiler warning
  2. B. A class library
  3. C. A primitive type
  4. D. A single assignment statement

Question 2 of 10

What is the main role of an API specification when a student uses those library classes?

  1. A. It tells the programmer how the documented classes may be used.
  2. B. It replaces the need for any variable types.
  3. C. It guarantees access to hidden, undocumented internals.
  4. D. It compiles the program without a compiler.

Question 3 of 10

In Java libraries, related classes are commonly grouped into which organizational unit?

  1. A. Packages
  2. B. Remainder operators
  3. C. Line comments
  4. D. Boolean literals

Question 4 of 10

Which statement about a library class is accurate?

  1. A. A class defines a specific reference type.
  2. B. A class is always a primitive int.
  3. C. A class is the same thing as one output line.
  4. D. A class cannot describe any data.

Question 5 of 10

This fictional LighthouseBeacon API documents only the members below. Which member is stored data rather than a behavior?

Member Kind Contract
rangeKm attribute int distance of the current beam
setRange method sets rangeKm to the given int value
dim method decreases rangeKm by 2, but not below 0
readRange method returns the current rangeKm
  1. A. setRange
  2. B. dim
  3. C. rangeKm
  4. D. readRange

Question 6 of 10

Using only the LighthouseBeacon contract below, a keeper needs the beam 2 km shorter. Which documented behavior should be used?

Member Kind Contract
rangeKm attribute int distance of the current beam
setRange method sets rangeKm to the given int value
dim method decreases rangeKm by 2, but not below 0
readRange method returns the current rangeKm
  1. A. Call dim, because that method is documented to decrease rangeKm by 2.
  2. B. Call a method named shrinkByTwo, because the name sounds likely.
  3. C. Assign to a field named secretRange, which is not listed.
  4. D. Call setRange with no argument, even though setRange requires a value.

Question 7 of 10

This fictional ParcelLocker API documents only the members below. After store is performed on an empty locker, what does isOccupied return?

Member Kind Contract
occupied attribute boolean, true when a parcel is inside
store method sets occupied to true
release method sets occupied to false
isOccupied method returns occupied
  1. A. false
  2. B. true
  3. C. 0
  4. D. The API does not say, so any value is allowed.

Question 8 of 10

A student wants a ParcelLocker method named explode that empties nearby lockers. The API is only the table below. What should the student do?

Member Kind Contract
occupied attribute boolean, true when a parcel is inside
store method sets occupied to true
release method sets occupied to false
isOccupied method returns occupied
  1. A. Treat explode as guaranteed because the class name sounds powerful.
  2. B. Invent explode in client code and assume the library contains it.
  3. C. Use only documented members such as release, or find another documented API.
  4. D. Comment that explode exists so the compiler will accept it.

Question 9 of 10

Which item is most naturally an attribute of a recital hall, rather than a behavior?

  1. A. Holding one additional seat
  2. B. Reporting remaining seats
  3. C. The stored remaining-seat count
  4. D. Clearing the occupancy flag

Question 10 of 10

This RecitalHall API documents only the members below. seats starts at 2. hold is used twice, then remaining is used. What value is returned?

Member Kind Contract
seats attribute int remaining seats
hold method decreases seats by 1 if seats is positive
remaining method returns seats
  1. A. 2
  2. B. 1
  3. C. 0
  4. D. -1