Practice Interface in CS Thinking
Use these practice problems to test your method after reviewing the concept explanation and worked examples.
Quick Recap
A software interface is the visible contract that tells other parts of a program how to interact with a module, function, or system. It defines the inputs, outputs, and rules of use without exposing the internal implementation.
An interface is like a menu: it tells you what you can ask for without showing the kitchen.
Showing a random 20 of 50 problems.
Example 1
hardA method declares it can throw `IOException`. A subclass override declares it can throw `IOException` or `SQLException`. Why does this violate Liskov substitution?
Example 2
mediumTwo queues, `RingBufferQueue` and `LinkedListQueue`, both implement `Queue` with `enqueue(x)`, `dequeue() -> x`. A caller depends only on `Queue`. What design pattern lets the caller swap implementations?
Example 3
hardA class `Stack<T>` exposes `push`, `pop`, and `getInternalArray()`. Why does the third method undermine the interface?
Example 4
easyFill in the blank: An interface specifies inputs, outputs, and ____ of use.
Example 5
challengeYou publish `Comparator<T>` with one method `compare(a, b) -> int`. A caller's class compares unstably (returns different signs across calls). Whose contract was broken โ the interface's or the caller's?
Example 6
mediumA library function `getUser(id)` used to return `null` for a missing user. A new version throws an error instead. Existing callers checked for `null`. What kind of change is this?
Example 7
mediumYou want to test a function `placeOrder(cart)` that internally calls `paymentGateway`. You don't want real charges in tests. Which property of `paymentGateway`'s interface enables a test double?
Example 8
mediumAn interface returns timestamps as 'a number' without saying seconds vs milliseconds. What kind of interface defect is this?
Example 9
easyA function is declared as `area(width, height) -> number`. What are its inputs?
Example 10
challengeYou must add a new optional behavior to a widely-used function `parse(text)` without breaking any existing caller. Which approach preserves the contract: A) add a required new parameter, B) add an optional parameter with a default, C) change the return type? Justify.
Example 11
medium`calcTax(amount, region)` was added a new required `currency` parameter. Existing callers don't pass it and stop compiling. Which interface principle was violated?
Example 12
easyAn interface specifies inputs, outputs, and rules of use. What does it deliberately HIDE?
Example 13
easyA REST endpoint promises to return JSON with a field `total`. A caller relies on `total`. What do we call this promise?
Example 14
mediumAn interface documents that `withdraw(amount)` throws `InsufficientFunds` when balance is too low. The implementation silently returns 0 instead. Is the interface satisfied?
Example 15
mediumA function's docs say 'idempotent: calling twice has the same effect as calling once.' A team rewrites it so a second call doubles the effect. Is this a breaking change even though the signature didn't change?
Example 16
challengeA graphics library promises `draw(shape)` runs in time. Internally a maintainer makes it where is total scene size. The output is identical. Did this break the interface?
Example 17
mediumYou want to deprecate `fetchOld()` without immediately removing it. What is the standard interface mechanism?
Example 18
easyFor `area(width, height) -> number`, what is the output type?
Example 19
mediumFunction contract: `divide(a, b) -> number`, constraint `b != 0`. A caller passes `b = 0`. Whose responsibility was the violated constraint, and what should the interface have done if it must accept any b?
Example 20
mediumA function `getConfig() -> Map` returns a map. A caller does `getConfig().get("timeout")` and gets nothing back for a missing key. To make the contract clear, what should the interface specify about absent keys?