JAVA Interview Questions

This blog is meant for the professional who are in java platform or interested to work on java platform. This is my small attempt to gather some useful questions generally asked in the java interviews. If anybody finds some errors in the answer please do mail me and give ur useful comments which helps me to improve this blog and make it error free.

My Photo
Name:
Location: Bhubaneswar, Orissa, India

Nothing on this planet is more powerful than a person who has made a decision to achieve something.

October 15, 2006

JFC SWING/AWT

Q. What is JFC?
Answer: Java Foundation Classes is a family of classes to support lightweight, platform independent MVC based GUI components.
Q. What is the base class for all swing components?
Answer: java.awt.Component
Q. What is the difference between JFC Swing and AWT?
Answer : AWT provides heavyweight GUI components which are have their own viewports depending on its underlying OS. Swing provides lightweight components which are not written to screen but redirect output to underlying components upon which Swing is built.Another difference is that Swing is pure Java, and therefore it is platform independent. Swing appears identically on all platforms, while AWT appears differently on different platforms.
Q. Why do you have Canvas?
Answer: java.lang.Object +--java.awt.Component +--java.awt.Canvas
A Canvas represents a blank rectangular area on the screen which is used for drawing by an application or trapping input events for an application.
An application must subclass the Canvas class in order to get useful functionality such as creating a custom component. The paint method must be overridden in order to perform custom graphics on the canvas.
Q. What are the benefits of Swing over AWT?
Answer : AWT components are heavyweight while Swing components are pure Java based and lightweight. Swing components have platform independent look and feel while AWT components have their look and feel dependent upon underlying OS.
Q. Where the CardLayout is used?
Answer: A CardLayout object is a layout manager for a container. It treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a CardLayout object is the visible component when the container is first displayed.
The ordering of cards is determined by the container's own internal ordering of its component objects. CardLayout defines a set of methods that allow an application to flip through these cards sequentially, or to show a specified card. The addLayoutComponent(java.awt.Component, java.lang.Object) method can be used to associate a string identifier with a given card for fast random access
Q. What is the layout for the ToolBar?
Answer: BorderLayout
Q. What is the difference between GridLayout and GridBagLayout?
Answer: GridLayout class lays all components in a rectangular grid like structure of container. The container is divided into an equal sized rectangles and each component is placed inside a rectangle.The GridBagLayout class is a flexible layout manager that aligns components vertically and horizontally, without requiring that the components be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display area.
Each component managed by a GridBagLayout is associated with an instance of GridBagConstraints. The constraints object specifies where a component's display area should be located on the grid and how the component should be positioned within its display area. In addition to its constraints object, the GridBagLayout also considers each component's minimum and preferred sizes in order to determine a component's size.
Q. How will you add a panel to a Frame?
Answer: import javax.swing.*;
public class XYZ extends JFrame{
public XYZ(){
JPanel panel =new JPanel();this.getContentPane().add(panel);
}
public static void main (String args[]){
XYZ obj=new XYZ();
obj.setVisible(true);}
}
Q. What is corresponding Layout for card in swing?
Answer : CardLayout
Q. What is the heavyweight component?
Answer: A heavyweight component is one that is associated with its own native screen resource (commonly known as a peer).
Q. What is the lightweight component?
Answer: A lightweight component is one that "borrows" the screen resource of an ancestor (which means it has no native resource of its own -- so it's "lighter").
Q. What is the difference between Application and Applet?
Answer: An applet runs in client side web browser. A class extending java.awt.Applet class which has methods like init(), start(), stop(), destroy(),paint() overridden.An applet has restriction of accessing client side resources like network connections, it cannot open socket connections and cannot write to client side files i.e. hard disk.
An application runs standalone with a support of virtual machine. An application does not have nay restrictions as Applets have over network and file related activities.They are free to open sockets over a network read and write to a file.
Q. Difference between Canvas class and Graphics class?
Answer : The Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components that are realized on various devices, as well as onto off-screen images.
A Canvas represents a blank rectangular area on the screen which is used for drawing by an application or trapping input events for an application.
An application must subclass the Canvas class in order to get useful functionality such as creating a custom component. The paint method must be overridden in order to perform custom graphics on the canvas.
Q. How will you initialize an applet?
Answer: Whenever a an applet is loaded in an applet viewer or a Java supporting client browser then an instance of applet subclass is instantiated and then init() method is called for initialization of an applet.
Q. Lifecycle of the Applet. / What is the order of method invocation in an applet?
Answer: First of all an instance of applet subclass will be created and then applet will be initialized. Here is a sequence of method calls in an applet given as below:init():An applet can initialize itselfstart(): An applet can start running itself.stop(): An applet can stop running itself.loading code clean up
Q. What is bean and where can it be used?
Answer: A bean is a reusable, cross platform software component which can be developed once and can be reused in various software requirements in a project.
Q. What is the difference between Java class and bean?
Answer : What differentiates Beans from typical Java classes is introspection. Tools that recognize predefined patterns in method signatures and class definitions can "look inside" a Bean to determine its properties and behavior. A Bean's state can be manipulated at the time it is being assembled as a part within a larger application. The application assembly is referred to as design time in contrast to run time. In order for this scheme to work, method signatures within Beans must follow a certain pattern in order for introspection tools to recognize how Beans can be manipulated, both at design time, and run time.
Q. What should you do to get your browser compatible with the swing component?
Answer: Download Java Plugin for web browser. In Java SDK 1.3.1 onwards a plugin is automatically installed with web browser for supporting Swing components.
Q. What are the methods in Applet?
Answer: init(),start(),stop(),paint(),destroy()
Q. When is init(),start() called?
Answer: As soon as an applet subclass instance is created init() method is called.
Q. What is difference between trusted and untrusted applet?
Answer : A trusted applet is one which signed by a trusted authority. The trusted applet is installed on the local hard disk, in a directory on the CLASSPATH used by the program that you are using to run the applet. Usually, this is a Java-enabled browser, but it could be the appletviewer, or other Java programs that know how to load applets.
The applet is signed by an identity marked as trusted in your identity database.By default all applets downloaded in client browser are untrusted. They cannot read or write files to clients’ local file system at all. They cannot start a program at client’s machine. They cannot do network operations i.e. cannot do Socket programming.They cannot load libraries and use native codes.

0 Comments:

Post a Comment

<< Home