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 freeSample questions
In Kotlin, what keyword is used to declare a variable that cannot be reassigned after its initial assignment?
- var
- val
- let
- 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.
What is the correct syntax for a multi-line comment in Kotlin?
- // comment // another comment
- /* comment another comment */
- -- comment -- another comment
- # comment # another comment
Show answer
Answer: B. /* comment another comment */
Multi-line comments in Kotlin are enclosed within `/*` and `*/`.
What does the `val` keyword signify in Kotlin?
- A mutable variable
- A read-only (immutable) variable
- A constant that is evaluated at runtime
- 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.
How do you declare a default parameter value for a function in Kotlin?
- fun greet(name: String = "Guest")
- fun greet(name: String default "Guest")
- fun greet(name: String with default "Guest")
- 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.
Which of the following is the Kotlin syntax for a `for` loop iterating over a range of numbers from 1 to 5 (inclusive)?
- for (i in 1..5) { ... }
- for i in 1 to 5 { ... }
- for i from 1 to 5 { ... }
- 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.