Unit 2: GUI and Event Handling - Practice Quiz

CSE406 — Advanced Java Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is Swing in Java primarily used for?

Introduction to Swing Easy
A. Managing database tables
B. Compiling Java source files
C. Creating graphical user interfaces
D. Writing operating system drivers

2 Which Swing class represents a top-level application window?

JFrame and JPanel Easy
A. JFrame
B. JButton
C. JLabel
D. JPanel

3 What is the main purpose of a JPanel?

JFrame and JPanel Easy
A. To capture keyboard input only
B. To display a database connection
C. To compile a Java program
D. To group and organize components

4 Which Swing component is commonly used to display a clickable button?

Swing components Easy
A. JButton
B. JTextField
C. JLabel
D. JCheckBox

5 Which Swing component is used to display non-editable text?

Swing components Easy
A. JTextArea
B. JPanel
C. JFrame
D. JLabel

6 What is the purpose of a layout manager?

Layout managers Easy
A. To change the Java compiler
B. To arrange components in a container
C. To handle database transactions
D. To create new event classes

7 Which layout manager places components in five regions: north, south, east, west, and center?

Layout managers Easy
A. GridLayout
B. BorderLayout
C. FlowLayout
D. CardLayout

8 Which Java class is used to represent colors in Swing applications?

Color class Easy
A. PaintBrush
B. ScreenColor
C. Color
D. Shade

9 Which predefined constant represents the color red?

Color class Easy
A. Color.GREEN
B. Color.YELLOW
C. Color.BLUE
D. Color.RED

10 Which Java class is used to specify the typeface, style, and size of text?

Font class Easy
A. LetterFormat
B. Typeface
C. TextStyle
D. Font

11 Which constant is used with the Font class to create bold text?

Font class Easy
A. Font.PLAIN
B. Font.NORMAL
C. Font.ITALIC
D. Font.BOLD

12 Which class provides methods for drawing shapes and text in a component?

Graphics class Easy
A. Painter
B. Graphics
C. Drawer
D. ShapeTool

13 Which method is commonly overridden to perform custom drawing in a Swing component?

Programs to create Graphical User Interface Easy
A. drawScreen()
B. start()
C. paintComponent()
D. createWindow()

14 In the event delegation model, which object receives and processes an event?

Event delegation model Easy
A. The color object
B. The font object
C. The event listener
D. The layout manager

15 What is an event source?

Event and event source Easy
A. A method that arranges components
B. An object that generates an event
C. A container that stores colors
D. A class that selects a font

16 Which listener interface is commonly used to handle button-click events?

Event listener interfaces Easy
A. WindowListener
B. ActionListener
C. KeyListener
D. MouseListener

17 Which method registers an ActionListener with a JButton?

Registration and handling events Easy
A. addActionListener()
B. setActionListener()
C. registerAction()
D. attachAction()

18 Which listener interface is used to handle events such as mouse clicks and mouse presses?

Mouse events Easy
A. ActionListener
B. FocusListener
C. KeyListener
D. MouseListener

19 Which listener interface is used to handle keyboard events?

Key events Easy
A. MouseListener
B. WindowListener
C. ActionListener
D. KeyListener

20 Why are anonymous classes often used for event listeners?

Anonymous class listeners Easy
A. They allow listener code to be written inline
B. They automatically create layout managers
C. They replace all Swing components
D. They prevent every event from occurring

21 Why should most Swing component updates be performed on the Event Dispatch Thread (EDT)?

Introduction to Swing Medium
A. Swing components are generally not thread-safe
B. Swing components require a separate JVM
C. The EDT automatically stores all GUI data
D. The EDT provides faster graphics rendering

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?

JFrame and JPanel Medium
A. Call repaint() on the panel
B. Call setTitle() on the panel
C. Call pack() on the panel
D. Call dispose() on the frame

23 A JTextField should accept input but must prevent the user from editing its contents. Which configuration achieves this?

Swing components Medium
A. setVisible(false)
B. setFocusable(false)
C. setEnabled(false)
D. setEditable(false)

24 A panel uses BorderLayout and adds two buttons without specifying constraints. Where will the buttons be placed?

Layout managers Medium
A. The first button will be replaced by the second
B. Both buttons will occupy the center region
C. The buttons will be arranged in a new row
D. Each button will occupy a separate edge region

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?

Layout managers Medium
A. FlowLayout
B. GridBagLayout
C. GridLayout
D. BorderLayout

26 What color is produced by new Color(255, 128, 0)?

Color class Medium
A. Dark blue
B. Purple
C. Bright green
D. Orange

27 Which Font configuration creates bold italic text at size 18?

Font class Medium
A. new Font("Arial", Font.BOLD | Font.ITALIC, 18)
B. new Font("Arial", Font.PLAIN, 18)
C. new Font("Arial", Font.ITALIC, 18)
D. new Font("Arial", Font.BOLD, 18)

28 Inside a component's paintComponent(Graphics g) method, why is super.paintComponent(g) normally called first?

Graphics class Medium
A. It changes the component's layout manager
B. It creates a new graphics context
C. It registers all mouse listeners
D. It clears and prepares the component background

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?

Graphics class Medium
A. g.fillRect(1, 1, 199, 99)
B. g.drawRect(0, 0, 200, 100)
C. g.fillRect(0, 0, 200, 100)
D. 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?

Programs to create Graphical User Interface Medium
A. Paint the button, hide the frame, then register listeners
B. Show the frame, add components, then choose layout
C. Dispose the frame, size it, then add components
D. Add components, choose layout, size the frame, then show it

31 In the Swing event delegation model, what happens when a user clicks a button?

Event delegation model Medium
A. The button creates an event and notifies registered listeners
B. The layout manager converts the click into a key event
C. The listener polls the button continuously for changes
D. The frame directly executes every button method

32 In an ActionListener attached to a button, what does event.getSource() return?

Event and event source Medium
A. The event listener interface
B. The object that generated the event
C. The current layout manager
D. The frame's default close operation

33 Which listener interface is used to respond to a button's activation through a mouse click or keyboard action?

Event listener interfaces Medium
A. MouseListener
B. ActionListener
C. KeyListener
D. WindowListener

34 A button has an ActionListener, but clicking it produces no response. Which missing operation is the most likely cause?

Registration and handling events Medium
A. Calling button.setHorizontalAlignment(...)
B. Calling button.addActionListener(listener)
C. Calling button.setBackground(Color.WHITE)
D. Calling 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?

Mouse events Medium
A. MouseListener and MouseMotionListener
B. MouseMotionListener only
C. MouseListener only
D. KeyListener and MouseWheelListener

36 A component must respond only when the user double-clicks it. Which condition is appropriate inside a mouse event handler?

Mouse events Medium
A. event.getButton() == 0
B. event.getClickCount() == 1
C. event.getModifiersEx() == 0
D. 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?

Key events Medium
A. keyFocused(KeyEvent e)
B. keyReleased(KeyEvent e)
C. keyTyped(KeyEvent e)
D. 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?

Key events Medium
A. The frame has a title
B. The component has keyboard focus
C. The component uses BorderLayout
D. The component has a custom font

39 Which code pattern registers an ActionListener without creating a separately named listener class?

Anonymous class listeners Medium
A. button.setListener(new ActionEvent());
B. button.addActionListener(ActionListener.class);
C. button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } });
D. button.register(ActionEvent e);

40 Why is MouseAdapter useful when a class needs to handle only mousePressed from MouseListener?

Listener interface adapter Medium
A. It automatically registers itself with every component
B. It prevents the component from receiving mouse events
C. It provides empty implementations for unused methods
D. It converts mouse events into action events

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?

Introduction to Swing Hard
A. Create the frame and model on separate worker threads
B. Create the frame on a worker thread and modify the model on the EDT
C. Create the frame on the EDT and modify the model from any thread
D. Create the frame on the EDT and modify the model on the EDT

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?

JFrame and JPanel Hard
A. The panel cannot receive mouse events
B. The panel becomes transparent only during resizing
C. Previous pixels may remain and the background may not be cleared
D. The panel automatically repaints twice per event

43 A disabled JButton is placed inside an enabled panel. A mouse click occurs at the button's coordinates. Which behavior is correct?

Swing components Hard
A. The panel receives the button's ActionEvent
B. The event is converted into a keyboard event
C. The button does not fire an ActionEvent
D. The button fires an 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?

Layout managers Hard
A. The center component replaces both directional components
B. The layout changes automatically to GridLayout
C. The two top-region constraints conflict and one component replaces the other
D. All three components remain visible in independent regions

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?

Layout managers Hard
A. The component receives no extra space from the grid
B. The component expands only if weighty is positive
C. The component expands because horizontal fill overrides weight
D. The component receives all extra horizontal space

46 A color is constructed as new Color(300, -10, 128). What is the correct result?

Color class Hard
A. A color with values clamped to valid bounds
B. A color with values wrapped modulo 256
C. A transparent color is created
D. An 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?

Color class Hard
A. The component is not double buffered
B. The color object is immutable
C. The EDT ignores color changes
D. The component is not opaque

48 A program creates new Font("UnknownFamily", Font.BOLD, 18). Which statement is correct?

Font class Hard
A. The requested family is silently installed
B. The style is ignored because the family is unknown
C. A fallback font may be used for rendering
D. Construction always throws an exception

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?

Font class Hard
A.
B.
C.
D.

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?

Graphics class Hard
A. Timers cannot run while a component is visible
B. Painting outside the EDT permanently disables repainting
C. The stored graphics context may be invalid outside painting
D. Graphics objects cannot draw text

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?

Graphics class Hard
A. The component must use absolute screen coordinates
B. Scrolling disables clipping for child components
C. The rectangle must be drawn only after setVisible(false)
D. The graphics origin may be translated to the component's visible region

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?

Programs to create Graphical User Interface Hard
A. The request automatically moves to a worker thread
B. The EDT blocks and the interface becomes unresponsive
C. Swing creates a second EDT for the request
D. Only the button becomes temporarily disabled

53 In the Swing event delegation model, what is the correct relationship among a source, an event, and a listener?

Event delegation model Hard
A. The listener creates events and the source consumes them
B. The event registers itself with the source
C. The source creates events and invokes registered listeners
D. The listener replaces the source after registration

54 A single JButton has two different ActionListener objects registered. When the user activates the button, what should a correct implementation expect?

Event and event source Hard
A. Only the most recently registered listener is invoked
B. Both listeners are eligible to receive the action event
C. Registration fails because a source allows one listener
D. The button creates two separate action events

55 A class implements MouseListener and needs behavior only when the mouse enters a component. What is required for compilation?

Event listener interfaces Hard
A. Implement every abstract method in MouseListener
B. Implement mouseEntered only
C. Implement KeyListener as well
D. Extend 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?

Registration and handling events Hard
A. Register the listener on the frame only
B. Request focus for the text field and make it focusable
C. Replace the listener with a MouseAdapter
D. Call 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?

Mouse events Hard
A. Coordinates relative to the top-level frame
B. Coordinates relative to the monitor's origin
C. Coordinates relative to the entire screen
D. Coordinates relative to the event source

58 A component listens for mouseClicked and must distinguish a double-click from a single click. Which property should it inspect?

Mouse events Hard
A. getModifiersEx()
B. getClickCount()
C. getWhen()
D. 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?

Key events Hard
A. keyPressed
B. keyTyped
C. actionPerformed
D. 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?

Anonymous class listeners Hard
A. Anonymous classes cannot access local variables; use a static method
B. Action listeners cannot modify state; use a repaint call
C. Captured locals must be effectively final; use a field or mutable holder
D. Captured locals must be public; change the variable modifier