Busy. Please wait.
or

show password
Forgot Password?

Don't have an account?  Sign up 
or

Username is available taken
show password

why


Make sure to remember your password. If you forget it there is no way for StudyStack to send you a reset link. You would need to create a new account.
We do not share your email address with others. It is only used to allow you to reset your password. For details read our Privacy Policy and Terms of Service.


Already a StudyStack user? Log In

Reset Password
Enter the associated with your account, and we'll email you a link to reset your password.

Remove Ads
Don't know
Know
remaining cards
Save
0:01
To flip the current card, click it or press the Spacebar key.  To move the current card to one of the three colored boxes, click on the box.  You may also press the UP ARROW key to move the card to the "Know" box, the DOWN ARROW key to move the card to the "Don't know" box, or the RIGHT ARROW key to move the card to the Remaining box.  You may also click on the card displayed in any of the three boxes to bring that card back to the center.

Pass complete!

"Know" box contains:
Time elapsed:
Retries:
restart all cards




share
Embed Code - If you would like this activity on your web page, copy the script below and paste it into your web page.

  Normal Size     Small Size show me how

Kotlin-2

Programming Language

QuestionAnswer
What are the 2 keywords for declaring variables ? val – immutable var - mutable one two three
Define constant foo=Test. val foo = "Test"
Define explicit type of Number for foo variable. var foo:Number = 12.3
Is referential equality guaranteed on boxed values ? No Even if same value, may have been boxed in different locations.
What are the built-in number types and their sizes ? Long - 64 Int - 32 Short - 16 Byte - 8 Double - 64 Float - 32
Define variable foo of type long = 12,333 val foo = 12333L
Define variable foo of type double = 12.33 val foo = 12.33
Define variable foo of type float = 12.33 val foo = 12.33F
Create a number literal for each built-in number type. Long val foo = 1234L Int val foo = 1234 Double val foo = 12.34 Float val foo = 12.34F Hexadecimal val foo = 0xAB Binary val foo = 0b01010101
What is the default type for floating point numbers ? Double
What is the default for integral numbers ? Int
Convert an Int to a Long. val fooInt = 1234 val fooLong = fooInt.toLong()
Convert Float to Double. val fooFloat = 12.34F val fooDouble = fooFloat.toDouble()
What are the boolean operations ? There are 3: negation conjunction disjunction Bonus: conjunction and disjunction are lazy
What are the bitwise operators ? shl - Shift Left shr - Shift Right ushr - Unsigned Right Shift and or xor
What are the Char escape sequences ? \t \b \n \r ' " \\ \$ \u1234 Bonus: Char type not treated as a number, as in Java.
What is a raw string ? What is the syntax ? Typical use ? No escaping is necessary, so all characters can be included. val foo = """ This is the first line second line of the string third line with special character / in the string""" Typically used for multi-line strings.
What functions are provided by an array ? iterator() size() get(index) set(inces, value) Bonus: get() and set() are also available through bracket syntax. val element1 = fooArray[0]
What are the primitive array classes ? Why use them ? ByteArray CharArray ShortArray IntArray LongArray BooleanArray FloatArray DoubleArray Avoids boxing to improve performance
What are the max values for built-in Number types ? They can be retrieved using Type.MAX_VALUE. Int.MAX_VALUE = Long.MAX_VALUE = Short.MAX_VALUE = Float.MAX_VALUE = Double.MAX_VALUE =
Create an Int array of 1, 2, 3. val fooIntArray = arrayOf(1, 2, 3)
How are Kotlin arrays different from Java arrays ? They are not part of the language. Arrays are regular collection classes.
What is the syntax for comments ? Same as Java. // line comment /* A block comment, which can span many lines. */
How are packages handled in Kotlin ? Same as Java. package com.company.theapp class Foo
How are imports handled in Kotlin ? Same as Java. import com.company.theapp.Foo import com.company/theapp/*
Give an example of import renaming. package com.company.theapp.Foo package com.company/theotherapp.Foo as Foo2
Give an example of string templates. val name = "Sam" val nameHello = "Hello $name" val nameLength = "$nameHello. Your name has ${name.length} characters." Bonus: Also called string interpolation.
Explain ranges. A range is an interval that has a start value and an end value. Any types which are comparable can be used to create a range, using the ".." operator val aToZ = "a".."z" val oneToNine = 1..9
Explain the "in" operator. The "in" operator is used to test whether a given value is included in the range. val aToZ = "a".."z" val isTrue = "c" in aToZ
Give an examples of using step() and reversed() with a range. val oddNumbers = (1..50).step(2) val countingDownEvenNumbers = (2..100).step(2).reversed() Bonus: val countDown = 100.downTo(0) val foo = 10 val rangeTo = foo.rangeTo(20)
Give an example of a while loop. while (true) { println("This will print forever!") }
Give an example of a for loop. val fooList = listOf(1,2,3,4) for (k in fooList) { println(k) } Note the required use of the "in" keyword. Bonus: "for" loop can be used with any object that implements iterator()
What must iterator() return ? An object that implements: hasNext() : Boolean next() : T
Why would you use a Range instead of a List ? Ranges are compiled into index-based for loops that are supported directly on the JVM, so the iterator introduces no performance penalty.
How would you iterate over the characters in a String ? val foo = "some characters" for (char in foo) { println(char) }
What is the key difference between Java and Kotlin for exception handling ? All exceptions in Kotlin are unchecked. In Java, unchecked exceptions are those that do not need to be added to method signatures. In Kotlin, exceptions are never part of the method signature.
Explain the rules for coding Kotlin exceptions. - try, catch and finally blocks (same as Java) - zero or more catch blocks - zero or one finally block - at least one catch or finally block is required
Give an example of a try-catch block. try { something() } catch (e: SomeException) { println("caught SomeException") } finally { cleanup() }
Instantiate class Foo. Constructor takes a single Long argument. val foo = Foo(100L) Unlike Java, there is no "new" operator.
Describe referential equality versus structural equality. Referential equality - 2 objects point to the same memory address. Operators: === !== Structural equality - determined by using the equals function of the class. Operators: == (not the same as Java) != (not the same as Java) Bonus: The == and != operators are null safe.
Explain the usage of "this". "this" refers to the current receiver. Current receiver is the instance that received the invocation of the function. Extension functions can reference "this".
What is the current receiver ? Current receiver is the instance that received the invocation of a function. Can be referenced with "this".
How would you refer to member variable "address" of an outer instance of type Building ? inner class Foo() { fun printAddress() = println(this@Building.address) } Note the use of the "@" notation.
What are the visibility modifiers ? 3 visibility modifiers: - private - protected - internal
Explain the private visibility modifier. Private top-level functions, classes or interfaces can only be accessed from the same file. Inside a class, interface, or object, any private function or property is only visible to other members.
Explain the protected visibility modifier. Top-level functions, classes or interfaces cannot be protected. Only functions or properties inside a class or interface can be protected. They are only visible to members of that class or interface, or to subclasses.
Explain the internal modifier. Any code that is marked internal is visible from other classes and functions inside the same module. This can be a Maven or Gradle module, or an IntelliJ/Android module.
Explain expression versus statement. An expression evaluates to a value. "hello".startsWith("h") A statement has no resulting value returned. val a = 1 Bonus: In Kotlin, if...else and try...catch blocks are expressions. return if (x==0) true else false // note that the if clause must have an else val success = try { doSomething() true } catch (e: IOException) { false } Note: Expressions can be blocks. The last line must be an expression, which is returned by the block.
Explain null syntax. A variable which can be assigned null must be declared with a "?". var str: String? = null
Explain the "is" operator. Used for type checking at run time. fun isString(fooAny: Any): Boolean { return if (fooAny is String) true else false } Note: "!is" can also be used.
Explain smart casts. If a variable has been type checked (is), it is implicitly cast to the more specific type within that scope/block.
Explain explicit casting. Use the "as" keyword to cast an object. fun length(fooAny: Any): Int { val aString = fooAny as String return aString.length } Bonus: val fooString: String? = fooAny as String // in case fooAny is null
What is the "safe cast" operator ? If we want to avoid the ClassCastException when a cast fails, we should use the "as?" operator. This will return null if the cast fails. val fooAny = "some string" val someString: String? = fooAny as? String val aFile: File? = fooAny as? File
Created by: flywheelms