AWT Interview Questions

  1. What is AWT?
    AWT stands for Abstract Window Toolkit and infact it is a package – java.awt. The classes of this package give the power of creating a GUI environment to the programmer in Java.
  2. What is component?
    By definition, the subclass of a java.awt.Component class is known as a component. A component can be a Button or TextField etc. Using the component, a programmer can interact with the running Java program (instead of using the traditional keyboard input) and can pass two numbers to get the product or pass user name and password for validation.
  3. How to interact with the Java system at runtime?
    Typically, it is by clicking with a mouse over the component or through keyboard.
  4. What is the super class of all components of Java?
    It is java.awt.Component.
  5. What is a container?
    By definition, the subclass of a java.awt.Container class is known as a container. A container can be a Frame or Applet or Dialog box etc. A container holds the components. If a button is to be displayed to the user, it must be added to a container.
  6. What is the super class of all containers?
    It is java.awt.Container.
  7. What is a layout manager?
    A layout manager dictates the style of arranging the components in a container.
  8. How many layout managers are available in Java?
    There are 5 layout managers defined in java.awt package – FlowLayout, BorderLayout, GridLayout, CardLayout and GridBagLayout.
  9. What is the style of arranging components in a container by FlowLayout manager?
    FlowLayout arranges the components (like buttons) on the north (top) side of the container (like frame) centered. The alignment of center can be changed. The default gap between the components is 5 pixels which can be changed programatically.
  10. How BorderLayout places the components?
    BorderLayout places the components along the borders and center of the container. That is, only 5 components (4 along the borders and 1 in center) can be placed. The default gap between the components is 0 pixels which can be changed programatically.
  11. What is the style of GridLayout?
    The whole container like frame is divided into rows and columns forming a grid of cells. Each cell can accomodate one component. That is, in a grid of 3 rows and 4 columns, we can place 12 components. The default gap of 0 pixels can be changed.
  12. When CardLayout can be used?
    It is a specific purpose layout manager. To arrange components like cards, this layout manager can be used.
  13. When GridBagLayout can be used?
    When the components are dissimilar size and each row contain different number of components, the GridBagLayout is preferred. The best example is the layout of the buttons of a calculator.
  14. What is a panel?
    To have a greater flexibility on the arrangement of components, panels are exetensively used with layout managers. Components are added to panel and panel in turn can be added to a container. That is, a panel can work like a container and a component. As container, components can be added to it and as a component, panel can be added to a frame or applet.
  15. What are the default layout managers for containers?
    Every container comes with a default layout manager which can be changed as per the need of the programmer. For Panel and Applet, the default layout manager is FlowLayout. For Frame, Dialog and Window, it is BorderLayout.
  16. Which layout manager gives the minimum size to a component?
    The size of the component is dictated by the layout manager and not by the container or component itself. The minimum size, known as "preferred size" is given by the FlowLayout manager.
  17. What is the method used to place some text in the text field?
    setText(String str) method of TextField class.
  18. What is the method used to get the data entered by the user in the text field?
    getText() method of TextField class.
  19. What for text field is used?
    To get the user input or display some message to th user.
  20. What is the difference between text field and text area?
    TextField and TextArea are used to get or display messages from user. The difference is text field displays the message in one line of text only but of any length where as text area is used to display multiple lines of text.
  21. What is the method used to change the characters entered by the user in the text field (used for password)?
    setEchoChar(char ch) of TextField class.
  22. How to make the text field non-editable by the user (user cannot enter anything)?
    setEditable(boolean state) of TextField class. This type of text field is used only to display text to the user.
  23. What is the method used to change the background color of components like text field?
    setBackground(Color clr) of java.awt.Component class.
  24. What is the method used to change the foreground (text) color of components like text field?
    setForeground(Color clr) of java.awt.Component class.
  25. What is the method used to know the label of the button clicked by the user?
    getActionCommand() method of ActionEvent class.
  26. What is the super class of TextField and TextArea?
    java.awt.TextComponent (a subclass of java.awt.Component).
  27. What is the method used to change the text of a Label?
    setText(String str) method of Label class.
  28. What is the method used to retrieve the text of a Label?
    getText() method of Label class.
  29. How many ways you can align the label in a container?
    3 ways. The variables defined in Label class LEFT, RIGHT and CENTER are used to change the alignment. Default alignment is left.
  30. What is the method used to change the label of a button?
    setLabel(String str) method of Button class.
  31. What is the method used to retrieve the label of a button?
    getLabel() method of Button class.
  32. What is HeadlessExceptin?
    It is an unchecked exception. This runtime exception is thrown when the code that is dependent on the hardware like keyboard, display or mouse is called in an environment that does not support a keyboard, display or mouse.
  33. What is the listener used to handle the events of a text field?
    java.awt.event.ActionListener interface

Pass your comments on this tutorial "AWT Interview Questions" to improve the quality.

Leave a Comment

Your email address will not be published.