Kotlin Quiz answers and some statistics

A week ago we announced a small challenge to let Singapore developers test their Kotlin knowledge, learn something new and of course to win some prizes. Congratulations everyone, you all did it awesome!


But special congratulations, of course goes to our winners, who passed the quiz without any mistake and were too lucky to survive our random winner chooser.

Matthew Lee Kay Fei — was the first who answered the quiz without any mistake, so he receives activation key to any JetBrains IDE and promo code for 20% discount on Startup Weekend Singapore!

Next four people answered all questions correctly as well, but were not so fast as Matthew, so they get 20% disount on Startup Weekend Singapore ticket:
- Pham The Anh
- Chew Boon Heng
- Jason Zaman
- Nguyen Van Minh


Guys who answered everything correctly but not in the list of our winners — do not be upset just come to us on our next meetup and claim your Kotlin stickers.

The Answers

Q: What was the first target platform of Kotlin?

A: The first target platform of Kotlin was JVM. You can read it on wikipedia or check the early talks about Kotlin by Andrey Breslav. Kotlin was invented as a new language for a huge, 10 years old Java codebase of JetBrains company. One of the top requirements for the new language was that it should be 100% interoperable with Java (no one wants to support 2 incompatible languages in one project, right?). Thats why JetBrains decided to target JVM first. JavaScript support appeared a bit later, Kotlin/Native is on preview stage even now and iOS has never been a target platform for Kotlin, though Kotlin Native can be run on iOS devices.

Here is the distribution of answers for this question. Almost everyone did it right. Good job, guys!

Q: Why is Kotlin interoperable with Java?

A: Because it compiles to JVM bytecode. You can read about it in the official Kotlin FAQ. Compiling it directly to bytecode gives an opportunity to reach faster compile time and makes no difference between Kotlin and Java for JVM. That is one of the reasons why you can call Kotlin code from Java and Java code from Kotlin and use all your favourite Java libraries and all the benefits of JVM platform.

This questions should be easy, if you answered the first one correctly. Here is the distribution:

Q: What does this code do?


A: Calls a function that is returned by a function foo(). To understand this you should be familiar with the concept of lambda functions. You can read about how to use it in Kotlin in the Kotlin documentation or visit our next meetup on Tuesday, September 19, there will be a talk about it. Here is a code snippet that shows how you can achieve this situation:

It means, that the function foo() will return us the new function of type () -> Int . This type means that returned function has no arguments (empty parentheses) and returns integer (the keyword Int after the arrow). In the body of function foo() you can see that we are returning the curly braces (that is how we declare the lambda function) and inside the braces we have the body of our returning function (last line of lambda function is the result. So in this function we are creating the variable a and returning a*42). The call foo() in this situation will return you the lambda function (that, after call, will return you 42), and then, by using the second pair of parentheses you are calling this returned function. Also this notation foo().invoke() is equal to foo()().

This question was a bit more complicated, so I hope someone just learned a bit more about Kotlin features. Here is the distribution:

Q: What does this code do?

A: Passes lambda function that prints “Hello, world!” as an argument into function bar(). One more question about lamba functions. If you understood nothing from the previous explanation, this one should make things a bit more clear. Here is the full code-snippet for this exmple:

You can see familiar pattern () -> Unit but now it is an argument of function bar. It means that you can pass one function as an argument for another function 😱

I believe the most confusing thing in this question was a lack of parentheses on the bar() call. Kotlin has this handy rule (and you can read about it in Kotlin lambdas documentation) that

In Kotlin, there is a convention that if the last parameter to a function is a function, and you're passing a lambda expression as the corresponding argument, you can specify it outside of parentheses
Stats for 4th question

Q: Can this code be compiled without errors?

1 doUntil 5

A: Yes if doUntil is declared as an extension function for Int. This is possible if with the declaration of doUntil you use infix keyword. More about infix functions you can read in Kotlin functions documentation. Here is a full code-snippet for this example:

Infix functions allow you to call functions without . sign. You can use infix functions only if they are member functions or extension functions and they have a single parameter. In this case, doUntil will be called on 1 (so inside the function this will be 1) and 5 will be an argument (x will be 5).

Seems like not so many people knew about infix functions because it is the only question where number of wrong answers is larger than number of correct answers!

Majority answered that there is no doUntil keyword, which is generally correct, but doesn’t relate to this question

Q: Suppose we have this code in file Sample.kt:

class Sample
fun foo() {}

If you call the function foo() from Java you should write something like this:



A: Because for non-class functions and variables Kotlin generates wrapper class SampleKt and adds these functions and variables as static members of this class. Answer is pretty self-explanatory and you can read about it in Kotlin interoperability documentation. One more thing is worth to say here: in Kotlin, if your class doesn’t contain any methods you can omit curly braces. That is why foo() here is not a method of class Sample. It would be only if foo() located in the curly braces of class Sample.

That’s it!

Thank you everyone for participating and congratulations to our winners. Please comment if you want to see more activities like this in future, share your feedback, we really appreciate it. We can’t wait to see you on our next meetup 19th of September at Google Office!

And, of course, thanks to JetBrains and Startup Weekend to support us with their awesome prizes.