COURSE 3 – PROGRAMMING FUNDAMENTALS IN KOTLIN

Week 4:  Graded Assessment 

Meta Android Developer Professional Certificate

Complete Coursera Answers & Study Guide

Enroll in Coursera Meta Android Developer Professional Certification

Graded Assessment INTRODUCTION

In this module, you’ll learn about the graded assessment. After you complete the units in this module, you’ll synthesize the skills you gained from the course to create code for the “Bank Account” project.

Learning Objectives

  • Apply the skills and knowledge from this course on Programming Fundamentals in Kotlin in a practical assessment

SELF REVIEW: BANK ACCOUNT PROJECT

In the ​bank account application project​,​ you used Kotlin code including functions, loops, conditional statements and ‘when’ expressions to create the functionality of the bank account application that was able to:
1. Create bank accounts  
2. Validate user input  
3. Check account balances  
4. Process deposits and withdrawals   

Part 1 
In ​part 1 of the project you built the visual user interface (UI) and set up accounts​.   You had an opening screen to allow the selection of an account type. 

1. You were prompted to make a note of the code output.

  • What was the sixth printed line of the output?
  • Credit account  
  • Checking account
  • Which option do you choose? (1, 2 or 3)  (CORRECT)

Correct! This is the output of the first print statement in the loop you created.   

2. What type of loop did you use to ensure that a valid bank account type (debit, credit or checking) was selected and used to create a bank account?   

  • Do while loop
  • For loop   
  • While loop (CORRECT)

Correct!  The while loop checks if its condition is true and then starts to execute its body.   

3. Why do you need an else case inside a when expression?   

  • To evaluate the when expression once again​.
  • To exit the when​ expression.
  • To make the when expression exhaustive.  (CORRECT)

C​orrect! When​ expressions must always handle all cases, and hence the else case is executed.

Part 2
In part 2 of the project you defined the bank account operations​.   

4. What type of conditional statement did you use to implement the debit withdrawal and credit deposit operations?   

  • If-else-if statement  (CORRECT)
  • If-else statement   
  • If statement  

Correct! You need chained conditions to implement both operations since the required logic for both of them is quite complex.

5. What is the relationship between the parameters and arguments of a function?   

  • The function’s arguments don’t have the same order, aren’t of the same type and aren’t as many as the function’s parameters  
  • The function’s arguments have the same order, are of the same type and are as many as the function’s parameters.    (CORRECT)
  • There is no relationship.   

Correct! The function’s arguments replace the function’s parameters when you call the function.   

6. When can you assign the result of a function to a constant or variable?   

  • When the function has a return type  (CORRECT)
  • You can never assign the result of a function    
  • When the function doesn’t have a return type  

Correct! The function must return​ a value of the function’s return type.   

​Part 3
In part 3 of the project you managed the bank account .

7. To print the account balance, you used the following statement: 
println("The current balance is ${accountBalance} dollars.")
What is the operator $ used for?

  • Add currency symbol before the variable’s value
  • Asserting that variable is not null  
  • Concatenating constant strings with variables (CORRECT)

Correct! It helps concatenate constant strings with variables, by converting the variable’s name to its equivalent string value. This is called string interpolation.   

8. What is the purpose of the isSystemOpen variable declared in the transfer() method?   

  • Denotes that the system is open to accepting further commands​.  (CORRECT)
  • Denotes that a deposit or transfer has been completed
  • Denotes that a bank operation is under processing​.

Correct! Loops often use variable(s) called ‘control variable(s)’ whose values can change after each iteration, and the loop runs until a condition based on one or more of such control variable(s) is met.

9. For part 3 of this project is this statement true or false?   

Variable money represents the amount requested to be deposited/withdrawn. Variable transferAmount represents the amount actually deposited/withdrawn​.

  • True  (CORRECT)
  • F​alse

C​orrect! All the methods, withdraw(), debitWithdraw(), deposit() and creditDeposit(), take in the variable money as an argument representing the amount requested to be deposited/withdraw and return the actual amount deposited/withdrawn, which is in turn set as the value fortransferAmount.

FINAL ASSESSMENT: PROGRAMMING FUNDAMENTALS IN KOTLIN

1. To create a list, the _________ function is used.

  • listFunc
  • listOf (CORRECT)
  • listArr

Correct. We create a list using `listOf` function, and then we specify next values using arguments.  

2. True or False: In Set, you can get elements by index.

  • True
  • False (CORRECT)

Correct. In set, you cannot get elements by index.

3. When you compare two values, the result is of type ________________

  • Boolean (CORRECT)
  • Float
  • Integer

Correct. When you compare two values, the result is of type ‘Boolean’.

4. What will you do if you want to define a variable that can be used by multiple functions?

  • Use the global keyword when creating the variable
  • Define the variable in a wider scope (CORRECT)
  • Define multiple instances of the variable across functions

Correct. You need to define the variable in a wider scope.

5. What access modifier informs a developer that a class has a limited number of subclasses?

  • abstract
  • void
  • sealed (CORRECT)

Correct. ‘sealed’ modifier is information to the developer who reads the code – it informs, that this class has a limited number of subclasses known in advance during compilation. 

6. Imagine you need to implement a function to open a web browser in different modes. Which of the following needs to be defined in a function to achieve this?

  • Default arguments (CORRECT)
  • Global scope
  • Function params

Correct. With default arguments, you can create a parameter that specifies what mode should be used.

7. When you use interface keyword before a class definition, you make this class abstract.

  • True
  • False (CORRECT)

Correct. When you use abstract keyword before a class definition, you make this class abstract.

8. _____________ block is used to perform operations that should always be done, no matter if an exception occurred or not.

  • Throws (CORRECT)
  • try
  • T​rial

Correct. To inform the user about the possibility of an exception, we can add the annotation Throws with exception class.

9. When you run the code below what is the output of the println statement?

1  fun main() {
2      val (a, b, c) = Triple(4, "y", listOf(null))
3      println(a)
4  }
  • null
  • 4 (CORRECT)
  • y

Correct. The println will display 4.

10. When you run the code below what is the output of the println statement?

1  fun main() {
2      val age = 10
3      println(age)
4  }
  • 10 (CORRECT)
  • l​istArr
  • undefined

Correct. The println statement will display 10.

11. True or False: The code snippet below is a valid way of creating an enum in Kotlin.

1  enum class WeekDay {
2      SUNDAY,
3      MONDAY,
4      TUESDAY,
5      WEDNESDAY,
6      THURSDAY,
7      FRIDAY,
8      SATURDAY
9  }
  • True (CORRECT)
  • False

Correct.  Having an interface class is not a requirement for calling a method.

12. In Kotlin, characters are represented in the ________ format.

  • Unicode (CORRECT)
  • Hexacode
  • Minicode

Correct. In Kotlin, characters are represented in the Unicode format.

13. In Kotlin, when a class is inherited from another class it takes the behavior constructor properties of the parent class.

  • True (CORRECT)
  • False

Correct! When a class is inherited from another class it takes all the behavior of the parent class. That includes its constructor properties.

14. ___________________ is used to define a specific set of values.

  • typeOf
  • print
  • enum class (CORRECT)

Correct. Enum classes are used whenever we need to define a specific set of values.

15. Kotlin functions are declared using the __________ keyword.

  • Use the global keyword when creating the variable
  • Define multiple instances of the variable across functions.
  • fun (CORRECT)

Correct. Kotlin functions are declared using the fun keyword.

16. In the code snippet below what will the output be of the printName function?

1  class Person (val name:String? , val surname?:String?)
2  
3  fun printName(person: Person) {
4      val name: String = person.name ?: "(unknown name)"
5      val surname: String = person.surname ?: "(unknown surname)"
6      println("$name $surname")
7  }
8  
9  fun main() {
10      printName(Person("John", "Locke"))
11  }
  • John (unknown surname)
  • John Locke (CORRECT)
  • John Wood

Correct. Since the surname is not null, the default name (unknown name) will not be used.

17. True or False: The code snippet below is a valid way of creating a function in Kotlin.

1  var name = ""
2  function setName() {
3     name = "Mike"
4  }
  • True
  • False (CORRECT)

Correct. The fun keyword is used to declare a function.

18. The ______________ keyword is used to check if an object is of a particular type.

  • Is (CORRECT)
  • type
  • cast

Correct.  We can check if an object is of a particular type using ‘is’ keyword.

19. True or False: In Kotlin, ‘Long’ is an integer representation that supports larger numbers.

  • True (CORRECT)
  • False

Correct. ‘Long’ is an integer representation that supports larger numbers.

20. True or False: In Kotlin, ‘Float’ is the default decimal number representation.

  • True 
  • False (CORRECT)

Correct. ‘Double’ is the default decimal number representation.

21. Abstract classes are used when we want to specify a set of generic operations for multiple classes.

  • True (CORRECT)
  • False

Correct. We mainly use abstract classes when we want to specify a set of generic operations for multiple classes.

22. The easiest way to add elements to lists is to use the ___________ sign.

  • *
  • + (CORRECT)

Correct. The easiest way to add elements to lists is using plus sign.

23. True or False: The code snippet below is a valid way of creating a map in Kotlin.

1  fun main() {
2      val capitals = mapOf("USA" to "Washington", "Poland" to "Warsaw", "Ukraine" to "Kyiv")
3  }
  • True (CORRECT)
  • False

Correct. You can create a set using the ‘mapOf’ function and then use key-value pairs as arguments to specify key-value associations.

24. Which operator throws an exception if its left side is null?

  • !! (CORRECT)
  • ==
  • !=

Correct. Not-null assertion is ‘!!’ operator, that throws an exception if its left side is null.

25. The following function calculates the factorial of a number by calling itself. What is this an example of?

fun factorial(number: Int): Int {
 
if (number <= 1) {
return 1
}
return factorial(number - 1) * number
}
  • An iterative method
  • A recursive method (CORRECT)
  • A generic method

Correct. One way of implementing such a function is using a recursive method.

26. illegalArgumentException is used when the state of a system is incorrect.

  • True
  • False (CORRECT)

Correct. illegalArgumentException is used when an argument has incorrect value.

27. _____________ block is used to perform operations that should always be done, no matter if an exception occurred or not.

  • try
  • finally (CORRECT)
  • f​in

Correct. We use finally block to do operations that should always be done, no matter if an exception occurred or not.

Interactive CSS CONCLUSION

TBW

Subscribe to our site

Get new content delivered directly to your inbox.

Liking our content? Then, don’t forget to ad us to your BOOKMARKS so you can find us easily!