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
Q

For loop - looping, index and item at that index

A

val items = listOf(1, 22, 83, 4)

for ((index, value) in items.withIndex()) {
println(“the element at $index is $value”)
}

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

While - standard loop

A

while(x <=10){
println( x )
x++
}

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

Return

A

Return is a keyword that returns some value to the calling function from the called function.

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

Continue & Break

A

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

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

Example Function declaration

A
fun doubleMe(x:Int):Int {
   return 2*x;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What does a break with a Label on it look like?

A

myLabel@

break@myLabel

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

OOP - 1st line - declaration

A

class myClass{

}

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

Declaring a private variable within a Class

A

private var name: String = “Tutorials.point”

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

val VS var

A

val - cannot be changed

var - can be changed

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

What is a Nested Class?

A

A class that has been created inside another class.

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

What is true about the default attribute fora Nested Class?

A
It is Static
Can be accessed without creating any class of that class
36
Q

How does Kotlin access a Nested Class?

A

val demo = Outer.Nested.foo( )
print (demo)

class Outer{
 class Nested{
  fun foo () = "Welcome"
 }
}
//demo - printed out?
// Welcome
37
Q

How does an Inner Class work?

A
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
Q

Anonymous Inner Class is…

A

The concept of creating an object of interface using runtime object reference is known as anonymous class

39
Q

What are Type Aliases?

A

A property of Kotlin compiler

Provides the flexibility of creating a new name of an Existing Type

40
Q

Types of constructors in Kotlin?

A
  1. primary constructor

2. secondary constructor

41
Q

Rules about Kotlin class(es) and constructors?

A

Kotlin class can have one (1) primary constructor and (1 or 1+) secondary constructor

42
Q

Java VS Kotlin - constructors?

A

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
Q

Kotlin Primary constructor - declared how?

A

Primary constructor can be declared at class header level!

44
Q

Kotlin Primary Constructor - Example

A
class Person(val firstName: String, var age: Int){
 //class body
45
Q

How to init a new Class?

A

val / var person1 = Person(name, birthday)

46
Q

Kotlin - primary constructor

A

Only declared in the head/beginning of Class

47
Q

Kotlin - secondary constructor

A

Needs ‘constructor’ keyword

Can be implemented 1 or more times

48
Q

Inheritance in Kotlin?

A

Base class is named “Any”

49
Q

“Any” is the…

A

Super class of the ‘any’ default class declared in Kotline

50
Q

Kotlin inheritance keyword?

A

”:”

51
Q

To override a function?

A

override fun example( )

52
Q

Kotlin Interface - keyword meaning?

A

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
Q

Uses of interface(s)

A

Being able to use an object without knowing its type of class,

54
Q

Limitation of Kotlin?

A

It doesn’t support multiple inheritances, however (!) the same thing can be achevied by implementing more than two interfaces at a time

55
Q

In Kotlin - what are the 4 different ‘Access modifiers’?

A

Private
Protected
Internal
Public

56
Q

Private (Access modifier)

A

when declared Private - only available within immediate scope

57
Q

Protected (Access modifier)

A

Not available for top level declaration; protected class or interface is visible to it’s subclass only

58
Q

Internal (Access modifier)

A
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
Q

Public (Access modifier)

A

Public modifier is accessible from anywhere in the project.

If no Access modifier is specified - autos to Public

60
Q

Extension

A

Allows you to define a method outside the main class. To test some code - without affecting the other

61
Q

Extension for Objejcts

A

Uses the ‘companion’ keyword.

62
Q

Define a Data Class

A

A type of class that is used to hold data and does not do much else

63
Q

Data Class

A

Just a function to allow for Objects to be initialized and populated

64
Q

“Sealed” Class

A
  • 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
Q

Generics

A

Usually written - t for template

66
Q

Kotlin - class VS type

A
  • List is a class

- List is a type

67
Q

Generic - behavior

A

Allows for different types of data to be accessed and used

68
Q

Want to assign the generic type to super type?

A

Use “out” keyword.

69
Q

Want to assign the generic type to sub-type?

A

Use “in” keyword

70
Q

What keyword shows Kotlin’s “delegation” design patter?

A
"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
Q

Delegation means?

A

Passing the responsibility to another class or method

72
Q

What to do when a property is already declared in some places?

A

Reuse the same code to initialize them

73
Q

What is a lambda function?

A

A function type to represent functions, and provides a set of specialized language constructs

74
Q

What is a higher-order function?

A

A higher-order function is a function that takes functions as parameters, or returns a function.

75
Q

Function types in Kotlin

A

Kotlin uses function types, such as (Int) -> String

76
Q

Using Lazy( )

A

Lazy( ) - is a lambda function. It will return Lazy, where is basically the type of the properties using it

77
Q

Delegaton.Observerable()

A

Takes 3 arguments to initialize the obj and returns the same to the called function

78
Q

General Syntax

A

get( ), set( )

getValue( ), setValue( )

79
Q

How to declare a Function?

A

fun myFunction

80
Q

Fun - example

A
fun main(args: Array) {
   println(MyFunction("tutorialsPoint.com"))
}
fun MyFunction(x: String): String {
   var c:String  = "Hey!! Welcome To ---"
   return (c+x)
}
81
Q

Syntax of Fun

A

fun (:):

82
Q

Lambda Function (?)

A

Lambda is a high level function that drastically reduces the boiler plate code while declaring a function and defining the same

83
Q

Inline Function

A

pass a lambda to another function to get our output which makes the calling function an inline function

84
Q

Declaring Multiple variables

A

val (name, age) = person

85
Q

Real World - Destructing Declarations

A
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
Q

Exception Handling

A
try {
//   does this code work?
} catch(e: Exception) {
//   what error tripped?
} finally {
//    always run this
}