Java AWT viva questions with answers

  1. What is AWT?
    Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. It contains numerous classes and methods that allow you to create windows and simple controls. It was Java’s first GUI framework, and it has been part of Java since version 1.0.

  2. Where are AWT classes contained? / Which package contains AWT classes? 
    The AWT classes are contained in the java.awt package. It is one of Java’s largest packages.
  3. What is a 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. Component class lies at the top of AWT hierarchy. Component class is an abstract class that encapsulates all of the attributes of a visual component.

  4. What is an abstract class ?
    A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract and non-abstract methods (method with body).
  5. Are menus subclass of a component?
    No. Except for menus, all user interface elements that are displayed on the screen and that interact with the user are subclasses of Component.

  6. What is a component?
    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.
  7. What is a container class? 
    The Container class is a subclass of Component.

  8. What is the super class of all containers?
  9. What is a window?
    The window is the container that has no borders and menu bars. You must use frame, dialog or another window for creating a window. Generally, you won’t create Window objects directly. Instead, you will use a subclass of Window called Frame.

  10. What is a panel? 
    The Panel class is a concrete subclass of Container. Like window panel also does not contain a title bar, menu bar, or border. 

    To have a greater flexibility on the arrangement of components, panels are extensively 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.

  11. What are subclasses of the component?
    Subclasses of Container include Frame, Panel, and Applet which is a subclass of Panel.
  12.  Among the standard classes in Java are Container and Component. In order to create a GUI, the programmer uses subclasses of these two classes. What are some examples of subclasses of Container and Component? What is the essential difference between Containers and Components?

    Java’s Component class represents visual elements of a Graphical User Interface. Its subclasses include Button, Checkbox, TextField, Choice, and Canvas. The Container class is another subclass of Component. A Container is a component that can contain other components (including other containers). This is the essential difference between containers and other types of component. Subclasses of Container include Frame, Panel, and Applet.

  13. How to add other components to a panel?
    Other components can be added to a Panel object by its  
    • add( ) method (inherited from Container).

  14. How to position components in a Panel? 
    setLocation( ), setBounds() methods defined by Components class can be used to position components. 

  15. How to resize components?
    setLocation( ) and setPreferredSize( ) methods. 

  16. What is a Frame? 
    Frame encapsulates what is commonly thought of as a “window.” It is a subclass of Window and has a title bar, menu bar, borders, and resizing corners. The Frame is the container that contains title bar and can have menu bars. It can have other components like button, textfield etc.

  17. Mention any 4 methods of component class? 
    public void add(Component c) -> inserts a component on this component.
    public void setSize(int width,int height) -> sets the size (width and height) of the component.
    public void setLayout(LayoutManager m) -> defines the layout manager for the component.
    public void setVisible(boolean status) -> changes the visibility of the component, by default false.

  18.  What are the two ways to create a frame in awt? 
    By extending Frame class (inheritance)
    By creating the object of Frame class (association) 
  19. What are the constructors of Frame? 
    Frame( ) throws HeadlessException  -> creates a standard window that does not contain a title.
    Frame(String title) throws HeadlessException -> creates a window with the title specified by title.
  20. Constructors of setSize() ?
    • void setSize(int newWidth, int newHeight)
    • void setSize(Dimension newSize)

      The new size of the window is specified by newWidth and newHeight, or by the width and height fields of the Dimension object passed in newSize. The dimensions are specified in terms of pixels.

  21. How to obtain a current size of a window? 
    The getSize( ) method is used to obtain the current size of a window.