Android
Android
The main difference between Kotlin and Java when it comes to variable
declaration is that Kotlin requires you to specify the type of the variable
explicitly, whereas in Java, the type can be inferred from the value being
assigned to the variable.
}
3. Write a program to print factorial of number
24 in kotlin.
Ans - fun main(args: Array<String>) {
val num = 5
var factorial = 1
for (i in 1..num) {
factorial *= i
}
println("Factorial of $num = $factorial")
}
println("Hello, World!")
fun main() {
sayHello()
}
8. Explain Boolean operator with example in
kotlin
Ans - Kotlin has three Boolean operators: && (logical AND), || (logical OR),
and ! (logical NOT). Here are some examples of how these operators work
in Kotlin:
Logical AND (&&): This operator returns true only if both operands are
true. Otherwise, it returns false. For example:
fun main(){
val a = true
val b = false
println(a && b)
Logical OR (||): This operator returns true if either operand is true. If both
operands are false, it returns false. For example:
fun main(){
val a = true
val b = false
println(a || b)
Logical NOT (!): This operator negates the operand, returning true if it is
false, and false if it is true. For example:
fun main(){
val a = true
val b = !a
println(b)
val a = 5
val b = 3
println(a + b)
}
Subtraction (-): This operator subtracts one value from another. For
example:
fun main(){
val a = 5
val b = 3
println(a - b)
}
Multiplication (*): This operator multiplies two values together. For
example:
fun main(){
val a = 5
val b = 3
println(a * b)
}
Division (/): This operator divides one value by another. For example:
fun main(){
val a = 5
val b = 3
println(a / b)
}
Modulo (%): This operator returns the remainder of one value divided by
another. For example:
fun main(){
val a = 5
val b = 3
println(a % b)
fun main(){
var c = MyClass("Hello")
for (i in 1..10) {
if (i == 5) {
break
}
println(i)
}
}
In this example, the for loop prints the numbers from 1 to 10, but the loop
terminates when i becomes 5 due to the break statement. Therefore, the
output of the program is:
1234
The continue statement is used to skip the current iteration of a loop
and move to the next iteration. When the continue statement is
encountered inside a loop, the current iteration is skipped, and the loop
continues with the next iteration. Here's an example:
fun main(){
for (i in 1..10) {
if (i % 2 == 0) {
continue
}
println(i)
}
In this example, the for loop prints only the odd numbers from 1 to 10.
The if statement inside the loop checks if i is even using the modulo
operator %. If i is even, the continue statement is executed, and the loop
continues with the next iteration. Therefore, the output of the program is:
13579
Both break and continue statements can be used with while and do-while
loops as well.
In this example, the loop iterates as long as the value of i is less than or
equal to 5. Inside the loop body, the current value of i is printed, and then
i is incremented by 1 using the ++ operator.
// private declaration
private class PrivateClass
// internal declaration
internal class InternalClass
// protected declaration
open class ParentClass {
protected val protectedProperty = "protected"
}