WorksheetsPrelimExam-PlatTech-Ch1-4
Total questions: 70
Worksheet time: 2hrs 45mins
Kotlin is primarily designed to interoperate with which language?
Python
Java
C++
Ruby
Which company developed Kotlin?
Microsoft
JetBrains
Oracle
Which function is used to print output in Kotlin?
printOut()
echo()
println()
display()
What is the output of println(5 + 3)?
8
5 + 3
53
Error
Which symbol denotes a single-line comment in Kotlin?
//
/*
#
What is the correct way to write a multi-line comment in Kotlin?
//
#...#
/.../
Which keyword is used to declare a mutable variable in Kotlin?
let
var
val
const
What will happen if you try to change the value of a val variable?
It will change successfully.
It will throw an error.
It will be ignored.
It will create a new variable.
Which of the following is not a built-in data type in Kotlin?
Int
String
Decimal
Boolean
What is the range of an Int data type in Kotlin?
-2^31 to 2^31-1
-2^63 to 2^63-1
0 to 255
-128 to 127
Which operator is used for string concatenation in Kotlin?
&
+
,
.
What is the result of 5 == 5 in Kotlin?
True
False
1
0
Which of the following correctly creates a string in Kotlin?
String name = "Kotlin"
var name: String = "Kotlin"
name: String = "Kotlin"
name = String("Kotlin")
How can you embed a variable in a string in Kotlin?
Using +
Using $
Using &
Using @
What will the expression true && false evaluate to?
True
False
1
0
Which keyword is used to declare a Boolean variable in Kotlin?
Boolean
Bool
Boolean
bool
What will the following code print: if (5 > 3) println("Yes") else println("No")?
Yes
No
Error
Nothing
Which of the following is a valid if-else syntax in Kotlin?
if x > 0 { println(x) }
if (x > 0) println(x) else println(-x)
if x > 0 then println(x) else println(-x)
if (x > 0): println(x) else: println(-x)
What is the correct usage of the when expression in Kotlin?
when x { 1 -> println("One") }
when (x) { 1 -> println("One") }
when x = 1 { println("One") }
when (x: 1) { println("One") }
What will the following code output if x is 2? when (x) { 1 -> "One" 2 -> "Two" else -> "Unknown" }
"One"
"Two"
"Unknown"
Error
What will the following code output: var i = 0; while (i < 3) { println(i); i++ }?
0 1 2
1 2 3
0 1 2 3
Error
Which of the following is the correct syntax for a while loop in Kotlin?
while (condition) { // code }
while condition { // code }
while { condition // code }
while (condition) // code
What does the break statement do in a loop?
Exits the loop
Skips the current iteration
Stops the program
Restarts the loop
What does the continue statement do in a loop?
Exits the loop
Skips to the next iteration
Stops the program
Restarts the loop
Which keyword is used to define a function in Kotlin?
func
define
fun
method
How do you declare a constant in Kotlin?
const val
let val
val
constant
What will val result = 5 + 3 * 2 evaluate to?
16
11
10
8
Which of the following is not a valid way to declare a string in Kotlin?
val str = "Hello"
val str = 'H'
val str = """Hello"""
val str = String("Hello")
What will the following expression return: if (true) 1 else 0?
0
1
true
false
What is the default value of an uninitialized variable in Kotlin?
0
null
Undefined
Error
Which operator can be used to check for null values in Kotlin?
?
!
&
*
What will val isEven = 4 % 2 == 0 evaluate to?
true
false
0
1
How do you convert a string to an integer in Kotlin?
str.toInt()
Int(str)
str.asInt()
convert(str)
What is the output of println("Hello" + "World")?
Hello World
HelloWorld
Hello+World
Error
Which of the following correctly checks if a variable x is greater than 10?
if x > 10
if (x > 10)
if [x > 10]
if x: 10
Which of the following is the correct syntax to declare an array in Kotlin?
val arr = [1, 2, 3]
val arr = arrayOf(1, 2, 3)
val arr = new Array(3)
val arr = List(1, 2, 3)
What will val num = "123".toInt() return?
"123"
123
Error
0
Which of the following keywords is used for type inference in Kotlin?
var
val
let
all of the above
What is the output of println(5 == 5 && 5 != 6)?
True
False
1
0
Which keyword is used to define a nullable type in Kotlin?
?
!
*
+
What does the !! operator do in Kotlin?
Converts to a non-null type
Converts to a nullable type
Throws an exception if null
Both A and C
Which of the following is a valid way to define a lambda function in Kotlin?
val sum = { a: Int, b: Int -> a + b }
val sum: (Int, Int) -> Int = { a, b -> a + b }
Both A and B
None of the above
How do you handle exceptions in Kotlin?
try-catch
catch-try
handle
error
What is the output of println(1.0 + 2)?
3
3.0
Error
3.00
Which of the following is not a valid way to declare a variable?
var x = 5
val y: Int = 10
let z = 15
const w = 20
What is the syntax for a basic while loop in Kotlin?
while condition: { ... }
while (condition) { ... }
while { condition } { ... }
loop while (condition) { ... }
What happens if the condition in a while loop is never false?
The loop will never execute.
The loop will execute only once.
The loop will run infinitely.
An error will occur.
What is the output of the following code?
var x = 0
while (x < 3) {
print(x)
x++
}
123
012
321
0123
How can you terminate a while loop prematurely?
Using exit.
Using stop.
Using terminate.
Using break.
What will happen if the condition in a while loop is initially false?
The loop will execute once.
The loop will execute twice.
The loop will not execute at all.
An error will be thrown.
Which of the following is true about the while loop?
It is used for iterating over arrays.
It is used when the number of iterations is known beforehand.
It is used when the number of iterations is unknown and depends on a condition.
It can only be used for numerical operations.
What is the output of the following code?
var n = 5
while (n > 0) {
n--
}
print(n)
4
0
1
5
How do you skip the current iteration of a while loop and proceed to the next one?
(a)
Which of the following loops is guaranteed to run at least once?
while loop
do-while loop
for loop
foreach loop
What is the purpose of the following code snippet?
var count = 0
while (true) {
println(count)
count++
if (count > 5) break
}
Prints numbers from 0 to 6.
Prints numbers from 1 to 5.
Prints numbers from 0 to 5.
Prints numbers from 1 to 6.
What is the shorthand if-else syntax in Kotlin?
condition ? expression1 : expression2
if (condition) expression1 else expression2
condition if expression1 else expression2
condition ? expression1 else expression2
Which of the following statements is true about shorthand if-else in Kotlin?
It must be used with the ? operator.
It can only be used with numeric expressions.
It always returns a value.
It cannot be used in variable assignments.
What is the output of the following code?
val result = if (3 > 2) "True" else "False"
println(result)
True
False
3 > 2
An error will occur.
Which of the following is the correct way to assign a value using shorthand if-else?
val x = 5 > 3 ? "Yes" : "No"
val x = if (5 > 3) "Yes" else "No"
val x = 5 > 3 if "Yes" else "No"
val x = (5 > 3) ? "Yes" : "No"
Which of the following can be used as a return value from a function using shorthand if-else?
Only String values.
Only Int values.
Any type of value.
Only Boolean values
What is the output of the following code?
val age = 18
val message = if (age >= 18) "Adult" else "Minor"
println(message)
12
Adult
Minor
An error will occur.
What is the output of the following code?
val x = 5
val y = 10
val max = if (x > y) x else y
println(max)
5
10
0
An error will occur.
How can you simplify this code using shorthand if-else?
val number = -5
val status = if (number >= 0) "Positive" else "Negative"
val status = number >= 0 ? "Positive" : "Negative"
val status = number if >= 0 "Positive" else "Negative"
val status = if (number >= 0) "Positive" else "Negative"
val status = number >= 0 then "Positive" else "Negative"
What is the result of the following code?
fun checkNumber(num: Int) = if (num > 0) "Positive" else "Negative"
val result = checkNumber(-10)
println(result)
Positive
Negative
0
An error will occur.
Which of the following is the correct syntax for a basic when expression in Kotlin?
when (expression) { ... }
when { expression -> ... }
when: expression { ... }
switch (expression) { ... }
What is the output of the following code?
val day = 3
val result = when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
else -> "Invalid day"
}
println(result)
Monday
Tuesday
Wednesday
Invalid day
Which of the following is true about the else branch in a when expression?
It is mandatory.
It is used as a default case.
It must be the first branch.
It can only be used with numeric types.
What is the output of the following code?
val x = 10
val result = when {
x < 5 -> "Low"
x in 5..15 -> "Medium"
else -> "High"
}
println(result)
Low
Medium
High
An error will occur.
What is the output of the following code?
val result = when ("Hello") {
"Hi" -> "Greeting"
"Bye" -> "Farewell"
"Hello" -> "Salutation"
else -> "Unknown"
}
println(result)
Greeting
Farewell
Salutation
Unknown
Which of the following statements is true about the when expression in Kotlin?
It can only be used with numbers.
It can be used as a statement or as an expression.
It always requires an else branch.
It cannot return a value.
