Programming Concepts Flashcards

1
Q

When, if/else as an expression

A

The body must return a value but you don’t have to use return statement:
val name = if (condition) {
body 1
} else { condition
body 2
}

val name = when (parameter) {
condition 1 → body 1
condition 2 → body 2
condition 3 → body 3
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Lambda expression

A

Shorthand syntax to declare function without fun keyword. You can store a lambda expression directly in a variable

val variableName ={
lambda expression
}

variableName () - to call it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

?.

A

Save call operator
nullableVariable ?. method / property

Stops any attempt off member access to null references and return null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

!!.

A

Not null assertion operator
nullableVariable!!. method / property

You assert that the value is not null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

, - in when statement

A

To separate multiple conditions

condition 1, condition 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

in in when statement

A

in 1…10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

is in when statement

A

To check the data type

is type → body 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

?:

A

Elvis operator to add default value when there is indeed null
val name = nullableVariable?.method ?: defaultValue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Class in Kotlin

A

ClassName {
Properties
Methods
Constructors
}

ClassName () - to create an object
ClassObj.methodName() - to call class method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Getter and setter functions

A

F.e.
var speakerVolume = 2
get () = field
set ( value ){
field = value
}

Variable read-only (val) don’t have set functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Constructor

A

Primary - class can have only one, defined as a part of a class header
ClassName constructor() { body}
Secondary - class can have multiple with or without parameters. If the class have primary each secondary must implement it. Enclose in the class body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Open class

A

Informs the compiler that this class is extendable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Inheritance

A

SubclassName ( optional parameters) : superclass (optional parameters) {
Body
}

To override functions:
- in superclass: open fun
- in subclass override fun
this same can be used for properties

Super - to reuse superclass code in subclass

var objName : superClassName = subclassName () - object of subclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Visibility modifiers

A
  • public (default) accessible everywhere
  • private accessible in the same class or source file
  • protected accessible in subclass
  • internal accessible in this same module

Modifier var name: date type = initial value
Modifier class name { body }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Module

A

A collection of source files and build settings that let you divide your project into discrete units of funcionality

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Package

A

A directory or folder that groups related classes, a module can contain multiple packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Functions as data type

A

They can be stored in variables, passed into functions and returned from functions.

To store a function in variable and refer to it as value use :: ( function reference operator )

val variable = :: functionName

val name:() → Unit ={
body
}

18
Q

Lambda expression with parameters

A

Names in order that they occurs
val functionName = { param1, param2 →
function body
}

When it has only one parameter be can omit its name and use it

19
Q

Function with return type

A

funcName (optional parameters) → returnType {
body
}

If return type is nothing () → Unit

20
Q

Function as a return type

A

fun functionName (): functionType {
return nameOfAnotherFunc
}

21
Q

Passing a function as a parameter to other function

A

If this function has marcumeters too no names only types after comma

fun funcName ( paramName : type, funcName : (paramType, …) → returnType) : () → Unit { }

22
Q

Trailing lambda syntax

A

When a function type is a last parameter
f.e. trickOrTreate(false) lambda expression

23
Q

repeat()

A

Function to express for loop
repeat( times) {

}

24
Q

random ()

A

To get random number
var result = (1..6).random()

25
Q

Text Field ( )

A

Composable function that lets user enter text in an app
Value = “ “ test box that displays string value you pass here
onValueChange ={} lambda callback that is triggered when the user enters texts in text box =it

26
Q

State

A

Any value that can change over time. The only way to modify the composition is through recomposition. Compose needs to know which state to track so it can schedule the recomposition when it receives an update.

27
Q

mutableStateOf()

A

Function to create observable mutable state il receive an initial value

28
Q

keyboardOptions

A

keyboardOptions = KeyboardOptions.

An option to configure the keyboard display on the screen to enter f.e. digits, email, password

29
Q

getCurrencyInstance()

A

To display as currency
NumberFormat.getCurrencyInstance().format(variable)

30
Q

variable.toDoubleOrNull

A

Change string to double or null if it can not to double

31
Q

State hosting

A

Defining state in a way that you can reuse and share yours composable. Use it when:
- you need to share state with multiple composable functions
- create a stateless composable that can be reused in app
This often means introducing to parameters to the composable:
1. value: T // current value to display
2. onValueChanged: (T) → Unit // callback lambda which is triggered when the value changes so that the state can be updated elsewhere

32
Q

%s

A

Placeholder for string

33
Q

Keyboard action button

A

Button at the end of keyboard (search, send, go, next, done)
f.e.
keyboardOptions = KeyboardOptions.Default.copy( keyboardType.Number, imeAction = ImeAction.Next)

34
Q

Switch

A

Components: track, thumb, icon

Switch (
checked = // state of switch composable, whether the switch is checked
onCheckedChange = // the callback to be called when the switch is clicked
)

35
Q

Kotlin.match.ceil()

A

Round up function

36
Q

Automated tests types

A
  • local tests directly tests a small piece of code to ensure that it works properly, you can test class, function or properties. Executed on your work station
  • instrumentation tests a UI test for parts of app that depend on the Android API and its platform API’s and services. You launch au app simulate user interaction and checked whether the app reacted properly.
    The test code is build into its om Android application package (APK)
37
Q

Local test rules

A
  • written in form of method - strictly check an expected output for a given input
  • annotate with @Test
  • name clearly describe what the test is for
  • typically end with assertion which is used to ensure that given condition is met: assertEquals(), assertNotEquals(), assertNotTrue(), assertFalse(), assertNull(), assertNotNull(), assertThat()
38
Q

Class with generic

A

When you want a property to have different date types depending on the specific use case

ClassName < generic data type >( properties ) - data type is provided when instantiating the class

val instanceName = ClassName < actual data type > (parameters)

39
Q

Enum class

A

Is used to create types with limited set of possible values ( each called enum constant )
EnumClassName {
case1, case 2, case 3
}

To refer to enum constant: enumName.caseName

You can use it as data type in other class property
ClassName (
val name : enumClassName
)

40
Q

Data class

A

Contain only data without any methods, defining it allows compiler to make some assumptions and to automatically implement some methods f.e. toString(), equals () , hashCode(), copy(). Needs to have at least one parameter in its constructor. All constructor parameters must be marked with var or val. It can not be: open, abstract, sealed or inner.

DataClassName ( )

41
Q

Singleton object

A

When you want a class to have only one instance
object Name {
class body
}
It can’t have constructor, you can’t create instance directly. All the properties are defined within the curly braces and are given an initial value.

To access: objectName.propertyName