COURSE 6: ADVANCED PROGRAMMING IN KOTLIN QUIZ ANSWERS

Week 4: Final Project

Meta Android Developer Professional Certificate

Complete Coursera Answers & Study Guide

Enroll in Coursera Meta Android Developer Professional Certification

Final project INTRODUCTION

In this module, you will be assessed on the key skills covered in the Course.

Learning Objectives

  • Synthesize the concepts and skills from this course to create a list with filtering and sorting.
  • Reflect on the next steps in the learning journey.

FINAL GRADED QUIZ: ADVANCED PROGRAMMING IN KOTLIN

1. Which option represents the correct way to call the function ‘sayHello’ defined in the object below:

object Greeting { 
 fun sayHello() = println("hello") 
}
  • println(Greeting. sayHello()) (CORRECT)
  • println(Greeting. sayHello)
  • println(Greeting(). sayHello())

Correct! You access a member of an object simply by using the object’s name and the dot operator.

2. Which of these is correct if you wish to navigate from an activity ‘SourceActivity’ to another activity ‘DestinationActivity’ in Android? (Assume ‘Context’ object can be referenced using ‘context’ and instance of ‘SourceActivity’ can be referenced as ‘sourceActivity’). Select all that apply.

  • val intent = Intent(sourceActivity, DestinationActivity::class.java) (CORRECT)
  • val intent = Intent(SourceActivity::class.java, DestinationActivity::class.java)
  • val intent = Intent(context, DestinationActivity::class.java) (CORRECT)
  • val intent = Intent(DestinationActivity::class.java, sourceActivity)

Correct! You can define an intent by passing the instance of calling activity (as ‘Activity’ class inherits from ‘Context’ class) and class reference of the activity to be started.

Correct! You can define an intent by passing the context object and class reference of the activity to be started.

3. You are asked to implement an extension function for a class named ‘FoodItem’ that would print out its ingredients field. How would the extension function look?

  • fun List<Ingredient>.printIngredients(foodItem: FoodItem) { println(this) } (CORRECT)
  • fun printIngredients(foodItem: FoodItem) { println(foodItem.ingredients) }
  • fun FoodItem.printIngredients() { println(ingredients) }
  • fun FoodItem.printIngredients(ingredients: List<Ingredient>) { println(ingredients) }

Correct! This is the right syntax for the requested extension function.

4. When should you use mocks in your tests?

  • When you need to define a complete alternate definition of an object to be used for testing.
  • When there are objects that are not to be tested but are needed because the code under test depends on them. (CORRECT)
  • When you need to test only some specific behavior of an object

Correct! You use mocks to simulate behavior of objects that the test code depends on.

5. Once features and software requirements are planned, what is the next step in a test-driven development approach?

  • Executing tests
  • Writing tests (CORRECT)
  • Writing code to implement requirements
  • Refactoring code to fix errors

Correct! The tests are written first such that they cover the scenarios of software application requirements. Later, code is written with the intent of passing the tests.

6. You need to instantiate a list of numbers. Which of the following statements are valid in Kotlin?

  • val numbers: List<Int> = 1, 4, 9
  • val numbers: List<Int> = [1, 4, 9]
  • val numbers: List<Int> = listOf(1, 4, 9) (CORRECT)
  • val numbers: List<Int> = (1, 4, 9)

Correct! This is the correct syntax for instantiating a list of strings.

7. What is the output of this code:

val map = mapOf(
1 to 90, 
2 to 93,
3 to 91,
4 to 93,
2 to 95,
5 to 93
)
println(map)
  • {1=90, 2=95, 3=91, 4=93, 5=93} (CORRECT)
  • {1=90, 2=93, 3=91, 4=93, 2=95, 5=93}
  • {1=90, 2=93, 3=91, 4=93, 5=93}

Correct! A map stores unique keys, but the values do not have to be unique.

8. Which of these represents a correct syntax of defining a generic class?

  • class Item<T>(t: T) { } (CORRECT)
  • class <T>.Item(t: T) { }
  • class <T> Item(t: T) { }

Correct. The generic parameter enclosed in the angle brackets is written after the class name.

9. Which of these below are higher-order functions? Select all that apply.

  • fun display(x: (Int) -> Unit) (CORRECT)
  • fun display(x: (Int)) -> Unit
  • fun display(): (Int) -> Unit (CORRECT)
  • fun display(x: Int) : Unit

Correct! This is a higher-order function as it takes another function as a parameter.

Correct! This is a higher-order function as it returns a function.

10. What will be the output of the following code?

val numberMap = mapOf(
 5 to 6,
 3 to 2,
 8 to 7,
 4 to 1
)
val output = numberMap.map { entry ->
 entry.value
}.filter { 
 it > 3
}.fold(2) { x, y ->
 x + y
}
println(output)
  • 13
  • 15 (CORRECT)
  • 17
  • 3

Correct! You correctly computed the outputs of map, filter and fold functions in the above code.

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!