HOME      BLOG       FORUMS      CONTACT       ABOUT

Swing/AWT interview questions – 1

May 22nd, 2011 Albin Joseph Posted in Swing interview question | 1 Comment »

1 Q What is JFC?
A JFC stands for Java Foundation Classes. The Java Foundation Classes (JFC) are a set of Java class libraries provided as part of Java 2 Platform, Standard Edition (J2SE) to support building graphics user interface (GUI) and graphics functionality for client applications that will run on popular platforms such as Microsoft Windows, Linux, and Mac OSX.

2 Q What is AWT?
A AWT stands for Abstract Window Toolkit. AWT enables programmers to develop Java applications with GUI components, such as windows, and buttons. The Java Virtual Machine (JVM) is responsible for translating the AWT calls into the appropriate calls to the host operating system.

3 Q What are the differences between Swing and AWT?
A AWT is heavy-weight components, but Swing is light-weight components. AWT is OS dependent because it uses native components, But Swing components are OS independent. We can change the look and feel in Swing which is not possible in AWT. Swing takes less memory compared to AWT. For drawing AWT uses screen rendering where Swing uses double buffering.

4 Q What are heavyweight components ?
A A heavyweight component is one that is associated with its own native screen resource (commonly known as a peer).

5 Q What is lightweight component?
A 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”).

6 Q What is double buffering ?
A Double buffering is the process of use of two buffers rather than one to temporarily hold data being moved to and from an I/O device. Double buffering increases data transfer speed because one buffer can be filled while the other is being emptied.

7 Q What is an event?
A Changing the state of an object is called an event.

8 Q What is an event handler ?
A An event handler is a part of a computer program created to tell the program how to act in response to a specific event.

9 Q What is a layout manager?
A A layout manager is an object that is used to organize components in a container.

10 Q What is clipping?
A Clipping is the process of confining paint operations to a limited area or shape.

11 Q Which containers use a border Layout as their default layout?
A The window, Frame and Dialog classes use a border layout as their default layout.

12 Q What is the preferred size of a component?
A The preferred size of a component is the minimum component size that will allow the component to display normally.

13 Q What method is used to specify a container’s layout?
A The setLayout() method is used to specify a container’s layout.

14 Q Which containers use a FlowLayout as their default layout?
A The Panel and Applet classes use the FlowLayout as their default layout.

15 Q Which method of the Component class is used to set the position and size of a component?
A setBounds

Tags: , , ,

AddThis Social Bookmark Button

Find out session created time in JSP/Servlet

May 14th, 2010 Albin Joseph Posted in javax.servlet.http | No Comments »

Find out session created time in JSP/Servlet

The getCreationTime of HttpSession interface allows us to find out the time in which the session was created in JSP and Servlet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		long creationTime = session.getCreationTime();

		System.out.println("Session created time = "+creationTime);
	}

Tags: ,

AddThis Social Bookmark Button

Create a StringWriter object

October 23rd, 2008 Albin Joseph Posted in java.io | No Comments »

The default constructor allows us to create a StringWriter with default string buffer size.

import java.io.StringWriter;

/**
 * StringWriter class examples.
 */
public class StringWriterExample {
	public static void main(String[] args){
		// Create a stringWriter object.
		StringWriter writer = new StringWriter();
	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Compare two files using java

June 24th, 2008 Albin Joseph Posted in java.io | 2 Comments »

Compare two files using java

The compareTo method of File class helps us to create a temporary file. This method returns an integer. If the value of the integer is greater than zero, the file will be greater than the argument, if the file is less than zero, the file will be less than the argument and a value of zero means both the files are equal.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args) {
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Create another file for comparison.
		File cFile = new File("d:\\temp\\Copy of test.txtt");

		// Compare two files.
		int value = file.compareTo(cFile);

		System.out.println("File comparison result :  "+value);
	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Create a temp file using java

June 24th, 2008 Albin Joseph Posted in java.io | No Comments »

Create a temp file using java

The createTempFile method of File class helps us to create a temporary file.

import java.io.File;
import java.io.IOException;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args) throws IOException{
		// Create file object representing the source file/directory
		File file = new File("d:\\temp");

		// Create the temporary file.
		File tempFile = File.createTempFile("test", ".tmp",file);
	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Finds out total used space in a partition using java

June 24th, 2008 Albin Joseph Posted in java.io | No Comments »

Finds out total used space in a partition using java

In Java we can find the total used space of any drive or partition using getTotalSpace and getFreeSpace methods.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Get the total space available in current drive
		long totalSpace = file.getTotalSpace();

		// Get the total free space available in current partition
		long freeSpace = file.getFreeSpace();

		// Finds the total used space
		long value = totalSpace - freeSpace;

		// Convert it to GigaBytes
		double valueGB = (double) value / 1024 / 1024 / 1024;

		System.out.println("Total used space in the current partition is "+valueGB);
	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Finds out total usable space available in a partition using java

June 24th, 2008 Albin Joseph Posted in java.io | No Comments »

Finds out total usable space available in a partition using java

The getUsableSpace method of File class can be used for getting the total usable space available in a partition or drive using java.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Get the total usable space available in current drive
		long value = file.getUsableSpace();

		// Convert it to GigaBytes
		double valueGB = (double) value / 1024 / 1024 / 1024;

		System.out.println("Total free space available in the current partition is "+valueGB);
	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Finds out total free space available in a partition using java

June 24th, 2008 Albin Joseph Posted in java.io | No Comments »

Finds out total free space available in a partition using java

The getFreeSpace method of File class can be used for getting the total free space available in a partition or drive using java.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Get the total free space available in current drive
		long value = file.getFreeSpace();

		// Convert it to GigaBytes
		double valueGB = (double) value / 1024 / 1024 / 1024;

		System.out.println("Total free space available in the current partition is "+valueGB);
	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Finds out total space available in a partition using java

June 24th, 2008 Albin Joseph Posted in java.io | No Comments »

Finds out total space available in a partition using java

The getTotalSpace method of File class can be used for getting the total space available in a partition or drive using java.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Get the total space available in current drive
		long value = file.getTotalSpace();

		// Convert it to GigaBytes
		double valueGB = (double) value / 1024 / 1024 / 1024;

		System.out.println("Total space available in the current partition is "+valueGB);
	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Lists all the drives present in Windows using java

June 24th, 2008 Albin Joseph Posted in java.io | No Comments »

Lists all the drives present in Windows using java

The listRoots method of File class is used for finding out all the drives available in Windows.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Get all the drives
		File drives[] = File.listRoots();

		// Loop through the drive list and display the drives.
		for (int index = 0; index < drives.length; index++) {
			System.out.println(drives[index]);
		}
	}
}

Note: Since Linux does not have any drives concept, the output of the above program in Linux operating system will be "/"

Tags: , , , ,

AddThis Social Bookmark Button