Quiz: Scala
Scala questions from real interviews as a quiz, with an explanation for every option.
Try it: 5 questions
Loading…
Sample questions
Which statements about resolving collisions in hash tables are correct?
- With open addressing, a collision leads to searching for another slot according to a probe sequence.
- Resizing the table guarantees that no more collisions will occur.
- After a hash selects a location, keys are generally compared for equality.
- With chaining, multiple entries that map to the same index are stored in one bucket.
- Equal keys may have different
hashCodevalues if that improves distribution.
Which statement correctly compares how Bitcoin and Ethereum account for funds?
- Bitcoin uses accounts with balances, while Ethereum uses UTXOs.
- Both networks use UTXOs, but Ethereum stores them in smart contracts.
- Bitcoin uses UTXOs, while Ethereum uses accounts with balances and state.
- Both networks store funds only as an address balance and differ only in transaction format.
Which description of
Future[A]in Scala is the most accurate?- It represents a lazy computation of type
Athat starts only whenAwaitis called. - It represents an already computed value of type
A, available without waiting or handling errors. - It represents the result of an asynchronous computation of type
A, which may complete with a value or an error. - It represents a thread of execution that stores state and can be cancelled with
cancel.
- It represents a lazy computation of type
What does a hash collision mean in a hash table?
- Two different keys are considered equal when compared.
- Different keys have the same hash code.
- A key's hash code matches its index in the table.
- Equal keys have different hash codes.
How are transaction fees generally calculated in Bitcoin and Ethereum?
- Bitcoin uses a fee rate per transaction size; Ethereum uses gas consumed and the effective gas price.
- Bitcoin charges a percentage of the transfer amount; Ethereum charges a fixed amount per transaction.
- Bitcoin charges by the number of inputs; Ethereum charges by the amount of ETH sent.
- Bitcoin charges by confirmation time; Ethereum charges by transaction size in bytes.
How does synchronous replication typically differ from asynchronous replication in write acknowledgements and failure outcomes?
- With synchronous replication, a write waits for the required replicas to confirm it; this usually reduces the risk of losing acknowledged data but increases latency.
- Both modes acknowledge writes in the same way; they differ only in how often replicas check the primary.
- With asynchronous replication, a write waits for every replica to confirm it, improving durability at the cost of availability.
- With synchronous replication, the primary acknowledges a write immediately and replicas receive it later, so latency is lower.
What happens when this Scala code is compiled?
class A class B extends A object Example extends App { def replace(values: Array[A]): Unit = { values(0) = new A } val values: Array[B] = Array(new B) replace(values) println(values) }- It compiles, but the write fails with
ArrayStoreException. - It compiles and replaces the array element with an instance of
A. - It does not compile:
Array[B]cannot be passed asArray[A]. - It compiles, and
printlnoutputs the unchanged array.
- It compiles, but the write fails with
Ten threads must each increment a shared counter once. Which approach guarantees that the final value is 10 after all threads have finished?
- Use an
AtomicInteger, start all threads, and immediately readget()without waiting for them to finish. - Use an
AtomicInteger, callincrementAndGet()in each thread, thenjoin()all threads and readget(). - Use
@volatile var counter = 0, runcounter += 1, andjoin()all threads. - Use a regular
var counter = 0, runcounter += 1in each thread, andjoin()all threads.
- Use an
How can
synchronizedbe used in Java and Scala?- Only blocks with an explicit monitor can be synchronized; methods cannot be declared synchronized.
- Only entire methods can be synchronized; individual code sections cannot be protected.
- Java supports methods and arbitrary blocks; Scala uses a block synchronized on an
AnyRefmonitor. - Both methods and blocks can be synchronized, but the monitor is always chosen automatically.
In the context of program correctness, what is an invariant?
- A type that cannot be used with different type parameters
- A condition that must remain true across valid operations
- A value that cannot be changed after an object is created
- A condition checked only when the program starts