Chapter - II (K2 - Understanding)
Design Android UI Layout
Classify the following types of layouts in Android:
a) LinearLayout, RelativeLayout, FrameLayout
b) DatabaseLayout, GraphLayout, ImageLayout
c) TextLayout, ButtonLayout, ListLayout
d) None of the above
Answer: a) LinearLayout, RelativeLayout, FrameLayout
Compare LinearLayout and RelativeLayout in terms of their arrangement of UI elements.
a) LinearLayout arranges elements vertically or horizontally, while RelativeLayout arranges them relative to each other.
b) Both layouts arrange elements randomly.
c) LinearLayout can only arrange elements vertically, while RelativeLayout can only arrange them horizontally.
d) Both layouts are identical in function.
Answer: a) LinearLayout arranges elements vertically or horizontally, while RelativeLayout arranges them relative to each other.
Contrast ConstraintLayout with LinearLayout: a) ConstraintLayout is simpler and easier to use than LinearLayout.
b) LinearLayout supports complex layouts, while ConstraintLayout does not.
c) ConstraintLayout allows for more flexible and complex arrangements compared to LinearLayout.
d) There is no difference; both serve the same purpose.
Answer: c) ConstraintLayout allows for more flexible and complex arrangements compared to LinearLayout.
Demonstrate how to make a Button clickable in XML. a) android
="true"
b) android
="true"
c) android
="true"
d) None of the above
Answer: a) android
="true"
Explain the purpose of the android:layout_width attribute.
a) It defines the height of a UI element.
b) It specifies the width of a UI element.
c) It sets the visibility of a UI element.
d) It provides padding for a UI element.
Answer: b) It specifies the width of a UI element.
Extend the understanding of XML layouts by stating how you can include images.
a) Using the <Image> tag
b) By placing images in the drawable folder and referencing them with android:src
c) Images cannot be included in XML layouts
d) By using the <View> tag with a specific attribute
Answer: b) By placing images in the drawable folder and referencing them with android:src
Illustrate how to create a TextView in XML.
a) <TextView>Sample Text</TextView>
b) <TextView android:text="Sample Text"/>
c) <TextView content="Sample Text"/>
d) <Text android:text="Sample Text"/>
Answer: b) <TextView android:text="Sample Text"/>
Infer the primary benefit of using a ScrollView in a layout.
a) It displays multiple items in a single row.
b) It allows users to scroll through content that exceeds the visible screen area.
c) It only displays images.
d) It is used for navigation purposes.
Answer: b) It allows users to scroll through content that exceeds the visible screen area.
Interpret the term "ViewGroup" in Android UI design.
a) A single UI element
b) A layout that holds multiple views
c) A data storage structure
d) An animation class
Answer: b) A layout that holds multiple views
Outline the main purpose of using Drawable resources in layouts.
a) To define interactive elements
b) To display images, shapes, and other graphics
c) To manage background processes
d) To save data
Answer: b) To display images, shapes, and other graphics
Relate the role of LayoutInflater in Android development.
a) It converts XML layout files into corresponding View objects.
b) It manages database connections.
c) It is used for network communication.
d) It handles background tasks.
Answer: a) It converts XML layout files into corresponding View objects.
Rephrase the concept of "Density Independence" in UI design.
a) Ensuring all layouts appear the same on all devices
b) Designing layouts that adapt to different screen sizes and densities
c) Creating static layouts that do not change
d) None of the above
Answer: b) Designing layouts that adapt to different screen sizes and densities
Show how you would implement padding in a layout.
a) android
="10dp"
b) android
="10dp"
c) android
="10dp"
d) android
="10dp"
Answer: a) android
="10dp"
Summarize the key attributes that control layout behavior in XML. a) layout_width, layout_height, layout_margin, layout_gravity
b) width, height, margin, gravity
c) size, area, space, position
d) None of the above
Answer: a) layout_width, layout_height, layout_margin, layout_gravity
Translate the XML code for a Button with the text "Click Me". a) <Button android:text="Click Me"/>
b) <Button content="Click Me"/>
c) <Button text="Click Me"/>
d) <Button title="Click Me"/>
Answer: a) <Button android:text="Click Me"/>
Activity Lifecycle
Classify the main states of an Android activity lifecycle. a) Active, Background, Stopped
b) Created, Started, Resumed, Paused, Stopped, Destroyed
c) Initial, Running, Ended
d) None of the above
Answer: b) Created, Started, Resumed, Paused, Stopped, Destroyed
Compare onStart() and onResume() methods. a) Both are called when the activity is first created.
b) onStart() is called when the activity becomes visible, while onResume() is called when it is interactive.
c) onResume() is called first, then onStart().
d) They serve the same function.
Answer: b) onStart() is called when the activity becomes visible, while onResume() is called when it is interactive.
Contrast onPause() with onStop() in terms of activity visibility. a) onPause() indicates the activity is no longer visible, while onStop() indicates it is finishing.
b) onPause() is when the activity is partially obscured, while onStop() is when it is completely hidden.
c) Both methods serve identical purposes.
d) onStop() is called before onPause().
Answer: b) onPause() is when the activity is partially obscured, while onStop() is when it is completely hidden.
Demonstrate how to save UI state in an activity. a) Override onSaveInstanceState()
b) Use SharedPreferences
c) Store data in a local database
d) Call onPause()
Answer: a) Override onSaveInstanceState()
Explain the lifecycle state transition from onPause() to onStop(). a) The activity is visible but not interactive; the user is leaving.
b) The activity is destroyed.
c) The activity is fully active.
d) The activity enters a background state.
Answer: a) The activity is visible but not interactive; the user is leaving.
Extend the functionality of an activity by describing how to restart it. a) Simply call onStart() again.
b) Finish the current activity and create a new instance.
c) Call onCreate() again.
d) Use a back navigation gesture.
Answer: b) Finish the current activity and create a new instance.
Illustrate the importance of the onDestroy() method. a) It initializes resources.
b) It releases resources before the activity is destroyed.
c) It is not important.
d) It is used for network operations.
Answer: b) It releases resources before the activity is destroyed.
Infer why it is necessary to handle the onStop() method. a) To manage memory effectively
b) To control UI transitions
c) It is not necessary
d) To prevent the app from crashing
Answer: a) To manage memory effectively
Interpret the purpose of onRestart() in the activity lifecycle. a) It is called when an activity comes back from the background to the foreground.
b) It initializes the activity.
c) It cleans up resources.
d) It is called when the activity is destroyed.
Answer: a) It is called when an activity comes back from the background to the foreground.
Outline how the activity lifecycle impacts user experience. a) Proper management prevents data loss and improves responsiveness.
b) It does not affect user experience.
c) Users do not notice activity states.
d) It only matters for developers.
Answer: a) Proper management prevents data loss and improves responsiveness.
Relate the activity lifecycle to managing background tasks. a) The lifecycle does not affect background tasks.
b) Activities should manage background tasks during onResume() and stop them in onPause().
c) Background tasks are handled independently of the activity lifecycle.
d) Background tasks should always run regardless of the activity state.
Answer: b) Activities should manage background tasks during onResume() and stop them in onPause().
Rephrase the role of onCreate() in the activity lifecycle. a) It is used for cleaning up resources.
b) It initializes the activity and sets up UI elements.
c) It manages activity transitions.
d) It is called when the activity is closed.
Answer: b) It initializes the activity and sets up UI elements.
Show the sequence of lifecycle method calls when an activity starts. a) onCreate() → onStart() → onResume()
b) onResume() → onStart() → onCreate()
c) onStart() → onCreate() → onResume()
d) onCreate() → onPause() → onStop()
Answer: a) onCreate() → onStart() → onResume()
Summarize the effect of configuration changes (like screen rotation) on an activity.
a) Activities are not affected by configuration changes.
b) They can cause the activity to be recreated, requiring state management.
c) Activities automatically save their state during configuration changes.
d) Configuration changes do not require any handling.
Answer: b) They can cause the activity to be recreated, requiring state management.
Translate the lifecycle method sequence when an activity is moved to the background. a) onPause() → onStop()
b) onStop() → onDestroy()
c) onResume() → onPause()
d) onStart() → onPause()
Answer: a) onPause() → onStop()