Explore foundational Kotlin concepts for Android app development and backend frameworks like Ktor and Spring. This quiz helps solidify your understanding of syntax, data classes, coroutines, dependency injection, HTTP handling, and key language features relevant to building robust Android and server applications.
Which keyword is used to declare an immutable variable in Kotlin, for example, when you want a variable that cannot be reassigned?
Explanation: The 'val' keyword in Kotlin is used to define immutable variables, which means their values cannot be changed after assignment. 'var' is used for mutable variables that can be reassigned. 'let' is a scoping function, not a variable declaration. 'imm' is not a valid Kotlin keyword. Therefore, 'val' is the correct choice here.
What is a key benefit of using a data class for representing models in Kotlin backend applications?
Explanation: Kotlin data classes automatically generate useful methods such as equals, hashCode, and toString, making them ideal for model objects. While data classes provide convenience, they do not guarantee better performance over regular classes. Properties in a data class can be either mutable or immutable depending on the declaration, and data classes can contain methods just like any other class. Thus, automatic method generation is the main benefit.
When handling asynchronous tasks in Kotlin, which keyword is used to define a function that can be paused and resumed?
Explanation: The 'suspend' keyword marks a function as suspendable, allowing it to be paused and resumed within coroutines for asynchronous programming. 'async' is a builder function for starting coroutines but not a keyword. 'pause' and 'defer' are not valid Kotlin keywords for this purpose. Therefore, 'suspend' is required when creating such suspendable functions.
In Kotlin, how do you indicate that a variable can hold a null value, such as for an optional user phone number?
Explanation: Appending a '?' after the type (e.g., String?) in Kotlin indicates that a variable can be null. Using 'nullable' is not the correct syntax. Parentheses have different purposes in Kotlin and do not affect nullability. Assigning 'null' as a default value is possible but does not make the type nullable unless the question mark is used.
What is the primary advantage of using dependency injection in Kotlin backend development?
Explanation: Dependency injection promotes easier testing and maintenance by decoupling components, allowing for easier substitution of dependencies. While it can sometimes affect code structure, it does not directly reduce code for all classes, nor does it prevent all runtime errors. Dependency injection also does not relate to generating database tables.
How do you define a primary constructor for a class in Kotlin, such as initializing a Person with a name?
Explanation: Kotlin defines the primary constructor by placing parameters in parentheses after the class name. Creating a method named 'constructor' is incorrect in Kotlin, and while the 'init' block can be used for initialization logic, it’s not how you declare constructor parameters. Parameters inside curly braces is not valid syntax for a constructor.
Which feature allows you to add new functions to existing classes in Kotlin without modifying their original code?
Explanation: Extension functions provide a way to add methods to existing classes without inheriting from them or altering their code. Data classes are used for model data structures. Type aliases offer alternative type names but do not add functionality. Open classes are related to inheritance, not the addition of functions externally.
Which HTTP method is typically used to create a new resource in a backend API developed with Kotlin?
Explanation: The POST method is conventionally used to create new resources in web APIs. GET is for retrieving data, DELETE is for removing resources, and FETCH is not an HTTP method (despite being a JavaScript function for making requests). Therefore, POST is the correct answer for creation operations.
What is the purpose of the 'when' expression in Kotlin, in a statement such as 'when(val x = checkValue()) { ... }'?
Explanation: The 'when' expression in Kotlin is a more flexible alternative to traditional switch-case statements, allowing for various types of conditional checks. It is not used for declaring constants (which is done with 'val' or 'const'), doesn't start loops, and does not affect coroutine execution directly. Its main purpose is handling conditional logic cleanly.
Which syntax defines a lambda expression in Kotlin, suitable for list filtering such as filtering even numbers?
Explanation: Kotlin lambda expressions use the syntax '{ parameter -u003E expression }', so '{ x -u003E x % 2 == 0 }' is correct. The '[x] =u003E' and '(x) =u003E' formats are from other languages and not valid in Kotlin. 'lambda: x, x % 2 == 0' is also not a recognized Kotlin syntax. Thus, the first option is the accurate lambda expression format.