Abstract Windows Toolkit (AWT) Interview Questions in Java

List of topic-wise frequently asked java interview questions with the best possible answers for job interviews.

Abstract Windows Toolkit (AWT) Interview Questions in Java

Question 1.
What is meant by controls and what are the different types of controls in AWT?
Answer:
Controls are components that allow a user to interact with your application and the AWT supports the following types of controls:
Labels, Push Buttons, Check Boxes, Choice Lists, Lists, Scrollbars, Text Components. These controls are subclasses of Component.

Question 2.
What is the difference between choice and list?
Answer:
A Choice is displayed in a compact form that requires you to pull it down to see the list of available choices and only one item may be selected from a choice. A-List may be displayed in such a way that several List items are visible and it supports the selection of one or more list items.

Question 3.
What is the difference between scrollbar and scrollpane?
Answer:
A Scrollbar is a Component, but not a Container whereas Scrollpane is a Container and handles its own events and performs its own scrolling.

Question 4.
What is a layout manager and what are the different types of layout managers available in java.awt?
Answer:
A layout manager is an object that is used to organize components in a container. The different layouts are available are FlowLayout, BorderLayout, CardLayout, GridLayout, and GridBagLayout.

Question 5.
How are the elements of different layouts organized?
Answer:
FlowLayout: The elements of a FlowLayout are organized in a top to bottom, left to right fashion.
BorderLayout: The elements of a BorderLayout are organized at the borders (North, South, East, and West)and the center of a container.
CardLayout: The elements of a CardLayout are stacked, on top of the other, like a deck of cards.
GridLayout: The elements of a GridLayout are of equal size and are laid out using the square of a grid.
GridBagLayout: 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.

Question 6.
Which containers use a Border layout as their default layout?
Answer:
Window, Frame, and Dialog classes use a BorderLayout as their layout.

Question 7.
Which containers use a Flow layout as, their default layout?
Answer:
Panel and Applet classes use the FlowLayout as their default layout.

Question 8.
Why the container does not support multiple layout managers?
Answer:
because when u r using a particular layout manager it useful frame for work you can use a grid bag layout for this purpose.

Question 9.
What is clipping?
Answer:
Clipping is the process of confining paint operations to a limited area or shape.

Question 10.
What is meant by controls and what are the different types of controls in AWT?
Answer:
Controls are components that allow a user to interact with your application and the AWT supports the following types of controls:
Labels, Push Buttons, Check Boxes, Choice Lists, Lists, Scrollbars, Text Components. These controls are subclasses of Component.

Question 11.
What is the difference between choice and list?
Answer:
A Choice is displayed in a compact form that requires you to pull it down to see the list of available choices and only one item may be selected from a choice. A-List may be displayed in such a way that several List items are visible and it supports the selection of one or more list items.

Question 12.
What is the difference between scrollbar and scrollpane?
Answer:
A Scrollbar is a Component, but not a Container whereas Scrollpane is a Container and handles its own events and performs its own scrolling.

Question 13.
What is a layout manager and what are the different types of layout managers available in java.awt?
Answer:
A layout manager is an object that is used to organize components in a container. The different layouts are available are FlowLayout, BorderLayout, CardLayout, GridLayout, and GridBagLayout.

Question 14.
What is a Layout Manager and what are the different Layout Managers available in java.awt and what is the default layout manager for the panel and the panel subclasses?
Answer:
A layout manager is an object that is used to organize components in a container. The different layouts are available in java.awt is:

• FlowLayout: The elements of a FlowLayout are organized in a top to bottom, left to right fashion.

• BorderLayout: The elements of a BorderLayout are organized at the borders (North, South, East, and West) and the center of a container.

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

• GridLayout: The elements of a GridLayout are of equal size and are laid out using the square of a grid.

• GridBagLayout: 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.
The default Layout Manager of Panel and Panel sub-classes is FlowLayout.

Question 15.
Which containers use a Border layout as their default layout?
Answer:
Window, Frame, and Dialog classes use a BorderLayout as their layout.

Question 16.
Which containers use a Flow layout as their default layout?
Answer:
Panel and Applet classes use the FlowLayout as their default layout.

Question 17.
Assume that g is an object belonging to the Graphics class. What image would be produced by the following commands? Draw a picture as part of your answer.
g;setColor(Color.gray);
g.fiIlRect(0,0,40,40);
g.fillRect(50,50,0,0);
g.setColor(Color.black);
g.fill0val(50,0,40,40);
g.fill0val(0,50,40,40);
Answer:
The method “g.fillRect(x,y,w,h)” draw a solid rectangle with its upper left corner at the point (x,y). The other two parameters, w, and h, give the width and height of the rectangle. In the second line “g.fillRect(50,50,0,0)” doesn’t produce any image at all, since the width and height are specified as zero. The method “g.fillOval(x,y,w,h)” draw a solid oval that just fits inside the rectangle with upper. left corner at (x,y) and with width w and height h. So, the picture produced contains one gray square and two black circles, arranged like this:
(Note: I actually meant the second fine to be “g.fillRect(50,50,40,40).” Without this typo, the picture would have another gray square.)

Question 18.
What is the use of the window class?
Answer:
The window class can be used to create a plain, bare-bones window that does not have a border or menu. The Window class can also be used to display introduction or welcome screens.

Question 19.
What are the benefits of Swing over AWT?
Answer:
Swing components are lightweight. We can have a pluggable look & feel feature that shows us how they appear on other platforms. We can add images to Swing components. We have toolbars and tooltips in Swing.

Question 20.
Where the CardLayout is used?
Answer:
Card Layout is used where we need to have a bunch of panels or frames one laying over another. It is replaced by TabbedPane in Swing.

Question 21.
What is the difference between Grid and GridbagLayout?
Answer:
In Grid layout, the size of each grid is constant whereas in GridbagLayout grid size can Vareid.

Question 22.
How will you add a panel to a Frame?
Answer:
Frame.add(panel,BorderLayout.NORTH) or Frame.add(panel,’’North”).

Question 23.
What is the use of the window class?
Answer:
The window class can be used to create a plain, bare-bones window that does not have a border or menu. The Window class can also be used to display introduction or welcome screens.

Question 24.
What is the difference between a Window and a Frame?
Answer:
The Frame class extends Window to define the main application window that can have a menu bar.

Question 25.
What is the difference between the Font and FontMetrics classes?
Answer:
The FontMetrics class is used to define implementation-specific properties, such as ascent and descent, of a Font object.

Question 26.
For which statements does it make sense to use a label?
Answer:
The only statements for which it makes sense to use a label are those statements that can enclose a break or continue statement.

Question 27.
Which TextComponent method is used to set a TextComponent to the read-only state?
Answer:
setEditable( )

Question 28.
How are the elements of a CardLayout organized?
Answer:
The elements of a CardLayout are stacked, one on top of the other, like a deck of cards.

Question 29.
What advantage do Java’s layout managers provide over traditional windowing systems?
Answer:
Java uses layout managers to layout 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 accommodate platform-specific differences among windowing systems.

Question 30.
What is the difference between the paint( ) and repaint( ) methods?
Answer:
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.

Question 31.
Which class is the immediate superclass of the Container class?
Answer:
Component

Question 32.
How can the Checkbox class be used to create a radio button?
Answer:
By associating Checkbox objects with a CheckboxGroup.

Question 33.
Name four Container classes.
Answer:
Window, Frame, Dialog, FileDialog, Panel, Applet, or ScrollPane

Question 34.
What Checkbox method allows you to tell if a Checkbox is checked?
Answer:
getState( )

Question 35.
How are the elements of a GridLayout organized?
Answer:
The elements of a GridBad layout are of equal size and are laid out using the squares of a grid.

Question 36.
Which Component subclass is used for drawing and painting?
Answer:
Canvas

Question 37.
What are the problems faced by Java programmers who don’t use layout managers?
Answer:
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.

Question 38.
What is Double Buffering?
Answer:
Double Buffering is a technique used to achieve smooth animations in Canvases or Panels. It is memory and CPU-consuming but is unavoidable as soon as you want to animate something. This technique is not applicable for native components like Buttons, Lists, etc… Since the redisplay is handled by the underlying toolkits (Motif or MsWin).

To handle double buffering, one must first draw the widget’s contents in an off¬screen image and then transfer the contents of the image to the visible window.

class DoubleBufferedcanvas extends Canvas {

    public void update(Graphics g) {
    Graphics offgc;
    Image offscreen = null;
    Dimension d = size( );

    // create the offscreen buffer and associated Graphics
    offscreen = createimage(d.width, d.height);
    offgc = off screen. getGraphics( );
    //clear the exposed area
    offgc.setColor(getBackground( ));
    offgc.fillRect(0, 0, d.width, d.height);
    offgc.setColor(getForeground( ));
    // do normal redraw
    paint(offgc);
    // transfer offscreen to window
    g.drawimage(offscreen, 0, 0, this);
   }
}

This method has one major drawback: it copies the whole buffer each time an update is required. This may consume a lot of time and CPU for a large window Hopefully, graphics contains a definition of the area that has to be redrawn: it is called clip region. The region, in Java Graphics, has two meanings:

  1. It tells which part needs to be drawn
  2. It clips drawing orders inside this area that means if you draw outside the clip region, nothing will appear.

Using clip regions, we can define an optimized version of the DoubleBufferedCanvas:

class OptimizedDoubleBufferedCanvas extends canvas {

   public void update(Graphics g) {
   Graphics offgc;
   Image offscreen = null;
   Rectangle box = g.getclipRect( );

   // create the offscreen buffer and associated Graphics
   offscreen = createimagefbox.width, box.height);
   offgc = offscreen.getGraphics( ));
   // clear the exposed area
   offgc.setcolor(getBackg round( ));
   offgc.fillRect(0, 0, box.width, box.height);
   offgc.setcolor(getForeground( ));
   // do normal redraw
   offgc.translateC-box.x, -box.y);
   paint(offgc);
   // transfer offscreen to window
   g.drawlmage(offscreen, box.x, box.y, this);
   }
}

Question 39.
What is the preferred size of a component?
Answer:
The preferred size of a component is the minimum component size that will allow the component to display normally.

Question 40.
What method is used to specify a container’s layout?
Answer:
The setLayout( ) method is used to specify a container’s layout.

Question 41.
Which containers use a FlowLayout as their default layout?
Answer:
The Panel and Applet classes use the FlowLayout as their default layout.

Question 42.
Name Component subclasses that support painting.
Answer:
The Canvas, Frame, Panel, and Applet classes support painting.

Question 43.
Can an anonymous class be declared as implementing an interface and extending a class?
Answer:
An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

Question 44.
Which Container method is used to cause a container to be laid out and redisplayed?
Answer:
validate( )

Question 45.
What is the difference between a Window and a Frame?
Answer:
The Frame class extends Window to define the main application window that can have a menu bar.

Question 46.
What do heavy-weight components mean?
Answer:
Heavyweight components like Abstract Window Toolkit (AWT), depending on the local windowing toolkit. For example, java.awt.The button is a heavy-weight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button. In this relationship, the Motif button is called the peer to the java.awt.Button. If you create two Buttons, two peers and hence two Motif Buttons are also created. The Java platform communicates with the Motif Buttons using the Java Native Interface. For each and every component added to the application, there is an additional overhead tied to the local windowing system, which is why these components are called heavyweight.

Question 47.
How are the elements of a GridBagLayout organized?
Answer:
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.

Question 48.
What advantage do Java’s layout managers provide over traditional windowing systems?
Answer:
Java uses layout managers to layout 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 accommodate platform-specific differences among windowing systems

Question 49.
What is the difference between the paint( ) and repaint( ) methods?
Answer:
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.

Question 50.
Name Container classes.
Answer:
Window, Frame, Dialog, FileDialog, Panel, Applet, or ScrollPane

Question 51.
What is the difference between a Scrollbar and a ScrollPane?
Answer:
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.

Question 52.
What is a Container in a GUI?
Answer:
A Container contains and arranges other components (including other containers) through the use of layout managers, which use specific layout policies to determine where components should go as a function of the size of the container.

Question 53.
How to add a menu shortcut to menu items?
Answer:
If you have a button instance called the about the button, you may add a menu shortcut by calling the about button.setMnemonic(A.’), so the user may be able to use Alt+A to click the button.

Question 54.
What is the difference between Swing and AWT components?
Answer:
AWT components are heavy-weight, whereas Swing components are lightweight. Heavyweight components depend on the local windowing toolkit. For example, java.awt.The button is a heavy-weight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button.

Question 55.
What is meant by controls and what are the different types of controls?
Answer:
Controls are components that allow a user to interact with your application. The AWT supports the following types of controls:
Labels, Pushbuttons, Checkboxes, Choice lists, Lists, Scroll bars, Text components These controls are subclasses of Component.

Question 56.
How can we get all public methods of an object dynamically?
Answer:
By using
get methods( ): It returns an array of method objects corresponding to the methods of this class.
getFields( ): It returns an array of Filed objects corresponding to the Fields(variables) of this class.
getConstructors( ): It returns an array of constructor objects corresponding to public constructors of this class.

Question 57.
What is the SubClass of Text component Class?
Answer:
TextField and TextArea

Question 58.
Which method of the Component class is used to set the position and the size of a component?
Answer:
setBounds( )

Question 59.
Which TextComponent method is used to set A TextComponent to the read¬only state?
Answer:
setEditable( )

Question 60.
How can the Checkbox class be used to create a radio button?
Answer:
By associating Checkbox objects with a CheckboxGroup.

Question 61.
What Checkbox method allows you to tell if a Checkbox is checked?
Answer:
getState( )

Question 62.
Which Component method is used to access a component’s immediate Container?
Answer:
getParent( )

Question 63.
What methods are used to get and set the text label displayed by a Button object?
Answer:
getLabel( ) and setLabel( )

Question 64.
What is the difference between a Choice and a List?
Answer:
A Choice is displayed in a compact form that requires you to pull it down to see the list of available choices. Only one item may be selected from a Choice.
A-List may be displayed in such a way that several List items are visible. A-List supports the selection of one or more List items.

Question 65.
Which Container method is used to cause a container to be laid out and redisplayed?
Answer:
validate( )

Question 66.
What is the difference between a Scrollbar and a Scrollpane?
Answer:
A Scrollbar is a Component, but not a Container. A Scrollpane is a Container and handles its own events and performs its own scrolling.

Question 67.
Which Component subclass is used for drawing and painting?
Answer:
Canvas.

Question 68.
Which method is used to set the text of a Label object?
Answer:
setText( )

Question 69.
Which method will cause a Frame to be displayed?
Answer:

a) . show( )
b) . setVisible( )

Question 70.
All the component classes and container classes are derived from
class.
Answer:
Object.

Question 71.
Which method of the container class can be used to add components to a Panel?
Answer:
add ( ) method.

Question 72.
What are the subclasses of the Container class?
Answer:
The Container class has three major subclasses. They are:
Window, Panel, ScrollPane

Question 73.
Can the Choice component allow multiple selections?
Answer:
No

Question 74.
Which components are used to get text input from the user?
Answer:
TextField and TextArea.

Question 75.
Which object is needed to group Checkboxes to make them exclusive?
Answer:
CheckboxGroup.

Question 76.
What are the types of Checkboxes and what is the difference between them?
Answer:
Java supports two types of Checkboxes. They are:
Exclusive and Non-exclusive.
In the case of exclusive Checkboxes, only one among a group of items can be selected at a time. If an item from the group is selected, the checkbox currently checked is deselected and the new selection is highlighted. The exclusive Checkboxes are also called Radio buttons.
The non-exclusive checkboxes are not grouped together and each one can be selected independent of the other.

Question 77.
What is a Layout Manager and what are the different Layout Managers available in java.awt and what is the default layout manager for the panel and the panel subclasses?
Answer:
A layout manager is an object that is used to organize components in a container.

Question 78.
What are the different layouts available in java.awt :
Answer:
FlowLayout, BorderLayout, CardLayout, GridLayout, GridBag Layout. The default Layout Manager of Panel and Panel sub classes is FlowLayout”.

Question 79.
Can I exert control over the size and placement of components in my interface?
Answer:
Yes.

Question 80.
Can I add the same component to more than one container?
Answer:
No. Adding a component to a container automatically removes it from any previous parent (container).

Question 81.
How do I specify where a window is to be placed?
Answer:
Use setBounds, setSize, or setLocation methods to implement this.
setBounds(int x, int y, int width, int height)
setBounds(Rectangle r)
setSize(int width, int height)
setSize(Dimension d)
setLocation(int x, int y)
setLocation(Point p)

Question 82.
How can we create a borderless window?
Answer:
Create an instance of the Window class, give it a size, and show it on the screen.
Ex. Frame obj =…………………..
Window WA = new Window(obj);
WA.setLayout(new FlowLayoutO);
WA.add(new Button(“OK”));
WA.getBounds(50,50,200,200);
WA.show( );

Question 83.
Can I create a non-resizable window? If so, how?
Answer:
Yes. By using setResizable( ) method in class Frame.

Question 84.
What is the default Layout Manager for the Window and Window subclasses (Frame, Dialog)?
Answer:
BorderLayout( )

Question 85.
Which containers use a BorderLayout as their default layout?
Answer:
The Window, Frame, and Dialog classes use a BorderLayout as their default layout.

Question 86.
Which containers use a FlowLayout as their default layout?
Answer:
The Panel and the Applet classes use the FlowLayout as their default layout.

Question 87.
What is the preferred size of a component?
Answer:
The preferred size of a component size will allow the component to display normally.

Question 88.
Which method is a method to set the layout of a container?
Answer:
setLayout( )

Question 89.
Which method returns the preferred size of a component?
Answer:
getPreferredSize( )

Question 90.
Which layout should you use to organize the components of a container in a tabular form?
Answer:
GridLayout

Question 91.
What are the default layouts for an applet, a frame, and a panel?
Answer:
For an applet and a panel, Flow layout is the default layout, whereas Border layout is the default layout for a frame.

Question 92.
How can you change the current layout manager for a container?
Answer:
By using setLayout( ) method.

Question 93.
Which is the default layout for Applet?
Answer:
The default layout manager for an Applet is FlowLayout, and The FlowLayout manager attempts to honor the preferred size of any components.

Question 94.
Which method does display the messages whenever there is an item selection or deselection of the CheckboxMenuItem menu?
Answer:
itemStateChanged method.

Question 95.
Which is a dual state menu item?
Answer:
CheckboxMenuItem.

Question 96.
Which container may contain a menu bar?
Answer:
Frame

Question 97.
What is the difference between a Menultem and a CheckboxMenuItem?
Answer:
The CheckboxMenuItem class extends the Menultem class to support a menu item that may be checked or unchecked.

Question 98.
What is the correct ordering for the import, class, and package declarations when found in a single file?
Answer:
package, import, class

Question 99.
What is the parameter specification for the public static void main method?
Answer:

  1. String args [ ]
  2. String [ ] args

Question 100.
What is the sweep and paint algorithm?
Answer:
The painting algorithm takes as input a source image and a list of brush sizes, sweep algo is that it computes the arrangement of n lines in the plane … a correct algorithm,

Question 101.
What are the component and container classes?
Answer:
A component is a graphical object. A few examples of components are Button, Canvas, Checkbox, Choice, etc.

Question 102.
Explain the purpose of invalidating and validate methods.
Answer:
The invalidate method is called a side effect of adding or deleting some component. Calling invalidates ( ) is however the first step of processing a COMPONENTJtESIZED event.

Question 103.
What are AWT peers? When are peers created and destroyed?
Answer:
A Component is associated with a standard AWT button object, a peer object (hidden button object internal to the native GUI), and an interfacing button object constructed per the native GUI.

Question 104.
Show with an example the usage of the FileDialog object.
Answer:
class FileDialog extends from Dialog class. It is used to display a dialog window from which the user can select a file.

Question 105.
Explain the purposes of the component’s request focus method.
Answer:
The purpose of the request focus( ) is to get the focus on the particular component and also on the window that contains the component.

Question 106.
Which method of the Component class is used to set the position and size of a component?
Answer:
The methods are: set maximum size(Dimension maximum size) Sets the maximum size of this component to a constant value.

Question 107.
What interface is extended by AWT event listeners?
Answer:
The java. util.EventListener interface is extended by all the AWT event listeners.

Question 108.
What is the paint method? What should we put in the paint method? When is it invoked?
Answer:
The AWT uses a Callback mechanism for painting which is the same for heavyweight and lightweight components.

Question 109.
What is the purpose of the repaint method? When should we use the repaint method?
Answer:
repaint( ) requests can erase and redraw (update) after a small time delay. When you invoke repaint( )

Question 110.
Explain the use of the update method. When is it invoked?
Answer:
An update method is called on calling the repaint method. The default implementation of the update( ) method clears the screen and calls the paint( ) method.

Question 111.
How does the XOR drawing mode work? What are the properties of XOR j drawing?
Answer:
setXORMode(Color cl) sets the paint mode of this graphics context to alternate between a graphics context’s current color and the new specified color.

Question 112.
Explain how to load an image from the net into my applet.
Answer:
private Imagelcon image;
inside the init method
image = new ImageIcon(new URL(http://www.xyz.gif));

Question 113.
Explain how to load an image from a file in a java application.
Answer:
Imagelcon icon = createImageIcon(“images/xyz.gif’, “text string”);

Question 114.
What is the difference between a Window and a Frame?
Answer:
A frame is a resizable, movable window with a title bar and a close button. Usually, it contains Panels. It’s derived from a window and has a BorderLayout by default.

Question 115.
Explain how to draw text over a background image.
Answer:
Bufferedlmage img = ImageIO.read(new File(“xxx”)); // xxx is the path TextOnBackground test = new TextOnBackground(img);

Question 116.
Define canvas?
Answer:
It is a simple drawing surface that is used for painting images or to perform other graphical operations.