Kotlin Professional Certificate study materials
Kotlin Professional Certificate study resources

Kotlin Professional Certificate practice questions

One question at a time. Build your Kotlin Professional Certificate understanding.

First 50 of 200 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.

Continue in the practice tool above to load more questions, 50 at a time.

Question 1Kotlin language fundamentals and coding tasks

In Kotlin, which declaration keyword creates a read-only local variable whose value cannot be reassigned after initialization?

  1. var
  2. val Correct answer.
  3. const
  4. let

Explanation

The val keyword declares a read-only reference: it must be initialized once and cannot be reassigned afterward, though the referenced object itself may still be mutable. The var keyword, by contrast, allows reassignment at any time.

Question 2Kotlin language fundamentals and coding tasks

What is the result of evaluating the expression "10 / 4" where both operands are Int in Kotlin?

  1. A compile-time type error
  2. 2.5
  3. 3
  4. 2 Correct answer.

Explanation

When both operands of the division operator are Int, Kotlin performs integer division and discards the fractional part, so 10 / 4 evaluates to 2. To obtain 2.5 you must convert at least one operand to a floating-point type, for example 10 / 4.0.

Question 3Kotlin language fundamentals and coding tasks

Which Kotlin string template syntax correctly embeds the value of a variable named count inside a string literal?

  1. "Total: %count%"
  2. "Total: (count)"
  3. "Total: ${count}" Correct answer.
  4. "Total: #{count}"

Explanation

Kotlin string templates use a dollar sign: $name for a simple identifier or ${expression} for arbitrary expressions. The braced form is required when the embedded value is followed by characters that would otherwise be parsed as part of the identifier.

Question 4Kotlin language fundamentals and coding tasks

Given "val name: String? = null", which expression safely produces the length of the string or the value 0 when it is null?

  1. name!!.length
  2. name.length
  3. name.length ?: 0
  4. name?.length ?: 0 Correct answer.

Explanation

The safe-call operator ?. returns null instead of throwing when the receiver is null, and the Elvis operator ?: supplies a fallback for that null result. Together they yield the length or 0 without any risk of a NullPointerException.

Question 5Kotlin language fundamentals and coding tasks

Which function creates a read-only list containing exactly the elements 1, 2, and 3 in Kotlin?

  1. arrayOf(1, 2, 3)
  2. listOf(1, 2, 3) Correct answer.
  3. mutableListOf(1, 2, 3)
  4. setOf(1, 2, 3)

Explanation

listOf returns a read-only List with the given elements in order, exposing no mutation methods. mutableListOf returns a MutableList, arrayOf returns an Array, and setOf returns a Set with no duplicate elements and no guaranteed index order.

Question 6Kotlin language fundamentals and coding tasks

What does the data modifier on a class automatically generate?

  1. Getter and setter methods for every constructor parameter
  2. An implementation of the Serializable interface
  3. equals, hashCode, toString, and componentN/copy functions Correct answer.
  4. A no-argument secondary constructor

Explanation

A data class derives equals, hashCode, toString, copy, and componentN functions from the properties declared in its primary constructor. This removes boilerplate while keeping value-based semantics for those properties.

Question 7Kotlin language fundamentals and coding tasks

Which expression correctly checks whether the variable obj, declared as Any, holds a String and, if so, uses it as a String in the same expression?

  1. obj as String && obj.length > 0
  2. obj is String ? obj.length : 0
  3. obj is String && obj.length > 0 Correct answer.
  4. obj instanceof String && obj.length > 0

Explanation

The is operator performs a type check, and the compiler smart-casts obj to String inside the right-hand side of the && because the check succeeded. This makes obj.length valid without an explicit cast.

Question 8Kotlin language fundamentals and coding tasks

Which statement about Kotlin's when expression is correct?

  1. It can be used as an expression that returns a value when every branch is exhaustive. Correct answer.
  2. It only accepts constant literal values in its branches.
  3. It falls through to the next branch unless you write break.
  4. It requires a subject argument and cannot be used without one.

Explanation

when can be used either as a statement or as an expression; in expression position the compiler requires the branches to be exhaustive so a value is always produced. Branches may contain arbitrary expressions, ranges, and type checks.

Question 9Kotlin language fundamentals and coding tasks

What is printed by the following code? fun main() { val numbers = listOf(1, 2, 3, 4) val result = numbers.filter { it % 2 == 0 }.map { it * 10 } println(result) }

  1. [1, 3]
  2. [20, 40] Correct answer.
  3. [10, 20, 30, 40]
  4. [2, 4]

Explanation

filter keeps only the even elements 2 and 4, and map then transforms each remaining element by multiplying it by 10, giving [20, 40]. The operations are applied in the order written, so filtering happens before mapping.

Question 10Kotlin language fundamentals and coding tasks

Which declaration correctly defines a function that takes a lambda parameter and returns an Int?

  1. fun apply(op: (Int) => Int): Int = op(5)
  2. fun apply(op: [Int -> Int]): Int = op(5)
  3. fun apply(op: Int -> Int): Int = op(5)
  4. fun apply(op: (Int) -> Int): Int = op(5) Correct answer.

Explanation

A function type in Kotlin is written with parentheses around the parameter types, an arrow, and the return type, as in (Int) -> Int. The parameter name and its function type are separated by a colon.

Question 11Kotlin language fundamentals and coding tasks

In a class, what is the difference between the primary constructor parameter "val id: Int" and "id: Int" (without val or var)?

  1. The plain parameter becomes a mutable property, while the val parameter is read-only.
  2. Both create a property, but the one without val is private.
  3. Only the val parameter becomes a property; the plain parameter is usable only in initializer blocks and property initializers. Correct answer.
  4. The plain parameter is automatically promoted to a public property by the compiler.

Explanation

Adding val or var to a primary constructor parameter declares a property on the class. Without either keyword the parameter is merely a constructor argument that can be referenced in initializer blocks and property initializers but is not stored as a member.

Question 12Kotlin language fundamentals and coding tasks

What does the following code print? open class Animal { open fun sound() = "..." } class Dog : Animal() { override fun sound() = "Woof" } fun main() { val a: Animal = Dog() println(a.sound()) }

  1. A compile-time error because sound is not open
  2. An empty line
  3. Woof Correct answer.
  4. ...

Explanation

Kotlin methods are virtual by default when marked open, so the call resolves to the overridden implementation in Dog at runtime. The declared type Animal does not prevent dynamic dispatch to the subclass override.

Question 13Kotlin language fundamentals and coding tasks

Which statement about Kotlin extension functions is correct?

  1. They can access private members of the receiver class.
  2. They override member functions with the same signature.
  3. They require the receiver class to be marked open.
  4. They are resolved statically based on the declared type of the receiver. Correct answer.

Explanation

Extension functions are compiled as static functions whose first parameter is the receiver, so the version invoked depends on the compile-time type of the expression. If a member function with the same signature exists, the member always wins.

Question 14Kotlin language fundamentals and coding tasks

What is the value of the expression "listOf(1, 2, 3).fold(0) { acc, x -> acc + x }"?

  1. 6 Correct answer.
  2. 3
  3. 0
  4. 1

Explanation

fold starts with the supplied initial value 0 and combines it with each element using the given operation, effectively summing 1 + 2 + 3. The accumulator is threaded through every iteration and returned at the end.

Question 15Kotlin language fundamentals and coding tasks

Which pair of declarations compiles in Kotlin?

  1. var y = 5; y = 6 Correct answer.
  2. val x = 5; x = 6
  3. val w: Int; w = 5; w = 6
  4. const val z = 5; z = 6

Explanation

A var may be reassigned any number of times after initialization, so assigning 6 to y is legal. A val can be assigned exactly once, so reassigning x, z, or w produces a compile-time error.

Question 16Kotlin language fundamentals and coding tasks

Given "sealed class Result" with subclasses Success and Failure, why is a when expression over a Result value exhaustive without an else branch?

  1. Because when automatically adds an else branch for sealed types at runtime.
  2. Because sealed classes are implicitly open and all subclasses are final.
  3. Because the compiler knows the complete set of direct subclasses declared in the same package and module. Correct answer.
  4. Because sealed classes cannot have any subclasses at all.

Explanation

A sealed class restricts its hierarchy to a known, closed set of subclasses declared in the same module and package, so the compiler can verify that every subtype is covered. That closed set is what makes an exhaustive when possible without an else.

Question 17Kotlin language fundamentals and coding tasks

What does the following code print? fun main() { val s: String? = "Kotlin" println(s?.length ?: -1) val t: String? = null println(t?.length ?: -1) }

  1. -1 then -1
  2. 6 then 0
  3. 6 then -1 Correct answer.
  4. 6 then a NullPointerException

Explanation

For the non-null string the safe call returns its length, 6, and the Elvis operator passes that value through. For the null reference the safe call yields null, so the Elvis operator substitutes the fallback -1.

Question 18Kotlin language fundamentals and coding tasks

Which statement about Kotlin's default and named arguments is correct?

  1. Default parameter values are only allowed in constructors.
  2. Named arguments cannot be combined with positional arguments in one call.
  3. A caller may skip a parameter that has a default value and supply later arguments by name. Correct answer.
  4. Default values are evaluated once and cached for all calls.

Explanation

Parameters with default values may be omitted, and remaining arguments can be passed by name so the call stays unambiguous. Named and positional arguments may be mixed as long as positional arguments come first.

Question 19Kotlin language fundamentals and coding tasks

What is the result of "listOf(1, 2, 3, 4, 5).partition { it % 2 == 0 }"?

  1. A Boolean indicating whether all elements are even
  2. A List containing only [2, 4]
  3. A Pair whose first component is [2, 4] and second is [1, 3, 5] Correct answer.
  4. A Map from each element to its parity

Explanation

partition splits a collection into a Pair of two lists: the first holds elements matching the predicate and the second holds the rest. Here the evens go into the first list and the odds into the second.

Question 20Kotlin language fundamentals and coding tasks

Which statement about Kotlin's object declaration ("object Singleton") is correct?

  1. It declares a single instance that is initialized lazily on first access. Correct answer.
  2. It creates a class whose instances must be created with a constructor call.
  3. It can be instantiated multiple times if marked open.
  4. It cannot contain functions, only properties.

Explanation

An object declaration defines a class with exactly one instance, created and initialized the first time it is accessed. This gives thread-safe lazy initialization without manual synchronization.

Question 21Kotlin language fundamentals and coding tasks

What does the following code print? fun main() { val x: Int? = null val y = x ?: 0 val z = x?.plus(1) ?: 10 println("$y $z") }

  1. 0 10 Correct answer.
  2. 0 null
  3. null 10
  4. 10 0

Explanation

Because x is null, the Elvis operator supplies 0 for y and 10 for z after the safe call yields null. The printed line therefore contains 0 followed by 10.

Question 22Kotlin language fundamentals and coding tasks

In Kotlin, what is the purpose of the companion object inside a class?

  1. It prevents the class from being instantiated more than once.
  2. It provides a way to declare members that are accessed through the class name rather than an instance. Correct answer.
  3. It is a required element for every class that extends another class.
  4. It makes all instance members static automatically.

Explanation

A companion object holds members associated with the class itself, so they are called via the class name, similar to static members in Java. It is a real object that can also implement interfaces and hold state.

Question 23Kotlin language fundamentals and coding tasks

Consider this code: class Counter { companion object { private var total = 0 fun increment() { total++ } fun get(): Int = total } } fun main() { Counter.increment() Counter.increment() println(Counter.get()) }

  1. 1
  2. A compile-time error because total is private
  3. 0
  4. 2 Correct answer.

Explanation

The companion object is a single shared instance, so its private total persists across calls and is incremented twice. The private modifier only restricts access from outside the companion, while increment and get are members of it and can read total.

Question 24Kotlin language fundamentals and coding tasks

Which statement about Kotlin's type system regarding nullability is correct?

  1. Any reference type is nullable by default, as in Java.
  2. The !! operator converts a nullable type into a non-nullable type permanently.
  3. A value of type String? cannot be assigned to a variable of type String without a null check or cast. Correct answer.
  4. String and String? are the same type at compile time.

Explanation

Kotlin distinguishes nullable types from non-nullable ones at compile time, so assigning String? to String requires a null check, safe call, or explicit cast. This is what moves most null failures from runtime to compile time.

Question 25Kotlin language fundamentals and coding tasks

What does the following code print? fun main() { val nums = listOf(3, 1, 2) val sorted = nums.sortedBy { it } val mapped = nums.map { it * 2 } println("$sorted $mapped") }

  1. [3, 1, 2] [2, 4, 6]
  2. [3, 1, 2] [6, 2, 4]
  3. [1, 2, 3] [6, 2, 4] Correct answer.
  4. [1, 2, 3] [2, 4, 6]

Explanation

sortedBy returns a new sorted list [1, 2, 3] without modifying the original, while map preserves the original order and doubles each element to [6, 2, 4]. Kotlin collection operations on a read-only List return new collections rather than mutating in place.

Question 26Kotlin language fundamentals and coding tasks

In Kotlin, which keyword marks a variable whose reference can be reassigned after initialization, while still forbidding type changes?

  1. val
  2. var Correct answer.
  3. lateinit
  4. const

Explanation

A var declares a mutable reference: the variable can point to a different value later, although the declared type stays fixed. A val is a read-only reference that may only be assigned once. The other keywords relate to compile-time constants and deferred initialization, not general reassignability.

Question 27Kotlin language fundamentals and coding tasks

Which Kotlin string template expression correctly embeds the value of a variable named count inside a literal?

  1. "Total: {{count}}"
  2. "Total: ${count}" Correct answer.
  3. "Total: %count%"
  4. "Total: #{count}"

Explanation

Kotlin string templates use a dollar sign: $name for simple identifiers and ${expression} for arbitrary expressions. Only the dollar-based form is interpreted by the compiler; the other placeholder styles are plain literal text.

Question 28Kotlin language fundamentals and coding tasks

Which expression produces the string "Kotlin" from the nullable variable name declared as val name: String? = "Kotlin"?

  1. name ?: ""
  2. name.toString()
  3. name as String?
  4. name!! Correct answer.

Explanation

The not-null assertion operator !! converts a nullable type to its non-null counterpart, throwing NullPointerException if the value is null. Here the value is non-null, so the result type is String and the value is "Kotlin". The other options either keep the nullable type or bypass the type system differently.

Question 29Kotlin language fundamentals and coding tasks

Which Kotlin collection type preserves insertion order and allows duplicate elements while offering indexed access?

  1. Set
  2. ArrayDeque
  3. List Correct answer.
  4. Map

Explanation

A List is an ordered collection with positional access via get and the index operator, and it permits duplicates. A Set forbids duplicates, a Map stores key-value pairs, and ArrayDeque is a double-ended queue without general positional access semantics.

Question 30Kotlin language fundamentals and coding tasks

Given val x = 7, what is the value of the expression when (x) { in 1..5 -> "low"; in 6..10 -> "mid"; else -> "high" }?

  1. A compile error, because when used as an expression must be exhaustive without else
  2. "high"
  3. "mid" Correct answer.
  4. "low"

Explanation

The when subject 7 falls inside the range 6..10, so the second branch is selected and yields "mid". Range checks with in are evaluated in order until a branch matches. Because an else branch is present, the expression is exhaustive and compiles.

Question 31Kotlin language fundamentals and coding tasks

Which declaration creates a read-only property with a custom getter that is computed on each access?

  1. val area: Int by lazy { width * height }
  2. var area: Int get() = width * height
  3. val area: Int = width * height
  4. val area: Int get() = width * height Correct answer.

Explanation

Defining get() after the property declaration provides a custom accessor that runs every time the property is read, so the value reflects current width and height. An initializer computes once at construction, and lazy computes once on first access and caches the result.

Question 32Kotlin language fundamentals and coding tasks

Which statement correctly describes the difference between the == operator and the === operator in Kotlin?

  1. == compares object identity, while === calls equals for structural comparison
  2. == works only for primitives, while === works only for references
  3. == calls equals for structural comparison, while === compares object identity Correct answer.
  4. Both compare identity, but === also checks the static type

Explanation

In Kotlin, == is translated to a null-safe call to equals, giving structural equality, while === checks whether two references point to the same instance. For value classes and primitives the compiler may optimize, but the semantic distinction remains. This differs from Java, where == on references means identity.

Question 33Kotlin language fundamentals and coding tasks

What does the expression listOf(1, 2, 3).map { it * 2 }.first() return?

  1. 2 Correct answer.
  2. A list containing 2, 4, 6
  3. 1
  4. 6

Explanation

map transforms each element, producing [2, 4, 6], and first() returns the element at index 0, which is 2. The chain does not return the whole list because first() reduces it to a single element. The original element 1 would only be returned without the map step.

Question 34Kotlin language fundamentals and coding tasks

Which construct lets a function accept a variable number of arguments of the same type, such as fun sum(vararg values: Int)?

  1. the spread operator on the function name
  2. default parameter values
  3. an Array parameter marked optional
  4. vararg modifier on the parameter Correct answer.

Explanation

Marking a parameter with vararg allows callers to pass any number of arguments of that type, which are exposed inside the function as an array. Default values only supply a fallback when an argument is omitted, and the spread operator is used at call sites to pass an existing array into a vararg parameter.

Question 35Kotlin language fundamentals and coding tasks

What is printed by this code? fun main() { val a = intArrayOf(1, 2, 3) val b = a b[0] = 99 println(a[0]) }

  1. 0
  2. A compile error, because intArrayOf returns an immutable array
  3. 99 Correct answer.
  4. 1

Explanation

Arrays in Kotlin are reference types, so b and a point to the same underlying array. Mutating an element through b is visible through a, so a[0] prints 99. The IntArray type is mutable in its elements even though the variable binding itself is a val.

Question 36Kotlin language fundamentals and coding tasks

Which declaration correctly defines a generic function that returns its argument unchanged?

  1. fun identity(value: T): T = value
  2. fun <T> identity(value: T) = T
  3. fun <T> identity(value: T): T = value Correct answer.
  4. fun identity<T>(value: T): T = value

Explanation

Kotlin places type parameters before the function name, so fun <T> identity(value: T): T = value is the valid form. The type parameter must be declared in that position to be usable in the parameter and return types. The other forms either omit the declaration or use Java-style syntax.

Question 37Kotlin language fundamentals and coding tasks

What does the following code print? fun main() { val s: String? = null println(s?.length ?: s?.length ?: 0) }

  1. A NullPointerException
  2. A compile error, because the second safe call is redundant
  3. 0 Correct answer.
  4. null

Explanation

The safe call s?.length yields null because s is null. The first Elvis operator then evaluates its right side, which is another null-yielding safe call, so the second Elvis falls through to 0. No exception is thrown because safe calls never dereference null.

Question 38Kotlin language fundamentals and coding tasks

Which statement about Kotlin interfaces is correct?

  1. An interface may declare abstract members and also provide default implementations of functions Correct answer.
  2. A class may implement at most one interface
  3. An interface may hold mutable instance state through backing fields
  4. An interface may declare only abstract members and no method bodies

Explanation

Kotlin interfaces can contain abstract declarations as well as functions with bodies, which act as default implementations. They cannot store instance state in backing fields, though they may declare abstract properties. A class may implement many interfaces, unlike single class inheritance.

Question 39Kotlin language fundamentals and coding tasks

What is printed by this code? fun main() { val items = listOf("a", "bb", "ccc") println(items.map { it.length }.reduce { acc, n -> acc + n }) }

  1. A compile error, because reduce requires an initial value
  2. abc
  3. 6 Correct answer.
  4. 3

Explanation

map produces [1, 2, 3], and reduce folds the elements by repeatedly applying the lambda to an accumulator and the next element, giving 1 + 2 + 3 = 6. Unlike fold, reduce uses the first element as the initial accumulator, which is why no seed is supplied.

Question 40Kotlin language fundamentals and coding tasks

Consider a sealed interface Shape with implementations Circle and Square. Which statement about a when expression over a Shape parameter is correct?

  1. It can only be used as a statement, never as an expression
  2. It requires the parameter to be cast to Any before branching
  3. It always requires an else branch because interfaces are open by nature
  4. It is exhaustive without an else branch when every direct implementation is covered Correct answer.

Explanation

Sealed types restrict their direct subtypes to the same package and module, so the compiler knows the complete set and can verify exhaustiveness. A when covering all direct implementations therefore needs no else branch. This exhaustiveness check works for sealed classes and sealed interfaces alike.

Question 41Kotlin language fundamentals and coding tasks

Which declaration correctly defines a class that implements a generic interface Comparable-like contract using a type parameter with an upper bound?

  1. class Box<T>(val value: T) where T : Number
  2. class Box(val value: T : Number)
  3. class Box<T : Number>(val value: T) Correct answer.
  4. class Box<T extends Number>(val value: T)

Explanation

Kotlin expresses an upper bound with a colon after the type parameter, as in T : Number, directly in the type parameter list. The extends keyword is Java syntax, and the where clause is used for multiple bounds, not as a replacement for the inline bound. The last form places a type constraint where a value parameter is expected.

Question 42Kotlin language fundamentals and coding tasks

What is printed by this code? fun main() { val list = mutableListOf(1, 2, 3) val view = list.subList(0, 2) list[0] = 10 println(view) }

  1. [1, 2]
  2. A ConcurrentModificationException
  3. [10]
  4. [10, 2] Correct answer.

Explanation

subList on a MutableList returns a view backed by the original list, so structural and element changes to the underlying list are reflected in the view. Setting index 0 to 10 therefore makes the view print [10, 2]. Creating a separate copy would require toList().

Question 43Kotlin language fundamentals and coding tasks

Which statement about lambdas and the return of the enclosing function is correct?

  1. A lambda can never contain a return statement
  2. A labeled return is required for every lambda, inline or not
  3. A plain return inside a lambda passed to an inline function returns from the enclosing function Correct answer.
  4. A plain return inside any lambda returns from the lambda itself

Explanation

Because inline functions have their lambda bodies inlined, a bare return inside such a lambda is a non-local return that exits the enclosing function. For lambdas passed to non-inline functions, a bare return is not allowed and a labeled return must be used to return from the lambda. This distinction is central to Kotlin's inline function design.

Question 44Kotlin language fundamentals and coding tasks

What is printed by this code? fun main() { val result = runCatching { "7".toInt() + 1 } println(result.getOrNull()) }

  1. 8 Correct answer.
  2. A NumberFormatException
  3. 7
  4. null

Explanation

toInt() on "7" succeeds and produces 7, and adding 1 gives 8, so runCatching wraps the successful value in a Result. getOrNull returns that value when the computation succeeded. Exceptions would be captured rather than propagated.

Question 45Kotlin language fundamentals and coding tasks

Which statement about Kotlin's Elvis operator ?: is correct?

  1. It converts the left operand to a non-null type only when it is a String
  2. It returns the right operand whenever the left operand is non-zero
  3. It returns the left operand when it is non-null, otherwise the right operand, and the right operand is evaluated lazily Correct answer.
  4. It throws when the left operand is null

Explanation

The Elvis operator yields the left-hand value if it is not null and otherwise evaluates and yields the right-hand expression. The right side is short-circuited, so it is only evaluated when needed. It works for any nullable type, not just strings.

Question 46Kotlin language fundamentals and coding tasks

What is printed by this code? fun main() { val words = listOf("apple", "banana", "cherry") println(words.groupBy { it.first() }.keys) }

  1. [a, b, c] Correct answer.
  2. 3
  3. [apple, banana, cherry]
  4. [1, 2, 3]

Explanation

groupBy builds a map from the key returned by the lambda to the list of elements sharing that key, so the keys are the distinct first characters a, b and c. Calling keys on the resulting map returns those keys in encounter order. The values would be the grouped word lists, not the keys.

Question 47Kotlin language fundamentals and coding tasks

What is printed by this code? fun main() { val x: Any = 5 when (x) { is String -> println("string") is Int -> println("int") else -> println("other") } }

  1. int Correct answer.
  2. A compile error, because Any cannot be used as a when subject
  3. string
  4. other

Explanation

The when subject is typed Any but its runtime value is an Int, so the is Int branch matches after the is String check fails. Type checks with is perform a smart cast inside the branch. Using Any as a when subject is legal.

Question 48Kotlin language fundamentals and coding tasks

Which statement about Kotlin's type checks with is and as is correct?

  1. A failed as cast throws ClassCastException, while as? yields null instead Correct answer.
  2. as? throws when the cast fails, while as returns null
  3. Both is and as perform compile-time-only checks with no runtime behavior
  4. A failed is check throws ClassCastException

Explanation

The as operator performs an unchecked cast that throws ClassCastException on failure, whereas the safe cast as? returns null when the value is incompatible. The is operator only tests the type and returns a Boolean without throwing. These operators have real runtime behavior, not just compile-time effects.

Question 49Kotlin language fundamentals and coding tasks

Consider a data class Point(val x: Int, val y: Int). Which statement about its generated members is correct?

  1. toString is generated only when the class also overrides hashCode
  2. copy can change selected properties while retaining the others Correct answer.
  3. componentN functions are generated only for properties declared as var
  4. equals compares only the first declared property

Explanation

The compiler-generated copy function accepts arguments defaulting to the current property values, so a caller can replace some properties and keep the rest. equals and hashCode are generated over all primary-constructor properties, and componentN functions are generated regardless of val or var. toString is generated independently of hashCode overrides.

Question 50Kotlin language fundamentals and coding tasks

What is printed by this code? fun main() { val seq = sequenceOf(1, 2, 3, 4) val result = seq.map { println("map $it"); it * 2 }.first() println(result) }

  1. map 1 map 2 2
  2. map 1 2 Correct answer.
  3. map 1 map 2 map 3 map 4 2
  4. 2

Explanation

Sequences are lazy, so map is applied element by element only as far as needed. first() requests the first transformed element, causing map to run for 1 only, printing "map 1" before the result 2. The remaining elements are never processed.