Libraries MCQ Practice

Question 1 of 10

A math library documents: sqrt(x) returns the nonnegative square root of x for x ≥ 0. Which call matches that documentation to store the square root of 49 in r?

  1. A. r ← sqrt(49)
  2. B. r ← sqrt(49, 49)
  3. C. sqrt(49) ← r
  4. D. r ← sqrt

Question 2 of 10

A text library documents: concat(a, b) returns a single string formed by placing b immediately after a. Which call produces "APCSP"?

  1. A. concat("A", "P", "C", "S", "P")
  2. B. concat("AP", "CSP")
  3. C. concat("CSP", "AP")
  4. D. concat("APCSP")

Question 3 of 10

Why does using a documented library procedure help when building a larger program?

  1. A. Library calls are never allowed to return values.
  2. B. It forces every subproblem to be rewritten from scratch each time.
  3. C. It avoids rewriting a solved subproblem by reusing tested code through its documented interface.
  4. D. Libraries remove the need to read any documentation.

Question 4 of 10

A robotics library documents: moveForward(steps) moves the robot steps units forward and returns the new odometer reading. The robot starts at odometer 0. Which sequence ends with odometer 7?

  1. A. moveForward(2) then moveForward(3)
  2. B. moveForward(0) then moveForward(0)
  3. C. moveForward(7, 0)
  4. D. moveForward(7) only

Question 5 of 10

An API entry reads: max(a, b) returns the larger of a and b. Which call returns 12?

  1. A. max(9, 12)
  2. B. max()
  3. C. max(9, 12, 15)
  4. D. max(12)

Question 6 of 10

A graphics library documents drawRect(x, y, w, h) as drawing a rectangle with top-left (x, y), width w, and height h. It returns nothing. Which call matches the documentation to draw a 10-by-4 rectangle at (2, 3)?

  1. A. drawRect(10, 4)
  2. B. drawRect(2, 3, 10, 4)
  3. C. drawRect(2, 3, 4, 10)
  4. D. drawRect(10, 4, 2, 3)

Question 7 of 10

The table is an excerpt from a string library API.

Procedure Parameters Returns
length(s) s: string number of characters in s
charAt(s, i) s: string; i: 1-based index the character at position i

Which call returns the character "C" from s ← "CSP"?

  1. A. charAt("CSP", 0)
  2. B. length("CSP")
  3. C. charAt("CSP", 1)
  4. D. charAt("CSP", 2)

Question 8 of 10

A student needs the greatest common divisor of two integers. A library already provides gcd(a, b). What is the best next step?

  1. A. Call gcd with no arguments because libraries fill parameters automatically.
  2. B. Copy the library binary into a comment instead of calling it.
  3. C. Ignore the library and invent an untested version with a different name for every program.
  4. D. Call gcd with two integers after reading its documentation.

Question 9 of 10

A time library documents: toMinutes(h, m) returns 60 * h + m. Which expression equals 125 minutes?

  1. A. toMinutes(2, 5)
  2. B. toMinutes(125)
  3. C. toMinutes(2, 25)
  4. D. toMinutes(1, 25)

Question 10 of 10

Documentation for clamp(n, lo, hi) says it returns n limited to the closed interval from lo to hi. Which call returns 10 when n would otherwise be 15?

  1. A. clamp(15)
  2. B. clamp(15, 0, 10)
  3. C. clamp(0, 10)
  4. D. clamp(15, 15, 15)