0% found this document useful (0 votes)
64 views8 pages

Swing MCQ

Uploaded by

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

Swing MCQ

Uploaded by

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

Swing MCQ:

1. What is Swing in Java?

 A) A lightweight GUI toolkit


 B) A type of database
 C) A web framework
 D) A file management system Answer: A) A lightweight GUI toolkit

2. Which package contains the Swing components?

 A) java.awt
 B) javax.swing
 C) java.io
 D) java.util Answer: B) javax.swing

3. Which of the following is a top-level container in Swing?

 A) JPanel
 B) JButton
 C) JFrame
 D) JLabel Answer: C) JFrame

4. What is the function of JPanel in Swing?

 A) To display text
 B) To manage layouts and contain other components
 C) To handle events
 D) To create menus Answer: B) To manage layouts and contain other components

5. Which method is used to make a JFrame visible?

 A) show()
 B) setVisible(true)
 C) display()
 D) render() Answer: B) setVisible(true)

6. What does JButton represent in Swing?

 A) A menu item
 B) A clickable button
 C) A text field
 D) A container Answer: B) A clickable button

7. Which of the following is not a Swing component?

 A) JLabel
 B) JTextField
 C) JRadioButton
 D) Thread Answer: D) Thread

8. Which layout manager arranges components in a row or column?

 A) BorderLayout
 B) FlowLayout
 C) GridLayout
 D) BoxLayout Answer: D) BoxLayout

9. How do you set a tooltip for a Swing component?

 A) setHint()
 B) setToolTipText()
 C) setToolTip()
 D) setTooltipText() Answer: B) setToolTipText()

10. What is the default layout manager for a JFrame's content pane?

 A) FlowLayout
 B) GridLayout
 C) BorderLayout
 D) BoxLayout Answer: C) BorderLayout

11. Which of the following is true about Swing?

 A) Swing components are platform-dependent.


 B) Swing is built on top of the Abstract Window Toolkit (AWT).
 C) Swing does not support MVC architecture.
 D) Swing components require a separate thread to run. Answer: B) Swing is built on top of the Abstract
Window Toolkit (AWT).

12. Which of these is used to create a scrollable component in Swing?

 A) JScrollPane
 B) JScrollBar
 C) JScrollPanel
 D) JScroller Answer: A) JScrollPane

13. Which component allows users to select a single item from a dropdown list in Swing?

 A) JComboBox
 B) JList
 C) JTextField
 D) JRadioButton Answer: A) JComboBox

14. What is the purpose of the setDefaultCloseOperation() method in a JFrame?

 A) To set the default action when the user clicks the close button on the JFrame
 B) To set the default size of the JFrame
 C) To set the default layout of the JFrame
 D) To set the default title of the JFrame Answer: A) To set the default action when the user clicks the close
button on the JFrame

15. Which of the following is used to display an image in Swing?

 A) JImage
 B) JIcon
 C) JLabel
 D) JPicture Answer: C) JLabel (with an ImageIcon)

16. What is the default layout manager for a JPanel?


 A) BorderLayout
 B) FlowLayout
 C) GridLayout
 D) BoxLayout Answer: B) FlowLayout

17. How can you create a non-editable text field in Swing?

 A) setEditable(false)
 B) setReadOnly(true)
 C) setEnabled(false)
 D) setInputDisabled(true) Answer: A) setEditable(false)

18. Which component would you use to create a popup menu in Swing?

 A) JMenu
 B) JMenuItem
 C) JPopupMenu
 D) JMenuBar Answer: C) JPopupMenu

19. Which of the following is used to handle events in Swing?

 A) EventHandler
 B) ActionListener
 C) EventManager
 D) EventQueue Answer: B) ActionListener

20. What method is used to add a component to a JFrame's content pane?

 A) addComponent()
 B) append()
 C) add()
 D) insert() Answer: C) add()

21. Which of the following components can be used to group multiple buttons in Swing?

 A) ButtonGroup
 B) JPanel
 C) JButton
 D) JRadioButton Answer: A) ButtonGroup

22. What is the purpose of the pack() method in a JFrame?

 A) To set the size of the JFrame to a predefined value


 B) To close the JFrame
 C) To arrange the components within the JFrame to their preferred sizes
 D) To set the title of the JFrame Answer: C) To arrange the components within the JFrame to their
preferred sizes

23. Which of these is a method in JComponent used to enable or disable a component?

 A) setEnabled(boolean)
 B) setVisible(boolean)
 C) setEditable(boolean)
 D) setComponentEnabled(boolean) Answer: A) setEnabled(boolean)

24. What does the JFileChooser component do?


 A) Allows the user to select a file or directory
 B) Allows the user to input text
 C) Displays a list of items
 D) Handles file I/O operations Answer: A) Allows the user to select a file or directory

25. Which method is used to set the title of a JFrame?

 A) setFrameTitle()
 B) setTitle()
 C) setWindowTitle()
 D) setCaption() Answer: B) setTitle()

26. How can you create a tooltip for a JButton?

 A) setToolTipText(String text)
 B) setHintText(String text)
 C) setToolTip(String text)
 D) setTextToolTip(String text) Answer: A) setToolTipText(String text)

27. What is the default orientation of a JProgressBar?

 A) Vertical
 B) Horizontal
 C) Diagonal
 D) Circular Answer: B) Horizontal

28. Which of the following is true about JTable in Swing?

 A) It is used to create a dropdown menu


 B) It allows the display of tabular data in rows and columns
 C) It is used to create a button
 D) It is used to create a text area Answer: B) It allows the display of tabular data in rows and columns

29. What is the role of the SwingUtilities.invokeLater() method?

 A) To execute a block of code on a new thread


 B) To schedule a task for execution in the event dispatch thread (EDT)
 C) To immediately stop a running thread
 D) To delay the execution of code for a specified time Answer: B) To schedule a task for execution in the
event dispatch thread (EDT)

30. Which Swing component can be used to display HTML content?

 A) JTextArea
 B) JLabel
 C) JEditorPane
 D) JList Answer: C) JEditorPane

31. Which of the following is a correct way to set the layout manager of a JPanel?

 A) setLayoutManager(LayoutManager mgr)
 B) setLayout(LayoutManager mgr)
 C) setPanelLayout(LayoutManager mgr)
 D) setComponentLayout(LayoutManager mgr) Answer: B) setLayout(LayoutManager mgr)

32. How do you create a button with both text and an icon in Swing?
 A) By using the JIconButton class
 B) By setting both text and an icon using JButton
 C) By using a JLabel instead of JButton
 D) By using two separate components for text and icon Answer: B) By setting both text and an icon using
JButton

33. Which layout manager arranges components in a grid of rows and columns?

 A) GridLayout
 B) FlowLayout
 C) BorderLayout
 D) CardLayout Answer: A) GridLayout

34. What is the purpose of the setOpaque(boolean) method in Swing?

 A) To set the transparency of a component


 B) To set the background color of a component
 C) To make a component non-editable
 D) To remove a component from the view Answer: A) To set the transparency of a component

35. Which method is used to add an item to a JComboBox?

 A) addItem(Object item)
 B) add(Object item)
 C) append(Object item)
 D) insertItem(Object item) Answer: A) addItem(Object item)

36. What is the default behavior of a JMenuBar when added to a JFrame?

 A) It appears at the top of the JFrame


 B) It appears at the bottom of the JFrame
 C) It appears as a popup
 D) It does not appear unless manually positioned Answer: A) It appears at the top of the JFrame

37. Which component is used to create a dialog window in Swing?

 A) JDialog
 B) JFrame
 C) JWindow
 D) JPopup Answer: A) JDialog

38. How can you create a multi-line label in Swing?

 A) By using a JLabel with HTML tags


 B) By using multiple JLabel components
 C) By using a JTextArea instead of JLabel
 D) By setting the label’s textWrap property to true Answer: A) By using a JLabel with HTML tags

39. What is the primary function of a JSlider in Swing?

 A) To display a list of items


 B) To select a value by moving a knob along a range of values
 C) To enter text
 D) To display images Answer: B) To select a value by moving a knob along a range of values

40. Which of the following is true about JTabbedPane?


 A) It allows switching between multiple panels using tabs
 B) It creates a dropdown list
 C) It is used to create tables
 D) It displays progress bars Answer: A) It allows switching between multiple panels using tabs

41. Which method is used to display a dialog box for user input in Swing?

 A) JDialog.show()
 B) JOptionPane.showMessageDialog()
 C) JOptionPane.showInputDialog()
 D) JFrame.showInput() Answer: C) JOptionPane.showInputDialog()

42. Which Swing component is used to create a non-editable combo box?

 A) JComboBox.setEditable(false)
 B) JComboBox.setReadOnly(true)
 C) JComboBox.disable()
 D) JComboBox.setUneditable(true) Answer: A) JComboBox.setEditable(false)

43. What is the role of UIManager in Swing?

 A) To handle low-level event management


 B) To manage the look and feel of Swing components
 C) To manage the layout of components
 D) To manage user interactions Answer: B) To manage the look and feel of Swing components

44. Which method is used to programmatically click a button in Swing?

 A) doClick()
 B) click()
 C) performClick()
 D) press() Answer: A) doClick()

45. Which of the following components allows for the display of a list of choices in Swing?

 A) JComboBox
 B) JList
 C) JTextArea
 D) JTextField Answer: B) JList

46. What is the purpose of GlassPane in a JFrame?

 A) To display a background image


 B) To handle drag-and-drop operations
 C) To provide a transparent overlay over the entire frame
 D) To manage window borders Answer: C) To provide a transparent overlay over the entire frame

47. How can you add a separator line in a JMenu?

 A) addSeparator()
 B) addLine()
 C) addDivider()
 D) insertSeparator() Answer: A) addSeparator()

48. Which of the following is true about JRootPane in Swing?


 A) It is the base pane for all top-level containers
 B) It is used to display text
 C) It is a type of layout manager
 D) It is used to handle events Answer: A) It is the base pane for all top-level containers

49. Which Swing component can be used to display a selectable list of hierarchical data?

 A) JTree
 B) JTable
 C) JComboBox
 D) JTextArea Answer: A) JTree

50. Which method is used to close a JFrame when the close button is clicked?

 A) setCloseOperation(JFrame.EXIT_ON_CLOSE)
 B) setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
 C) closeFrame()
 D) dispose() Answer: B) setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

51. What does JLayeredPane allow you to do?

 A) Stack components in layers, where some components can overlap others


 B) Create a draggable component
 C) Apply multiple themes to a pane
 D) Add multiple layout managers to a pane Answer: A) Stack components in layers, where some
components can overlap others

52. Which method is used to refresh the contents of a JPanel?

 A) refresh()
 B) repaint()
 C) reload()
 D) redraw() Answer: B) repaint()

53. What is the use of setResizable(false) in a JFrame?

 A) To prevent the user from resizing the frame


 B) To fix the frame size to maximum
 C) To allow only horizontal resizing
 D) To minimize the frame size Answer: A) To prevent the user from resizing the frame

54. Which Swing component is used to display an uneditable text field?

 A) JTextArea
 B) JLabel
 C) JTextPane
 D) JPasswordField Answer: B) JLabel

55. What is the primary purpose of the JSpinner component?

 A) To allow the user to input a numeric value with increment and decrement buttons
 B) To create a progress bar
 C) To select a color
 D) To display a list of items Answer: A) To allow the user to input a numeric value with increment and
decrement buttons
56. Which layout manager would you use to create a UI with a fixed number of equally sized components?

 A) GridLayout
 B) FlowLayout
 C) BorderLayout
 D) BoxLayout Answer: A) GridLayout

57. Which method is used to make a JComponent respond to mouse clicks?

 A) addMouseListener()
 B) addClickListener()
 C) setMouseClick()
 D) enableMouseEvents() Answer: A) addMouseListener()

58. Which Swing component is typically used to create a horizontal or vertical split in a window?

 A) JSplitPane
 B) JDivider
 C) JScrollPane
 D) JSeparator Answer: A) JSplitPane

59. Which method is used to scroll a JScrollPane to the top?

 A) scrollToTop()
 B) setScrollPosition(0)
 C) getViewport().setViewPosition(new Point(0, 0))
 D) scrollToBeginning() Answer: C) getViewport().setViewPosition(new Point(0, 0))

60. Which of the following is used to create a password input field in Swing?

 A) JTextField
 B) JPasswordField
 C) JTextPane
 D) JTextArea Answer: B) JPasswordField

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy