Any2Quiz
Computer Science / Languages / Kotlin

Syntax Quiz - Languages Kotlin

Free practice quiz: Syntax (Languages Kotlin). 51 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 51 questions free

Sample questions

  1. In Kotlin, what keyword is used to declare a variable that cannot be reassigned after its initial assignment?

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

    Answer: B. val

    In Kotlin, `val` is used to declare read-only variables (immutable), meaning they cannot be reassigned after their initial assignment. `var` is used for mutable variables.

  2. What is the correct syntax for a multi-line comment in Kotlin?

    1. // comment // another comment
    2. /* comment another comment */
    3. -- comment -- another comment
    4. # comment # another comment
    Show answer

    Answer: B. /* comment another comment */

    Multi-line comments in Kotlin are enclosed within `/*` and `*/`.

  3. What does the `val` keyword signify in Kotlin?

    1. A mutable variable
    2. A read-only (immutable) variable
    3. A constant that is evaluated at runtime
    4. A nullable variable
    Show answer

    Answer: B. A read-only (immutable) variable

    `val` declares a read-only property in Kotlin, which means it can be assigned a value only once.

  4. How do you declare a default parameter value for a function in Kotlin?

    1. fun greet(name: String = "Guest")
    2. fun greet(name: String default "Guest")
    3. fun greet(name: String with default "Guest")
    4. fun greet(name: String := "Guest")
    Show answer

    Answer: A. fun greet(name: String = "Guest")

    Default parameter values are assigned using the `=` operator in the function signature.

  5. Which of the following is the Kotlin syntax for a `for` loop iterating over a range of numbers from 1 to 5 (inclusive)?

    1. for (i in 1..5) { ... }
    2. for i in 1 to 5 { ... }
    3. for i from 1 to 5 { ... }
    4. loop i from 1 to 5 { ... }
    Show answer

    Answer: A. for (i in 1..5) { ... }

    Kotlin's `for` loop uses the `in` keyword and the range operator `..` for iterating over sequences, including number ranges.

Take the full quiz

More quizzes

Browse all Kotlin quizzes