1/79
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
1. What is a GUI in Java and how is it different from console programs?
A GUI (Graphical User Interface) allows users to interact visually using components like buttons, text fields, and menus. Unlike console programs that run top-to-bottom once, GUI programs are event-driven, meaning they wait for user actions (clicks, typing) and respond accordingly.
2. What are the three main packages used in Java GUI programming and what does each do?
java.awt.* โ basic graphics, colors, layouts
java.awt.event.* โ event handling (clicks, mouse, keyboard)
javax.swing.* โ GUI components (JFrame, JButton, etc.)
3. What is the difference between a top-level container and a component?
Top-level container (JFrame): the main window provided by the OS
Components (JButton, JPanel, etc.): elements inside the window
๐ Components must always be placed inside a container.
4. Explain the containment hierarchy in Java GUIs. Why is it important?
GUI elements are arranged in a tree structure:
JFrame โ JPanel โ ComponentsThis matters because:
Everything must be inside another object
Components wonโt appear unless properly added
Layout and rendering depend on this structure
5. What is a JFrame and what are its key responsibilities?
A JFrame is the main application window. It:
Holds all components
Controls size, title, and visibility
Manages closing behavior
6. Why doesnโt a JFrame appear or close properly by default?
It is invisible until setVisible(true) is called
Closing the window does not stop the program unless:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);7. Why should setVisible(true) be called last when building a GUI?
Because:
The GUI should be fully constructed first
Prevents flickering or partial rendering
Ensures all components appear correctly
8. What is a JComponent and why is it important?
JComponent is the base class for most Swing components (JPanel, JButton, etc.).
It provides shared functionality like:
Rendering
Handling events
Setting size and colors
9. Compare JLabel, JButton, JTextField, and JTextArea.
JLabel โ display text/image (no interaction)
JButton โ clickable, triggers actions
JTextField โ single-line input
JTextArea โ multi-line input/output
10. What is a JPanel and why is it heavily used?
A JPanel is a container used to:
Organize components
Apply layout managers
Serve as a drawing surface for graphics
11. Why are layout managers necessary?
Without them:
Components overlap
Only the last added may be visible
Layout managers automatically control positioning and sizing.
12. Explain FlowLayout.
Default layout
Places components left โ right
Wraps to next line when space runs out
Similar to text in a paragraph
13. Explain GridLayout.
Divides container into rows ร columns
All cells are equal size
Components fill their entire cell
14. Explain BoxLayout.
Arranges components in one direction
X_AXIS โ horizontal
Y_AXIS โ vertical
Good for stacking components neatly
15. What does event-driven programming mean in GUIs?
The program:
Waits for user actions (events)
Executes code only when events occur
๐ You donโt control flow โ the user does
16. What is an ActionListener and when is it used?
An ActionListener listens for actions like:
Button clicks
Pressing Enter in a text field
17. What are the three steps to implement event handling?
Implement interface (implements ActionListener)
Attach listener (addActionListener)
Override method (actionPerformed)
18. What is the role of actionPerformed(ActionEvent e)?
It is automatically called when an action occurs (e.g., button click).
You define what happens inside it.
19. Why is e.getSource() important?
It identifies which component triggered the event.
Useful when multiple components share the same listener.
20. Why canโt you create a Graphics object directly?
Because Graphics is abstract.
Java automatically provides a concrete Graphics object when drawing.
21. What coordinate system does Java graphics use?
(0,0) = top-left corner
x increases right
y increases downward
22. Difference between draw and fill methods?
drawRect() โ outline
fillRect() โ solid shape
23. What is paintComponent(Graphics g) and why is it important?
It is the main method used for custom drawing in Swing.
You override it in a JPanel subclass to render graphics.
24. Why must you call super.paintComponent(g)?
It:
Clears the screen
Prevents overlapping drawings
Ensures proper rendering
25. Why should you NOT override paint()?
Because:
It handles more complex painting tasks
Can break component rendering
๐ Always use paintComponent() instead
26. What does repaint() do?
Requests the GUI to redraw
Calls paintComponent() again
๐ Use when visuals change
27. What does validate() do?
Updates layout after structural changes
Fixes component hierarchy
28. When do you use repaint vs validate?
repaint() โ visual changes
validate() โ adding/removing components
29. How are colors represented in Java?
Using RGB values (0โ255):
new Color(255, 0, 0); // red30. Difference between foreground and background color?
Background โ panel/window color
Foreground โ text/shapes drawn
31. How do you set a font?
new Font("Verdana", Font.BOLD, 20);32. What is panel switching and why is it useful?
Instead of modifying one panel, you:
Create multiple panels
Swap them using:
remove(oldPanel);
add(newPanel);
validate();
repaint();๐ Cleaner and easier for complex GUIs
33. Why can events feel โsimultaneousโ?
Because GUIs use threads โ multiple events can be processed at the same time, leading to unexpected behavior if not handled carefully.
34. What are the most common mistakes students make?
Forgetting setVisible(true)
Forgetting super.paintComponent(g)
Confusing repaint() vs validate()
Not using getSource() with multiple buttons
Not using layout managers
35. If you had to explain the entire GUI system in 3 sentences, what would you say?
A Java GUI is a hierarchy of components inside a JFrame, organized using layout managers. The program is event-driven, meaning it waits for user actions and responds using listeners like ActionListener. Graphics are drawn by overriding paintComponent(), and the display is updated using repaint() and validate() when needed.
Q: What is File I/O in Java?
A: Moving data between files (disk) and RAM (memory).
Q: What are streams?
A: Streams are pathways that transfer data between memory and files.
Q: What is the difference between input and output streams?
Input โ file โ RAM
Output โ RAM โ file
Q: What is the โwater slideโ model?
A mental model where:
Data starts at the top (object in memory)
Flows through streams
Ends at the bottom (file)
Q: Why is the water slide model important?
A: It helps you reconstruct any File I/O problem instead of memorizing code.
Q: What does the File class represent?
A: A file or directory path (not the actual data).
Q: Difference between relative and absolute path?
Relative โ "file.txt"
Absolute โ "C:/folder/file.txt"
Q: Does creating a File object create the file?
A: Noโit only creates a reference.
Q: Why must File I/O use try/catch?
A: Because file operations can fail (missing file, permissions, etc.).
Q: What is IOException?
A: General error for file operations failing.
Q: What is ClassNotFoundException?
A: Happens when reading an object whose class Java cannot find.
Q: What are FileInputStream and FileOutputStream used for?
A: Reading/writing raw bytes.
Q: What type of data do byte streams handle?
A: Only byte arrays (no structure, no objects).
Q: When should you use byte streams?
A: When working with raw binary data or byte arrays.
Q: What is ObjectOutputStream used for?
A: Writing objects to a file.
Q: What is ObjectInputStream used for?
A: Reading objects from a file.
Q: What is serialization?
A: Converting an object into bytes so it can be stored in a file.
Q: What must a class implement to be serialized?
A: Serializable
Q: Does String need Serializable?
A: Noโit already implements it.
Q: What are the TWO hidden stages of writing an object?
Object โ byte array
Byte array โ file
Q: What are the TWO stages of reading an object?
File โ byte array
Byte array โ object
Q: Why does readObject() require casting?
A: It returns a generic Object, not a specific type.
Q: What happens if you donโt cast a readObject() function?
A: Compile-time error.
Q: What happens if you cast incorrectly?
A: Runtime ClassCastException.
Q: What is the object writing pattern?
File โ FileOutputStream โ ObjectOutputStream โ writeObject()
Q: What is the object reading pattern?
File โ FileInputStream โ ObjectInputStream โ readObject()
Q: What are DataOutputStream and DataInputStream used for?
A: Writing/reading primitive types (int, double, etc.).
Q: Why are primitive streams easier than object streams?
No casting
No ClassNotFoundException
Q: What is the MOST IMPORTANT rule for primitives?
A: Read in the SAME order you write.
Q: What happens if order is wrong?
A: Data becomes corrupted.
Q: How do you write a byte array?
A: FileOutputStream โ write(byte[])
Q: How do you read a byte array?
A: FileInputStream โ readAllBytes()
Q: Why is the byte array the simplest case?
A: No conversion, no casting, no extra streams.
Q: Which streams for objects?
A: ObjectInputStream / ObjectOutputStream
Q: Which streams for primitives?
A: DataInputStream / DataOutputStream
Q: Which streams for byte arrays?
A: FileInputStream / FileOutputStream only
Q: What happens if you forget to close streams?
A: Data may not save + memory leaks.
Q: What happens if Serializable is missing?
A: Program throws exception when writing object.
Q: What happens if you read wrong type?
A: ClassCastException.
Q: Why canโt files store objects directly?
A: Files only store bytes, not Java objects.
Q: Why does Java use layered streams?
Each stream handles one responsibility:
File stream โ bytes
Object/Data stream โ interpretation
Q: Why is ObjectInputStream more complex?
A: It reconstructs objects using metadata + class info.
Q: If question says โobject,โ what do you use?
A: Object streams
Q: If question says โint/doubleโ?
A: Data streams
Q: If question says โbyte[]โ?
A: File streams only