Unit 1 Quizzies¶

  1. What are the differences between previewing an app in the Design view, and running the app on an Android Virtual Device?

    #solulu

    Previewing the app on Design View

    Running the app on an Android Virtual Device

    No Android device, whether real or emulated, is involved.

    An Android Virtual Device is used, which emulates real Android hardware and software. Your laptop “pretends” to be an Android device, so that the app thinks it’s running on an Android device.

    Uses only the app code that is annotated with @Preview.

    All app code is used.

    Doesn’t install the app anywhere. It “runs” within Android Studio.

    App is installed and runs on the Android Virtual Device.

    Build process is faster. Can see changes to the composables sooner.

    Build process is slower. Need to compile the entire app, install it onto the AVD, and run it on the AVD.

    Mainly for viewing the layout, styling, and UI elements. Interactivity (e.g., button clicks, navigation, network calls) is limited.

    Full functionality: the entire app works as it would on a real device.

    Lower resource usage (resource: your laptop’s CPU, RAM)

    Higher resource usage

  2. What’s the difference between compile-time and run-time?

    #solulu

    Compile-time is when the source code is converted to a form that the machine understands (machine code). Run-time is when the machine code is executed on a mobile device.

  3. What are the pros and cons of using named arguments as compared to positional arguments?

    #solulu

    Pros

    Cons

    • Makes function calls more descriptive and easier to understand. Clearly identifies the purpose of each argument. Code becomes more self-explanatory.

    • Changes to parameter order don’t affect all callers of that function.

    • Allows passing arguments in any order, less risk of messing up the order, prevents mistakes caused by passing arguments in wrong order.

    • For simple functions with just 1 parameter, could make the code longer and more cluttered.

    • Changes to parameter names requires updates to callers of that function using that parameter.

    • Slightly extra typing.

  4. In the code below, what might be some reasons for not including Surface() in TaskCompletedPreview()?

    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                TaskCompletedTheme {
                    // A surface container using the 'background' color from the theme
                    Surface(
                        modifier = Modifier.fillMaxSize(),
                        color = MaterialTheme.colorScheme.background
                    ) {
                        TaskCompletedScreen()
                    }
                }
            }
        }
    }
    
    @Composable
    fun TaskCompletedScreen() {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            val image = painterResource(R.drawable.ic_task_completed)
            Image(painter = image, contentDescription = null)
            Text(
                text = stringResource(R.string.all_task_completed),
                modifier = Modifier.padding(top = 24.dp, bottom = 8.dp),
                fontWeight = FontWeight.Bold
            )
            Text(
                text = stringResource(R.string.nice_work),
                fontSize = 16.sp
            )
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun TaskCompletedPreview() {
        TaskCompletedTheme {
            TaskCompletedScreen()
        }
    }
    
    #solulu
    • Simplifies the TaskCompletedPreview() code.

    • In Surface(), fillmodifier = Modifier.fillMaxSize() makes its contents fill the entire screen (of an Android device). TaskCompletedPreview() doesn’t need this, because the preview is a small window within Android Studio, not the screen of an Android device.

    • In Surface(), color = MaterialTheme.colorScheme.background applies a background color to the surface. This is done for TaskCompletedPreview() by using @Preview(showBackground = true), which does not have as many color options as Surface(), but is sufficient in this case.

  5. What are the similarities and differences between Kotlin, Python, and C? Individually, compare Kotlin with only Python, or C. In a team, combine your answers to compare all 3.

    #solulu

    C

    Kotlin

    Python

    Statically typed. Variables must have a type declared, and cannot change their type.

    Statically typed.

    Dynamically typed. Variables don’t need a type declared, and can change types.

    Variable types must be declared explicity.

    Variable types can be inferred.

    Variable types can be specified in some cases, but are optional.

    Not object-oriented. No classes.

    Object-oriented. Has classes.

    Object-oriented. Has classes.

    Function definitions start with a return type.

    Function definitions start with the keyword fun.

    Function definitions start with the keyword def.

    Can assign a function to a variable.

    Can assign a function to a variable.

    Can assign a function to a variable.

    Parameters are mutable (can be changed within the function body).

    Parameters are immutable (cannot be changed within the function body).

    Parameters are mutable (can be changed within the function body).

    Semicolons must be used to end statements.

    Semicolons are optional.

    Semicolons are optional.

    Uses attributes to add metadata to functions. (Some compilers only)

    Uses annotations to add metadata to functions.

    Uses decorators to add metadata to functions.

    No named arguments.

    Supports named arguments.

    Supports named arguments.