1. Inside a fragment, which call returns a non-null Context for a Toast?
- getApplication()
- R.context
- this
- requireContext()
Answer: D) requireContext()
Explanation: requireContext() returns the host context or throws if not attached.
MCQs / Computer Science / Lab: Bottom Navigation with Fragments
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.
Answer: D) requireContext()
Explanation: requireContext() returns the host context or throws if not attached.
Answer: B) The fragment's arguments Bundle
Explanation: arguments = bundleOf(...) is the correct way.
Answer: D) menu resource (res/menu)
Explanation: The app:menu attribute points to a menu XML with items.
Answer: A) setOnItemSelectedListener
Explanation: setOnItemSelectedListener is called when a tab is selected.
Answer: B) replace()
Explanation: replace() removes the current fragment in the container and adds the new one.
Answer: D) Material Components (com.google.android.material)
Explanation: BottomNavigationView is part of Material Components for Android.
Answer: A) The item should be shown as selected
Explanation: true tells the view to display the item as selected.
Answer: B) setupWithNavController()
Explanation: setupWithNavController links menu item IDs to nav graph destinations.
Answer: A) FragmentContainerView
Explanation: FragmentContainerView is the container for fragments.
Answer: B) onViewCreated()
Explanation: The view exists in onViewCreated().
Answer: C) Gives the layout that the fragment inflates automatically
Explanation: Passing the layout ID lets Fragment inflate it without overriding onCreateView.
Answer: A) New → Vector Asset (clip art)
Explanation: Vector Asset Studio imports Material icons as vector drawables.
Answer: D) To avoid adding a duplicate fragment after rotation, since the FragmentManager restores fragments
Explanation: After recreation the existing fragment is restored automatically.
Answer: B) New → Fragment → Fragment (Blank)
Explanation: The Fragment (Blank) template creates both the class and the layout.
Practise Lab: Bottom Navigation with Fragments MCQs