COURSE 6: ADVANCED PROGRAMMING IN KOTLIN
Week 1: Kotlin and Android
Meta Android Developer Professional Certificate
Complete Coursera Answers & Study Guide
Enroll in Coursera Meta Android Developer Professional Certification
INTRODUCTION
Learn how to work with object declarations and experience how they are used. Develop your understanding of companion objects, classes and constant values. In addition, have a walk-through of declaring an object in Android and explore what is involved with implementing a function start for an Activity.
Learning Objectives
- Implement a start function in an Activity
- Define and use a companion object in Kotlin
- Explain how to use inheritance to decide how a component should behave.
- Implement an application with an Activity.
- Implement a list adapter.
- Declare an object in Android.
- Create and use static inner classes and inner modifiers when appropriate.
- Set and read Activity arguments.
- Implement a companion object with constant values and arguments.
SELF REVIEW: PRACTICE USING COMPANION OBJECTS
1. What was the output when you ran your solution?
- 1.5
- 1.8
- 1.33 (CORRECT)
Correct! This is the sales tax amount for the list of OrderItem and given sales tax percentage.
2. What is the output of the program if you add another OrderItem in the list – Ice cream (4$)?
- 1.71 (CORRECT)
- 1.9
- 1.805
Correct! This is the sales tax amount for the list of OrderItem and given sales tax percentage.
3. How can you call a function fun sayHello() defined within a companion object of a class ‘Greeting’?
- Greeting().sayHello()
- Greeting.sayHello() (CORRECT)
Correct! You can invoke the function using the class name and the dot operator.
4. Which of the following core concepts are covered in this course? Check all that apply.
- Kotlin and Android (CORRECT)
- UX and UI design
- Functional programming (CORRECT)
- Advanced Object-Oriented Features (CORRECT)
That’s right! Kotlin and Android integration is part of this course
Correct! The concept of functional programming is part of this course
Correct! An introduction toadvanced object-oriented features is included in this course
5. Your current understanding of the programming fundamentals in Kotlin should already include which of these items? Check all that apply
- Extension functions
- Class inheritance (CORRECT)
- Collections (CORRECT)
- Nullability (CORRECT)
Correct! The concept of Class inheritance is part of the earlier Kotlin course.
Correct! An introduction to collections is part of the earlier Kotlin course.
That’s right! Nullability is part of the earlier Kotlin course.
6. When you first create an Android Studio project you must choose at least one activity to include.
- True
- False (CORRECT)
That’s correct. You do not have to select any activity options to create a project, although it is considered best practice to include at least an “empty” activity.
7. A Singleton is an object-oriented pattern where a class can have only one object at a time.
- True (CORRECT)
- False
That’s correct. A Singleton is an object-oriented pattern where a class can have only one object at a time
8. A singleton object can inherit another class or interface.
- True (CORRECT)
- False
That’s correct. A singleton object can inherit another class or interface.
9. Which of the following are true about companion objects? Select all that apply.
- Companion objects must be declared outside of a class.
- A companion object is initialized when you instantiate the class for the first time. (CORRECT)
- Companion objects have the functionality of static classes. (CORRECT)
That’s correct. Companion objects initialization happens when you instantiated the class.
That’s correct. Companion objects can be used like static classes in other languages.
SELF REVIEW: IMPLEMENTING START FUNCTION IN AN ACTIVITY
1. In the companion object of IngredientsActivity, you have a start function. In that function, you initialize an Intent. Which of the following calls did you use?
- val intent = Intent(context, IngredientsActivity::class.java) (CORRECT)
- val intent = Intent(IngredientsActivity::class.java , context)
- val intent = Intent(context, IngredientsActivity::class)
Correct! The intent constructor expects a context followed by the Java class of the activity to start.
2. In the onCreate function of the IngredientsActivity class, replace the Pasta string with Pizza. Run the code and tap the Pasta button. What happens?
- IngredientsActivity launches with Unknown dish (CORRECT)
- IngredientsActivity no longer launches.
- IngredientsActivity launches with the ingredients for a pizza
Correct! The Pasta argument is no longer recognized by IngredientsActivity. Any unknown argument leads to Unknown dish being presented.
3. Which of the following will start IngredientsActivity from MainActivity? Select all that apply.
- IngredientsActivity.start(this, “Pasta”) (CORRECT)
- startActivity(IngredientsActivity)
- startActivity(Intent(this, IngredientsActivity::class.java)) (CORRECT)
Correct! Calling the static start function on IngredientsActivity will start this activity.
Correct! You can instantiate Intent with the appropriate arguments and pass it to startActivity to start IngredientsActivity.
4. Which of these are good examples of constants?
- [The number of months in a year] (CORRECT)
- [The current balance in the cash register]
- [The name of the restaurant] (CORRECT)
- [The total number of orders received]
That’s right! The number of months is not going to change. This is a good constant.
That’s right! It is unlikely that the name of the restaurant would change while the app is running. This makes it a good constant.
5. How would you set up an intent from an activity named ‘FirstActivity’ with its destination as ‘SecondActivity’, where the current activity instance is denoted by ‘this’ keyword?
- Intent(this,SecondActivity::class.java) (CORRECT)
- Intent(FirstActivity.class::java, SecondActivity.class.java)
- Intent(SecondActivity::class.java)
That’s correct. An intent contains the context which in this case is the FirstActivity’s instance represented by ‘this’, and the destination activity’s class.
6. You wish to set up an intent and pass a string valued “Hello World” with its key as “data” to an activity that is to be launched, what is the correct syntax?
- intent.putExtra(“Hello World”, “data”)
- intent.putExtra(“data”, “Hello World”) (CORRECT)
- intent.putExtra(“Hello World”)
That’s correct. To add an intent extra the key is passed as the first argument and corresponding value is passed as the second argument.
KNOWLEDGE CHECK: OBJECT DECLARATION
1. RestaurantMenu is a Kotlin object with a mutable list. Which of the following statements is true? Select all that apply.
- Adding the same element from activity A or from activity B will lead to different results.
- An element added to the list from activity A will be accessible from activity B (CORRECT)
- Clearing the list from activity A will clear the list for activity B as well. (CORRECT)
- An element added to the list from activity A will not be accessible from activity B
Correct! Objects in Kotlin are singletons. Adding an element from anywhere would be reflected everywhere.
Correct! Objects in Kotlin are singletons. Any changes to the RestaurantMenu will be reflected everywhere.
2. Little Lemon has only one restaurant. Which entity would best represent it?
- Object (CORRECT)
- Interface
- Class
- List
Correct! An object in Kotlin guarantees you will only have a single instance of Little Lemon.
3. Which of the following statements about companion objects are correct?
- A companion object is a singleton (CORRECT)
- Companion objects can belong to multiple classes
- You can have multiple companion objects per class
- You must add a companion object for each class
Correct! Companion objects are singletons. There is only one instance of each companion object per class.
MODULE QUIZ: KOTLIN AND ANDROID
1. Which option demonstrates the correct way to initialize an object in Kotlin?
- object CustomObject( )
- object CustomObject( ) { }
- object CustomObject { } (CORRECT)
Correct! You use a block enclosed by a pair of curly braces that represent the object’s body.
2. Which option represents the correct way to call the function getNumberOfWheels defined in the object below:
object Car {
fun getNumberOfWheels() = 4
}
- println(Car.getNumberOfWheels()) (CORRECT)
- println(Car().getNumberOfWheels())
- println(Car.getNumberOfWheels)
Correct! You access a member of an object simply by using the object’s name and the dot operator.
3. What is the correct syntax to call the function printHello defined in the code below:
class Outer {
class Nested {
fun printHello() {
println("Hello")
}
}
}
- Outer().Nested().printHello()
- Nested().printHello()
- Outer.Nested().printHello() (CORRECT)
Correct! The nested class is accessed using the class name and the dot operator.
4. What is the correct syntax to call the function printHello defined in the code below:
class Outer {
inner class Inner {
fun printHello() {
println("Hello")
}
}
}
- Outer().Inner().printHello() (CORRECT)
- Outer.Inner().printHello()
- Inner().printHello()
Correct! The inner class is accessed using an instance of the outer class.
5. Which of these are correct ways to access the variable wheelCount in the code below:
class Car {
companion object {
val wheelCount = 4
}
}
- Car.wheelCount (CORRECT)
- Car.Companion.wheelCount (CORRECT)
- Car().wheelCount
- Car.companion.wheelCount
Correct. You can access the members of companion object using Classname.memberName and omit the word ‘Companion’ until companion object explicity states a name.
Correct. You can access the companion object using Classname.Companion
6. When should you use a companion object in a class?
- To define members that should be accessible without an object of a class and only using the class name (CORRECT)
- To define members that should be accessible by an object of a class
- To define members that should not be accessible outside the class
Correct. You can access the companion object using the class name without the need to have an instance of a class.
7. Which of the following keywords is used to declare a constant in Kotlin?
- const (CORRECT)
- constant
- final
Correct! You use the const keyword to declare a constant in Kotlin.
8. Which of these is correct if you wish to navigate from an activity called SourceActivity to another activity called DestinationActivity in Android?
- (Assume that the Context object can be referenced using context and an instance of SourceActivity can be referenced as sourceActivity)
- val intent = Intent(DestinationActivity::class.java, sourceActivity)
- val intent = Intent(SourceActivity::class.java, DestinationActivity::class.java)
- val intent = Intent(sourceActivity, DestinationActivity::class.java) (CORRECT)
- val intent = Intent(context, DestinationActivity::class.java) (CORRECT)
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.
9. What is the right way to use the function startActivity to start an activity?
- context.startActivity(intent) (CORRECT)
- Activity.startActivity(intent)
- startActivity(context, intent)
Correct! You need to call the startActivity function using an instance of Context and pass an intent to the function.
10. Which one of the below lines can you use to pass an argument to an activity? (Assume an instance of Intent can be referenced using intent variable)
- intent.getExtra(“message”, “hello”)
- intent.putExtra(“message”, “hello”) (CORRECT)
- intent.putExtra(“hello”)
Correct! You pass the key for the data as the first argument and its corresponding value as the second argument.
Interactive CSS CONCLUSION
TBW
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!