Kotlin_TutorialPoints Flashcards

1
Q

Language type?

A

Strongly typed
OOP
Functional

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

When is Kotlin made official language for Adroid Dev?

A

2017

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

Prerequisites

A

Knowledge of JAVA, OOP and Class(es)

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

Kotlin VS Java

A
Kotlin: 
Open Source Software
More expressive
Less boiler plate
Better preformance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Kotlin Functions

A

Allowed to be declared at the top level

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

Declarations?

A

Doesn’t need to be Static

Be careful - can cause problems for Devs coming from Java

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

Hello World .kt

A
fun main(args: Array){
 println("Hello, World")
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does run Code on the JVM (Java Virtual Machine)?

A

byte code!

Similar to Java’s (.class)

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

Byte code behavior when in the same JVM?

A

They can communicate with eachother

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

How does Kotlin translate into JavaScript?

A

.kt file is converted into ES5.1

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

Declaring a ‘character’?
Assigning it?
Printing it?

A

val letter: Char
letter = ‘A’
println(“$letter”)

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

Declaring Boolean?

A

val letter: Boolean
letter = true
println(“Your character value is “+”$letter”)

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

Declaring Strings?

A

var rawString :String

val escapedString : String

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

Declaring Arrays?

A

val numbers: IntArray = intArray(1,2,3,4,5)

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

Types of Collections

A

Immutable Collection - cannot be edited

Mutable Collection - can be edited

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

Collection Caution

A

Kotlin system does not represent a difference between the 2 Types

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

Collection - Mutable

A

val numbers: MutableLIst = mutableListOf(1,2,3)

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

Collection - Immutable

A

val readOnlyView: List = numbers

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

Adding item to Mutable Collection

A

.add(item)

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

Collection Functions

A

first( ) - gets first elem
last( ) - gets last
filter( ) - filters through collection

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

How does Ranges work in Kotlin?

A

rangeTo( )
or (. .)
if(i in 1..10) //same as 1<=i && i < = 10

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

if - else

A
if ( ) {
  //code
} else {
  //otherwise code
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Use of When

A

val x:Int = 5
when (x) {
1 -> print(“x = = 1”)
2 -> print(“x = = 2”)

  else -> { // Note the block
     print("x is neither 1 nor 2")
  }    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

For Loop

A

val items = listOf((1,2,3,4)

for (i in items) println(‘values of the array” +i)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
For loop - looping, index and item at that index
val items = listOf(1, 22, 83, 4) for ((index, value) in items.withIndex()) { println("the element at $index is $value") }
26
While - standard loop
while(x <=10){ println( x ) x++ }
27
Return
Return is a keyword that returns some value to the calling function from the called function.
28
Continue & Break
Continue & Break − Continue and break are the most vital part of a logical problem. The “break” keyword terminates the controller flow if some condition has failed and “continue” does the opposite
29
Example Function declaration
``` fun doubleMe(x:Int):Int { return 2*x; } ```
30
What does a break with a Label on it look like?
myLabel@ break@myLabel
31
OOP - 1st line - declaration
class myClass{ }
32
Declaring a private variable within a Class
private var name: String = "Tutorials.point"
33
val VS var
val - *cannot* be changed | var - *can* be changed
34
What is a Nested Class?
A class that has been created inside another class.
35
What is true about the default attribute fora Nested Class?
``` It is Static Can be accessed without creating any class of that class ```
36
How does Kotlin access a Nested Class?
val demo = Outer.Nested.foo( ) print (demo) ``` class Outer{ class Nested{ fun foo () = "Welcome" } } ``` ``` //demo - printed out? // Welcome ```
37
How does an Inner Class work?
``` val demo = Outer().Nested().foo() // calling nested class method print(demo) ``` ``` class Outer { class Nested { fun foo() = "Welcome to The TutorialsPoint.com" } } //======== Uses an Obj declaration and uses the inner Class to access a private val ```
38
Anonymous Inner Class is...
The concept of creating an object of interface using runtime object reference is known as anonymous class
39
What are Type Aliases?
A property of Kotlin compiler | Provides the flexibility of creating a new name of an Existing Type
40
Types of constructors in Kotlin?
1. primary constructor | 2. secondary constructor
41
Rules about Kotlin class(es) and constructors?
Kotlin class can have one (1) primary constructor and (1 or 1+) secondary constructor
42
Java VS Kotlin - constructors?
Java constructor - initializes the member variables Kotlin constructor - the primary constructor initializes the class, where as the secondary constructor; helps to include logic while initializing the same
43
Kotlin Primary constructor - declared how?
Primary constructor can be declared at class header level!
44
Kotlin Primary Constructor - Example
``` class Person(val firstName: String, var age: Int){ //class body ```
45
How to init a new Class?
val / var person1 = Person(name, birthday)
46
Kotlin - primary constructor
Only declared in the head/beginning of Class
47
Kotlin - secondary constructor
Needs 'constructor' keyword | Can be implemented 1 or more times
48
Inheritance in Kotlin?
Base class is named "Any"
49
"Any" is the...
Super class of the 'any' default class declared in Kotline
50
Kotlin inheritance keyword?
":"
51
To override a function?
override fun example( )
52
Kotlin Interface - keyword meaning?
# Define an interface in Kotlin as shown in the following piece of code. [visibility] interface InterfaceName [extends other interfaces] { constant declarations abstract method declarations }
53
Uses of interface(s)
Being able to use an object without knowing its type of class,
54
Limitation of Kotlin?
It doesn't support multiple inheritances, however (!) the same thing can be achevied by implementing more than two interfaces at a time
55
In Kotlin - what are the 4 different 'Access modifiers'?
Private Protected Internal Public
56
Private (Access modifier)
when declared Private - only available within immediate scope
57
Protected (Access modifier)
Not available for top level declaration; protected class or interface is visible to it's subclass only
58
Internal (Access modifier)
``` New to Kotlin(!) If marked as Internal -then that specific file will be in the internal field. Internal package - only available inside the module under which it is implemented Internal class - only viewable to by other class(es) in the same package or module ```
59
Public (Access modifier)
Public modifier is accessible from anywhere in the project. | If no Access modifier is specified - autos to Public
60
Extension
Allows you to define a method outside the main class. To test some code - without affecting the other
61
Extension for Objejcts
Uses the 'companion' keyword.
62
Define a Data Class
A type of class that is used to hold data and does not do much else
63
Data Class
Just a function to allow for Objects to be initialized and populated
64
"Sealed" Class
- Used to represent a restricted hierarchy. - Allows the developers to maintain a data type. - Need to use the Sealed keyword - Sealed class can have its own subclass, butt they must all be in the same Kotlin file
65
Generics
Usually written - t for template
66
Kotlin - class VS type
- List is a class | - List is a type
67
Generic - behavior
Allows for different types of data to be accessed and used
68
Want to assign the generic type to super type?
Use "out" keyword.
69
Want to assign the generic type to sub-type?
Use "in" keyword
70
What keyword shows Kotlin's "delegation" design patter?
``` "by" Using this keyword or delegation methodology, Kotlin allows the derived class to access all the implemented public methods of an interface through a specific object ```
71
Delegation means?
Passing the responsibility to another class or method
72
What to do when a property is already declared in some places?
Reuse the same code to initialize them
73
What is a lambda function?
A function type to represent functions, and provides a set of specialized language constructs
74
What is a higher-order function?
A higher-order function is a function that takes functions as parameters, or returns a function.
75
Function types in Kotlin
Kotlin uses function types, such as (Int) -> String
76
Using Lazy( )
Lazy( ) - is a lambda function. It will return Lazy, where is basically the type of the properties using it
77
Delegaton.Observerable()
Takes 3 arguments to initialize the obj and returns the same to the called function
78
General Syntax
get( ), set( ) | getValue( ), setValue( )
79
How to declare a Function?
fun myFunction
80
Fun - example
``` fun main(args: Array) { println(MyFunction("tutorialsPoint.com")) } fun MyFunction(x: String): String { var c:String = "Hey!! Welcome To ---" return (c+x) } ```
81
Syntax of Fun
fun (:):
82
Lambda Function (?)
Lambda is a high level function that drastically reduces the boiler plate code while declaring a function and defining the same
83
Inline Function
pass a lambda to another function to get our output which makes the calling function an inline function
84
Declaring Multiple variables
val (name, age) = person
85
Real World - Destructing Declarations
``` fun main(args: Array) { val s = Student("TutorialsPoint.com","Kotlin") val (name,subject) = s println("You are learning "+subject+" from "+name) } data class Student( val a :String,val b: String ){ var name:String = a var subject:String = b } ```
86
Exception Handling
``` try { // does this code work? } catch(e: Exception) { // what error tripped? } finally { // always run this } ```