Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Swing is a Java toolkit used to create graphical user interfaces such as windows, buttons, and text fields.
Question 2 of 60 · JFrame and JPanel · Easy
Which Swing class represents a top-level application window?
Not quite. Try another option.
Answer: JFrame
JFrame provides a window with features such as a title bar, borders, and controls.
Question 3 of 60 · JFrame and JPanel · Easy
What is the main purpose of a JPanel?
Not quite. Try another option.
Answer: To group and organize components
A JPanel is a container that holds and organizes GUI components.
Question 4 of 60 · Swing components · Easy
Which Swing component is commonly used to display a clickable button?
Not quite. Try another option.
Answer: JButton
JButton creates a button that users can click to perform an action.
Question 5 of 60 · Swing components · Easy
Which Swing component is used to display non-editable text?
Not quite. Try another option.
Answer: JLabel
JLabel displays text or an image that users generally cannot edit directly.
Question 6 of 60 · Layout managers · Easy
What is the purpose of a layout manager?
Not quite. Try another option.
Answer: To arrange components in a container
A layout manager controls the size and position of components inside a container.
Question 7 of 60 · Layout managers · Easy
Which layout manager places components in five regions: north, south, east, west, and center?
Not quite. Try another option.
Answer: BorderLayout
BorderLayout divides a container into five regions: north, south, east, west, and center.
Question 8 of 60 · Color class · Easy
Which Java class is used to represent colors in Swing applications?
Not quite. Try another option.
Answer: Color
The Color class from java.awt represents colors used by GUI components and graphics.
Question 9 of 60 · Color class · Easy
Which predefined constant represents the color red?
Not quite. Try another option.
Answer: Color.RED
Color.RED is the predefined Color constant for red.
Question 10 of 60 · Font class · Easy
Which Java class is used to specify the typeface, style, and size of text?
Not quite. Try another option.
Answer: Font
The Font class defines a text font's name, style, and size.
Question 11 of 60 · Font class · Easy
Which constant is used with the Font class to create bold text?
Not quite. Try another option.
Answer: Font.BOLD
Font.BOLD specifies that the text should use a bold style.
Question 12 of 60 · Graphics class · Easy
Which class provides methods for drawing shapes and text in a component?
Not quite. Try another option.
Answer: Graphics
The Graphics class provides methods such as drawLine, drawRect, and drawString.
Question 13 of 60 · Programs to create Graphical User Interface · Easy
Which method is commonly overridden to perform custom drawing in a Swing component?
Not quite. Try another option.
Answer: paintComponent()
Custom painting in a Swing component is commonly performed by overriding paintComponent(Graphics g).
Question 14 of 60 · Event delegation model · Easy
In the event delegation model, which object receives and processes an event?
Not quite. Try another option.
Answer: The event listener
An event listener receives notification from an event source and handles the event.
Question 15 of 60 · Event and event source · Easy
What is an event source?
Not quite. Try another option.
Answer: An object that generates an event
An event source is a GUI object, such as a button, that generates events when users interact with it.
Question 16 of 60 · Event listener interfaces · Easy
Which listener interface is commonly used to handle button-click events?
Not quite. Try another option.
Answer: ActionListener
ActionListener is commonly used to respond to action events such as button clicks.
Question 17 of 60 · Registration and handling events · Easy
Which method registers an ActionListener with a JButton?
Not quite. Try another option.
Answer: addActionListener()
The addActionListener() method registers an object to receive action events from a button.
Question 18 of 60 · Mouse events · Easy
Which listener interface is used to handle events such as mouse clicks and mouse presses?
Not quite. Try another option.
Answer: MouseListener
MouseListener provides methods for handling mouse actions such as clicking, pressing, releasing, entering, and exiting.
Question 19 of 60 · Key events · Easy
Which listener interface is used to handle keyboard events?
Not quite. Try another option.
Answer: KeyListener
KeyListener handles keyboard events such as key presses, releases, and typed characters.
Question 20 of 60 · Anonymous class listeners · Easy
Why are anonymous classes often used for event listeners?
Not quite. Try another option.
Answer: They allow listener code to be written inline
An anonymous class lets a programmer define and register listener behavior at the point where it is needed.
Question 21 of 60 · Introduction to Swing · Medium
Why should most Swing component updates be performed on the Event Dispatch Thread (EDT)?
Not quite. Try another option.
Answer: Swing components are generally not thread-safe
Swing is mostly single-threaded, so creating or updating components on the EDT helps prevent race conditions and inconsistent GUI states.
Question 22 of 60 · JFrame and JPanel · Medium
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?
Not quite. Try another option.
Answer: Call repaint() on the panel
Calling repaint() requests that the panel be redrawn. When the component hierarchy changes, revalidate() may also be required to recalculate the layout.
Question 23 of 60 · Swing components · Medium
A JTextField should accept input but must prevent the user from editing its contents. Which configuration achieves this?
Not quite. Try another option.
Answer: setEditable(false)
setEditable(false) prevents text modification while allowing the component to remain visible and generally usable for selection or copying.
Question 24 of 60 · Layout managers · Medium
A panel uses BorderLayout and adds two buttons without specifying constraints. Where will the buttons be placed?
Not quite. Try another option.
Answer: The first button will be replaced by the second
BorderLayout allows only one component in each region. The default region is CENTER, so adding the second button there replaces the first.
Question 25 of 60 · Layout managers · Medium
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?
Not quite. Try another option.
Answer: GridBagLayout
GridBagLayout supports flexible row and column placement, component spanning, weights, and insets, making it suitable for structured forms.
Question 26 of 60 · Color class · Medium
What color is produced by new Color(255, 128, 0)?
Not quite. Try another option.
Answer: Orange
The constructor uses red, green, and blue values. Maximum red, medium green, and zero blue produce an orange color.
Question 27 of 60 · Font class · Medium
Which Font configuration creates bold italic text at size 18?
Not quite. Try another option.
Answer: new Font("Arial", Font.BOLD | Font.ITALIC, 18)
Font.BOLD and Font.ITALIC are combined with the bitwise OR operator to apply both styles.
Question 28 of 60 · Graphics class · Medium
Inside a component's paintComponent(Graphics g) method, why is super.paintComponent(g) normally called first?
Not quite. Try another option.
Answer: It clears and prepares the component background
The superclass implementation performs necessary painting work, including clearing the previous contents, before custom drawing is performed.
Question 29 of 60 · Graphics class · Medium
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?
Not quite. Try another option.
Answer: g.fillRect(0, 0, 200, 100)
fillRect paints the interior, while drawRect paints only the border. Starting at (0, 0) uses the full component coordinate range.
Question 30 of 60 · Programs to create Graphical User Interface · Medium
A GUI is built using a JFrame containing a panel and a button. Which sequence is generally appropriate before displaying the frame?
Not quite. Try another option.
Answer: Add components, choose layout, size the frame, then show it
Components and layout should be configured before display. pack() or explicit sizing can then determine the frame dimensions before calling setVisible(true).
Question 31 of 60 · Event delegation model · Medium
In the Swing event delegation model, what happens when a user clicks a button?
Not quite. Try another option.
Answer: The button creates an event and notifies registered listeners
The event source generates an event and delegates its handling to listener objects that have registered for that event type.
Question 32 of 60 · Event and event source · Medium
In an ActionListener attached to a button, what does event.getSource() return?
Not quite. Try another option.
Answer: The object that generated the event
getSource() returns the event source, such as the button that was clicked, and it can be cast to the expected component type.
Question 33 of 60 · Event listener interfaces · Medium
Which listener interface is used to respond to a button's activation through a mouse click or keyboard action?
Not quite. Try another option.
Answer: ActionListener
Buttons generate action events for activation, and classes implementing ActionListener handle them through actionPerformed(ActionEvent e).
Question 34 of 60 · Registration and handling events · Medium
A button has an ActionListener, but clicking it produces no response. Which missing operation is the most likely cause?
The listener must be registered with the event source using addActionListener before the source can notify it.
Question 35 of 60 · Mouse events · Medium
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?
Not quite. Try another option.
Answer: MouseListener and MouseMotionListener
MouseListener handles press and release events, while MouseMotionListener handles movement events such as mouseDragged.
Question 36 of 60 · Mouse events · Medium
A component must respond only when the user double-clicks it. Which condition is appropriate inside a mouse event handler?
Not quite. Try another option.
Answer: event.getClickCount() == 2
getClickCount() reports the number of consecutive clicks associated with the mouse event, so a value of 2 identifies a double-click.
Question 37 of 60 · Key events · Medium
A game should react immediately when the user presses the left-arrow key, before the key is released. Which KeyListener method should be used?
Not quite. Try another option.
Answer: keyPressed(KeyEvent e)
keyPressed is invoked when a physical key is pushed down, making it suitable for immediate movement or command handling.
Question 38 of 60 · Key events · Medium
A KeyListener is correctly registered on a text-area component, but it receives no keyboard events. Which condition is most likely missing?
Not quite. Try another option.
Answer: The component has keyboard focus
Key events are delivered to the component that owns the keyboard focus. The component may need to be focusable and request focus appropriately.
Question 39 of 60 · Anonymous class listeners · Medium
Which code pattern registers an ActionListener without creating a separately named listener class?
An anonymous class implements ActionListener inline and supplies the required actionPerformed method at the registration point.
Question 40 of 60 · Listener interface adapter · Medium
Why is MouseAdapter useful when a class needs to handle only mousePressed from MouseListener?
Not quite. Try another option.
Answer: It provides empty implementations for unused methods
MouseListener has several methods. Extending MouseAdapter allows the class to override only the callback it needs.
Question 41 of 60 · Introduction to Swing · Hard
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?
Not quite. Try another option.
Answer: Create the frame on the EDT and modify the model on the EDT
Swing component creation, mutation, and most model notifications should occur on the Event Dispatch Thread (EDT). Long-running work belongs on a worker thread, with results published back to the EDT.
Question 42 of 60 · JFrame and JPanel · Hard
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?
Not quite. Try another option.
Answer: Previous pixels may remain and the background may not be cleared
Calling super.paintComponent(g) lets Swing perform normal background clearing and UI painting. Omitting it can produce stale pixels and incorrect rendering, especially during partial repaints.
Question 43 of 60 · Swing components · Hard
A disabled JButton is placed inside an enabled panel. A mouse click occurs at the button's coordinates. Which behavior is correct?
Not quite. Try another option.
Answer: The button does not fire an ActionEvent
A disabled Swing component does not activate its normal user interaction or fire its associated action event. The enabled parent does not automatically receive the button's action event.
Question 44 of 60 · Layout managers · Hard
A BorderLayout container receives three components using BorderLayout.NORTH, BorderLayout.PAGE_START, and BorderLayout.CENTER. Which statement describes the result most accurately?
Not quite. Try another option.
Answer: The two top-region constraints conflict and one component replaces the other
PAGE_START is the orientation-aware equivalent of the north region. Both constraints target the same logical region, so the later component can replace the earlier one rather than creating two separate areas.
Question 45 of 60 · Layout managers · Hard
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?
Not quite. Try another option.
Answer: The component receives no extra space from the grid
weightx controls how extra horizontal space is distributed among grid columns. With zero weight, the component's cell does not receive additional space, regardless of the horizontal fill setting.
Question 46 of 60 · Color class · Hard
A color is constructed as new Color(300, -10, 128). What is the correct result?
Not quite. Try another option.
Answer: An IllegalArgumentException is thrown
The integer red, green, and blue components must each be in the inclusive range through . Values outside that range cause an IllegalArgumentException.
Question 47 of 60 · Color class · Hard
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?
Not quite. Try another option.
Answer: The component is not opaque
Many Swing components only paint their background when they are opaque. Calling setOpaque(true) may be necessary, especially for labels, panels with custom backgrounds, and similar components.
Question 48 of 60 · Font class · Hard
A program creates new Font("UnknownFamily", Font.BOLD, 18). Which statement is correct?
Not quite. Try another option.
Answer: A fallback font may be used for rendering
Java permits a font object to be created with an unavailable family name. The platform's font mechanism selects a fallback physical font when rendering is required.
Question 49 of 60 · Font class · Hard
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?
Not quite. Try another option.
Answer:
Text is positioned relative to its baseline. The ascent extends upward from the baseline, so the approximate top is .
Question 50 of 60 · Graphics class · Hard
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?
Not quite. Try another option.
Answer: The stored graphics context may be invalid outside painting
The graphics context supplied during painting is managed by Swing and should not be retained. State should be updated, followed by repaint(), which causes Swing to provide a fresh context.
Question 51 of 60 · Graphics class · Hard
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?
Not quite. Try another option.
Answer: The graphics origin may be translated to the component's visible region
Swing may translate the graphics coordinate system and apply a clip region during painting. Drawing is relative to the current component graphics context, not necessarily the screen origin.
Question 52 of 60 · Programs to create Graphical User Interface · Hard
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?
Not quite. Try another option.
Answer: The EDT blocks and the interface becomes unresponsive
Action listeners normally execute on the EDT. Blocking I/O there prevents repainting and event processing, so the request should run in a background worker and publish UI changes on the EDT.
Question 53 of 60 · Event delegation model · Hard
In the Swing event delegation model, what is the correct relationship among a source, an event, and a listener?
Not quite. Try another option.
Answer: The source creates events and invokes registered listeners
An event source detects an interaction and creates or dispatches an event object. Registered listener objects receive callbacks through the appropriate listener interface.
Question 54 of 60 · Event and event source · Hard
A single JButton has two different ActionListener objects registered. When the user activates the button, what should a correct implementation expect?
Not quite. Try another option.
Answer: Both listeners are eligible to receive the action event
Swing event sources generally maintain listener lists. Adding a listener does not replace existing listeners, so each registered ActionListener can receive the event.
Question 55 of 60 · Event listener interfaces · Hard
A class implements MouseListener and needs behavior only when the mouse enters a component. What is required for compilation?
Not quite. Try another option.
Answer: Implement every abstract method in MouseListener
MouseListener declares several callback methods. A concrete class implementing it must provide all of them, even when most have empty bodies; an adapter can avoid that boilerplate.
Question 56 of 60 · Registration and handling events · Hard
A JTextField has a KeyListener registered, but key callbacks are never called while a button has focus. Which change is most directly relevant?
Not quite. Try another option.
Answer: Request focus for the text field and make it focusable
Key events are delivered to the component that owns keyboard focus. The intended component must be focusable and must obtain focus at an appropriate time, often after display.
Question 57 of 60 · Mouse events · Hard
A component receives a MouseEvent with getX() = 20 and getY() = 15 after the parent has been scrolled. What do these coordinates represent?
Not quite. Try another option.
Answer: Coordinates relative to the event source
MouseEvent coordinates are expressed in the coordinate system of the component that is the event source. Scrolling and parent offsets do not change that local interpretation.
Question 58 of 60 · Mouse events · Hard
A component listens for mouseClicked and must distinguish a double-click from a single click. Which property should it inspect?
Not quite. Try another option.
Answer: getClickCount()
MouseEvent.getClickCount() reports the number of consecutive clicks recognized for the event, allowing handlers to distinguish single-click and double-click actions.
Question 59 of 60 · Key events · Hard
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?
Not quite. Try another option.
Answer: keyPressed
keyPressed and keyReleased describe key actions and provide a key code. keyTyped represents a character input and is not intended for identifying every physical key.
Question 60 of 60 · Anonymous class listeners · Hard
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?
Not quite. Try another option.
Answer: Captured locals must be effectively final; use a field or mutable holder
A local variable captured by an anonymous class must be final or effectively final. Mutable state can instead be stored in an instance field or an explicitly mutable holder.
That's unit 2
Answer a few questions and your score shows up here.
Nearly through. Did unit 2 of CSE406 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 2 of CSE406 save you a night before the exam?
Then you can help keep it free for the next batch. 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.