Created
November 26, 2021 12:21
-
-
Save rozer007/bd0e6e4dfbdcf8b0543fe67de1d5b21d to your computer and use it in GitHub Desktop.
interface and null safety in kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q1) What is an elvis operator? | |
ans: It is an operator that give a default value when the original value is null. | |
Q2) Why do we use interface ? | |
ans: We use interface to achieve total abstraction. | |
Q3) Why can't we assign null to a normal variable? | |
ans: Because to eliminate the danger of null references. | |
Q4)What will happen if we assigned null to a normal variable? | |
ans:The program will give a NullPointerException. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q1) What is the correct way to assign null ? | |
a) var a = null | |
b) var b: String!! = null | |
c) var c:String?=null | |
d) none of the above | |
ans : c) | |
Q2) what is the output ? | |
fun main() | |
{ | |
var c: String ?= null | |
var n2=n?:"null" | |
println(n2) | |
} | |
a) not null | |
b) error | |
c) null | |
d) nullpointerexceptio | |
ans:c) | |
Q3)what will be the output ? | |
fun main() { | |
var b=ddd("ko",true); | |
b.get() | |
} | |
interface jiop{ | |
var b:Boolean | |
fun get():Unit | |
} | |
class ddd(val name:String,var b:Boolean):jiop | |
{ | |
override fun get():Unit | |
{ | |
print("dd is ${name}") | |
} | |
} | |
a) null | |
b) error | |
c) dd is ko | |
d) none of the above | |
ans: error | |
Q4) Which is not null operator? | |
a) ? | |
b) !! | |
c) non null | |
d) none of the above | |
ans: b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment