

















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Aimed at Kotlin developers, this practice exam evaluates skills in using the Kotlin programming language for building Android applications. Topics include syntax, functional programming, Android app development, integration with Java, and best practices in Kotlin.
Typology: Exams
1 / 89
This page cannot be seen from the preview
Don't miss anything!


















































































Question 1. Which keyword is used to declare an immutable reference in Kotlin? A) var B) let C) const D) val Answer: D Explanation: val creates a read‑only reference; its value cannot be reassigned after initialization. Question 2. In Kotlin, what is the type of the literal 123L? A) Int B) Long C) Double D) Float Answer: B Explanation: The suffix L denotes a Long literal. Question 3. Which of the following correctly concatenates the variable name into a string using a template expression? A) "Hello, $name!" B) "Hello, ${name}" C) Both A and B D) Neither A nor B Answer: C Explanation: Both $name and ${name} are valid template expressions; the latter is needed for complex expressions.
Question 4. What does the Elvis operator (?:) do in Kotlin? A) Provides a default value when the left side is null B) Performs a safe cast C) Checks type equality D) Executes a block if condition is true Answer: A Explanation: a ?: b returns a if it is non‑null, otherwise returns b. Question 5. Which operator safely accesses a member of a nullable variable? A) !! B) ?. C) :: D) ?! Answer: B Explanation: The safe call operator ?. returns null if the receiver is null, avoiding a NullPointerException. Question 6. How would you declare a nullable string variable named email? A) var email: String = null B) var email: String? = null C) var email: Nullable? after the type makes it nullable; you can then assign null.
B) "medium" C) "high" D) Compile error Answer: B Explanation: 5 falls inside the range 5..9. Question 10. How can you break out of an outer loop labeled outer from an inner loop? A) break outer B) continue outer C) return@outer D) break@outer Answer: D Explanation: break@outer exits the loop marked with the label outer. Question 11. Which of the following defines a function with a default argument value? A) fun greet(name: String = "Guest") {} B) fun greet(name: String?) {} C) fun greet(name: String) = "Hello" D) fun greet(name: String) { default = "Guest" } Answer: A Explanation: The = syntax after a parameter provides a default value. Question 12. How do you declare an extension function isEven() for Int? A) fun Int.isEven() = this % 2 == 0 B) fun isEven(Int) = this % 2 == 0 C) extension Int.isEven() = ...
D) Int.isEven() {} Answer: A Explanation: Extension functions are defined with the receiver type before the function name. Question 13. Which higher‑order function takes another function as a parameter? A) map B) filter C) fold D) All of the above Answer: D Explanation: All listed functions accept lambda arguments, making them higher‑order. Question 14. What is the correct syntax for a trailing lambda when calling list.forEach { println(it) }? A) list.forEach({ println(it) }) B) list.forEach { println(it) } C) Both A and B are valid D) Neither A nor B Answer: C Explanation: Kotlin allows the lambda to be placed outside the parentheses; both forms compile. Question 15. Which keyword must be added to a class to allow it to be subclassed? A) open B) abstract C) final D) sealed
Explanation: Kotlin uses a comma‑separated list after the colon to specify multiple super‑types. Question 19. What automatically generated method does a data class provide for component decomposition? A) component1(), component2(), … B) decompose() C) split() D) None of the above Answer: A Explanation: Data classes generate componentN() functions enabling destructuring declarations. Question 20. Which statement about sealed classes is true? A) They can be instantiated directly. B) Subclasses must be declared in the same file. C) They are always abstract. D) They cannot be used with when expressions. Answer: B Explanation: Sealed class hierarchies are restricted to the same file, ensuring exhaustive when checks. Question 21. How do you declare an enum constant with a custom property code? A) enum class Status(val code: Int) { SUCCESS(0), FAILURE(1) } B) enum class Status { SUCCESS, FAILURE } C) enum class Status { SUCCESS = 0, FAILURE = 1 } D) enum class Status { SUCCESS(0), FAILURE(1) } Answer: A
Explanation: Enum classes can have a primary constructor; each constant supplies arguments. Question 22. Which construct creates a thread‑safe singleton in Kotlin? A) object Database {} B) class Database private constructor() C) companion object Database D) val Database = object {} Answer: A Explanation: object declarations are initialized lazily and are thread‑safe by default. Question 23. How would you define a type alias for a function type (Int, String) - > Boolean? A) typealias Predicate = (Int, String) - > Boolean B) typealias Predicate = Function2<Int, String, Boolean> C) Both A and B are valid D) Neither A nor B Answer: C Explanation: Kotlin allows both the lambda syntax and the FunctionN class for type aliases. Question 24. What is the main benefit of marking a function inline? A) It forces the function to run on the UI thread. B) It reduces the overhead of lambda allocations. C) It makes the function asynchronous. D) It disables null‑safety checks. Answer: B Explanation: Inlining substitutes the function body at call sites, eliminating the creation of wrapper objects for lambdas.
Explanation: lazy {} creates a Lazy<T> instance with SYNCHRONIZED mode unless a different mode is specified. Question 28. Which collection type in Kotlin is immutable by default? A) MutableList B) List C) ArrayList D) HashMap Answer: B Explanation: List is a read‑only interface; its concrete implementation can still be mutable, but the reference does not allow modification. Question 29. What does the map extension function do on a collection? A) Filters elements based on a predicate B) Transforms each element to a new value C) Reduces the collection to a single value D) Groups elements by a key Answer: B Explanation: map applies the given lambda to each element and returns a list of the results. Question 30. Which function aggregates elements using an initial value and a binary operation? A) fold B) reduce C) groupBy D) associate Answer: A
Explanation: fold takes an initial accumulator and applies the operation sequentially. Question 31. How does flatMap differ from map? A) It returns a flattened list of collections produced by the lambda. B) It filters out null values. C) It sorts the collection. D) It groups elements by a key. Answer: A Explanation: flatMap maps each element to an Iterable and then concatenates all results into a single list. Question 32. What does the zip function produce when applied to two lists of equal size? A) A list of pairs combining corresponding elements B) A map from elements of the first list to the second C) A concatenated list D) A set of unique elements Answer: A Explanation: zip pairs the i‑th element of each collection into a Pair. Question 33. When should you prefer asSequence() over direct collection operations? A) When the collection is very small B) When you need eager evaluation C) When chaining many intermediate operations on a large dataset D) When you want to modify the original collection in place Answer: C Explanation: Sequences evaluate lazily, avoiding creation of intermediate collections and improving performance for large pipelines.
Explanation: The use function implements Closeable handling and closes the resource when the block finishes. Question 37. What is the main difference between a coroutine and a thread? A) Coroutines are managed by the OS. B) Coroutines are lightweight and scheduled by the runtime. C) Threads cannot run concurrently. D) Coroutines require a separate JVM. Answer: B Explanation: Coroutines are lightweight, non‑blocking, and dispatched by Kotlin’s runtime, unlike heavyweight OS threads. Question 38. Which dispatcher is optimized for CPU‑intensive work? A) Dispatchers.IO B) Dispatchers.Default C) Dispatchers.Main D) Dispatchers.Unconfined Answer: B Explanation: Dispatchers.Default uses a shared pool sized to the number of CPU cores, suitable for computation. Question 39. How do you launch a coroutine that returns a result without blocking the caller? A) launch {} B) async {} C) runBlocking {} D) coroutineScope {}
Answer: B Explanation: async starts a coroutine and returns a Deferred<T> from which the result can be obtained via await(). Question 40. Which keyword marks a function that can be suspended? A) suspend B) async C) yield D) coroutine Answer: A Explanation: The suspend modifier allows the function to be paused and resumed without blocking a thread. Question 41. In structured concurrency, which builder ensures that all child coroutines complete before the scope ends? A) launch B) async C) coroutineScope D) GlobalScope Answer: C Explanation: coroutineScope creates a scope that waits for all its child coroutines to finish before returning. Question 42. How can a coroutine be cancelled cooperatively? A) Call Thread.interrupt() B) Throw CancellationException inside the coroutine C) Use isActive or ensureActive() checks within the coroutine body
Answer: A Explanation: map applies a transformation to each element emitted by the upstream flow. Question 46. What is a platform type in Kotlin when interoperating with Java? A) A type that can be treated as nullable or non‑null without compiler warnings B) A type that is always nullable C) A type that is always non‑null D) A type that cannot be used in Kotlin code Answer: A Explanation: Platform types arise from Java declarations without nullability annotations; Kotlin lets you decide nullability. Question 47. How would you expose a Kotlin function to Java as a static method? A) Annotate with @JvmStatic inside a companion object B) Declare it as static in Kotlin C) Use @JvmOverloads only D) Kotlin functions are never static in Java Answer: A Explanation: @JvmStatic on a companion object member makes it a true static method in the generated Java class. Question 48. Which annotation generates overloads for functions with default parameters for Java callers? A) @JvmStatic B) @JvmOverloads C) @JvmName D) @JvmField
Answer: B Explanation: @JvmOverloads creates multiple Java‑compatible overloads covering omitted default arguments. Question 49. How does Kotlin handle SAM conversion for Java interfaces? A) Kotlin cannot use SAM conversion. B) Lambdas can be passed directly where a Java SAM interface is expected. C) You must create an anonymous object implementing the interface. D) Only Kotlin interfaces support SAM conversion. Answer: B Explanation: Kotlin automatically converts a lambda to an instance of a Java Single Abstract Method interface. Question 50. Which Kotlin feature allows you to obtain a KClass reference of a class at runtime? A) ::class B) ClassName::class.java C) javaClass D) All of the above Answer: D Explanation: ::class yields a KClass; appending .java gives the Java Class; javaClass on an instance also works. Question 51. What is the purpose of the expect keyword in Kotlin Multiplatform projects? A) To declare a function that must be overridden in each platform module B) To create a coroutine that runs on any platform C) To mark a class as experimental
D) @Serializable Answer: C Explanation: The kotlin-noarg compiler plugin generates a zero‑argument constructor for classes annotated with @NoArg. Question 55. What does the copy method of a data class do? A) Creates a shallow copy with optionally overridden properties B) Performs a deep copy of all nested objects C) Returns the same instance D) Only works for immutable classes Answer: A Explanation: copy creates a new instance copying all fields, allowing selective property changes. Question 56. Which standard library function can be used to observe changes to a mutable property? A) observable delegate B) lazy delegate C) by delegation D) watch function Answer: A Explanation: observable from Delegates lets you provide a handler that runs on each property change. Question 57. How can you create a read‑only view of a mutable list mutableList? A) mutableList.toList() B) mutableList.asReadOnly()
C) mutableList.unmodifiable() D) mutableList is already read‑only Answer: A Explanation: toList() returns a new immutable List containing the same elements. Question 58. Which collection operation returns a Map grouping elements by a key selector? A) groupBy B) associateBy C) partition D) zip Answer: A Explanation: groupBy creates a map where each key maps to a list of elements sharing that key. Question 59. What does the foldIndexed function provide that fold does not? A) Access to the element’s index during accumulation B) Parallel execution C) Early termination capability D) Automatic type inference of the accumulator Answer: A Explanation: foldIndexed passes the current index to the operation lambda. Question 60. Which of the following creates a sequence that lazily reads lines from a file? A) File("data.txt").useLines { it } B) File("data.txt").readLines().asSequence() C) File("data.txt").bufferedReader().lineSequence()