Sunday, 1 December 2024

Kotlin paper solutions for unit -1





Paper April-2023



---


1(a) Fill in the blanks: (1 mark each)


1. Who developed Kotlin?

 JetBrains


2. Which extension is responsible to save Kotlin files?

 .kt


3. In Kotlin, _____ is the default visibility modifier.

 public

4. What is super keyword?

 It refers to the parent class's properties or methods.



1(b) Answer in brief: (2 marks)


(1) What is the meaning of interface?

An interface in Kotlin is a blueprint of a class that can contain abstract methods and properties, along with method implementations. A class can implement multiple interfaces, enabling multiple inheritance.


(2) What is the meaning of this keyword?

In Kotlin, this refers to the current instance of the class. It is used to access class members and distinguish between instance variables and local variables when they have the same name.



1(c) Answer in detail: (3 marks)


(1) Explain visibility modifier.

Kotlin provides four visibility modifiers to control the accessibility of classes, objects, and members:

public (default): Accessible from anywhere.

private: Accessible only within the same class.

protected: Accessible within the class and its subclasses.

internal: Accessible within the same module.

These modifiers help to encapsulate and protect data from unintended usage.


(2) Explain abstract class.

An abstract class in Kotlin is a class that cannot be instantiated directly. It can contain both abstract (unimplemented) methods and concrete (implemented) methods.

Declared using the abstract keyword.

It is used as a base class for inheritance.

Example:

abstract class Animal {  

    abstract fun sound()  

}  

class Dog : Animal() {  

    override fun sound() { println("Bark") }  

}


1(d) Write a note in detail: (5 marks each)


(1) Explain loop in Kotlin


Loops in Kotlin allow repetitive execution of a block of code. Kotlin provides several types of loops for different scenarios:


1. for loop:

Used to iterate over a range, collection, or array.

Syntax:

for (item in collection) {

    // Code to execute

}

Example:

for (i in 1..5) {

    println(i) // Prints 1 to 5

}

step can be used to change the increment value, and downTo is used for reverse iteration.

for (i in 10 downTo 1 step 2) {

    println(i) // Prints 10, 8, 6, 4, 2

}

2. while loop:

Executes the block of code as long as the condition is true.

Syntax:

while (condition) {

    // Code to execute

}

Example:

var count = 1

while (count <= 5) {

    println(count) // Prints 1 to 5

    count++

}

3. do-while loop:

Similar to the while loop but ensures the block of code executes at least once.

Syntax:

do {

    // Code to execute

} while (condition)

Example:

var count = 1

do {

    println(count) // Prints 1 to 5

    count++

} while (count <= 5)

4. break and continue:

break: Terminates the loop immediately.

continue: Skips the current iteration and moves to the next.

Example:

for (i in 1..5) {

    if (i == 3) break // Stops the loop when i = 3

    println(i) 

}


(2) Explain inheritance in Kotlin

Inheritance is a feature in Kotlin that allows a class to acquire the properties and methods of another class. This promotes code reusability and modularity.

1. Key Concepts of Inheritance:

open keyword:

In Kotlin, all classes are final by default and cannot be inherited. The open keyword is used to allow inheritance.

open class Parent {

    fun greet() = println("Hello from Parent")

}

class Child : Parent() // Inherits Parent

override keyword:

Used to redefine a parent class method in the child class.

open class Parent {

    open fun greet() = println("Hello from Parent")

}

class Child : Parent() {

    override fun greet() = println("Hello from Child")

}

super keyword:

Refers to the parent class's methods or properties.

open class Parent {

    open fun greet() = println("Hello from Parent")

}

class Child : Parent() {

    override fun greet() {

        super.greet() // Calls Parent's greet()

        println("Hello from Child")

    }

}

2. Types of Inheritance in Kotlin:

      a)Single Inheritance:

A class inherits from one parent class.


open class Animal {

    fun eat() = println("Eating...")

}

class Dog : Animal()


  b)Multiple Inheritance (via interfaces):

Kotlin allows implementing multiple interfaces to achieve multiple inheritance.


interface A {

    fun show() = println("Interface A")

}

interface B {

    fun display() = println("Interface B")

}

class C : A, B


3. Benefits of Inheritance:

Code reusability.

Polymorphism (overriding methods).

Improved maintainability.

Inheritance in Kotlin is crucial for building modular, scalable, and reusable code.





No comments:

Post a Comment

Kotlin paper solutions for unit -1

Paper April-2023 --- 1(a) Fill in the blanks: (1 mark each) 1. Who developed Kotlin?  JetBrains 2. Which extension is responsible to save Ko...