Android Development Question Bank
Android Development Question Bank
Unit I
Kotlin is a general-purpose, statically typed, and open-source programming language. It runs on JVM
and can be used anywhere Java is used today. It can be used to develop Android apps, server-side
apps and much more.
o Concise: Kotlin reduces writing the extra codes. This makes Kotlin more concise.
o Null safety: Kotlin is null safety language. Kotlin aimed to eliminate the
NullPointerException (null reference) from the code.Interoperable.
o Interoperable: Kotlin easily calls the Java code in a natural way as well as Kotlin
code can be used by Java.
o Smart cast: It explicitly typecasts the immutable values and inserts the value in
its safe cast automatically.
o Compilation Time: It has better performance and fast compilation time.
o Tool-friendly: Kotlin programs are build using the command line as well as any
of Java IDE.
o Extension function: Kotlin supports extension functions and extension
properties which means it helps to extend the functionality of classes without
touching their code.
2. What are different forms of Kotlin If Expression. Explain any one with example.
In Kotlin, if is an expression is which returns a value. It is used for control the flow
of program structure. There is various type of if expression in Kotlin.
o if-else expression
o if-else if-else ladder expression
o nested if expression
if(condation){
//code statement
}
Syntax of traditional if else statement
if(condation){
//code statement
}
else{
//code statement
}
Example:-
fun main(args: Array<String>) {
val num1 = 10
val num2 =20
val result = if (num1 > num2) "$num1 is greater than $num2" else "$num1 is
smaller than $num2"
println(result)
}
Example:-
var number = 4
when(number) {
1 -> println("One")
2 -> println("Two")
3 -> println("Three")
4 -> println("Four")
5 -> println("Five")
else -> println("invalid number")
}
}
Output:- Four
4. What are different forms of for Loop in Kotlin? Explain any one in detail.
Kotlin for loop is used to iterate a part of program several times. It iterates through arrays,
ranges, collections, or anything that provides for iterate. Kotlin for loop is equivalent to
the foreach loop in languages like C#.
If the body of for loop contains only one single line of statement, it is not necessary to
enclose within curly braces {}.
o public
o protected
o internal
o private
public modifier
A public modifier is accessible from everywhere in the project. It is a default modifier
in Kotlin. If any class, interface etc. are not specified with any access modifier then that
class, interface etc. are used in public scope.
protected modifier
A protected modifier with class or interface allows visibility to its class or subclass only.
A protected declaration (when overridden) in its subclass is also protected modifier
unless it is explicitly changed.
internal modifier
The internal modifiers are newly added in Kotlin, it is not available in Java. Declaring
anything makes that field marked as internal field. The internal modifier makes the
field visible only inside the module in which it is implemented.
internal class Example{
internal val x = 5
internal fun getValue(){
}
}
internal val y = 10
In above, all the fields are declared as internal which are accessible only inside the module
in which they are implemented.
private modifier
A private modifier allows the declaration to be accessible only within the block in which
properties, fields, etc. are declare. The private modifier declaration does not allow to access
the outside the scope. A private package can be accessible within that specific file.
private class Example {
private val x = 1
private valdoSomething() {
}
}
In above class Example,val x and function doSomthing() are declared as private. The class
"Example" is accessible from the same source file, "val x" and "fun doSomthing()" are
accessible within Example class.
Example of Visibility Modifier
Kotlin Constructor
In Kotlin, constructor is a block of code similar to method. Constructor is declared
with the same name as the class followed by parenthesis '()'. Constructor is used to
initialize the variables at the time of object creation.
1. Primary constructor
2. Secondary constructor
There is only one primary constructor in a Kotlin class whereas secondary constructor
may be one or more.
When the object of myClasss is created, it initializes name and id with "Ashu"
and "101" respectively.
println("Name = ${ myclass.name}")
println("Id = ${ myclass.id}")
Output:
Name = Ashu
Id = 101
println("Name = ${e_name}")
println("Id = ${e_id}")
}
}
1. fun main(args: Array<String>){
2. val myclass = myClass ("Ashu", 101)
3.
4. }
Output:
Name = Ashu
Id = 101
In above code, parameters name and id accept values "Ashu" and "101" when myclass
object is created. The properties name and id are used without "val" or "var", so they
are not properties of myClass class.
class myClass{
constructor(id: Int){
//code
}
constructor(name: String, id: Int){
//code
}
}
Let's see an example of secondary constructor assigning the value while object of class
is created.
class myClass{
Output:
Name = Ashu
Id = 101
We can also use both primary as well as secondary constructor in a same class. By
using primary as well secondary constructor in same class, secondary constructor
needs to authorize to primary constructor. Authorization to another constructor in
same class is done using this() keyword.
AD
For example:
Output:
Name = Ashu
Id = 101
Password = mypassword
Calling one secondary constructor from another
secondary constructor of same class
In Kotlin, one secondary constructor can call another secondary constructor of same
class. This is done by using this() keyword.
For example:
class myClass{
Output:
AD
Output:
7. How Kotlin functions are defined? Explain named and default argument in Kotlin.
Kotlin Function
Function is a group of inter related block of code which performs a specific task.
Function is used to break a program into different sub module. It makes reusability of
code and makes program more manageable.
In Kotlin, functions are declared using fun keyword. There are two types of functions
depending on whether it is available in standard library or defined by user.
For example
Output:
o Here, sqrt() is a library function which returns square root of a number (Double value).
o print() library function which prints a message to standard output stream.
AD
Kotlin functions are declared using the fun keyword. For example:
fun functionName(){
// body of function
}
We have to call the function to run codes inside the body of the function.
functionName()
Kotlin simple function example
fun main(args: Array<String>){
sum()
print("code after sum")
}
fun sum(){
var num1 =5
var num2 = 6
println("sum = "+(num1+num2))
}
Output:
sum = 11
code after sum
Kotlin Parameterize Function and Return Value
Functions are also takes parameter as arguments and return value. Kotlin functions are
defined using Pascal notation, i.e. name:type (name of parameter and its type).
Parameters in function are separated using commas.
If a function does not returns any value than its return type is Unit. It is optional to
specify the return type of function definition which does not returns any value.
Output:
11
If you have already programmed in Java, you will find Kotlin a more explicit language. In Java,
every method is virtual by default; therefore, each method can be overridden by any derived
class. In Kotlin, you would have to tag the function as being opened to redefine it. To do so, you
need to add the open keyword as a prefix to the method definition, and when you redefine the
method, you specifically have to mark it using the override keyword:
Example:-
println("Flying a cessna")
println("Flying a cessna")
get() = "Base::value"
get() = "Derived::value"
get() = _propFoo
set(value) {
_propFoo = value
println("BaseB:${baseB.propertyFoo}")
println("DerivedB:${derivedB.propertyFoo}")
interface VendorImage {
super<VendorImage>.save(output)
super<Image>.save(output)
}
val os = ByteArrayOutputStream()
pngImage.save(os)
Android Architecture
android architecture or Android software stack is categorized into five parts:
1. linux kernel
2. native libraries (middleware),
3. Android Runtime
4. Application Framework
5. Applications
2) Native Libraries
On the top of linux kernel, their are Native libraries such as WebKit, OpenGL,
FreeType, SQLite, Media, C runtime library (libc) etc.
The WebKit library is responsible for browser support, SQLite is for database, FreeType
for font support, Media for playing and recording audio and video formats.
3) Android Runtime
In android runtime, there are core libraries and DVM (Dalvik Virtual Machine) which is
responsible to run android application. DVM is like JVM but it is optimized for mobile
devices. It consumes less memory and provides fast performance.
AD
4) Android Framework
On the top of Native libraries and android runtime, there is android framework.
Android framework includes Android API's such as UI (User Interface), telephony,
resources, locations, Content Providers (data) and package managers. It provides a lot
of classes and interfaces for android application development.
5) Applications
On the top of android framework, there are applications. All applications such as home,
contact, settings, games, browsers are using android framework that uses android
runtime and libraries. Android runtime and native libraries are using linux kernal.
10. Describe Android activity life cycle with the help of diagram.
By the help of activity, you can place all your UI components or widgets in a single
screen.
The 7 lifecycle method of Activity describes how activity will behave at different states.
Method Description
onResume called when activity will start interacting with the user.
</android.support.constraint.ConstraintLayout>
Android Activity Lifecycle Example
It provides the details about the invocation of life cycle methods of activity. In this
example, we are displaying the content on the logcat.
File: MainActivity.java
package example.javatpoint.com.activitylifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
}
}