Assessments
Chapter 1, Getting Started with Kotlin
Question 1
What’s the difference between var
and val
in Kotlin?
Answer
The val
keyword is used to declare an immutable reference, which means the value it holds cannot be changed once it’s assigned. On the other hand, the var
keyword declares a mutable reference, allowing the value it holds to be reassigned multiple times.
Question 2
How do you extend a class in Kotlin?
Answer
To extend a class, you specify a colon followed by the superclass name and its constructor. If the superclass is a regular class, it must be declared as open
, as, by default, Kotlin classes are final
and cannot be extended unless explicitly allowed using the open
keyword.
Question 3
How do you add functionality to a
Get Kotlin Design Patterns and Best Practices - Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.