Kotlin Quiz - Multiple Choice Questions (MCQ)

Welcome to the Kotlin quiz. In this Kotlin quiz, we've prepared 40+ multiple-choice questions to challenge your understanding of Kotlin concepts. Each question comes with detailed explanations to help you learn and solidify your knowledge. Let's dive in and see how well you know your Kotlin!

1. What is Kotlin?

a) A new version of Java.
b) A JavaScript framework.
c) A statically-typed programming language for the JVM, Android, and browser.
d) A database management system.

2. Which platform does Kotlin primarily target?

a) Python Bytecode
b) JavaScript
c) JVM (Java Virtual Machine) Bytecode
d) PHP

3. Are semicolons (;) mandatory at the end of code statements in Kotlin?

a) True
b) False
c)
d)

4. What paradigm(s) does the Kotlin programming language follow?

a) Only Object-Oriented
b) Procedural
c) Only Functional
d) Both Object-Oriented and Functional

5. How do you declare a variable in Kotlin?

a) let myVariable = 10;
b) val myVariable: Int = 10
c) const myVariable = 10;
d) var myVariable: Int = 10

6. How do you define a variable in Kotlin that cannot be reassigned?

a) var
b) val
c) const
d) final

7. How do you declare a nullable variable in Kotlin?

a) var name: String?
b) var name: String
c) var name: String = null
d) String name = null

8. What is the difference between val and var in Kotlin?

a) They are identical and can be used interchangeably.
b) 'val' declares mutable variables, and 'var' declares immutable ones.
c) 'val' declares immutable variables, and 'var' declares mutable ones.
d) 'val' is used for local variables, and 'var' is used for global variables.

9. How do you define a function in Kotlin?

a) fun myFunction() {}
b) def myFunction() {}
c) function myFunction() {}
d) fun = myFunction() {}

10. What is the when expression used in Kotlin?

a) To create a loop.
b) To define a switch-case statement.
c) To define a conditional expression.
d) To create a lambda function.

11. What does ?. operator do in Kotlin?

a) Null-safe type casting
b) Null-safe function calling
c) Null-safe member access
d) None of the above

12. What is the default visibility modifier for functions in Kotlin if no modifier is specified?

a) public
b) private
c) internal
d) protected

13. Which keyword is used to create a singleton in Kotlin?

a) static
b) singleton
c) single
d) object

14. What is the main purpose of the let function in Kotlin?

a) To facilitate null checks
b) To execute a block of code and return a result
c) To transform an object
d) None of the above

15. Which feature in Kotlin helps to prevent NullPointerExceptions?

a) Safe Call Operator (?.)
b) Non-null Assertion Operator (!!)
c) Elvis Operator (?:)
d) Safe Cast Operator (as?)

16. What is the purpose of the open modifier in Kotlin?

a) It allows a class to be instantiated.
b) It makes a function available for overriding.
c) It enforces strict typing for variables.
d) It allows a function to be called only from within its own class.

17. How do you create a single-line comment in Kotlin?

a) // This is a comment
b) /* This is a comment */
c)
d) # This is a comment

18. How can we write a multi-line comment in Kotlin?

a) /* Comment */
b) // Comment
c)
d) # Comment

19. What is the role of the init block in Kotlin?

a) To initialize the superclass
b) To initialize an object after the constructor has been called
c) To initialize static variables
d) None of the above

20. How do you call a function in Kotlin?

a) functionName()
b) call functionName
c) Function -> functionName
d) functionName:call

21. Which Kotlin construct allows a block of code to be executed a specific number of times?

a) for
b) while
c) repeat
d) loop

22. Which keywords are used to handle conditional statements in Kotlin?

a) if
b) when
c) both a and b
d) none of the above

23. What is the correct way to define a primary constructor in Kotlin?

a) constructor()
b) class constructor()
c) primary constructor()
d) class Person()

24. In Kotlin, what is the main purpose of the return keyword?

a) To declare a function
b) To create a loop
c) To terminate a function execution and return a value
d) To define a class

25. What is the primary use of the with function in Kotlin?

a) To create an extension function
b) To apply multiple transformations to a collection
c) To establish a scope in which an object's properties and functions can be accessed directly without specifying the object's name
d) To create an anonymous function

26. What does the also function do in Kotlin?

a) It's a scoping function that also executes a block of code
b) It runs a block of code and returns the object it was called on
c) It runs a block of code and returns the result
d) None of the above

27. Which of these is not a loop structure in Kotlin?

a) for loop
b) while loop
c) until loop
d) do-while loop

28. How do we throw an exception in Kotlin?

a) throw Exception()
b) raise Exception()
c) Exception.throw()
d) None of the above

29. What methods can be used to achieve abstraction in Kotlin?

a) Through abstract classes only
b) Through interfaces only
c) Through both abstract classes and interfaces
d) None of the above

30. How do you declare an array of integers in Kotlin?

a) val numbers = arrayOf(1, 2, 3)
b) val numbers = listOf(1, 2, 3)
c) val numbers = [1, 2, 3]
d) val numbers = Array(3) {0, 1, 2}

31. Which of the following is not a basic data type in Kotlin?

a) Boolean
b) String
c) Float
d) Char

32. How do you declare a String in Kotlin?

a) val str: String = "Hello, World!"
b) String str = "Hello, World!"
c) val str = String("Hello, World!")
d) String str = new String("Hello, World!")

33. In Kotlin, how do you compare two Strings for equality?

a) str1 == str2
b) str1.equals(str2)
c) Both a and b
d) str1.sameAs(str2)

34. Which property can be used to find the length of a string?

a) size
b) length
c) count
d) charCount

35. In Kotlin, which collection type has an order and can contain duplicate elements?

a) Set
b) List
c) Map
d) All of the above

36. Which function is used to iterate over a collection in Kotlin?

a) forEach()
b) for()
c) map()
d) filter()

37. Which of these functions can transform a list in Kotlin?

a) map()
b) filter()
c) forEach()
d) None of the above

38. Which collection type ensures element uniqueness in Kotlin?

a) List
b) Set
c) Map
d) MutableList

39. How do you create an empty list in Kotlin?

a) emptyList()
b) listOf()
c) list()
d) mutableListOf()

40. What does the mapOf() function do in Kotlin?

a) It creates a new List.
b) It creates a new Set.
c) It creates a new Map.
d) It creates a new Queue.

Conclusion

Congratulations on completing the Kotlin quiz! We hope you enjoyed the challenge and learned some new Kotlin concepts along the way. Kotlin is a powerful and expressive language that brings a lot of modern features to the table. Keep practicing and experimenting to become a Kotlin master! Happy coding!

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare