Unit 6 (GUI) Flashcards
Which Java package is used for basic GUI programming?
A) java.util
B) java.awt
C) java.io
D) javax.swing
B) java.awt
What does the JOptionPane class primarily use for?
A) Handling database connections
B) Creating simple dialogues
C) Performing file operations
D) Managing threads
B) Creating simple dialogues
Which method from JOptionPane is used to display a dialog asking the user to enter some text?
A) showMessageDialog
B) showInputDialog
C) showConfirmDialog
D) showOptionDialog
B) showInputDialog
What is the primary purpose of containers in GUI applications?
A) To contain and manage other components
B) To display data in a table format
C) To execute background tasks
D) To provide networking capabilities
A) To contain and manage other components
What type of component is a JButton?
A) Container
B) Basic component
C) Layout manager
D) Event handler
B) Basic component
What will be the output of the following code snippet?
JOptionPane.showMessageDialog(null, “Hello, world!”);
A) Compiles and displays a message dialog with “Hello, world!”
B) Runtime error
C) Compilation error
D) No output
A) Compiles and displays a message dialog with “Hello, world!”
Which layout manager arranges components in a left-to-right flow?
A) BorderLayout
B) GridLayout
C) FlowLayout
D) CardLayout
C) FlowLayout
What is true about the JPanel class?
A) It cannot contain other components.
B) It is not a subclass of Container.
C) It uses BorderLayout as its default layout manager.
D) It is commonly used to group components within a frame.
D) It is commonly used to group components within a frame.
What is a JFrame?
A) A component that functions as a container for other components.
B) A layout manager.
C) A type of event.
D) A basic GUI component.
A) A component that functions as a container for other components.
Which method is used to make a JFrame visible?
A) display()
B) show()
C) setVisible(true)
D) open()
C) setVisible(true)
What will happen if two buttons are added to a frame that uses FlowLayout and the frame is resized?
A) The buttons will overlap.
B) The buttons will adjust to maintain the flow from left to right.
C) Only one button will be visible.
D) The buttons will disappear.
B) The buttons will adjust to maintain the flow from left to right.
What is the correct way to add a JButton to a JFrame?
A) frame.add(new JButton(“Click Me!”));
B) JButton button = “Click Me!”; frame.add(button);
C) frame.setButton(new JButton(“Click Me!”));
D) JButton button = new JButton(); frame.add(button.setText(“Click Me!”));
A) frame.add(new JButton(“Click Me!”));
What does the setForeground method do in a GUI component?
A) Changes the background color of the component.
B) Adjusts the size of the component.
C) Sets the color used to draw the text or graphics within the component.
D) Sets the font of the text within the component.
C) Sets the color used to draw the text or graphics within the component.
What will be the output if the following code is executed? Assume the button is clicked by the user.
JButton button = new JButton(“Press me”);
button.addActionListener(e -> System.out.println(“Button pressed!”));
A) “Press me”
B) “Button pressed!”
C) No output
D) Compiles with an error
B) “Button pressed!”
What is the primary use of a JLabel?
A) To display editable text fields.
B) To create a clickable button.
C) To display a short text string or an image.
D) To select one option from a set of choices.
C) To display a short text string or an image.
How do you create a JCheckBox that is initially checked?
A) JCheckBox checkBox = new JCheckBox(“Check me”, true);
B) JCheckBox checkBox = new JCheckBox(“Check me”); checkBox.setChecked(true);
C) JCheckBox checkBox = new JCheckBox(“Check me”); checkBox.setTrue();
D) JCheckBox checkBox = new JCheckBox(true); checkBox.setText(“Check me”);
A) JCheckBox checkBox = new JCheckBox(“Check me”, true);
What is the correct way to group several JRadioButtons so that selecting one will deselect the others?
A) Put them in a JPanel.
B) Add them to a ButtonGroup.
C) Set them in a JFrame.
D) Use a GridLayout.
B) Add them to a ButtonGroup.
Which component is typically used for accepting a line of text input from the user?
A) JLabel
B) JTextArea
C) JTextField
D) JPasswordField
C) JTextField
Which statement is true about the JComboBox?
A) It allows only fixed set of strings as items.
B) It does not fire any events.
C) It can provide a drop-down list of choices.
D) It is used to toggle between true and false states.
C) It can provide a drop-down list of choices.
What happens in Java Swing when you call setSize(300, 300) on a JFrame?
A) The size of the JFrame window is set to 300x300 pixels.
B) A compilation error occurs.
C) A runtime error occurs.
D) The JFrame is minimized.
A) The size of the JFrame window is set to 300x300 pixels.
Which of the following is an abstract class in Java GUI programming?
A) JFrame
B) JButton
C) JComponent
D) JLabel
C) JComponent
What is a primary purpose of the java.awt.event and java.swing.event packages?
A) To provide layout managers
B) To handle user actions through events
C) To manage UI themes
D) To store data
B) To handle user actions through events
What does the getBackground() method do in a GUI component?
A) Returns the current background color of the component
B) Changes the size of the component
C) Gets the text within the component
D) Sets the visibility of the component
A) Returns the current background color of the component
What is the default layout manager for JPanel?
A) FlowLayout
B) BorderLayout
C) GridLayout
D) CardLayout
A) FlowLayout