1
What is the correct way to declare an Activity in the AndroidManifest.xml file?
<activity name=".MainActivity" />
<activity android:name=".MainActivity" />
<activity android:activity=".MainActivity" />
<activity class=".MainActivity" />
View Answer
The XML snippet <activity android:name=".MainActivity" /> is used in Android's manifest file (AndroidManifest.xml) to declare an activity component within an application. Here’s a breakdown of its components and significance:
-
<activity> Element: This XML element is used to define an activity component of the application.
-
android:name Attribute: Specifies the name of the activity class. In this case, .MainActivity refers to the MainActivity class located in the current package (package.name), which is specified in the package attribute of the <manifest> element in the manifest file.
-
Activity Declaration: This declaration informs the Android system about the existence of the MainActivity class as an activity component that can be launched and interacted with by the user or other components of the application.
-
Purpose: Activities in Android represent screens or windows where the user can interact with the application. Each activity typically corresponds to a single screen with a user interface, and this declaration allows the system to manage the activity's lifecycle, launch it when necessary (e.g., in response to user actions or intents), and handle its interactions with other activities and components of the application.
-
Manifest Integration: The <activity> declaration is part of the overall manifest file (AndroidManifest.xml), which acts as a descriptor of the application’s structure and components. Activities, along with other components like services and broadcast receivers, are defined within this file to declare their existence, capabilities, and interactions within the Android system.
In summary, <activity android:name=".MainActivity" /> in the manifest file specifies that MainActivity is a component of the application, allowing it to be recognized and managed by the Android system, and enabling its integration into the application's user interface and navigation structure.
2
Which keyword in Kotlin is used to declare a variable whose value cannot be changed once assigned?
var
const
val
final
View Answer
val
Keyword in Kotlin:
-
Immutable Variables:
- Variables declared with
val
are immutable, meaning their value cannot be reassigned once it has been set.
-
Initialization:
val
variables must be initialized when they are declared or in the constructor of the class (for properties).
-
Similarity to final
in Java:
- In Java,
final
is used to declare constants or variables whose value cannot change after initialization. In Kotlin, val
serves a similar purpose for declaring read-only variables.
Benefits of val
:
-
Thread Safety: Immutable variables (val
) are inherently thread-safe because their values cannot be changed after initialization, reducing the risk of concurrency issues.
-
Code Clarity: Using val
makes code more readable by clearly indicating that a variable is intended to be constant or immutable.
-
Functional Programming: Immutable variables align well with functional programming principles, where avoiding mutable state can simplify code and reasoning about program behavior.
When to Use val
:
- Use
val
when the value of a variable is not intended to change after initialization.
- Prefer
val
over var
whenever possible to enforce immutability and reduce unintended side effects.
In summary, val
in Kotlin is essential for declaring read-only variables that maintain their value throughout their scope, providing clarity, safety, and promoting good coding practices akin to using final
in Java.
3
Which of the following is used to declare a dependency in the build.gradle (Module: app) file of an Android project?
implementation
dependency
compile
include
View Answer
In an Android project, particularly when using the Gradle build system, the implementation
keyword plays a crucial role in managing dependencies. Here’s how it is used and its significance:
implementation
in build.gradle:
-
Dependency Declaration:
implementation
is a keyword used in the dependencies
block of the build.gradle
file to declare dependencies that are required for compiling the project's source code.
-
Scope:
- Dependencies declared with
implementation
are used only internally within the module where they are declared. They are not exposed to other modules or libraries that depend on the current module.
Why Use implementation
?
-
Encapsulation: By using implementation
, you encapsulate dependencies within the module, ensuring that they are not leaked to dependent modules unless explicitly exposed.
-
Dependency Management: Gradle handles version conflicts and ensures that dependencies are resolved based on the project's requirements and compatibility constraints.
-
Build Optimization: Using implementation
instead of api
or compileOnly
where appropriate helps optimize build times by reducing unnecessary dependencies exposed to other modules.
4
What is the purpose of the onCreate()
method in an Android Activity?
To initialize Views
To perform background tasks
To handle touch events
To create an instance of the Activity
View Answer
onCreate()
is called when the Activity is first created and is used to initialize the Activity's UI (Views).
While onCreate()
is indeed called when the Activity is first created, its primary purpose extends beyond just initializing the Activity's UI (Views). Here’s a more comprehensive explanation of what onCreate()
does in Android activities:
Purpose of onCreate()
:
-
Initialization of UI and Components:
- UI Initialization:
onCreate()
is often used to inflate the layout XML that defines the UI components (Views
) of the activity using setContentView()
.
- View Binding: It also includes setting up any initial state of UI components (like setting initial text, listeners, etc.).
-
Activity Setup:
- Lifecycle Setup: Initialization of lifecycle-dependent components or services that should only be set up once during the activity's lifecycle.
- Data Binding: Initializing data sources or adapters for
RecyclerView
, ListView
, or other data-driven UI components.
-
Configuration Handling:
- Configuration Changes:
onCreate()
handles configuration changes, ensuring that the activity behaves correctly when the device configuration changes (e.g., orientation change).
- Saved Instance State: It retrieves any saved instance state data (from
onSaveInstanceState()
) and restores the activity’s state accordingly.
Lifecycle Considerations:
-
First Lifecycle Method: onCreate()
is the first method called when an activity is created or recreated after a configuration change.
-
Initialization Order: It is crucial for initializing essential components of the activity before they are interacted with by the user or other parts of the application.
-
Execution Context: The onCreate()
method provides the savedInstanceState
parameter, which allows the activity to restore its previous state if it was previously destroyed and recreated.
5
Which of the following is NOT a valid scope function in Kotlin?
let
also
do
run
View Answer
do
is not a valid scope function in Kotlin. Valid ones include let
, also
, run
, apply
, and with
.
6
In Android, what does the compileSdkVersion
specify in the build.gradle file?
Minimum Android SDK version required
Target Android SDK version
Maximum Android SDK version supported
Android Support Library version
View Answer
compileSdkVersion
specifies the version of the Android SDK against which the app is compiled.
7
What is the purpose of the apply
function in Kotlin?
To execute a block of statements repeatedly
To check if an object is of a certain type
To change the state of an object and return it
To initialize an object and configure its properties
View Answer
apply
is used to initialize an object and configure its properties within a scope.
8
Which of the following is NOT a valid layout file in Android?
activity_main.xml
main_activity.xml
fragment_home.xml
content_layout.xml
View Answer
In Android, layout file names conventionally use activity_
, fragment_
, or content_
prefixes followed by a meaningful name.
9
What does the minSdkVersion
attribute in the build.gradle file specify?
Minimum Android SDK version required
Target Android SDK version
Maximum Android SDK version supported
Android Support Library version
View Answer
minSdkVersion
specifies the minimum version of the Android SDK required to run the application.
10
What is JSON in Android development?
Java Serialization Object Notation
JavaScript Object Notation
Java Object Naming
JavaScript Object Network
View Answer