0% found this document useful (0 votes)
19 views11 pages

Text 2

Java mcq questions for revision

Uploaded by

r9hbbykxpc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

Text 2

Java mcq questions for revision

Uploaded by

r9hbbykxpc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Here are additional multiple-choice questions to further cover the depth of

Chapter 13 topics:

36. Which Swing component would you use to create a menu bar with
dropdown menus?
• A) JMenu
• B) JMenuItem
• C) JMenuBar
• D) JPopupMenu
Answer: C) JMenuBar
37. If you want to add a keyboard shortcut (mnemonic) to a JMenuItem,
which method would you use?
• A) setAccelerator
• B) setShortcut
• C) setMnemonic
• D) setKeyBinding
Answer: C) setMnemonic
38. What does the setToolTipText method do when applied to a component?
• A) Adds a descriptive label above the component
• B) Adds an icon to the component
• C) Displays a small help text when hovering over the component
• D) Changes the text of the component
Answer: C) Displays a small help text when hovering over the component
39. Which of these classes would you use to create a slider that allows users
to select a value from a range?
• A) JProgressBar
• B) JScrollBar
• C) JSlider
• D) JRangeBar
Answer: C) JSlider
40. To change the appearance of a Swing application to look like a specific
operating system’s interface, which method should be used?
• A) UIManager.setAppearance()
• B) UIManager.setLookAndFeel()
• C) JComponent.setStyle()
• D) JFrame.setLook()
Answer: B) UIManager.setLookAndFeel()
41. Which method is used to set the label text of a JButton component?
• A) setText
• B) setLabel
• C) setButtonText
• D) setTitle
Answer: A) setText
42. Which of the following methods would you use to programmatically
select an item in a JComboBox?
• A) selectItem
• B) chooseItem
• C) setSelectedItem
• D) setSelection
Answer: C) setSelectedItem
43. If you need to retrieve the selected items from a JList in an array format,
which method would you use?
• A) getSelectedArray
• B) getItems
• C) getSelectedValuesList
• D) getSelection
Answer: C) getSelectedValuesList
44. Which Swing component should you use to let users open files or folders
from their system?
• A) JColorChooser
• B) JFileChooser
• C) JMenu
• D) JOptionPane
Answer: B) JFileChooser
45. In Java Swing, which method would you use to remove a selected item
from a JComboBox?
• A) removeItem
• B) deleteItem
• C) clearSelection
• D) deselectItem
Answer: A) removeItem
46. When adding a JLabel with an image, which constructor parameter is
used to specify the image?
• A) ImageIcon
• B) Image
• C) Picture
• D) Icon
Answer: A) ImageIcon
47. What is the return type of the getSelectedIndices method in JList?
• A) int
• B) Object[]
• C) int[]
• D) Integer[]
Answer: C) int[]
48. Which method would you use to add a new item to a JComboBox after it
has been created?
• A) addElement
• B) add
• C) addItem
• D) insertItem
Answer: C) addItem
49. What is the purpose of the JPanel class in Swing?
• A) To display images
• B) To contain and organize other components
• C) To create a drop-down list
• D) To display messages
Answer: B) To contain and organize other components
50. When using JMenu, which method is used to add individual menu items?
• A) addMenuItem
• B) addItem
• C) add
• D) appendItem
Answer: C) add

These additional questions should provide a thorough review of Swing


components, event handling, and GUI building principles discussed in Chapter 13.
Let me know if you need more questions or further clarification on any topic!
Here are even more multiple-choice questions based on Chapter 13, covering
additional concepts:

21. Which component would you use to let users choose a color in a GUI
application?
• A) JColorChooser
• B) JFileChooser
• C) JColorPicker
• D) JTextField
Answer: A) JColorChooser
22. What does getSelectedItem return when used with a JComboBox?
• A) The index of the selected item
• B) The selected item as a String
• C) The selected item as an Object
• D) The count of items in the combo box
Answer: C) The selected item as an Object
23. In the JList class, which method is used to display items from a new
array?
• A) addListData
• B) setData
• C) setListData
• D) updateList
Answer: C) setListData
24. Which of the following methods can be used to add tooltips to
components in Swing?
• A) setToolTipText
• B) setHint
• C) addTooltip
• D) showTooltip
Answer: A) setToolTipText
25. What is the purpose of the addListSelectionListener method in JList?
• A) To add items to the list
• B) To set a listener for handling selection events
• C) To remove items from the list
• D) To display the selected item
Answer: B) To set a listener for handling selection events
26. To retrieve multiple selected items as a list in JList, which method should
you use?
• A) getSelectedItems
• B) getSelectedValues
• C) getSelectedValuesList
• D) getAllSelectedItems
Answer: C) getSelectedValuesList
27. In JComboBox, which method would you use to make the first item in the
list appear by default?
• A) setSelectedItem
• B) setDefaultItem
• C) setSelectedIndex(0)
• D) selectItem
Answer: C) setSelectedIndex(0)
28. What does the setVisibleRowCount method in JList do?
• A) Sets the number of rows that can be selected
• B) Sets the maximum number of items that can be added
• C) Sets the number of items visible without scrolling
• D) Sets the index of the last visible item
Answer: C) Sets the number of items visible without scrolling
29. What type of event does JButton generate when clicked?
• A) Action event
• B) Mouse event
• C) Item event
• D) Selection event
Answer: A) Action event
30. Which method would you use to set the items displayed in a JComboBox
at runtime?
• A) addItems
• B) setData
• C) setComboData
• D) addItem
Answer: D) addItem
31. If you want a GUI component to display an image, which of these can be
used?
• A) JLabel
• B) JTextField
• C) JPanel
• D) JMenuItem
Answer: A) JLabel
32. What is the primary class used for creating dialog boxes to open or save
files?
• A) JFileSaver
• B) JFileChooser
• C) FilePicker
• D) JFileDialog
Answer: B) JFileChooser
33. In Java Swing, what method allows you to retrieve the text entered by the
user in a JTextField?
• A) getText
• B) getValue
• C) getString
• D) getInput
Answer: A) getText
34. Which component should you use if you need a scrollable text area?
• A) JTextField
• B) JScrollArea
• C) JTextArea inside a JScrollPane
• D) JTextBox
Answer: C) JTextArea inside a JScrollPane
35. What will getSelectedIndex return in a JComboBox if no item is selected?
• A) -1
• B) 0
• C) null
• D) It throws an exception
Answer: A) -1

These questions cover more advanced details and options in Java Swing,
providing a comprehensive review of Chapter 13 topics. Let me know if you need
further assistance!
Here’s a summary of the key concepts and optional questions for Chapter 13:

Key Concepts:

1. Swing and AWT Class Hierarchy: Understand the structure of these


classes in Java GUI development.
2. Read-Only Text Fields: Learn to use JTextField with setEditable(false) for
fields that shouldn’t be modified.
3. Lists and Selection Modes:
• JList Component: Allows creating and displaying lists.
• Selection Modes: Single, single interval, and multiple interval selection.
• List Events: Handle list selection with ListSelectionListener.
4. Combo Boxes (JComboBox):
• Combo Box Types: Editable and non-editable combo boxes.
• Retrieving Selected Items: Using getSelectedItem and getSelectedIndex.
• Combo Box Events: Use actionPerformed to respond to item selection.
5. Displaying Images in Labels and Buttons: Enhances GUI interactivity by
adding visuals.
6. File and Color Choosers: Using JFileChooser and JColorChooser for user
inputs.
7. Advanced Text Components: Such as text areas and font options for
customization.
8. Sliders: Using JSlider for graphical input.
9. Menus: Creating menu bars, items, and handling events.
10. Look and Feel: Adjusting the GUI’s appearance across platforms.

Sample Optional Questions:

1. What method do you use to make a JTextField read-only?


• Answer: setEditable(false)
2. Which component would you use to create a list of items for user
selection?
• Answer: JList
3. How can you retrieve the selected item in a combo box?
• Answer: Use getSelectedItem or getSelectedIndex.
4. What is the difference between single selection and multiple interval
selection in JList?
• Answer: Single selection allows only one item selection, while multiple
interval selection allows selection of multiple items without restrictions.
5. How do you make a combo box editable?
• Answer: By calling setEditable(true) on the combo box instance.

Let me know if you need additional details on any section or more specific
questions!
Here are additional multiple-choice questions to deepen understanding of
Chapter 13 topics:

11. To handle events when an item in a JList is selected, which interface


should be implemented?
• A) ActionListener
• B) ItemListener
• C) ListSelectionListener
• D) MouseListener
Answer: C) ListSelectionListener
12. In Java Swing, which method is used to set a specific color border
around a JList?
• A) setBorderColor
• B) setListBorder
• C) setColor
• D) setBorder
Answer: D) setBorder
13. Which of the following is true about getSelectedValue in JList?
• A) It returns the index of the selected item.
• B) It returns the selected item as an Object.
• C) It only works with single selection mode.
• D) It returns a boolean value.
Answer: B) It returns the selected item as an Object.
14. When creating a scrollable JList, which of the following is NOT required?
• A) Setting the visible row count
• B) Wrapping the list in a JScrollPane
• C) Adding the JScrollPane to a container
• D) Setting the list items in alphabetical order
Answer: D) Setting the list items in alphabetical order
15. In which mode does JList allow selection of multiple contiguous items?
• A) Single selection mode
• B) Single interval selection mode
• C) Multiple interval selection mode
• D) Default selection mode
Answer: B) Single interval selection mode
16. Which method can be used to retrieve the index of the first selected item
in a multiple interval selection JList?
• A) getSelectedValue
• B) getSelectedValues
• C) getSelectedIndex
• D) getSelectedIndices
Answer: C) getSelectedIndex
17. Which of these is NOT a function of a JComboBox?
• A) Allows single selection from a drop-down list.
• B) Can be made editable to accept text input.
• C) Allows multiple selection at once.
• D) Generates an action event when an item is selected.
Answer: C) Allows multiple selection at once.
18. Which class provides the showOpenDialog method for file selection?
• A) JFileChooser
• B) JColorChooser
• C) JDialog
• D) JFileReader
Answer: A) JFileChooser
19. To apply a “Look and Feel” in Swing, which method is commonly used?
• A) UIManager.setLookAndFeel()
• B) JComponent.setLookAndFeel()
• C) setLook()
• D) JOptionPane.setFeel()
Answer: A) UIManager.setLookAndFeel()
20. What will getSelectedIndex return if no item is selected in a JList?
• A) -1
• B) 0
• C) Null
• D) An exception will be thrown
Answer: A) -1

These questions should provide additional challenge on Java Swing components


and event handling. Let me know if you need further topics or adjustments to the
questions!
Here are multiple-choice questions based on Chapter 13 topics:

1. What is the default selection mode for a JList component?


• A) Single selection mode
• B) Single interval selection mode
• C) Multiple interval selection mode
• D) No selection mode
Answer: C) Multiple interval selection mode
2. Which method is used to make a JTextField read-only?
• A) setEnabled(false)
• B) setEditable(false)
• C) setVisible(false)
• D) setReadOnly(true)
Answer: B) setEditable(false)
3. To add a scrollbar to a JList, which class is typically used?
• A) JScrollBar
• B) JScrollPane
• C) JScrollList
• D) JListPane
Answer: B) JScrollPane
4. What type of event does a JComboBox generate when an item is
selected?
• A) Action event
• B) Item event
• C) Mouse event
• D) Key event
Answer: A) Action event
5. In a JComboBox, which method returns the index of the currently
selected item?
• A) getSelectedValue
• B) getSelectedIndex
• C) getSelectedItem
• D) getIndex
Answer: B) getSelectedIndex
6. Which method in JList can be used to retrieve the selected item as an
Object?
• A) getItem
• B) getSelectedObject
• C) getSelectedValue
• D) getValue
Answer: C) getSelectedValue
7. How can you make a JComboBox editable?
• A) setEditable(true)
• B) setEditable(false)
• C) enableEditing(true)
• D) enableEditing(false)
Answer: A) setEditable(true)
8. Which method allows you to change the selection mode of a JList?
• A) setSelectionMode
• B) setSelectionType
• C) setSelection
• D) setSelectionOptions
Answer: A) setSelectionMode
9. What does the getSelectedValues method return in JList?
• A) A single selected item as a String
• B) A single selected item as an Integer
• C) An array of selected items
• D) An empty array
Answer: C) An array of selected items
10. Which component is used to create drop-down lists in Java Swing?
• A) JList
• B) JMenu
• C) JTextField
• D) JComboBox
Answer: D) JComboBox

These questions should cover the major concepts of Chapter 13 while giving
good practice on important topics. Let me know if you’d like more questions or
adjustments!

You might also like