Unit 2: GUI and Event Handling - Practice Quiz
1 What is Swing in Java primarily used for?
2 Which Swing class represents a top-level application window?
JFrame
JButton
JLabel
JPanel
3
What is the main purpose of a JPanel?
4 Which Swing component is commonly used to display a clickable button?
JButton
JTextField
JLabel
JCheckBox
5 Which Swing component is used to display non-editable text?
JTextArea
JPanel
JFrame
JLabel
6 What is the purpose of a layout manager?
7 Which layout manager places components in five regions: north, south, east, west, and center?
GridLayout
BorderLayout
FlowLayout
CardLayout
8 Which Java class is used to represent colors in Swing applications?
PaintBrush
ScreenColor
Color
Shade
9 Which predefined constant represents the color red?
Color.GREEN
Color.YELLOW
Color.BLUE
Color.RED
10 Which Java class is used to specify the typeface, style, and size of text?
LetterFormat
Typeface
TextStyle
Font
11
Which constant is used with the Font class to create bold text?
Font.PLAIN
Font.NORMAL
Font.ITALIC
Font.BOLD
12 Which class provides methods for drawing shapes and text in a component?
Painter
Graphics
Drawer
ShapeTool
13 Which method is commonly overridden to perform custom drawing in a Swing component?
drawScreen()
start()
paintComponent()
createWindow()
14 In the event delegation model, which object receives and processes an event?
15 What is an event source?
16 Which listener interface is commonly used to handle button-click events?
WindowListener
ActionListener
KeyListener
MouseListener
17
Which method registers an ActionListener with a JButton?
addActionListener()
setActionListener()
registerAction()
attachAction()
18 Which listener interface is used to handle events such as mouse clicks and mouse presses?
ActionListener
FocusListener
KeyListener
MouseListener
19 Which listener interface is used to handle keyboard events?
MouseListener
WindowListener
ActionListener
KeyListener
20 Why are anonymous classes often used for event listeners?
21 Why should most Swing component updates be performed on the Event Dispatch Thread (EDT)?
22
A JPanel is added to a JFrame, but it does not appear after the frame becomes visible. Which action is most appropriate when components were added after the frame was displayed?
repaint() on the panel
setTitle() on the panel
pack() on the panel
dispose() on the frame
23
A JTextField should accept input but must prevent the user from editing its contents. Which configuration achieves this?
setVisible(false)
setFocusable(false)
setEnabled(false)
setEditable(false)
24
A panel uses BorderLayout and adds two buttons without specifying constraints. Where will the buttons be placed?
25 A form requires labels and fields to align in rows and columns, with consistent control over horizontal and vertical gaps. Which layout manager is most suitable?
FlowLayout
GridBagLayout
GridLayout
BorderLayout
26
What color is produced by new Color(255, 128, 0)?
27
Which Font configuration creates bold italic text at size 18?
28
Inside a component's paintComponent(Graphics g) method, why is super.paintComponent(g) normally called first?
29 A component has width 200 and height 100. Which call draws a rectangle that fills the component's drawable area when the coordinate origin is at its top-left corner?
g.fillRect(1, 1, 199, 99)
g.drawRect(0, 0, 200, 100)
g.fillRect(0, 0, 200, 100)
g.drawRect(1, 1, 200, 100)
30
A GUI is built using a JFrame containing a panel and a button. Which sequence is generally appropriate before displaying the frame?
31 In the Swing event delegation model, what happens when a user clicks a button?
32
In an ActionListener attached to a button, what does event.getSource() return?
33 Which listener interface is used to respond to a button's activation through a mouse click or keyboard action?
MouseListener
ActionListener
KeyListener
WindowListener
34
A button has an ActionListener, but clicking it produces no response. Which missing operation is the most likely cause?
button.setHorizontalAlignment(...)
button.addActionListener(listener)
button.setBackground(Color.WHITE)
button.setBorderPainted(false)
35 A drawing program should start a new shape only when the mouse button is pressed, continue tracking while it moves, and finish when it is released. Which listener combination is appropriate?
MouseListener and MouseMotionListener
MouseMotionListener only
MouseListener only
KeyListener and MouseWheelListener
36 A component must respond only when the user double-clicks it. Which condition is appropriate inside a mouse event handler?
event.getButton() == 0
event.getClickCount() == 1
event.getModifiersEx() == 0
event.getClickCount() == 2
37
A game should react immediately when the user presses the left-arrow key, before the key is released. Which KeyListener method should be used?
keyFocused(KeyEvent e)
keyReleased(KeyEvent e)
keyTyped(KeyEvent e)
keyPressed(KeyEvent e)
38
A KeyListener is correctly registered on a text-area component, but it receives no keyboard events. Which condition is most likely missing?
BorderLayout
39
Which code pattern registers an ActionListener without creating a separately named listener class?
40
Why is MouseAdapter useful when a class needs to handle only mousePressed from MouseListener?
41
A Swing application creates its JFrame, adds components, and calls setVisible(true) from the main thread while another thread modifies a JTable model. Which design best preserves Swing's threading guarantees?
42
A JPanel overrides paintComponent(Graphics g) but does not call super.paintComponent(g). The panel has a non-opaque background and is placed over another component. What is the most likely consequence?
43
A disabled JButton is placed inside an enabled panel. A mouse click occurs at the button's coordinates. Which behavior is correct?
ActionEvent
ActionEvent
ActionEvent normally
44
A BorderLayout container receives three components using BorderLayout.NORTH, BorderLayout.PAGE_START, and BorderLayout.CENTER. Which statement describes the result most accurately?
GridLayout
45
A GridBagLayout component has weightx = 0, fill = GridBagConstraints.HORIZONTAL, and a preferred width smaller than the available cell width. What happens when the container grows horizontally?
weighty is positive
46
A color is constructed as new Color(300, -10, 128). What is the correct result?
IllegalArgumentException is thrown
47
A component calls setBackground(Color.RED) but still appears with its default background. Which condition most directly explains this behavior for a standard Swing component?
48
A program creates new Font("UnknownFamily", Font.BOLD, 18). Which statement is correct?
49
A component draws text using FontMetrics.getAscent() and getDescent(). If the baseline is at , where should the top of the normal text bounds approximately begin?
50
Inside paintComponent, a program stores the supplied Graphics object in a field and uses that field later from a timer to draw directly. What is the principal problem?
51
A custom component draws a filled rectangle at coordinates (0, 0) but the rectangle appears clipped or partly missing after scrolling. Which explanation is most accurate?
setVisible(false)
52
A form validates user input and then performs a network request directly inside an ActionListener. The request takes several seconds. What visible behavior is most likely?
53 In the Swing event delegation model, what is the correct relationship among a source, an event, and a listener?
54
A single JButton has two different ActionListener objects registered. When the user activates the button, what should a correct implementation expect?
55
A class implements MouseListener and needs behavior only when the mouse enters a component. What is required for compilation?
MouseListener
mouseEntered only
KeyListener as well
MouseEvent and override dispatch
56
A JTextField has a KeyListener registered, but key callbacks are never called while a button has focus. Which change is most directly relevant?
MouseAdapter
setOpaque(false) on the text field
57
A component receives a MouseEvent with getX() = 20 and getY() = 15 after the parent has been scrolled. What do these coordinates represent?
58
A component listens for mouseClicked and must distinguish a double-click from a single click. Which property should it inspect?
getModifiersEx()
getClickCount()
getWhen()
getButtonCount()
59
A KeyListener receives a key event for the uppercase character A. Which event type is designed to identify the physical key press independently of the character produced by modifiers?
keyPressed
keyTyped
actionPerformed
keyReleased
60
An anonymous ActionListener captures a local variable named count and attempts to increment it inside actionPerformed. Why does this fail, and which replacement is valid?
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →