Tuesday, April 19, 2011

Java AWT Interview Questions

The following are some questions you might encounter with respect to the AWT components in any Interview. AWT stands for Abstract Window Toolkit and is used for creating UI components using Java. Though AWT is outdated and its successor Java Swings is more widely used, AWT is still being used by many projects.

Questions:

1. Which containers use a border Layout as their default layout?

The window, Frame and Dialog classes use a border layout as their default layout.

2. What is the preferred size of a component?

The preferred size of a component is the minimum component size that is required for the component to display normally.

3. What method is used to specify a container's layout?

The setLayout() method is used to specify a container's layout.

4. Which containers use a FlowLayout as their default layout?

The Panel and Applet classes use the FlowLayout as their default layout.

5. Which method of the Component class is used to set the position and size of a component?

The setBounds() method

6. Which java.util classes and interfaces support event handling?

The EventObject class and the EventListener interface support event processing.

7. What is the purpose of using Event Handlers?

Any UI based application would have to respond to user actions. For ex: If you click on the Save button in a web page, you would expect the system to save the values you entered in the screen. Similarly there might be numerous actions that might need to be captured and handled by the system. Event handlers help us with that. Capturing user button clicks, opening of new pages etc.

8. What is the immediate superclass of the Applet class?

Panel

9. Name few Component subclasses that support painting.

The Canvas, Frame, Panel, and Applet classes support painting.

10. What is the immediate superclass of the Dialog class?

Window

11. What is clipping?

Clipping is the process of confining paint operations to a limited area or shape.

12. What is the difference between a MenuItem and a CheckboxMenuItem?

The CheckboxMenuItem class extends the MenuItem class to support a menu item that may be checked or unchecked.

13. What class is the top of the AWT event hierarchy?

The java.awt.AWTEvent class is the highest-level class in the AWT event-class hierarchy.

14. In which package are most of the AWT events that support the event-delegation model defined?

Most of the AWT-related events of the event-delegation model are defined in the java.awt.event package. The AWTEvent class is defined in the java.awt package.

15. What is the immediate superclass of Menu?

MenuItem

16. Which class is the immediate superclass of the MenuComponent class.

Object

17. Name three subclasses of the Component class.

Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, TextComponent etc are all subclasses of the Component class.

18. Which Container method is used to cause a container to be laid out and redisplayed?

validate()

19. Name two subclasses of the TextComponent class.

TextField and TextArea

20. What is the advantage of the event-delegation model over the earlier event-inheritance model?

The event-delegation model has two advantages over the event-inheritance model. First, it enables event handling to be handled by objects other than the ones that generate the events (or their containers). This allows a clean separation between a component's design and its use. The other advantage of the event-delegation model is that it performs much better in applications where many events are generated. This performance improvement is due to the fact that the event-delegation model does not have to repeatedly process unhandled events, as is the case of the event-inheritance model.

21. Which containers may have a MenuBar?

Frame

22. What is the relationship between the Canvas class and the Graphics class?

A Canvas object provides access to a Graphics object via its paint() method.

23. How are the elements of a BorderLayout organized?

The elements of a BorderLayout are organized at the borders (North, South, East, and West) and the center of a container. Usually the Center is the largest and occupies the most space in the layout.

24. What is the difference between a Window and a Frame?

The Frame class extends Window to define a main application window that can have a menu bar.

25. Which TextComponent method is used to set a TextComponent to the read-only state?

setEditable(). By setting setEditable(false) you can ensure that no one can edit that particular text component.

26. How are the elements of a CardLayout organized?

The elements of a CardLayout are stacked, one on top of the other, like a deck of cards.

27. What is the relationship between clipping and repainting?

When a window is repainted by the AWT painting thread, it sets the clipping regions to the area of the window that requires repainting.


28. What is the relationship between an event-listener interface and an event-adapter class?

An event-listener interface defines the methods that must be implemented by an event handler for a particular kind of event. An event adapter provides a default implementation of an event-listener interface.


29. What event results from the clicking of a button?

The ActionEvent event is generated as the result of the clicking of a button.

30. How can a GUI component handle its own events?

A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener.

31. How are the elements of a GridBagLayout organized?

The elements of a GridBagLayout are organized according to a grid. However, the elements are of different sizes and may occupy more than one row or column of the grid. In addition, the rows and columns may have different sizes.

32. What advantage do Java's layout managers provide over traditional windowing systems?

Java uses layout managers to lay out components in a consistent manner across all windowing platforms. Since Java's layout managers aren't tied to absolute sizing and positioning, they are able to accomodate platform-specific differences among windowing systems. Also, when you resize a window, the usage of layout manager helps to adjust the components to fit the window size in a graceful manner while the use of no layout manager may result in the screen components displayed in a chaotic fashion.


33. What is the difference between the paint() and repaint() methods?

The paint() method supports painting via a Graphics object. The repaint() method is used to cause paint() to be invoked by the AWT painting thread.


34. Which class is the immediate superclass of the Container class?

Component


35. How can the Checkbox class be used to create a radio button?

By associating Checkbox objects with a CheckboxGroup.


36. What is the purpose of the enableEvents() method?

The enableEvents() method is used to enable an event for a particular object. Normally, an event is enabled when a listener is added to an object for a particular event. The enableEvents() method is used by objects that handle events by overriding their event-dispatch methods.


37. What interface is extended by AWT event listeners?

All AWT event listeners extend the java.util.EventListener interface.

38. What is a layout manager?

A layout manager is an object that is used to organize components in a container.


39. What is the difference between a Scrollbar and a ScrollPane?

A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.


40. What methods are used to get and set the text label displayed by a Button object?

getLabel() and setLabel()

41. Which Component subclass is used for drawing and painting?

Canvas

42. What are the problems faced by Java programmers who don't use layout managers?

Without layout managers, Java programmers are faced with determining how their GUI will be displayed across multiple windowing systems and finding a common sizing and positioning that will work within the constraints imposed by each windowing system. When you resize a window, the usage of layout manager helps to adjust the components to fit the window size in a graceful manner while the use of no layout manager may result in the screen components displayed in a chaotic fashion.


If you have any questions that you want answer for - please leave a comment on this page and I will answer them.

If you have any more questions on AWT that you have faced during your interviews and wish to add them to this collection - pls drop a note to anandvijayakumar007@gmail.com and I shall be glad to add them to this list.

More Java & J2EE Interview Questions with Answers - Click Here

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers