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

hard
A method declares it can throw `IOException`. A subclass override declares it can throw `IOException` or `SQLException`. Why does this violate Liskov substitution?

Example 2

medium
Two 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

hard
A class `Stack<T>` exposes `push`, `pop`, and `getInternalArray()`. Why does the third method undermine the interface?

Example 4

easy
Fill in the blank: An interface specifies inputs, outputs, and ____ of use.

Example 5

challenge
You 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

medium
A 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

medium
You 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

medium
An interface returns timestamps as 'a number' without saying seconds vs milliseconds. What kind of interface defect is this?

Example 9

easy
A function is declared as `area(width, height) -> number`. What are its inputs?

Example 10

challenge
You 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

easy
An interface specifies inputs, outputs, and rules of use. What does it deliberately HIDE?

Example 13

easy
A REST endpoint promises to return JSON with a field `total`. A caller relies on `total`. What do we call this promise?

Example 14

medium
An interface documents that `withdraw(amount)` throws `InsufficientFunds` when balance is too low. The implementation silently returns 0 instead. Is the interface satisfied?

Example 15

medium
A 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

challenge
A graphics library promises `draw(shape)` runs in O(1)O(1) time. Internally a maintainer makes it O(n)O(n) where nn is total scene size. The output is identical. Did this break the interface?

Example 17

medium
You want to deprecate `fetchOld()` without immediately removing it. What is the standard interface mechanism?

Example 18

easy
For `area(width, height) -> number`, what is the output type?

Example 19

medium
Function 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

medium
A 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?