Strings & Misc Questions with Answers

18 practice questions on Strings & Misc (Kotlin) with answers and step-by-step solutions. Try 10 samples below, then practise all 18 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

println("abc".reversed())
  1. cba
  2. abc
  3. acb
  4. bca

Answer: A. cba

Explanation

reversed() returns the characters in reverse order.

Step-by-step solution

  1. a b c.
  2. Reverse.
  3. 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}")
  1. n = n * 2
  2. n = 6
  3. n = 3
  4. n = ${6}

Answer: B. n = 6

Explanation

${...} evaluates an expression inside a string template.

Step-by-step solution

  1. The template part inside the braces is evaluated: n * 2 gives 6, since n is 3.
  2. 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(","))
  1. a,b,c
  2. [a, b, c]
  3. [a,b,c]
  4. abc

Answer: B. [a, b, c]

Explanation

split returns a List<String> that prints with brackets and ", ".

Step-by-step solution

  1. Split at commas.
  2. Three parts.
  3. [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?

  1. Single quotes '...'
  2. Backticks ...
  3. Triple quotes: """..."""
  4. 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

  1. Triple quotes = raw string.
  2. Newlines are kept.
  3. 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)
  1. a3
  2. 3a
  3. a12
  4. Compile error

Answer: C. a12

Explanation

"a" + 1 gives "a1", then + 2 gives "a12".

Step-by-step solution

  1. "a" + 1 = "a1".
  2. "a1" + 2 = "a12".
  3. 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")
  1. 1x
  2. Pair(1, x)
  3. Compile error
  4. x1

Answer: D. x1

Explanation

Destructuring assigns a=1 and b="x", so "$b$a" is x1.

Step-by-step solution

  1. Destructuring assigns a = 1 and b = "x".
  2. "$b$a" replaces $b with "x" and $a with 1, making x1.
  3. Println prints the result as x1.
  4. 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?

  1. const val can be reassigned later on; val can never be reassigned
  2. const val is allowed only inside function bodies and lambdas
  3. There is no difference between the two declarations at all
  4. 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

  1. Const val is a compile-time constant; it must be known before runtime.
  2. Val is set during runtime and can depend on other variables or inputs.
  3. Const val can only be used for primitives and Strings, not objects.
  4. Val can hold any type of value, including objects and complex data structures.
  5. The key difference is when the value is determined: const at compile time, val at runtime.
  6. 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' })
  1. 2
  2. 1
  3. 6
  4. 3

Answer: D. 3

Explanation

b-a-n-a-n-a has three 'a' characters.

Step-by-step solution

  1. Check each char.
  2. a at positions 1,3,5.
  3. 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?

  1. A val that is initialised lazily on first access by a delegate
  2. A nullable var that quietly defaults to null until assigned later
  3. A non-null reference var set later; reading it early throws UninitializedPropertyAccessException
  4. 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

  1. Lateinit var requires a non-null variable declared without an initial value.
  2. It lets you assign the variable later; Kotlin does not check that this happens before use.
  3. Reading the variable before assignment throws UninitializedPropertyAccessException.
  4. This ensures no null checks are needed for the variable.
  5. The variable must be of a reference type, not primitive or nullable.
  6. 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)
  1. 98
  2. a1
  3. b
  4. Compile error

Answer: C. b

Explanation

Char + Int returns the Char shifted by that many code points.

Step-by-step solution

  1. 'a' is 97.
  2. 97 + 1 = 98.
  3. 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 →

Related topics

See all Kotlin topics · All topics