MCQs / Computer Science / Lab: Bottom Navigation with Fragments

Lab: Bottom Navigation with Fragments MCQs — Computer Science

14 multiple-choice questions on Lab: Bottom Navigation with Fragments from Computer Science, each with the correct answer and, where available, a teacher's explanation. Read through them to revise the chapter, then practise the same questions one by one to test yourself.

1. Inside a fragment, which call returns a non-null Context for a Toast?

  1. getApplication()
  2. R.context
  3. this
  4. requireContext()

Answer: D) requireContext()

Explanation: requireContext() returns the host context or throws if not attached.

Discuss this question

2. Data such as the student name is passed to ProfileFragment using:

  1. putExtra on an Intent
  2. The fragment's arguments Bundle
  3. A public static variable
  4. SharedPreferences only

Answer: B) The fragment's arguments Bundle

Explanation: arguments = bundleOf(...) is the correct way.

Discuss this question

3. The items of a BottomNavigationView are defined in a:

  1. layout resource
  2. drawable
  3. string array
  4. menu resource (res/menu)

Answer: D) menu resource (res/menu)

Explanation: The app:menu attribute points to a menu XML with items.

Discuss this question

4. Which listener handles taps on bottom navigation items?

  1. setOnItemSelectedListener
  2. addTextChangedListener
  3. setOnLongClickListener
  4. setOnCheckedChangeListener

Answer: A) setOnItemSelectedListener

Explanation: setOnItemSelectedListener is called when a tab is selected.

Discuss this question

5. Which transaction method swaps the current tab's fragment for a new one?

  1. add()
  2. replace()
  3. detach()
  4. hide()

Answer: B) replace()

Explanation: replace() removes the current fragment in the container and adds the new one.

Discuss this question

6. BottomNavigationView belongs to which library?

  1. Retrofit
  2. AppCompat
  3. Room
  4. Material Components (com.google.android.material)

Answer: D) Material Components (com.google.android.material)

Explanation: BottomNavigationView is part of Material Components for Android.

Discuss this question

7. Returning true from the item-selected listener means:

  1. The item should be shown as selected
  2. The menu should be hidden
  3. The fragment should be removed
  4. The app should close

Answer: A) The item should be shown as selected

Explanation: true tells the view to display the item as selected.

Discuss this question

8. With the Navigation component, BottomNavigationView is connected to the NavController using:

  1. addToBackStack()
  2. setupWithNavController()
  3. replace()
  4. startActivity()

Answer: B) setupWithNavController()

Explanation: setupWithNavController links menu item IDs to nav graph destinations.

Discuss this question

9. The host activity layout places fragments into a:

  1. FragmentContainerView
  2. Toolbar
  3. RecyclerView
  4. ImageView

Answer: A) FragmentContainerView

Explanation: FragmentContainerView is the container for fragments.

Discuss this question

10. In which fragment callback should you find buttons and set click listeners?

  1. onDestroyView()
  2. onViewCreated()
  3. onDetach()
  4. onAttach()

Answer: B) onViewCreated()

Explanation: The view exists in onViewCreated().

Discuss this question

11. In class HomeFragment : Fragment(R.layout.fragmenthome), the constructor argument:

  1. Sets the menu
  2. Starts a new activity
  3. Gives the layout that the fragment inflates automatically
  4. Names the host activity

Answer: C) Gives the layout that the fragment inflates automatically

Explanation: Passing the layout ID lets Fragment inflate it without overriding onCreateView.

Discuss this question

12. Icons for the tabs can be added with:

  1. New → Vector Asset (clip art)
  2. Logcat
  3. Gradle sync
  4. AVD Manager

Answer: A) New → Vector Asset (clip art)

Explanation: Vector Asset Studio imports Material icons as vector drawables.

Discuss this question

13. Why does the lab check if (savedInstanceState == null) before showing HomeFragment?

  1. To clear the back stack
  2. To save battery
  3. To request permissions
  4. To avoid adding a duplicate fragment after rotation, since the FragmentManager restores fragments

Answer: D) To avoid adding a duplicate fragment after rotation, since the FragmentManager restores fragments

Explanation: After recreation the existing fragment is restored automatically.

Discuss this question

14. Which wizard option creates a fragment class and its layout together?

  1. New → Service
  2. New → Fragment → Fragment (Blank)
  3. New → Activity → Basic
  4. New → Module

Answer: B) New → Fragment → Fragment (Blank)

Explanation: The Fragment (Blank) template creates both the class and the layout.

Discuss this question

Practise Lab: Bottom Navigation with Fragments MCQs

More Computer Science chapters