Unit 6 (GUI) Flashcards

1
Q

Which Java package is used for basic GUI programming?

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

A

B) java.awt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the JOptionPane class primarily use for?

A) Handling database connections
B) Creating simple dialogues
C) Performing file operations
D) Managing threads

A

B) Creating simple dialogues

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

B) showInputDialog

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A) To contain and manage other components

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What type of component is a JButton?

A) Container
B) Basic component
C) Layout manager
D) Event handler

A

B) Basic component

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A) Compiles and displays a message dialog with “Hello, world!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which layout manager arranges components in a left-to-right flow?

A) BorderLayout
B) GridLayout
C) FlowLayout
D) CardLayout

A

C) FlowLayout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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.

A

D) It is commonly used to group components within a frame.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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) A component that functions as a container for other components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which method is used to make a JFrame visible?

A) display()
B) show()
C) setVisible(true)
D) open()

A

C) setVisible(true)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

B) The buttons will adjust to maintain the flow from left to right.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A) frame.add(new JButton(“Click Me!”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

C) Sets the color used to draw the text or graphics within the component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

B) “Button pressed!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

C) To display a short text string or an image.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A) JCheckBox checkBox = new JCheckBox(“Check me”, true);

17
Q

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.

A

B) Add them to a ButtonGroup.

18
Q

Which component is typically used for accepting a line of text input from the user?

A) JLabel
B) JTextArea
C) JTextField
D) JPasswordField

A

C) JTextField

19
Q

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.

A

C) It can provide a drop-down list of choices.

20
Q

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

A) The size of the JFrame window is set to 300x300 pixels.

21
Q

Which of the following is an abstract class in Java GUI programming?
A) JFrame
B) JButton
C) JComponent
D) JLabel

A

C) JComponent

22
Q

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

A

B) To handle user actions through events

23
Q

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

A) Returns the current background color of the component

24
Q

What is the default layout manager for JPanel?
A) FlowLayout
B) BorderLayout
C) GridLayout
D) CardLayout

A

A) FlowLayout

25
Q

How would you add multiple buttons to a JFrame to ensure they appear side by side?
A) Use FlowLayout for the frame
B) Use BorderLayout and add each button to a different border
C) Add each button to a separate JPanel
D) None of the above; buttons automatically align side by side

A

A) Use FlowLayout for the frame