Recall & Review
beginner
What is the basic syntax to declare a class in Kotlin?Use the <code>class</code> keyword followed by the class name and curly braces. Example: <pre>class MyClass { }</pre>Click to reveal answer
beginner
How do you declare a primary constructor in a Kotlin class?
Add the constructor parameters directly after the class name in parentheses. Example: <pre>class Person(val name: String, var age: Int)</pre>Click to reveal answer
beginner
What keyword is used to declare a class in Kotlin?The keyword is
class. It tells Kotlin you are defining a new class.Click to reveal answer
beginner
Can a Kotlin class have no body? Show an example.Yes, if there is no need for properties or functions inside, you can omit the curly braces. Example: <pre>class Empty</pre>Click to reveal answer
beginner
How do you declare properties inside a Kotlin class primary constructor?Use <code>val</code> for read-only and <code>var</code> for mutable properties inside the constructor parentheses. Example: <pre>class Car(val brand: String, var year: Int)</pre>Click to reveal answer
Which keyword starts a class declaration in Kotlin?
✗ Incorrect
The
class keyword is used to declare a class in Kotlin.How do you declare a primary constructor with two properties in Kotlin?
✗ Incorrect
Primary constructor parameters with
val or var declare properties directly.Can a Kotlin class be declared without curly braces?
✗ Incorrect
If the class has no body, curly braces can be omitted.
What does
val mean in a Kotlin class constructor?✗ Incorrect
val declares a read-only property.Which of these is NOT a valid Kotlin class declaration?
✗ Incorrect
Option D uses invalid syntax;
function keyword is not used in class declaration.Explain how to declare a simple Kotlin class with a primary constructor that has two properties.
Think about how you write the class name and put properties inside parentheses.
You got /4 concepts.
Describe when you can omit the curly braces in a Kotlin class declaration.
Consider if the class has no properties or functions inside.
You got /3 concepts.