Kotlin OOP Essentials: Classes, Objects, and Interfaces Quiz Quiz

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.

  1. Defining a Basic Class

    Which of the following is the correct way to define a simple class named Car in Kotlin?

    1. object Car
    2. define class Car
    3. Car class
    4. class Car

    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.

  2. Object Creation

    How do you create an instance of the Book class in Kotlin and assign it to a variable named novel?

    1. novel = Book{}
    2. new Book novel
    3. val novel = Book()
    4. Book novel = new Book()

    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.

  3. Primary Constructor Usage

    If you want the Person class to always require a name parameter when being created, which definition is correct in Kotlin?

    1. class Person(name: String = 'John')
    2. class Person {name: String}
    3. class Person(val name: String)
    4. class Person()

    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'.

  4. Declaring an Interface

    What is the correct way to declare an interface named Drawable in Kotlin?

    1. Drawable interface
    2. class Drawable interface
    3. interface Drawable
    4. declare Drawable interface

    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.

  5. Implementing an Interface

    Given interface Flyable, which is the correct syntax for a class Bird to implement it in Kotlin?

    1. class Bird implements Flyable
    2. class Bird extends Flyable
    3. class Bird u003Eu003E Flyable
    4. class Bird : Flyable

    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.

  6. Instantiating Objects

    Which statement correctly creates an object of the Student class if the constructor takes no parameters?

    1. val s = Student()
    2. Student val s
    3. val s = new Student()
    4. Student s = Student()

    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.

  7. Data Classes

    Why would you use the 'data' keyword before a class in Kotlin, such as in 'data class Point(val x: Int, val y: Int)'?

    1. To mark the class as abstract
    2. To make all properties private
    3. To automatically generate useful methods like equals and toString
    4. To enable inheritance by default

    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'.

  8. Default Property Visibility

    If you declare a property inside a Kotlin class without specifying a modifier, what is the default visibility?

    1. public
    2. protected
    3. private
    4. internal

    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.

  9. Object Keyword Purpose

    What does the 'object' keyword define in Kotlin when used before a name, as in 'object Singleton'?

    1. An abstract class
    2. A data class
    3. An interface
    4. A singleton object

    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'.

  10. Extending from a Parent Class

    Which is the correct syntax for class Dog to inherit from class Animal in Kotlin?

    1. class Dog : Animal()
    2. class Dog extends Animal
    3. class Dog implements Animal
    4. Dog inherits Animal

    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.