Free Sun Certified Java Programmer Basic Questions and Answers — Questions and Answers
Question 1: Only one of the five Component methods listed below is also a method of the class MenuItem. Which one?
- setEnabled( boolean b ) (Correct answer)
- setForeground( Color c )
- setVisible( boolean b )
- getSize()
Correct answer: setEnabled( boolean b )
The `setEnabled(boolean b)` method is a common method found in both the `Component` class and the `MenuItem` class. This method controls whether the component or menu item is active and can respond to user input. While other options like `setForeground`, `setVisible`, and `getSize` are `Component` methods, `MenuItem` specifically shares the `setEnabled` functionality.
Question 2: Which of the java.awt.Graphics class's following methods would be used to draw the outline of a rectangle in a single method call?
- drawRect() (Correct answer)
- fillPolygon()
- drawPolygon() (Correct answer)
- drawLine()
Correct answer: drawRect()
The `drawRect()` method of the `java.awt.Graphics` class is specifically designed to draw the outline of a rectangle using a single method call. Other methods like `fillPolygon()` and `drawPolygon()` are for drawing polygons, and `drawLine()` is for drawing individual line segments, not a complete rectangle outline.
Question 3: If comp is a __________, the Container methods add(Component comp) and add(String name, Component comp) will raise an IllegalArgumentException.
- window (Correct answer)
- textarea
- container that contains this container (Correct answer)
- button
Correct answer: window
In AWT, `Window` objects (which include `Frame` and `Dialog`) are top-level containers and cannot be added to other `Container` objects using the `add()` methods. Attempting to add a `Window` to another `Container` will result in an `IllegalArgumentException` because `Window` objects are designed to be standalone and not nested within other components.
Question 4: Which one or ones of the following AWT classes are in charge of carrying out the components layout?
- FlowLayout (Correct answer)
- LayoutManager
- WindowAdapter
- GridBagLayout (Correct answer)
Correct answer: FlowLayout
`FlowLayout` and `GridBagLayout` are concrete layout manager classes in AWT that are responsible for arranging components within a container. `LayoutManager` is an interface that defines the contract for layout managers, not a class that performs layout itself. `WindowAdapter` is an adapter class for handling window events, unrelated to component layout.
Question 5: If a component needs to resize vertically but not horizontally, it should go in a:
- FlowLayout as the first component
- BorderLayout in the North or South location
- BorderLayout in the Center location
- BorderLayout in the East or West location (Correct answer)
Correct answer: BorderLayout in the East or West location
In a `BorderLayout`, components placed in the `EAST` or `WEST` regions will automatically resize vertically to fill the available height of the container. However, their horizontal width is typically fixed or determined by their preferred size, allowing them to resize vertically without changing horizontally. Components in `NORTH` or `SOUTH` resize horizontally, and `CENTER` resizes both ways.
Question 6: Which of the following best describes the order in which a component is redrawn as a result of method calls?
- invoke update which calls paint()
- invoke repaint() which invokes update(), which in turn invokes paint() (Correct answer)
- invoke repaint() which invokes paint directly
- invoke paint() directly
Correct answer: invoke repaint() which invokes update(), which in turn invokes paint()
When a component needs to be redrawn, the `repaint()` method is typically invoked. This method does not draw directly but schedules an update request. The AWT event dispatcher then calls the `update()` method, which by default clears the component's background and then calls the `paint()` method to perform the actual drawing.
Question 7: The following class(es)' methods can be used to create directories:
- DataOutput
- FileDescriptor
- File (Correct answer)
- Directory
Correct answer: File
The `java.io.File` class provides methods for interacting with the file system, including creating directories. Specifically, the `mkdir()` method creates a single directory, and `mkdirs()` creates a directory along with any necessary but nonexistent parent directories. `DataOutput` and `FileDescriptor` serve different purposes, and `Directory` is not a standard Java class for this function.
Only one of the five Component methods listed below is also a method of the class MenuItem.
Which one?