Sample questions
Question 1Easy
What is the output?
println("abc".reversed())
- cba
- abc
- acb
- bca
Answer: A. cba
Explanation
reversed() returns the characters in reverse order.
Step-by-step solution
- a b c.
- Reverse.
- c b a.
Why the other options are wrong
- B. reversed returns a reversed copy.
- C. The order is fully reversed.
- D. Not a rotation.
Question 2Easy
What is the output?
val n = 3
println("n = ${n * 2}")
- n = n * 2
- n = 6
- n = 3
- n = ${6}
Answer: B. n = 6
Explanation
${...} evaluates an expression inside a string template.
Step-by-step solution
- The template part inside the braces is evaluated: n * 2 gives 6, since n is 3.
- The string prints n = followed by the value. Marked answer: option B.
Why the other options are wrong
- A. The template is evaluated, so the expression is not printed as text.
- C. N is 3, but the printed value is n * 2, which is 6.
- D. The template is replaced by its value; it does not print the braces or the number in braces.
Tip: Inside a string template, the expression is computed before printing.
Question 3Easy
What is the output?
println("a,b,c".split(","))
- a,b,c
- [a, b, c]
- [a,b,c]
- abc
Answer: B. [a, b, c]
Explanation
split returns a List<String> that prints with brackets and ", ".
Step-by-step solution
- Split at commas.
- Three parts.
- [a, b, c].
Why the other options are wrong
- A. split returns a list.
- C. The list prints with spaces after commas.
- D. Commas are removed, but it remains a list.
Question 4Easy
Which string literal allows multiple lines without escape sequences?
- Single quotes '...'
- Backticks
... - Triple quotes: """..."""
- Double quotes with \n only
Answer: C. Triple quotes: """..."""
Explanation
Raw strings in triple quotes keep line breaks and don't process escapes.
Step-by-step solution
- Triple quotes = raw string.
- Newlines are kept.
- Escapes are not interpreted.
Why the other options are wrong
- A. Single quotes are for Char.
- B. Backticks escape identifiers.
- D. That needs escape characters.
Question 5Medium
What is the output?
println("a" + 1 + 2)
- a3
- 3a
- a12
- Compile error
Answer: C. a12
Explanation
"a" + 1 gives "a1", then + 2 gives "a12".
Step-by-step solution
- "a" + 1 = "a1".
- "a1" + 2 = "a12".
- Prints a12.
Why the other options are wrong
- A. String concatenation is left to right.
- B. The string comes first.
- D. String.plus(Any?) accepts numbers.
Question 6Medium
What is the output?
val (a, b) = Pair(1, "x")
println("$b$a")
- 1x
- Pair(1, x)
- Compile error
- x1
Answer: D. x1
Explanation
Destructuring assigns a=1 and b="x", so "$b$a" is x1.
Step-by-step solution
- Destructuring assigns a = 1 and b = "x".
- "$b$a" replaces $b with "x" and $a with 1, making x1.
- Println prints the result as x1.
- The output is x1.
Why the other options are wrong
- A. The string interpolation puts b first, so the output starts with x, not 1.
- B. Pair is destructured, so no Pair text is printed; only the values are used.
- C. The destructuring and string template are valid Kotlin, so the code compiles.
Tip: Remember that "$b$a" puts the values in the order written, so b comes first.
Question 7Medium
What is the difference between const val and val?
- const val can be reassigned later on; val can never be reassigned
- const val is allowed only inside function bodies and lambdas
- There is no difference between the two declarations at all
- const is a compile-time constant (primitives, String); val is set at runtime
Answer: D. const is a compile-time constant (primitives, String); val is set at runtime
Explanation
const values must be known at compile time and be top-level, in an object or a companion.
Step-by-step solution
- Const val is a compile-time constant; it must be known before runtime.
- Val is set during runtime and can depend on other variables or inputs.
- Const val can only be used for primitives and Strings, not objects.
- Val can hold any type of value, including objects and complex data structures.
- The key difference is when the value is determined: const at compile time, val at runtime.
- Final answer: const is a compile-time constant (primitives, String); val is set at runtime.
Why the other options are wrong
- A. Neither can be reassigned; const val and val are both read-only.
- B. const val must be top-level or in an object or companion object, not inside a function.
- C. They differ: const must be a compile-time constant, while val can be computed at runtime.
Tip: Remember: const is for compile-time constants, val for runtime values.
Question 8Medium
What is the output?
println("banana".count { it == 'a' })
- 2
- 1
- 6
- 3
Answer: D. 3
Explanation
b-a-n-a-n-a has three 'a' characters.
Step-by-step solution
- Check each char.
- a at positions 1,3,5.
- Count 3.
Why the other options are wrong
- A. banana has three a's.
- B. The count lambda counts all matches.
- C. That is the string length.
Question 9Hard
What does lateinit var require and guarantee?
- A val that is initialised lazily on first access by a delegate
- A nullable var that quietly defaults to null until assigned later
- A non-null reference var set later; reading it early throws UninitializedPropertyAccessException
- A primitive Int that defaults to zero until it is assigned
Answer: C. A non-null reference var set later; reading it early throws UninitializedPropertyAccessException
Explanation
lateinit is for non-null reference types set after construction; by lazy is the one for val.
Step-by-step solution
- Lateinit var requires a non-null variable declared without an initial value.
- It lets you assign the variable later; Kotlin does not check that this happens before use.
- Reading the variable before assignment throws UninitializedPropertyAccessException.
- This ensures no null checks are needed for the variable.
- The variable must be of a reference type, not primitive or nullable.
- Final answer: A non-null reference var set later; reading it early throws UninitializedPropertyAccessException.
Why the other options are wrong
- A. lateinit is only for var, and it does not use a delegate or lazy initialisation.
- B. lateinit vars are non-null and throw when read early, so they are not null by default.
- D. lateinit cannot be used on primitives like Int; it needs a non-primitive reference type.
Tip: Use lateinit for non-null references that need setup later, not for primitives or nullable types.
Question 10Hard
What is the output?
println('a' + 1)
- 98
- a1
- b
- Compile error
Answer: C. b
Explanation
Char + Int returns the Char shifted by that many code points.
Step-by-step solution
- 'a' is 97.
- 97 + 1 = 98.
- Char 98 is 'b'.
Why the other options are wrong
- A. Char + Int stays a Char.
- B. It is arithmetic, not concatenation.
- D. Char.plus(Int) exists.
Practise all 18 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →