Apple Most Frequently Asked Latest Core Java Interview Questions Answers
What's The Difference Between Applets And Standalone Program?
Sun designed Java from the start to fit hand-in-glove on the Internet. Applets are special Java programs that execute from within a Web browser. In contrast, a Java application (an application program as opposed to an applet) does not run within a browser. As it turns out, Java applets are not much different from standalone Java application. You can start developing your Java program from either an applet or an application and cross over any time. For example, assume that you are writing a new Java-based application that is initially designed as a standalone (non-Internet) game for Mac computers. At the end of your program's development cycle, you decide that you want it to run on Web browsers. Your task to make it into a Web-based applet involves very trivial changes, and the addition of some simple HTML code. At the same time, you will find that the game will also run on computers other than Macs! The point you should remember is that Java applets run within browsers across the Web, whereas Java application programs do not.
Why Java Applets Are More Useful For Intranets As Compared To Internet?
An intranet is an in-house version of the Internet. An intranet uses the same technologies, software, and equipment that the Internet uses (primarily TCP/IP). Whereas the Internet has information servers miles away controlled by other organizations, your company controls the servers and client computers inside your office in an intranet. During the past year, intranets have experienced explosive popularity growth because they offer a very low cost way for companies to maintain and distribute internal information in an easy-to-use format.
Because intranets work like the Internet, Java also finds a home on company intranets. All the techniques that you will learn and, in fact the same applets that you will use for the Internet, may be applied in your intranet. You may find that Java applets will help you solve special software problems within the intranet. You can use applets to provide a friendly interface to company databases, documentation stores, equipment controls, and so on, while running on any computer from a Mac to a PC to a UNIX workstation.
How Can You Set The Applet Size?
Java applets run within their own window: Within your HTML file, you set the size of an applet window, using the <APPLET> tag's WIDTH and HEIGHT attributes. The size values you specify for each attribute are in pixels. For example, the following <APPLET> entry creates applet window that is 30 pixels tall by 100 pixels wide
<APPLET CODE=clock.class WIDTH=100 HEIGHT=30> </APPLET>
How Can You Set An Applet's Height And Width As A Percentage?
You use the <APPLET> tag WIDTH and HEIGHT attributes to specify the pixel size of an applet window. In addition to letting you specify the applet window size in terms of pixels, many browsers let you specify the applet window's size as a percentage of the browser's window's size. You do this by specifying a percentage value for the HEIGHT and WIDTH attributes within the <APPLET> tag. For example, the following <APPLET> entry creates an applet window based on 50% of the height and 100% of the width of the browser's window
<applet code=test.class width=100% height=50%> </applet>.
What Is Codebase?
The Java applets your browser executes reside in a file with the .class extension. When you create a Web page, you can store you Java .class files in a directory which is different from the directory that contains the page's HTML files. Within the HTML <APPLET> tag, you can use the CODEBASE attribute to specify the directory within which the applet's .class files reside. The CODEBASE location can be a directory on the same computer or a directory at another computer. The CODEBASE attribute specifies (to the browser) the base URL (relative or specifies) of the directory that contain the .class files. If an <APPLET> tag does not use the CODEBASE attribute, the browser uses the current directory (the one containing the HTML file) to locate the .class files. The following <APPLET> tag directs the browser to look for the applet files in the directory called /server_a/applets.
<APPLET CODE="MyFirst.class"CODEBASE^"/server_a/applets" WIDTH=300
HEIGHT=300>.</APPLET>
What Is Appletviewer?
Most Java development environments include a special program called an appletviewer. using the appletviewer, you can execute your applet just as if it were running within a Web page displayed a browser. In most cases, the appletviewer can run your applet faster than a browser, making the appletviewer convenient for testing your applet. As you develop your applet, you will want to run it each time you add features. By using the appletviewer, you can quickly try out your applet without starting your Java-enabled Web browser. You run the appletviewer that accompanies Sun's Java Developer's Kit from the command line, specifying the name of the HTML file that contains the <APPLET> entry for applet you want to view
C:> appletviewer SomeFileName.HTML <ENTER>
Explain, Java Is Compatible With All Servers But Not All Browsers?
When a Java applet runs over a network, two sides are working. One is the server, which is responsible for maintaining and handling browser requests for the various files it controls. On the server side, a Java applet is just a file like any other file an HTTP server already handles. You do not need any special server software for Java since the real work of executing the Java applet is performed by the browser, not the server.
On the other side is the client, or browser, which request, receives, and interprets files from the server. The browser is responsible for displaying the Web page, playing sounds, running animations and. in general, determining the type of data the server is sending and handling that data accordingly.
When a Web page contains a Java applet, the page's HTML file will contain an <APPLET> entry. If the browser is Java-enabled, the browser will request the applet file from the server. The server, in turn, will send the applet's bytecode to the browser, which will start its Java interpreter to execute the code.
What Is The Program Development Process?
Depending on what development package you use to create your Java programs, will go about compiling, organizing, and testing your programs in different ways. However, the general development process is mostly the same no matter which package or platform you use.
As discussed, the end result of the Java programs development process is a bytecode file which the server downloads to the browser for interpretation and execution. When you compile your Java source file, you are creating a bytecode file. Java source-code files, on the other hand, contain the class definitions and program statements you write using the Java language. In addition to the Java source-code files, you must create a small HTML file, which the browser uses to invoke your applet.
After the compiler creates the bytecode and you create an HTML file, you can test your applet using either your browser or an appletviewer. If your applet contains errors, many Java development environments provide a debugger program that you can use to track down the errors. When you are ready to release your applet to the public, you must place the applet and a corresponding HTML file on a Web server.
Therefore, the general development cycle of a Java applet includes the creation of source-code and HTML files, compiling the source into bytecode, testing the bytecode through an appletviewer, detecting and removing any errors from the applet using a debugger and, finally, releasing the applet for use on a Web server.
What Is The File Type?
When you create Java source-code files that contain classes and methods, you need to be aware of the Java file-naming convention. Each Java source-code file must end with the Java extension. In addition, the file's name must match the name of the public class the file defines. For example, if your source-code file creates a class named MorphaMatic, your source-code file must use the name MorphaMatic.java. As you can see, the letters of the source-code file name must match the class name exactly, including the use of upper and lowercase letters.
The HTML file that you use to run the applet can have any name. As a rule, however, you should use the standard extension for your system, typically .html or .htm. Also, to help organize your files, you may want to use a name similar to that of your applet for the HTML file name, such as MorphaMatic. html.
Finally, the bytecode file the Java compiler creates will have the .class extension. In this case, the bytecode file will have the name MorphaMatic.class. As discussed, the .class file is the file the Web server downloads to your browser which, in turn, interprets and executes the files's contents.
What Is Javac_g?
As you have learned, the javac compiler examines your source-code files and produces the bytecode .class files. As your Java applets become more complex, it may become difficult for you to locate errors (bugs) within your code. To help you locate such errors, most Java development environments provide a special debugger program. In the case of the Sun's Java Developer's Kit, the debugger program is named jdb (for Java debugger). To provide jdb with more information to work with, the Sun's JDK provides special version of the compiler named javac__g. Sun designed the javac _g compiler for use with debuggers. Essentially, javacjg is a non-optimized version of the javac compiler which place tables of information within the bytecode that the debugger can use to track down errors. To compile a Java applet using the javac__g compiler, you simply specify the applet source-file named within the javac^g command line, as shown here
C:JAVACODE> javac „ g SomeFile.Java <Enter>
How To Optimize The Javac Output?
When you compare the performance of a Java program against that of a C/C++ program, you will find that the current generation of Java programs can be as much as twenty times slower than their C/C++ counterparts. This performance loss is mostly due to the fact that the browser musf interpret the Java bytecode and convert it into the computer's native code (such as a Pentium or Motorola-specific code) before the code can run. In C/C++, the code is in the processor's native format to begin with, so this time-consuming translation step is not required. Remember, however, that Java's generic bytecode allows the same Java code to run on multiple platforms.
The Java designers are working on various solutions to speed up Java. In the meantime, you can use the -O compiler switch with javac, which may increase the applet's performance. The -0 switch directs javac to optimize its bytecode by "inlining" static, final and private methods. For now, don't worry what "inlining" such code means other than it may improve your applet performance. Unfortunately, when you use inlining, you may increase the size of your bytecode file, which will increase the applet's download time. The following Javac command illustrates how you use the -0 switch
C:JAVACODE> javac -O MyFirst.java <Enter>
What Is The Difference Between Java Applets And Applications?
With Java, you can create two types of programs: an applet or an application. As you have learned, a Java applet is a program that executes from within a Web browser. A Java application, on the other hand, is a program that is independent of the browser and can run as a standalone program.
Because an applet is run from within a Web browser, it has the advantage of having an existing vindow and the ability to respond to user interface events provided though the browser. In addition, because applets are designed for network use Java is much restrictive in the types of access that applets can have to your file system than it is with non-network applications.
As you will, when you write a Java application, you must specify a main method (much like the C/ C++ main), which the program executes when it begins. Within the main method, you specify the functionality that your application performs. With an applet, on the other hand, you need to write additional methods that respond to events which are an important part of the applet's life cycle. The methods include init, start, stop, destroy and paint. Each of these events has a corresponding method and, when the event occurs, Java will call the appropriate method to handle it.
When you write your first Java programs, you can write them as if .they were applets and use the appletviewer to execute them. As it turns out, you can later convert your applet to an application by replacing your init method with a main method.
Can You Explain The Cs Option Of Java Interpreter?
When you develop Java using Sun's JDK, you normally compile your source by using the javac compiler. If no compile errors exist, you then run your application using the java interpreter. As a shortcut, you can specify the -cs command-line switch when you invoke the java interpreter. The java command, in turn, will automatically compile out-of-date (modified) source-code files for you.
By using the -cs switch, you can make changes to your source and immediately execute the java interpreter without having to manually run the java compiler yourself. The java interpreter knows which files it needs to recompile by comparing each file's modification dates against the corresponding class modification date. Normally, programmers use the -cs option when they have made a minor change to the source files and know that the files contain no compilation errors. The following command illustrates the use of the -cs switch
C: JAVACODE> Java -cs MyProgram <Enter>
What Is The Statements?
A Java program consists of instructions that you want the computer to perform. Within a Java applet, you use statements to express these instructions in a format the Java compiler understands. If you are already familiar with C/C++, you will discover that Java statements are very similar. For example, the following statements produce a programs which prints the words "Hello, Java!" in applet window
import java.applet.*; import java.awt.Graphics;
public class hello_java extends Applet
{
public void paint(Graphics g)
{
g.drawstring("Hello, Java!", 20, 20);
}
}
What Is Style And Indentation?
As you write Java programs, you will discover that you have considerable flexibility in how you line your statements and indent lines. Some editor programs help you line up indented lines and indent new block. If you have already programmed in another language, you probably have your own style of indentation. Java does not impose any specific style, However, you may want to be consistent with your own style and always be conscious that a well-formatted program is easier to read and maintain. For example, the following two programs function equivalently. However, one is much easier to read than the other
import java.applet. * ;
public class am_i_readable extends
Applet{public void init()
{
System.out.println("Can you guess whatI do?");
}
}
import java.applet.*;
public class am__i_readable extends Applet
{
public void init()
{
System.out.println("Can you guess what I do?");
}
}
What Is The Program Compilation Process?
When you create programs, you will normally follow the same steps. To begin, you will use an editor to create your source file. Next, you will compile the program using a Java compiler. If the program contains syntax errors, you must edit the source file, correct the errors, and re-compile.
After the program successfully compiles, the compiler generates a new file, known as bytecode. By using a Java interpreter, or appletviewer, you can execute the bytecode to test if it runs successfully. If the program does not work as you expected, you must review the source code to locate the error. After you correct the error, you must compile the source code to create a new byte code file. You can then test the new program to ensure that it performs the desired task. This illustrates the program development process.
What's The Difference Between Applets And Standalone Program?
Sun designed Java from the start to fit hand-in-glove on the Internet. Applets are special Java programs that execute from within a Web browser. In contrast, a Java application (an application program as opposed to an applet) does not run within a browser. As it turns out, Java applets are not much different from standalone Java application. You can start developing your Java program from either an applet or an application and cross over any time. For example, assume that you are writing a new Java-based application that is initially designed as a standalone (non-Internet) game for Mac computers. At the end of your program's development cycle, you decide that you want it to run on Web browsers. Your task to make it into a Web-based applet involves very trivial changes, and the addition of some simple HTML code. At the same time, you will find that the game will also run on computers other than Macs! The point you should remember is that Java applets run within browsers across the Web, whereas Java application programs do not.
Why Java Applets Are More Useful For Intranets As Compared To Internet?
An intranet is an in-house version of the Internet. An intranet uses the same technologies, software, and equipment that the Internet uses (primarily TCP/IP). Whereas the Internet has information servers miles away controlled by other organizations, your company controls the servers and client computers inside your office in an intranet. During the past year, intranets have experienced explosive popularity growth because they offer a very low cost way for companies to maintain and distribute internal information in an easy-to-use format.
Because intranets work like the Internet, Java also finds a home on company intranets. All the techniques that you will learn and, in fact the same applets that you will use for the Internet, may be applied in your intranet. You may find that Java applets will help you solve special software problems within the intranet. You can use applets to provide a friendly interface to company databases, documentation stores, equipment controls, and so on, while running on any computer from a Mac to a PC to a UNIX workstation.
How Can You Set The Applet Size?
Java applets run within their own window: Within your HTML file, you set the size of an applet window, using the <APPLET> tag's WIDTH and HEIGHT attributes. The size values you specify for each attribute are in pixels. For example, the following <APPLET> entry creates applet window that is 30 pixels tall by 100 pixels wide
<APPLET CODE=clock.class WIDTH=100 HEIGHT=30> </APPLET>
How Can You Set An Applet's Height And Width As A Percentage?
You use the <APPLET> tag WIDTH and HEIGHT attributes to specify the pixel size of an applet window. In addition to letting you specify the applet window size in terms of pixels, many browsers let you specify the applet window's size as a percentage of the browser's window's size. You do this by specifying a percentage value for the HEIGHT and WIDTH attributes within the <APPLET> tag. For example, the following <APPLET> entry creates an applet window based on 50% of the height and 100% of the width of the browser's window
<applet code=test.class width=100% height=50%> </applet>.
Apple Most Frequently Asked Latest Core Java Interview Questions Answers |
What Is Codebase?
The Java applets your browser executes reside in a file with the .class extension. When you create a Web page, you can store you Java .class files in a directory which is different from the directory that contains the page's HTML files. Within the HTML <APPLET> tag, you can use the CODEBASE attribute to specify the directory within which the applet's .class files reside. The CODEBASE location can be a directory on the same computer or a directory at another computer. The CODEBASE attribute specifies (to the browser) the base URL (relative or specifies) of the directory that contain the .class files. If an <APPLET> tag does not use the CODEBASE attribute, the browser uses the current directory (the one containing the HTML file) to locate the .class files. The following <APPLET> tag directs the browser to look for the applet files in the directory called /server_a/applets.
<APPLET CODE="MyFirst.class"CODEBASE^"/server_a/applets" WIDTH=300
HEIGHT=300>.</APPLET>
What Is Appletviewer?
Most Java development environments include a special program called an appletviewer. using the appletviewer, you can execute your applet just as if it were running within a Web page displayed a browser. In most cases, the appletviewer can run your applet faster than a browser, making the appletviewer convenient for testing your applet. As you develop your applet, you will want to run it each time you add features. By using the appletviewer, you can quickly try out your applet without starting your Java-enabled Web browser. You run the appletviewer that accompanies Sun's Java Developer's Kit from the command line, specifying the name of the HTML file that contains the <APPLET> entry for applet you want to view
C:> appletviewer SomeFileName.HTML <ENTER>
Explain, Java Is Compatible With All Servers But Not All Browsers?
When a Java applet runs over a network, two sides are working. One is the server, which is responsible for maintaining and handling browser requests for the various files it controls. On the server side, a Java applet is just a file like any other file an HTTP server already handles. You do not need any special server software for Java since the real work of executing the Java applet is performed by the browser, not the server.
On the other side is the client, or browser, which request, receives, and interprets files from the server. The browser is responsible for displaying the Web page, playing sounds, running animations and. in general, determining the type of data the server is sending and handling that data accordingly.
When a Web page contains a Java applet, the page's HTML file will contain an <APPLET> entry. If the browser is Java-enabled, the browser will request the applet file from the server. The server, in turn, will send the applet's bytecode to the browser, which will start its Java interpreter to execute the code.
What Is The Program Development Process?
Depending on what development package you use to create your Java programs, will go about compiling, organizing, and testing your programs in different ways. However, the general development process is mostly the same no matter which package or platform you use.
As discussed, the end result of the Java programs development process is a bytecode file which the server downloads to the browser for interpretation and execution. When you compile your Java source file, you are creating a bytecode file. Java source-code files, on the other hand, contain the class definitions and program statements you write using the Java language. In addition to the Java source-code files, you must create a small HTML file, which the browser uses to invoke your applet.
After the compiler creates the bytecode and you create an HTML file, you can test your applet using either your browser or an appletviewer. If your applet contains errors, many Java development environments provide a debugger program that you can use to track down the errors. When you are ready to release your applet to the public, you must place the applet and a corresponding HTML file on a Web server.
Therefore, the general development cycle of a Java applet includes the creation of source-code and HTML files, compiling the source into bytecode, testing the bytecode through an appletviewer, detecting and removing any errors from the applet using a debugger and, finally, releasing the applet for use on a Web server.
What Is The File Type?
When you create Java source-code files that contain classes and methods, you need to be aware of the Java file-naming convention. Each Java source-code file must end with the Java extension. In addition, the file's name must match the name of the public class the file defines. For example, if your source-code file creates a class named MorphaMatic, your source-code file must use the name MorphaMatic.java. As you can see, the letters of the source-code file name must match the class name exactly, including the use of upper and lowercase letters.
The HTML file that you use to run the applet can have any name. As a rule, however, you should use the standard extension for your system, typically .html or .htm. Also, to help organize your files, you may want to use a name similar to that of your applet for the HTML file name, such as MorphaMatic. html.
Finally, the bytecode file the Java compiler creates will have the .class extension. In this case, the bytecode file will have the name MorphaMatic.class. As discussed, the .class file is the file the Web server downloads to your browser which, in turn, interprets and executes the files's contents.
What Is Javac_g?
As you have learned, the javac compiler examines your source-code files and produces the bytecode .class files. As your Java applets become more complex, it may become difficult for you to locate errors (bugs) within your code. To help you locate such errors, most Java development environments provide a special debugger program. In the case of the Sun's Java Developer's Kit, the debugger program is named jdb (for Java debugger). To provide jdb with more information to work with, the Sun's JDK provides special version of the compiler named javac__g. Sun designed the javac _g compiler for use with debuggers. Essentially, javacjg is a non-optimized version of the javac compiler which place tables of information within the bytecode that the debugger can use to track down errors. To compile a Java applet using the javac__g compiler, you simply specify the applet source-file named within the javac^g command line, as shown here
C:JAVACODE> javac „ g SomeFile.Java <Enter>
How To Optimize The Javac Output?
When you compare the performance of a Java program against that of a C/C++ program, you will find that the current generation of Java programs can be as much as twenty times slower than their C/C++ counterparts. This performance loss is mostly due to the fact that the browser musf interpret the Java bytecode and convert it into the computer's native code (such as a Pentium or Motorola-specific code) before the code can run. In C/C++, the code is in the processor's native format to begin with, so this time-consuming translation step is not required. Remember, however, that Java's generic bytecode allows the same Java code to run on multiple platforms.
The Java designers are working on various solutions to speed up Java. In the meantime, you can use the -O compiler switch with javac, which may increase the applet's performance. The -0 switch directs javac to optimize its bytecode by "inlining" static, final and private methods. For now, don't worry what "inlining" such code means other than it may improve your applet performance. Unfortunately, when you use inlining, you may increase the size of your bytecode file, which will increase the applet's download time. The following Javac command illustrates how you use the -0 switch
C:JAVACODE> javac -O MyFirst.java <Enter>
What Is The Difference Between Java Applets And Applications?
With Java, you can create two types of programs: an applet or an application. As you have learned, a Java applet is a program that executes from within a Web browser. A Java application, on the other hand, is a program that is independent of the browser and can run as a standalone program.
Because an applet is run from within a Web browser, it has the advantage of having an existing vindow and the ability to respond to user interface events provided though the browser. In addition, because applets are designed for network use Java is much restrictive in the types of access that applets can have to your file system than it is with non-network applications.
As you will, when you write a Java application, you must specify a main method (much like the C/ C++ main), which the program executes when it begins. Within the main method, you specify the functionality that your application performs. With an applet, on the other hand, you need to write additional methods that respond to events which are an important part of the applet's life cycle. The methods include init, start, stop, destroy and paint. Each of these events has a corresponding method and, when the event occurs, Java will call the appropriate method to handle it.
When you write your first Java programs, you can write them as if .they were applets and use the appletviewer to execute them. As it turns out, you can later convert your applet to an application by replacing your init method with a main method.
Can You Explain The Cs Option Of Java Interpreter?
When you develop Java using Sun's JDK, you normally compile your source by using the javac compiler. If no compile errors exist, you then run your application using the java interpreter. As a shortcut, you can specify the -cs command-line switch when you invoke the java interpreter. The java command, in turn, will automatically compile out-of-date (modified) source-code files for you.
By using the -cs switch, you can make changes to your source and immediately execute the java interpreter without having to manually run the java compiler yourself. The java interpreter knows which files it needs to recompile by comparing each file's modification dates against the corresponding class modification date. Normally, programmers use the -cs option when they have made a minor change to the source files and know that the files contain no compilation errors. The following command illustrates the use of the -cs switch
C: JAVACODE> Java -cs MyProgram <Enter>
What Is The Statements?
A Java program consists of instructions that you want the computer to perform. Within a Java applet, you use statements to express these instructions in a format the Java compiler understands. If you are already familiar with C/C++, you will discover that Java statements are very similar. For example, the following statements produce a programs which prints the words "Hello, Java!" in applet window
import java.applet.*; import java.awt.Graphics;
public class hello_java extends Applet
{
public void paint(Graphics g)
{
g.drawstring("Hello, Java!", 20, 20);
}
}
What Is Style And Indentation?
As you write Java programs, you will discover that you have considerable flexibility in how you line your statements and indent lines. Some editor programs help you line up indented lines and indent new block. If you have already programmed in another language, you probably have your own style of indentation. Java does not impose any specific style, However, you may want to be consistent with your own style and always be conscious that a well-formatted program is easier to read and maintain. For example, the following two programs function equivalently. However, one is much easier to read than the other
import java.applet. * ;
public class am_i_readable extends
Applet{public void init()
{
System.out.println("Can you guess whatI do?");
}
}
import java.applet.*;
public class am__i_readable extends Applet
{
public void init()
{
System.out.println("Can you guess what I do?");
}
}
What Is The Program Compilation Process?
When you create programs, you will normally follow the same steps. To begin, you will use an editor to create your source file. Next, you will compile the program using a Java compiler. If the program contains syntax errors, you must edit the source file, correct the errors, and re-compile.
After the program successfully compiles, the compiler generates a new file, known as bytecode. By using a Java interpreter, or appletviewer, you can execute the bytecode to test if it runs successfully. If the program does not work as you expected, you must review the source code to locate the error. After you correct the error, you must compile the source code to create a new byte code file. You can then test the new program to ensure that it performs the desired task. This illustrates the program development process.
Post a Comment