COURSE 3 – PROGRAMMING FUNDAMENTALS IN KOTLIN QUIZ ANSWERS
Week 2: Functions, classes and objects
Meta Android Developer Professional Certificate
Complete Coursera Answers & Study Guide
Enroll in Coursera Meta Android Developer Professional Certification
TABLE OF CONTENT
- INTRODUCTION
- KNOWLEDGE CHECK: PRACTICE USING FUNCTIONS
- SELF-REVIEW: PRACTICE USING FUNCTIONS WITH RESULTS
- PRACTICE QUIZ: PRACTICE USING DEFAULT AND NAMED ARGUMENTS
- KNOWLEDGE CHECK: CLASSES AND OBJECTS
- SELF-REVIEW: PRACTICE DEFINING CLASSES, METHODS AND FUNCTIONS
- KNOWLEDGE CHECK: INTERFACES AND OPEN CLASSES
- KNOWLEDGE CHECK: VISIBILITY MODIFIERS
- KNOWLEDGE CHECK: DEFINING AND USING ABSTRACT CLASSES
- MODULE QUIZ: FUNCTIONS, CLASSES AND OBJECTS
- CONCLUSION
Functions, classes and objects INTRODUCTION
In this module, you will learn about functions, classes and objects. You will discover more about a function and how programs are constructed from functions. You will also learn how use a class, object and type while you are writing code. Furthermore, you will practice how to use classes, objects and types.
Learning Objectives
- Describe up-casting and down casting
- Explain the open variable and methods and how to override them
- Explore the uses of interface
- Describe classes inheritance
- Describe different scopes of access that a modifier can let the variable have
- Explain different visibility modifiers
- Explain the concept of a function
- Differentiate between a parameter and an argument
- Explain that a function can return a value and describe how it works
- Explain the difference between a class, an object and a type
- Differentiate between various visibility modifiers
KNOWLEDGE CHECK: PRACTICE USING FUNCTIONS
1. ___ main__ { println(“ABC”) }
- def, ()
- fun,
- function, ()
- function,
- fun, () (CORRECT)
- def,
Correct! The complete code is 1 fun main () { println("ABC") }
2. This function is created within your code.
fun callme (){
// funtion body
}
Which of the following is a proper way of calling the function within your program?
- A function doesn’t need to be called
- callme() (CORRECT)
- fun callme { }
That’s correct. You have to call the function this way to run the code inside the body of the function.
3. What is the output of the code below?
1 getLocation() { println("Printing from getLocation() function.") }
2
3 fun main() {
4 getLocation()
5 }
- The code will not compile. (CORRECT)
- Printing from getLocation() function
That’s correct. The fun keyword is not included in the function declaration. This compiler will throw an error.
4. You are writing code with multiple repeating lines. Therefore, you make use of functions in your code. What are the benefits of using functions in your code? Select all that apply.
- Functions make code reusable (CORRECT)
- Functions make code easy to understand (CORRECT)
- Functions help you to avoid the use of variables in your code
That’s correct! Functions help you to avoid repetition of code for a specific operation.
That’s correct! Functions organise code in way that makes it easy to understand.
5. What does the ‘body’ in programming do? Select all that apply.
- Call functions (CORRECT)
- Call loops (CORRECT)
- Call conditions (CORRECT)
That’s correct! The body is the place where you can call functions.
That’s correct! The body is the place where you can call loops.
That’s correct! The body is the place where you can call conditions
SELF-REVIEW: PRACTICE USING FUNCTIONS WITH RESULTS
1. In the exercise, when you run your code, what output do you get?
- 10
0
40
50
55 - 0
10
50
40
55 (Correct) - 0
10
50
55
40
Correct! When the function is set up correctly, this will be the output of running the program.
2. What is the return type of the function below?
1 fun getPointsOutput(basePoints: Int, boost: Int): String {
2 }
- Int
- String (CORRECT)
- Double
Correct! The type specified after the parameters is the return type.
3. How many parameters are defined for the following function?
1 fun getPointsOutput(basePoints: Int, boost: Int, player: Int): String{
2 }
- 2
- 3 (CORRECT)
- 1
Correct! There are three parameters defined between the round brackets.
4. You want to write a program for your robot to send it to different stores. Which one of the following would you use?
- A parameter (CORRECT)
- An argument
- An absolute function
That’s correct! A parameter is a named variable that is defined as a part of function. A Parameter also allows an argument to be passed into the function during execution and let’s your robot go to a different store.
PRACTICE QUIZ: PRACTICE USING DEFAULT AND NAMED ARGUMENTS
1. Why are default arguments used? Select all that apply.
- To simplify function calls. (CORRECT)
- To make it possible to call arguments at any order.
- To make function calls more explicit.
- To express what is the default option for an argument. (CORRECT)
Correct! For readability purposes, default arguments help to simplify function calls.
Correct! Default arguments allow us to express the default option for an argument.
2. Why are named arguments used? Select all that apply.
- To simplify function calls.
- To express what is the default option for an argument.
- To specify a later parameter without specifying the previous one. (CORRECT)
- To make it possible to call arguments at any order. (CORRECT)
- To make function calls more explicit. (CORRECT)
Correct! Naming enables you to specify a later parameter without specifying the previous one.
Correct! Arguments can be called at any order by naming.
Correct! Named arguments make function calls more explicit.
3. What is the result of the following code?
1 fun printSurrounded(value: String = "", prefix: String = "<", postfix: String = ">") {
2
3 println("$prefix$value$postfix")
4 }
5
6 fun main() {
7
8 printSurrounded("ABC")
9 }
- ABC
- <ABC
- >ABC<
- <ABC> (CORRECT)
Correct! A prefix and postfix are concatenated to the string value.
4. What is the result of the following code?
1 fun printSurrounded(value: String = "", prefix: String = "<", postfix: String = ">") {
2 println("$prefix$value$postfix")
3 }
4
5 fun main() {
6 printSurrounded("ABC", ".")
7 }
8
- <ABC>
- .ABC.
- .ABC> (CORRECT)
Correct! A prefix and postfix are concatenated to the string value.
5. What is the result of the following code?
1 fun printSurrounded(value: String = "", prefix: String = "<", postfix: String = ">") {
2
3 println("$prefix$value$postfix")
4 }
5
6 fun main() {
7 printSurrounded(postfix = ".")
8 }
- <. (CORRECT)
- <>
- .>
- <.>
Correct! A prefix and postfix are concatenated to an empty string value.
KNOWLEDGE CHECK: CLASSES AND OBJECTS
1. What specifies how an object should be created, with what methods and what properties it has?
- A class (CORRECT)
- A function
- A file.kt
That’s correct. A class is a template that is used to create objects. It specifies how objects should be created, with what methods and what properties it has.
2. Which of the following is a valid way of creating a class object in Kotlin?
- val mark = Employee
- val mark = Employee() (CORRECT)
- Object mark = new Employee
That’s correct. This is a valid way to create a class object in Kotlin.
3. What is used to access the properties and methods of a class?
- The – (hyphen) operator
- The . (dot) operator (CORRECT)
- The ? (Elvis) operator
Correct. You can access the properties and methods of a class using the . (dot) operator.
4. True or False: To define a class, you use the fun keyword and then specify a name.
- False (CORRECT)
- True
That’s correct! To define a class, you use the class keyword and then specify a name.
5. A class consists of a class header and a class body surrounded by curly braces.
- False
- True (CORRECT)
That’s correct! A class consists of a class header and a class body surrounded by curly braces.
6. A class is a template that you use to create objects. What does it specify? Select all that apply.
- How the object should be created (CORRECT)
- Which methods and properties the object consists of (CORRECT)
- The instance that is in a class
That’s correct! A class is a template that specifies how the object should be created.
That’s correct! A class is a template that specifies which methods and properties the object consists of.
7. If you define a property inside the class body, Does it need to be initialized?
- Yes (CORRECT)
- No
That’s correct! The properties defined in the body of class must be initliazed.
SELF-REVIEW: PRACTICE DEFINING CLASSES, METHODS AND FUNCTIONS
1. In task 1, to create the method fullName which keyword did you use?
- fun (CORRECT)
- method
- met
Correct! A method is created the same way as every other function – using the fun keyword. In task 1, fullName is referred to as a method, even though it is created the same way as every other function, because it is also a member of the class Player.
2. True or false
In task 1, you implemented a method fullName that should return Int
- True
- False (CORRECT)
Not quite, Int would not be an appropriate data type to store player names. Revise the exercise Practice defining Classes, Methods and Functions before you continue.
3. In the exercise Practice defining classes, a Score class is created to keep the code separate and more readable. It contains a method that compares the current score and best score.
- How do you call the score method?
- You can call that method by creating an instance type of the Score class. (CORRECT)
- You can call that method by creating an instance type of the Player class.
- You can call that method without an instance type.
That’s correct! To call any method of a class, you first need to create an instance of that class.
4. In the exercise Practice defining Classes, Methods and Functions you expanded the Player class to hold the player’s scores. The total score and best score were referred to as ?
- Properties (Correct)
- Return
- Strings
Correct. You expanded the Player class to have properties that can hold the player’s total score and best score.
5. What is a method?
- A function associated to a class, for example a defined class (CORRECT)
- Another name for a function
- A function or property in a class
That’s correct! Functions defined in classes are methods.
KNOWLEDGE CHECK: INTERFACES AND OPEN CLASSES
1. What methods can you call on the variable o?
1 interface I {
2 fun a()
3 }
4 class C: I {
5 override fun a() {}
6 fun b() {}
7 }
8 class D:I{
9 fun y(){}
10 }
11 fun main() {
12 val o: I = C()
13 // here
14 }
- a (CORRECT)
- y
- b
Correct! Interface I defines method a, so you can use it.
2. What methods can you call on the variable o? Select all that apply.
1 open class P {
2 fun a() {}
3 }
4
5 class C: P() {
6 fun b() {}
7 }
8 Class D:P(){
9 fun e() {
10 }
11 }
12 fun main() {
13 val o = C()
14 // here
15 }
- e
- b (CORRECT)
- a (CORRECT)
Correct! C has method a, because it inherited it from P.
Correct! C has method a, because it inherited it from P.
3. What methods can you call on the variable o? Select all that apply.
1 open class P {
2 fun a() {}
3 }
4
5 open class C: P() {
6 fun b() {}
7 }
8 class D: C() {
9 fun u() {}
10 }
11 fun main() {
12 val o = D()
13 // here
14 }
- u (CORRECT)
- a (CORRECT)
- b (CORRECT)
Correct. D has method u
Correct. D has method a, because it inherited it from C and C is inherited from P.
Correct. D has method b because it inherited it from C
4. What should the relationship between Animal and Dog types be?
- Animal should be a supertype of Dog. (CORRECT)
- Dog should be a supertype of Animal.
Correct! The types that are more generic are supertypes of those that are more specific.
5. The following code will compile.
1 interface Animal
2 class Dog: Animal
3
4 fun main() {
5 val dog: Dog = Dog()
6 val animal: Animal = dog
7 val dog2: Dog = animal
8 }
- True
- False (CORRECT)
Correct! Because Animal cannot be used as Dog or as Animal.
6. You have defined the class computer, which has the attributes processor and RAM. You’ve also created a child class laptop, for which you’ve added the attribute size, in addition to those of the parent class. Which one of the four concepts of object-oriented programming does this exemplify?
- Inheritance (CORRECT)
- Polymorphism
- Encapsulation
- Abstraction
Correct! An instance taking attributes from a class is known as inheritance.
7. You need a function that can take on many different forms, for example an animal. Which object-oriented programming concept describes this the best?
- Interfaces
- Polymorphism (CORRECT)
- Objects
That’s correct! Polymorphism means “many forms”.
8. You have a drone and want to build another one with new variations. You want to use class inheritance to do this. Which of the following needs to be true for you to use class inheritance? Select all that apply.
- Define a class that extends from another class (CORRECT)
- Define a class that introduces modifications to thebehavior (CORRECT)
- Define a class with an open modifier (CORRECT)
A parent class called Drone with generic properties and functions is created. Other variations can inherit the behavior of this parent class by extending it.
The new drone variation class will be able to create new implementations based on existing behavior without necessarily making changes to the parent drone class.
By default, Kotlin classes are final – they can’t be inherited. To make a class inheritable, mark it with the open keyword:
KNOWLEDGE CHECK: VISIBILITY MODIFIERS
1. What methods and properties defined in SomeClass can be used in the line marked as ‘here’? Select all that apply.
1 class SomeClass {
2 private val privateProperty = "ABC"
3 protected fun protectedFunction() {}
4 fun publicFunction() {}
5 internal val internalProperty = "ABC"
6
7 fun test() {
8 // here
9 }
10 }
11
- PublicFunction (CORRECT)
- internalProperty (CORRECT)
- protectedFunction (CORRECT)
- privateProperty (CORRECT)
Correct! You can use public elements in the scope of the same class.
Correct! You can use internal elements in the scope of the same class.
Correct! You can use protected elements in the scope of the same class.
Correct! You can use private elements in the scope of the same class.
2. What methods and properties defined in SomeClass can be used on the object o in the line marked as ‘here’? Select all that apply.
1 class SomeClass {
2 private val privateProperty = "ABC"
3 protected fun protectedFunction() {}
4 fun publicFunction() {}
5 internal val internalProperty = "ABC"
6 }
7
8 fun test() {
9 val o = SomeClass()
10 // here
11 }
- publicFunction (CORRECT)
- protectedFunction
- privateProperty
- internalProperty (CORRECT)
Correct! You can use public elements outside of the scope of those elements’ definition.
Correct! You can use internal elements outside of the scope of their definition if you are at the same module.
3. What methods and properties defined in ParentClass can be used in the line marked as ‘here’? Select all that apply.
1 open class ParentClass {
2 private val privateProperty = "ABC"
3 protected fun protectedFunction() {}
4 fun publicFunction() {}
5 internal val internalProperty = "ABC"
6 }
7
8 class ChildClass: ParentClass() {
9 fun test() {
10 // here
11 }
12 }
- PublicFunction (CORRECT)
- InternalProperty (CORRECT)
- ProtectedFunction (CORRECT)
- privateProperty
Correct! You can use public elements outside of the scope of those elements’ definition.
Correct! You can use internal elements outside of the scope of their definition if you are at the same module.
Correct! You can use protected elements in the scope of subclasses of their definition class.
4. What methods and properties defined in SomeClass can be used on the object o in the line marked as ‘here’? Select all that apply.
1 open class ParentClass {
2 private val privateProperty = "ABC"
3 protected fun protectedFunction() {}
4 fun publicFunction() {}
5 internal val internalProperty = "ABC"
6 }
7
8 class ChildClass: ParentClass()
9
10 fun test() {
11 val o = ChildClass()
12 // here
13 }
- PublicFunction (CORRECT)
- privateProperty
- internalProperty (CORRECT)
- protectedFunction
Correct! You can use public elements outside of the scope of those elements’ definition.
Correct! You can use internal elements outside of the scope of their definition if you are at the same module.
5. Will the following code compile?
1 // File1.kt
2 protected val a = 10
3 fun b() {}
- Yes
- No (CORRECT)
Correct! The keyword protected cannot be used for top-level elements (defined outside of a class).
6. What methods and properties defined at the same file can be used in the line marked as ‘here’? Select all that apply.
1 private val privateProperty = "ABC"
2 fun publicFunction() {}
3 internal val internalProperty = "ABC"
4
5 fun test() {
6 // here
7 }
- InternalProperty (CORRECT)
- PrivateProperty (CORRECT)
- PublicFunction (CORRECT)
Correct! You can use internal elements outside of the scope of their definition as long as you are at the same module.
Correct! You can use private top-level elements in the scope of the same file.
Correct! You can use public elements outside of the scope of those elements’ definition.
7. What methods and properties defined in file File1.kt can be used in the line marked as ‘here’? (Assume that the code started with File1.kt comment is placed in a file named File1.kt and the code started with File2.kt comment is placed in a file named File2.kt). Select all that apply.
1 // File1.kt
2 private val privateProperty = "ABC"
3 fun publicFunction() {}
4 internal val internalProperty = "ABC"
5
6 // File2.kt
7 fun test() {
8 // here
9 }
- internalProperty (CORRECT)
- privateProperty
- publicFunction (CORRECT)
Correct! You can use internal elements outside of the scope of their definition as long as you are at the same module.
Correct! You can use public elements outside of the scope of those elements’ definition.
8. When you define a property inside a class as ‘private’, which one can access that property?
- Only members of the same file. (CORRECT)
- The whole project classes.
- The class which defined in it and its subclasses.
That’s correct. A private proprety can only be accessed in the same class.
KNOWLEDGE CHECK: DEFINING AND USING ABSTRACT CLASSES
1. What are the key differences between abstract classes and interfaces? Select all that apply.
- You can inherit from as many classes as you want but inherit only from one interface.
- You can implement as many interfaces as you want but inherit only from one class. (CORRECT)
- Abstract classes can have non-abstract properties, while interfaces cannot. (CORRECT)
- Abstract classes can have non-open methods, while interfaces cannot. (CORRECT)
Correct! You can implement as many interfaces as you want but inherit only from one class.
Correct! Abstract classes can have non-abstract properties, while interfaces cannot.
Correct! Abstract classes can have non-open methods, while interfaces cannot.
2. What methods and properties does the class SomeClass have? Select all that apply.
1 abstract class AbstractClass(
2 val name: String
3 ) {
4 val a = 10
5 abstract fun c()
6 fun d() {}
7 }
8
9 class SomeClass: AbstractClass("SomeName") {
10 val b = 20
11 override fun c() {}
12 fun e() {}
13 }
- c (CORRECT)
- b (CORRECT)
- name (CORRECT)
- e (CORRECT)
- a (CORRECT)
- d (CORRECT)
Correct! The method c defined in AbstractClass is also inherited by SomeClass.
Correct! The method b defined in AbstractClass is also inherited by SomeClass.
Correct! The property name defined in AbstractClass is also inherited by SomeClass.
Correct! The method e defined in AbstractClass is also inherited by SomeClass.
Correct! The property defined in AbstractClass is also inherited by SomeClass.
Correct! The method d defined in AbstractClass is also inherited by SomeClass.
3. What should you define when you need to specify what methods and properties you expect a set of classes to have?
- An open class
- An interface (CORRECT)
- An abstract class
Correct! An interface is preferred in such cases.
4. Will the following code compile?
1 abstract class A
2
3 fun main() {
4 val a = A()
5 }
- No (CORRECT)
- Yes
Correct! You cannot create objects from abstract classes.
5. Will the following code compile?
1 abstract class A {
2 abstract fun a()
3 }
4
5 class B: A
6
7 fun main() {
8 val b = B()
9 }
- No (CORRECT)
- Yes
Correct! The code will not compile, because B does not override the abstract function a.
6. What is the output of the following code?
1 b.a()1 abstract class A {
2 abstract fun a()
3 }
4 class B: A {
5 override fun a() {println("Hello")}
6 }
7 fun main() {
8 val b = B()
10 }
- The code won’t compile
- Hello (CORRECT)
- The code will compile correctly but with no output.
Correct! The overridden method a() will implement its body.
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
MODULE QUIZ: FUNCTIONS, CLASSES AND OBJECTS
1. What is the result of the following code?
1 fun a() {
2 print("A")
3 }
4
5 fun b() {
6 print("B")
7 a()
8 }
9
10 fun c() {
11 b()
12 print("C")
13 b()
14 }
15
16 fun main() {
17 c()
18 }
- ABC
- BAC
- BACBA (CORRECT)
- CBA
Correct! c calls b, prints ‘C’, then calls b. b calls a and prints ‘B’. a prints ‘A’. So c first prints ‘B’, then ‘A’, then ‘C’, then ‘B’ again and then ‘A’.
2. When class A inherits from class B, then… Select all that apply.
- B is a superclass of A. (CORRECT)
- A is a child of B. (CORRECT)
- B is a child of A.
- B is a parent of A.(CORRECT)
Correct. Inheritance makes a relationship, where the class that inherits is a subclass and the class it inherits from is a superclass.
Correct! Inheritance makes a parent-child relationship, where the class that inherits is a child of the class it inherits from.
Correct! Inheritance makes a parent-child relationship, where the class that inherits is a child of the class it inherits from.
3. You can call the eat method on the variable o?
1 open class Animal {
2 fun eat() {}
3 }
4
5 class Dog: Animal() {
6 fun play() {}
7 }
8
9 fun main() {
10 val o = Dog()
11 // here
12 }
- False
- True (CORRECT)
Correct! This method is inherited from the Animal.
4. What methods can you call on the variable o?
1 open class Animal {
2 fun eat() {}
3 }
4
5 class Dog: Animal() {
6 fun play() {}
7 }
8
9 fun main() {
10 val o: Animal = Dog()
11 // here
12 }
- eat
- No methods can be called.
- play
Not quite. Please review More on interfaces in Week 2, Lesson 3.
5. Can objects of abstract classes be instantiated?
- Yes
- No (CORRECT)
Correct! Abstract classes cannot be used to create objects. You need to define classes that inherit from those abstract classes.
6. Objects be instantiated from an interface
- False (CORRECT)
- True
Correct! Interfaces cannot be used to create objects. You need to define classes that implement those interfaces.
7. Objects of open classes be instantiated
- True (CORRECT)
- False
Correct! Open classes are like regular classes that additionally support inheritance.
8. The term polymorphism means…
- The process by which one object can acquire the properties of another object
- The possibility for an object to have many forms, so to be created using different classes (CORRECT)
- The capability to hide certain methods
- The possibility to inherit from a class
Correct! The name polymorphism means ‘many forms’. Polymorphism is mainly achieved in Kotlin with interfaces and open and abstract classes.
9. The term inheritance means…
- The capability to hide certain methods
- The process by which one object can acquire the properties of another object (CORRECT)
- The possibility for an object to have many forms, so to be created using different classes
Correct. Class inheritance is the process by which one object can acquire the properties of another.
10. What methods and properties defined in SomeClass can be used on the object o in the line marked as //here? Select all that apply.
1 open class ParentClass {
2 private val privateProperty = "ABC"
3 protected fun protectedFunction() {}
4 fun publicFunction() {}
5 internal val internalProperty = "ABC"
6 }
7
8 class ChildClass: ParentClass()
9
10 fun test() {
11 val o = ChildClass()
12 // here
13 }
- InternalProperty (CORRECT)
- privateProperty
- publicFunction (CORRECT)
- protectedFunction
Correct! You can use internal elements outside of the scope of their definition as long as you are in the same module.
Correct! You can use public elements outside of the scope of those elements’ definition.
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!