COURSE 3 – PROGRAMMING FUNDAMENTALS IN KOTLIN QUIZ ANSWERS
Week 1: Introduction to Programming in Kotlin
Meta Android Developer Professional Certificate
Complete Coursera Answers & Study Guide
Enroll in Coursera Meta Android Developer Professional Certification
TABLE OF CONTENT
- INTRODUCTION
- PRACTICE QUIZ: VARIABLES, VALUES AND TYPES
- PRACTICE QUIZ: SELF-REVIEW: PRACTICE USING MATH OPERATIONS IN KOTLIN
- PRACTICE QUIZ: KNOWLEDGE CHECK: STRING
- PRACTICE QUIZ: KNOWLEDGE CHECK: BOOLEAN VALUES AND LOGICAL OPERATIONS
- PRACTICE QUIZ: KNOWLEDGE CHECK: CONDITIONAL STATEMENTS
- PRACTICE QUIZ: KNOWLEDGE CHECK: WHEN CONDITIONAL STATEMENT
- PRACTICE QUIZ: SELF-REVIEW: PRACTICE USING CONDITIONS
- PRACTICE QUIZ: KNOWLEDGE CHECK: WHILE STATEMENT
- PRACTICE QUIZ: KNOWLEDGE CHECK: LOOPS
- PRACTICE QUIZ: SELF REVIEW: PRACTICE CREATING LOOPS
- QUIZ: MODULE QUIZ: INTRODUCTION TO PROGRAMMING IN KOTLIN
- CONCLUSION
Introduction to Programming in Kotlin INTRODUCTION
In this module, you will start with an overview of programming in Kotlin. Once you are more familiar with the careers and usages of Kotlin, you will move on to learn about programming in Kotlin, discovering and exploring the fundamental concepts that underpin the Kotlin programming language.
Learning Objectives
- Describe basic types and variables
- Explain numbers in Kotlin
- Demonstrate the use of different types of operators
- Explain what conditions are
- Describe how to use loops
PRACTICE QUIZ: VARIABLES, VALUES AND TYPES
1. In the following code, if the variable str never changes its value, what should fill the gap marked as ___?
fun main() {
___ str = "ABC"
println(str)
}
- val (CORRECT)
- var
- let
- String
That’s correct! You define a read-only variable using the val keyword.
2. In the following code, if the variable str changes value within the function, what should fill the gap marked as ___?
fun main() {
___ str = "ABC"
str = "XYZ"
println(str)
}
- var (CORRECT)
- let
- string
- val
That’s correct! You can define a read-write variable using the var keyword.
3. What value type is “Hello, World”?
- Int
- Boolean
- String (CORRECT)
That’s correct! The value of “Hello, World” is of String type.
4. What is the data type of the value true?
- String
- Boolean (CORRECT)
- Int
That’s correct! The values of ‘true’ and ‘false’ are of the Boolean data type.
5.What is the data type of the value 10.0?
- Boolean (w)
- Double (CORRECT)
- Int
That’s correct! Number values with decimal points are of the Double data type.
6. Which of the following is of the type ‘Any’? Select all that apply.
- Int (CORRECT)
- String (CORRECT)
- Double (CORRECT)
- Boolean (CORRECT)
That’s correct! Int is a subtype of ‘Any’.
That’s correct! String is a subtype of ‘Any’.
That’s correct! Double is a subtype of ‘Any’.
That’s correct! Boolean is a subtype of ‘Any’.
7. Which of the following operations can be done on the var variable of type String? Select all that apply.
var str = "ABC"
- str = “123” (CORRECT)
- str = “ABC” (CORRECT)
- str = 123
That’s right. “123” is of type String, so you can assign it to a variable of type String.
That’s right. “ABC” is of type String, so you can assign it to a variable of type String.
8. Which of the following operations can be done on line 2 of the following code?
val i = 123
//Code here
- print(i) (CORRECT)
- i = “123”
- i = 123
That’s correct! You can print a variable value.
9. You want to represent the weight of a customer. Which value type will you use?
- Double (CORRECT)
- String
- Boolean
That is correct! If you have numbers with a decimal part, it is called a “Double”.
10. Is this code incorrect?
var a = "A"
a = 5
- Yes (CORRECT)
- No
Correct! You will receive an error.
11. True or False
Kotlin is a high-level programming language. This means that it must be converted to binary code before a CPU can work with it.
- True (CORRECT)
- False
Yes, that’s correct! Kotlin does need to be converted to binary code so that a CPU can work with it.
PRACTICE QUIZ: SELF-REVIEW: PRACTICE USING MATH OPERATIONS IN KOTLIN
1. What is the output of the following code?
fun main() {
println(50+2*3)
}
- 56 (CORRECT)
- 55
- 156
Correct! You multiply 2 by 3 using the multiplication operator, and then you add 50 to the outcome. Remember that multiplication has higher precedence than addition.
2. What is the output of the following code?
println(150+2+100*2)
- 352 (CORRECT)
- 504
- 254
Correct! Using the multiplication operator, you multiply 100 by 2 and then add 150 and 2 to the result.
3. What is the output of the following code?
fun main() {
println(150+(2+100)*2)
}
- 354 (CORRECT)
- 352
- 254
Correct. You start by resolving the parentheses. In other words, 2 and 100 are added first, multiplied by 2 afterward, and the resulting value is then added to 150.
4. What is the result of the following:
fun main() {
val x = 5
val y = 3
val z = 8
println(x+y*z)
}
- 16
- 29 (CORRECT)
- 64
Correct! You multiply 3 and 8 first and then add the result 24 to 5.
5. You want to store a decimal number such as 3.14. Which of the following number types is used to declare the variable in the code below?
1 val pi = 3.14
- Long
- Float
- Double (CORRECT)
That’s correct! You will use Double number type to define PI to store a value of 3.14
PRACTICE QUIZ: KNOWLEDGE CHECK: STRING
1. What is the result of the following code?
fun main() {
val num = 10
println("Result is $num")
}
- $num
- num
- Result is 10 (CORRECT)
Correct! By using the $ symbol and a variable name, you inline the value of this variable.
2. What is the result of the following code?
fun main() {
val num = 10 + 20
println("Result is $num")
}
- Result is 30 (CORRECT)
- $num
- 10 + 20
Correct! The value of num is 30.
3. What is the result of the following code?
fun main() {
val num = 10
println("Result is ${num + 20}")
}
- Result is 30 (CORRECT)
- Result is 10 + 20
- ${num + 20}
Correct! By using $ and braces, you inline the result of expression inside this bracket, which might be 10 + 20.
4. What is the result of the following code?
fun main() {
val num = 10
println("Result is $num + 20")
}
- $num + 20
- Result is 10 + 20 (CORRECT)
- 30
Correct! When you use $ without bracket, you only inline a variable value, not the result of an expression.
5. What is the result of the code below?
fun main() {
val s = "Hello world"
println(s[0])
}
- H (CORRECT)
- Hello
- D
That’s correct. The index zero points to the first character in the string sequence.
6. You would like to inline a single variable value into a string value. Which one of the following do you use?
- Dollar sign (CORRECT)
- ‘f’ character
- Dollar sign and braces
That’s correct! You will use the dollar sign followed by the variable name.
PRACTICE QUIZ: KNOWLEDGE CHECK: BOOLEAN VALUES AND LOGICAL OPERATIONS
1. What is the result of the following code?
fun main() {
print(11 > 10)
print(11 <= 10)
print(10 >= 11)
print(10 < 11)
}
- falsefalsefalsefalse
- truetruetruetrue
- truefalsefalsetrue (CORRECT)
Correct! Operator > returns true when the value on its left side is greater. Operator < returns true when the value on its right side is greater.
2. What is the result of the following code?
fun main() {
print(10 > 10)
print(10 >= 10)
print(10 <= 10)
}
- Falsetruetrue (CORRECT)
- falsefalsefalse
- truetruetrue
- truefalsefalse
Correct! Both >= and <= return true when there is the same value on both sides.
3. What is the result of the following code?
fun main() {
print("A" == "A")
print("A" == "B")
print("A" == "AB")
}
- falsefalsefalse
- truefalsefalse (CORRECT)
- truetruetrue
- truefalsetrue
Correct! Operator == returns true when there is the same value on both sides.
4. What is the result of the following code?
fun main() {
print(true && true)
print(true && false)
print(false && true)
print(false && false)
}
- Truefalsefalsefalse (CORRECT)
- falsetruetruefalse
- truefalsefalsetrue
- truetruetruefalse
Correct! Operator && returns true only when it is true on both sides.
5. What is the result of the following code?
fun main() {
print(true || true)
print(true || false)
print(false || true)
print(false || false)
}
- truefalsefalsetrue
- falsetruetruefalse
- truefalsefalsefalse
- truetruetruefalse (CORRECT)
Correct! Operator || returns true when it is true on any of its sides.
6. What is the result of the following code?
fun main() {
print(true)
print(!true)
print(!!true)
print(!!!true)
}
- truetruetruetrue
- truefalsetruefalse (CORRECT)
- truefalsefalsefalse
Correct! Operator ! changes true to false and false to true. It can be used multiple times.
7. What is the result of the following code?
fun main() {
val finishedHomework = true
val cleanedRoom = true
val passedMathExam = false
val canPlayGames = finishedHomework && cleanedRoom
val canEatSweets = finishedHomework && passedMathExam
print(canPlayGames)
print(canEatSweets)
}
- falsetrue
- truetrue
- truefalse (CORRECT)
- falsefalse
PRACTICE QUIZ: KNOWLEDGE CHECK: CONDITIONAL STATEMENTS
1. What is the result of the below code?
fun main() {
val hour = 9
val userName = "Alex"
if (hour <= 10) {
print("Good morning, ")
} else if (hour >= 20) {
print("Good evening, ")
} else {
print("Hello, ")
}
if (userName == "admin") {
println("what would you like to do?")
} else {
println("how can I help you, $userName?")
}
}
- Hello, how can I help you, Alex?
- Good morning, what would you like to do?
- Good evening, what would you like to do?
- Hello, what would you like to do?
- Good morning, how can I help you, Alex? (CORRECT)
Correct! The result is: Good morning, how can I help you, Alex?
2. What is the result of the below code?
fun main() {
val hour = 11
val userName = "admin"
if (hour <= 10) {
print("Good morning, ")
} else if (hour >= 20) {
print("Good evening, ")
} else {
print("Hello, ")
}
if (userName == "admin") {
println("what would you like to do?")
} else {
println("how can I help you, $userName?")
}
}
- Good evening, what would you like to do?
- Hello, what would you like to do? (CORRECT)
- Good morning, how can I help you, admin?
- Hello, how can I help you, Alex?
- Good morning, how can I help you, Alex?
3. What is the result of the below code?
fun main() {
val password = "ABC"
val error = if (password.length < 7) "Password is too short." else "Password is ok."
print(error)
}
- Password is too short. (CORRECT)
- Error
- Password is ok.
Correct! The answer is Password is too short.
4. Yes or No: The If statement is used to specify a block of code to be executed if a condition is true.
- Yes (CORRECT)
- No
That’s correct The ‘If’ statement is used to specify a block of code to be executed if a condition is true.
5. The output of the code below is Good evening.
fun main() {
val time = 20
val greeting =
if (time < 18) {
"Good day."
} else {
"Good evening."
}
println(greeting)
}
- False
- True (CORRECT)
That’s correct. The time variable is set to 20 which is greater than 18 when the greeting variable is evaluated by the ‘if’ on line 4. Therefore, the output will be: Good evening.
6. Every bodyline in Kotlin starts with four additional spaces called ‘formatting’.
- Yes
- No (CORRECT)
That’s right, the spacing is called the indent, the term for this is formatting.
PRACTICE QUIZ: KNOWLEDGE CHECK: WHEN CONDITIONAL STATEMENT
1. What is the result of the below code?
fun main() {
val value = 0
when {
value > 0 -> println("Positive")
value < 0 -> println("Negative")
else -> println("Other")
}
}
- Positive
- Other (CORRECT)
- Negative
Perfect! The value 0 is neither greater than 0 nor smaller than 0.
2. What is the result of the below code?
fun main() {
val dogType = "Border Collie"
val expectedWeight =
when (dogType) {
"Labrador Retriever" -> "25 - 36"
"Fox Terrier" -> "7 - 8"
"Border Collie" -> "12 - 20"
"Foxhound" -> "31 - 32"
else -> "(unknown)"
}
println("The weight of $dogType should be $expectedWeight kg")
}
- The weight of Fox Terrier should be 7 – 8 kg
- The weight of Border Collie should be (unknown) kg
- The weight of Border Collie should be 12 – 20 kg (CORRECT)
Correct! The weight of Border Collie should be between 12 and 20 kg
3. What is the result of the below code?
fun main() {
val name = "Rex"
val age = 3
val status =
when (age) {
1 -> "puppy"
in 2..10 -> "dog"
else -> "older dog"
}
println("$name is $status")
}
- Rex is puppy
- Rex is dog (CORRECT)
- Rex is older dog
Perfect! The age is in range from 2 to 10, so the second branch is chosen.
4. What is missing in the place of ___ ?
val number = 123
val text =
when {
number > 0 -> "Positive"
number < 0 -> "Negative"
____ -> "Zero"
}
- true
- else (CORRECT)
- false
If you use when as an expression, you need to provide the else branch.
5. What is the output of the code below?
fun main() {
val i = 16
when {
i > 0 -> print("Number is positive.")
i == 0 -> print("Number is zero.")
i < 0 -> print("Number is negative.")
}
}
- Number is positive. (CORRECT)
- Number is zero.
- Number is negative.
That’s correct. I is greater than zero, so the output is “Number is positive.”
6. You want to create a “when” conditional statement for the costs of items on your website. You then add a parenthesis with a value directly after the “when” keyword and then branches for possible values instead of conditions. Is this correct?
- Yes (CORRECT)
- No
That is correct! You will use branches instead of conditions.
PRACTICE QUIZ: SELF-REVIEW: PRACTICE USING CONDITIONS
1. The code below will compile.
if(a = 10) {
print("Welcome to if")
} else {
print("Welcome to else")
}
- False (CORRECT)
- True
Correct! There is a typing error in the condition, it should be a == 10
2. What is the output of the following code when a = 21
if (a < 20)
if(a > 10)
print("Hi $a")
else if(a < 10)
print ("Bonjour $a")
else
print ("Welcome $a")
- Hi 21
- Welcome 21
- Noting will be printed on the screen. (CORRECT)
Correct. Since the a is larger than 20
3. Which statement is used to check if Little Lemon is currently open or closed?
- The ‘if-else’ statement (CORRECT)
- The ‘if-else-if’ statement
- The ‘when’ statement
Correct! The if statement was used to compare if a weekday is equal to a string value.
PRACTICE QUIZ: KNOWLEDGE CHECK: WHILE STATEMENT
1. What are the differences between the if condition and the while loop? Select all that apply.
- If condition will never call its body more than once. (CORRECT)
- A while loop will run once if its condition is false.
- While loop can call its body more than once. (CORERCT)
Correct! The if condition will never call its body more than once.
Correct! While loop can call its body more than once.
2. What will be printed by the below code?
fun main() {
var toBePrinted = true
while (toBePrinted) {
print("A")
toBePrinted = false
}
}
- AA
- (nothing)
- AAA
- A (CORRECT)
Correct! This code prints A only once as the predicate changed to false in the statement “toBePrinted = false”.
3. What sequence of numbers will be printed by the following code?
fun main() {
var i = 12
while (i < 100) {
print("$i ")
i = i + 13
}
}
- 12
- 12 25 38 51 64 77 90 (CORRECT)
- 0 13 26 39 52 65 78 91
- 12 25
Correct! A sequence of numbers starting from 12, with a step 13
4. What sequence of numbers will be printed by the following code?
fun main() {
var i = 8
while (i < 100) {
println("$i ")
i = i * 3
}
}
- 8 24 72 (CORRECT)
- 8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53 56 59 62 65 68 71 74 77 80 83 86 89 92 95 98
- 3 24
Correct! A sequence of numbers starting from 12
5. True or False: The while loop loops through a block of code as long as a specified condition is false.
- True
- False (CORRECT)
That’s correct. The while loop loops through a block of code as long as a specified condition is true.
6. What is the output of the code below?
fun main{
var i = 0
while (i < 5) {
println(i)
i++
}
}
- 0 1 2 3 4 (CORRECT)
- 1 2 3 4 5
That’s correct. The code in the loop will run, over and over again, as long as the counter variable (i) is less than 5.
7. The while loop can be thought of as repeating if statements.
- Yes (CORRECT)
- No
That’s correct! The while loop consists of a block of code and a condition. This then repeats until the condition becomes false.
PRACTICE QUIZ: KNOWLEDGE CHECK: LOOPS
1. What is missing in the following code if it needs to print 12345?
fun main() {
for (i __ 1..5) {
print(i)
}
}
- in (CORERCT)
- for
Correct! In is missing.
2. What is missing in this code, if it needs to print 1234?
fun main() {
for (i in 1__5) {
print(i)
}
}
- until (CORRECT)
- downTo
Correct! You use until to make a range from the left value to the right one, but without the right one.
3. What is missing in the below code, if it needs to print 54321?
fun main() {
for (i in 5__1) {
print(i)
}
}
- downTo (CORRECT)
- ..until
Correct! To iterate from higher numbers to lower, you use downTo.
4. What is the result of the following code?
fun main() {
for (i in 1..4) {
for (j in 1..i) {
print("*")
}
println()
}
}
- *
**
***
****
(CORRECT) - ****
***
**
*
(CORRECT)
5. What is the output of the code below?
fun main() {
for (i in 1..5)
print(i)
}
- null
- 1234
- 12345 (CORRECT)
That’s correct. The for-loop iteration starts from 1 and ends at 5.
6. A for loop iterates through anything that contains a countable number of values.
- Yes (CORRECT)
- No
That’s correct! A for loop iterates through anything that provides that contains a countable number of values.
7. You need to print a square of stars of size five by five. Which one of the following is responsible for printing the row?
- Outer loop (CORRECT)
- Inner loop
- Variable
That’s correct! The outer loop is responsible for printing the row.
PRACTICE QUIZ: SELF REVIEW: PRACTICE CREATING LOOPS
1. In Task 1 of the exercise, you were prompted to make a note of the code output. Which for in creates each line in the output of Task 1?
- for(i in 1..5) (CORRECT)
- for (i in 5….5) {
- for (i in 2….5) {
Correct! The `for in’ loop in Step 1 of Task 1 iterates over the “1..5” range
2. Which code snippet allows you to determine the number of stars per line for Task 1?
- val numberOfSpaces = i – 1
- print(“*”)
- val numberOfStars = 6 – i (CORRECT)
Correct! The first line is expected to have 5 stars, so numberOfStars= 6 -i and the intial value of ‘i” is 1.
3. In Task 2 of the exercise, you were prompted to make a note of the code output. Check the pattern created by Task 2. How many for in loops were used to achieve the triangle shape output?
- 3 (CORRECT)
- 5
- 0
Correct! Three loops were used to build the triangle shape.
Coursera Meta Android Developer Professional Certificate Answers and Study Guide
Liking our content? Then don’t forget to ad us to your bookmarks so you can find us easily!
Weekly Breakdown | Meta Study Guides | Back to Top
QUIZ: MODULE QUIZ: INTRODUCTION TO PROGRAMMING IN KOTLIN
1. What is the result of the below code?
fun main() {
val value = true
println("$value || false")
}
- true
- false
- true || false (CORRECT)
Correct! When you use $ without bracket, you only inline a variable value, not the result of an expression.
2. What is the result of the below code?
fun main() {
println(true && false)
println (true || false)
}
- True
True - false
false - False
True
(Correct) - true
False
Correct! && gives true only when both sides are true. || gives true if any of its sides is true.
3. What is the result of the below code?
fun main() { val dogType = "Foxhound"
val expectedWeight =
when (dogType) {
"Labrador Retriever" -> "25 - 36"
"Fox Terrier" -> "7 - 8"
"Border Collie" -> "12 - 20"
"Foxhound" -> "31 - 32"
else -> "(unknown)"
}
println("The weight of $dogType should be $expectedWeight kg")
}
- The weight of Fox Terrier should be 7 – 8 kg
- The weight of Foxhound should be 31 – 32 kg (CORRECT)
- The weight of Foxhound should be (unknown) kg
- The weight of Border Collie should be 12 – 20 kg
4. What is the output of the code below?
fun main() {
val i = 1
if (i > 8) {
println("Michael")
} else {
println("Tonia")
}
}
- Tonia (CORRECT)
- Michael
Correct. Prints Tonia since i is equals to 1 and less than 8.
5. What will happen to the while statement in the code below?
fun main() {
while (true) {
println("Hello world!")
}
}
- Prints forever (CORRECT)
- Prints once
- Prints Intermittently
Correct. You should not use while conditions with a predicate that returns true. Such a code will run forever, unless you stop it.
6. What is the output of the code below?
fun main() {
val i = 1
if (i < 3) {
println("Smaller")
} else {
println("Bigger")
}
}
- Smaller (CORRECT)
- Bigger
Correct. Prints Smaller since i is equal to 1 and lesser than 3.
7. The data type of the variable c in the code below is
val c = 40.0
- Float
- Decimal
- Double (CORRECT)
Correct. Double is the default decimal number representation.
8. The data type of the variable b in the code below is
val b = 15L
- float
- long (CORRECT)
- decimal
Correct. Long is an integer representation supporting larger numbers. You create it using ‘L.’
9. The Int data type is used to store ___________________numbers.
- decimal
- short
- whole (CORRECT)
Correct. You use the int data type to store whole numbers.
Interactive CSS CONCLUSION
To be written
Subscribe to our site
Get new content delivered directly to your inbox.
Quiztudy Top Courses
Popular in Coursera
- Meta Marketing Analytics Professional Certificate.
- Google Digital Marketing & E-commerce Professional Certificate.
- Google UX Design Professional Certificate.
- Meta Social Media Marketing Professional Certificate
- Google Project Management Professional Certificate
- Meta Front-End Developer Professional Certificate
Liking our content? Then, don’t forget to ad us to your BOOKMARKS so you can find us easily!