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. Writing operating system drivers
B. Creating graphical user interfaces
C. Managing database tables
D. Compiling Java source files

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

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

3 What is the main purpose of a JPanel?

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

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

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

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

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

6 What is the purpose of a layout manager?

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

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

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

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

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

9 Which predefined constant represents the color red?

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

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

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

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

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

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

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

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

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

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

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

15 What is an event source?

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

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

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

17 Which method registers an ActionListener with a JButton?

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

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

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

19 Which listener interface is used to handle keyboard events?

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

20 Why are anonymous classes often used for event listeners?

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

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

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

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 dispose() on the frame
D. Call pack() on the panel

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. setEnabled(false)
C. setFocusable(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. Each button will occupy a separate edge region
C. The buttons will be arranged in a new row
D. Both buttons will occupy the center 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. GridBagLayout
B. FlowLayout
C. BorderLayout
D. GridLayout

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

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

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.BOLD, 18)
C. new Font("Arial", Font.PLAIN, 18)
D. new Font("Arial", Font.ITALIC, 18)

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

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

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.drawRect(1, 1, 200, 100)
B. g.drawRect(0, 0, 200, 100)
C. g.fillRect(1, 1, 199, 99)
D. g.fillRect(0, 0, 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. Dispose the frame, size it, then add components
C. Show the frame, add components, then choose layout
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 frame's default close operation
D. The current layout manager

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. WindowListener
B. ActionListener
C. KeyListener
D. MouseListener

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.setBackground(Color.WHITE)
B. Calling button.setBorderPainted(false)
C. Calling button.setHorizontalAlignment(...)
D. Calling button.addActionListener(listener)

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. KeyListener and MouseWheelListener
C. MouseMotionListener only
D. MouseListener only

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.getClickCount() == 1
B. event.getModifiersEx() == 0
C. event.getButton() == 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. keyPressed(KeyEvent e)
B. keyTyped(KeyEvent e)
C. keyFocused(KeyEvent e)
D. keyReleased(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 component has a custom font
B. The component has keyboard focus
C. The component uses BorderLayout
D. The frame has a title

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

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

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 converts mouse events into action events
D. It provides empty implementations for unused methods

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 on the EDT and modify the model from any thread
B. Create the frame and model on separate worker threads
C. Create the frame on the EDT and modify the model on the EDT
D. Create the frame on a worker thread 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 becomes transparent only during resizing
B. The panel automatically repaints twice per event
C. Previous pixels may remain and the background may not be cleared
D. The panel cannot receive mouse events

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

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 layout changes automatically to GridLayout
B. All three components remain visible in independent regions
C. The two top-region constraints conflict and one component replaces the other
D. The center component replaces both directional components

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

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 EDT ignores color changes
C. The color object is immutable
D. The component is not opaque

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

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

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. The stored graphics context may be invalid outside painting
B. Painting outside the EDT permanently disables repainting
C. Graphics objects cannot draw text
D. Timers cannot run while a component is visible

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. Scrolling disables clipping for child components
B. The rectangle must be drawn only after setVisible(false)
C. The component must use absolute screen coordinates
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. Only the button becomes temporarily disabled
B. Swing creates a second EDT for the request
C. The EDT blocks and the interface becomes unresponsive
D. The request automatically moves to a worker thread

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 source creates events and invokes registered listeners
B. The listener creates events and the source consumes them
C. The event registers itself with the source
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. Registration fails because a source allows one listener
C. The button creates two separate action events
D. Both listeners are eligible to receive the action event

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. Call setOpaque(false) on the text field
B. Request focus for the text field and make it focusable
C. Replace the listener with a MouseAdapter
D. Register the listener on the frame only

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 monitor's origin
B. Coordinates relative to the event source
C. Coordinates relative to the top-level frame
D. Coordinates relative to the entire screen

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. getWhen()
C. getButtonCount()
D. getClickCount()

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. keyTyped
B. keyPressed
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. Captured locals must be public; change the variable modifier
C. Action listeners cannot modify state; use a repaint call
D. Captured locals must be effectively final; use a field or mutable holder