var x = 12
What is the data type for the variable 'x'? Answer must be in the correct Kotlin syntax.
Int
What keyword do we use to declare a function?
fun
What are the 3 main loop operations?
for, while, and do-while
What is the difference between the keyword "var" and "val"?
"var" means the variable is mutable.
"val" means the variable is immutable.
fun greeting(name: String, age: Int): String {
return "Hello my name is $name and I am ${age} years old"
}
print(greeting("Bently", 19))
What is the output of the print statement?
Hello my name is Bently and I am 19 years old
What is the equivalent of "switch" statement in Kotlin?
when
How do you declare a mutable array named "words" with the elements ["Android", "Adventure", "Quest"]
var words = arrayOf("Android", "Adventure", "Quest")
If a function does not have a "return" statement, does it mean it returns "null"?
No, it will return "Unit" instead.
If you want to return "null", you must explicitly state it as "return null".
var b = true
if (b) {
print("Google")
} else {
print("Apple")
}
What is the output of the above code?
fun main() {
var result = 5 / 2
println(result)
}
What is the output of the code above?
a. 2.5
b. 2.0
c. 2
d. 5/2
e. 0.1
f. Error
g. * console does not output anything *
h. none of the above. Your answer: _____
C. 2
Because division in kotlin results in integer, the numbers before the (.) are omitted, leaving the one in front of it. Unless at least one of the number (either numerator or denominator) is a decimal, e.g. 5.0/2 or 5/2.0 or 5.0/2.0
What is the return type of this function?
fun greet(name: String) {
println("Hi $name")
}
Unit
Which of the following is the correct syntax to declare a range for a for loop in Kotlin? There can be more than one answer.
A. for (i=0; i<5; i++)
B. for (i in 0..5)
C. for (i in 0 to 4)
D. for (i in 0 until 4)
B and D
B. for (i in 0..5)
D. for (i in 0 until 4)
val num: Int = 1
val str: String = num + "abc"
println(str)
What is the output of this code?
error, because numbers should be at the back side in order to concatenate to a string.
val str: String = "abc" + 2
OR
val str:String = num.toString + "abc
Create a function that adds the sum of two integer numbers and return the result.
fun add(num1: Int, num2: Int): Int {
return num1 + num2
}
What is the difference between conditional statement and conditional expression?
Conditional expression can be assigned to a variable. The expression however should have an else after the if statement block.
e.g.
Statement:
if () {
} else {}
Expression:
var a = if () {
} else {}