Syntax Quiz - Languages Kotlin
Free practice quiz: Syntax (Languages Kotlin). 51 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages Kotlin practice quiz has 51 multiple-choice questions on Syntax, and 51 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 51 questions, freeSample questions
In Kotlin, what keyword is used to declare a variable that cannot be reassigned after its initial assignment?
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?
Show answer
Answer: B. /* comment another comment */
Multi-line comments in Kotlin are enclosed within `/*` and `*/`.
What does the `val` keyword signify in Kotlin?
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?
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)?
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.
More questions in this quiz
Answers and explanations are in the full quiz.
- Which of the following is the correct way to declare a nullable String in Kotlin?
- What is the primary difference between `==` and `===` operators in Kotlin?
- Which keyword is used to define a function in Kotlin?
- What is the correct syntax for a single-line comment in Kotlin?
- Which of the following represents an immutable list in Kotlin?
- How do you create an instance of a class named `MyClass` in Kotlin?
- Which statement correctly defines an extension function for the `String` class in Kotlin?
- What is the purpose of the `when` expression in Kotlin?
- Which of the following is NOT a valid way to handle nullability in Kotlin?
- Which keyword is used to create a data class in Kotlin, which automatically generates `equals()`, `hashCode()`, `toString()`, etc.?
- What does the `lateinit` keyword do in Kotlin?
- Which of the following is the correct way to access an element at index 2 in a Kotlin `List` named `myList`?
- What is the purpose of the `object` keyword in Kotlin?
- Which of the following is the Kotlin equivalent of Java's `final` keyword?
- What is the scope resolution operator in Kotlin?
- How do you declare a variable that can be reassigned in Kotlin?
- What is the correct syntax for creating a `HashMap` in Kotlin?
- Which keyword is used to define a constructor for a class in Kotlin?
- Which of the following is a correct way to declare a function that returns an `Int` in Kotlin?
- What is the role of the `as?` operator in Kotlin?
- What is the Kotlin keyword for an immutable property that can be a compile-time constant?
- Which of the following correctly defines a nullable `Int` variable initialized to null?
- What is the purpose of the `object` keyword when used with `companion`?
- Which of the following is a valid way to declare an empty `ArrayList` in Kotlin?
- What is the Kotlin equivalent of Java's `instanceof` operator?
- Which of these expressions is used for safe iteration over a nullable collection in Kotlin?
- What is the primary purpose of the `require` function in Kotlin?
- What does the `return@` syntax in Kotlin achieve?
- Which keyword is used to define a read-only property that is initialized at runtime?
- What is the Kotlin syntax for string templating or interpolation?
- Which of the following operators is used to invoke a lambda expression or a function reference?
- What is the purpose of the `by` keyword in Kotlin delegation?
- How do you declare a nullable `Boolean` in Kotlin?
- What is the Kotlin syntax for a `while` loop?
- Which operator is used for safe property access on a nullable object in Kotlin?
- What does the `inline` keyword imply for a function in Kotlin?
- What is the correct syntax for a sealed class in Kotlin?
- Which symbol denotes a lambda expression in Kotlin?
- What is the purpose of the `infix` modifier for a function in Kotlin?
- Which of the following is NOT a primitive type in Kotlin?
- What is the correct way to declare a top-level property in Kotlin?
- Which keyword is used to override a member from a superclass or interface in Kotlin?
- What is the purpose of the `vararg` keyword in Kotlin function parameters?
- Which of the following is the Kotlin syntax for a `do-while` loop?
- What is the `?.let { ... }` construct in Kotlin used for?
- Which keyword is used to define a singleton object in Kotlin?