Explore foundational object-oriented programming concepts in Kotlin with this quiz focusing on classes, objects, and interfaces. Strengthen your understanding of Kotlin's syntax and best practices for building robust OOP solutions.
Which of the following is the correct way to define a simple class named Car in Kotlin?
Explanation: The correct syntax to define a basic class in Kotlin is 'class Car'. 'Car class' is not valid syntax and reverses the keyword order. 'define class Car' uses an incorrect keyword not recognized by Kotlin. 'object Car' defines an object singleton, not a class, which is a different concept.
How do you create an instance of the Book class in Kotlin and assign it to a variable named novel?
Explanation: 'val novel = Book()' is the correct way to create an instance of the Book class in Kotlin. The second option uses syntax from another language and is not suitable for Kotlin. The third option incorrectly uses curly braces, which are not used for calling class constructors. The fourth option also follows another language's pattern and is incorrect for Kotlin.
If you want the Person class to always require a name parameter when being created, which definition is correct in Kotlin?
Explanation: 'class Person(val name: String)' defines a primary constructor that makes the 'name' property mandatory. The second option uses braces for class body, not constructor, and is not valid syntax. The third provides a default value, making the parameter optional, not required. The fourth option has no parameters and does not require 'name'.
What is the correct way to declare an interface named Drawable in Kotlin?
Explanation: 'interface Drawable' uses the correct Kotlin keyword for defining an interface. 'Drawable interface' swaps the order and does not use proper keywords. 'class Drawable interface' incorrectly combines class and interface keywords. 'declare Drawable interface' uses unsupported words for Kotlin syntax.
Given interface Flyable, which is the correct syntax for a class Bird to implement it in Kotlin?
Explanation: In Kotlin, interfaces are implemented using the colon syntax as shown in 'class Bird : Flyable'. 'implements' and 'extends' are keywords from other languages and not used in Kotlin. 'u003Eu003E' has no relevance for class or interface implementation.
Which statement correctly creates an object of the Student class if the constructor takes no parameters?
Explanation: In Kotlin, 'val s = Student()' is the correct way to instantiate a no-argument class. 'Student val s' uses an incorrect order for the declaration. 'val s = new Student()' uses 'new', which is not used in Kotlin. 'Student s = Student()' uses type declaration and assignment as in other languages, but is not the proper way for Kotlin.
Why would you use the 'data' keyword before a class in Kotlin, such as in 'data class Point(val x: Int, val y: Int)'?
Explanation: The 'data' keyword tells Kotlin to generate methods like equals, hashCode, and toString automatically. It does not make properties private. It also does not make the class abstract, nor does it enable inheritance by default since inheritance rules are not affected by 'data'.
If you declare a property inside a Kotlin class without specifying a modifier, what is the default visibility?
Explanation: In Kotlin, the default visibility for classes and their members is 'public'. 'Private' restricts access to within the class. 'Protected' is more restrictive and only available within subclasses. 'Internal' restricts usage to the same module.
What does the 'object' keyword define in Kotlin when used before a name, as in 'object Singleton'?
Explanation: The 'object' keyword creates a singleton instance, meaning only one instance exists. It does not define an abstract class or an interface, which have their own keywords. A 'data class' must use the 'data' modifier, not 'object'.
Which is the correct syntax for class Dog to inherit from class Animal in Kotlin?
Explanation: 'class Dog : Animal()' uses the correct inheritance syntax in Kotlin. The colon and parentheses indicate that Dog inherits from Animal by calling its constructor. 'extends' and 'implements' are from other languages, and 'inherits' is not a Kotlin keyword for this purpose.