Java面试汇总(三)

最全的Java面试题目下载

Java面试中最经常被问到的问题

1.      What is the difference between an Applet and an Application?

A Java application is made up of a main() method declared as public static void that accepts a string array argument, along with any other classes that main() calls. It lives in the environment that the host OS provides.
A Java applet is made up of at least one public class that has to be subclassed from java.awt.Applet. The applet is confined to living in the user's Web browser, and the browser's security rules, (or Sun's appletviewer, which has fewer restrictions).
The differences between an applet and an application are as follows:

1. Applets can be embedded in HTML pages and downloaded over the Internet whereas Applications have no special support in HTML for embedding or downloading.

2. Applets can only be executed inside a java compatible container, such as a browser or appletviewer whereas Applications are executed at command line by java.exe or jview.exe.

3. Applets execute under strict security limitations that disallow certain operations(sandbox model security) whereas Applications have no inherent security restrictions.

4. Applets don't have the main() method as in applications. Instead they operate on an entirely different mechanism where they are initialized by init(),started by start(),stopped by stop() or destroyed by destroy().

2. What are java beans?
JavaBeans is a portable, platform-independent component model written in the Java programming language, developed in collaboration with industry leaders. It enables developers to write reusable components once and run them anywhere -- benefiting from the platform-independent power of Java technology. JavaBeans acts as a Bridge between proprietary component models and provides a seamless and powerful means for developers to build components that run in ActiveX container applications.
Java beans is very powerful tool you can use in your servlet/JSP bridge. You can use the servlets to build the bean and can be passed over to the JSP for reading. This provides tight encapsulation of the data while preserving the sanctity of servlets and JSP.


3. What is RMI?
RMI stands for Remote Method Invocation. Traditional approaches to executing code on other machines across a network have been confusing as well as tedious and error-prone to implement. The nicest way to think about this problem is that some object happens to live on another machine, and that you can send a message to the remote object and get a result as if the object lived on your local machine. This simplification is exactly what Java Remote Method Invocation (RMI) allows you to do.


4. What gives java it's "write once and run anywhere" nature?
Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platorm specific and hence can be fed to any platform. After being fed to the JVM, which is specific to a particular operating system, the code platform specific machine code is generated thus making java platform independent.


5. How does Java inheritance work?
A class can only directly extend one class at a time. Multiple inheritance is only allowed with regard to interfaces. A class can implement many interfaces. But a class can only extend one non-interface class.


6. What are native methods? How do you use them?
Native methods are used when the implementation of a particular method is present in language other than Java say C, C++.
To use the native methods in java we use the keyword native
public native method_a()
This native keyword is signal to the java compiler that the implementation of this method is in a language other than java.
Native methods are used when we realize that it would take up a lot of rework to write that piece of already existing code in other language to java.


7. Class A subclass B subclass C. All override foo(). I cast C to A and call foo(). What happens? Can C call A->foo()?
An instance of Class C is of type Class B and A (both). SO you can cast C to A. You CANNOT cast an instance of A to C.


8. What does the "static" keyword mean in front of a variable? A method? A class? Curly braces {}?
-- static variables: These are class level variable whose value remain same irrespective of the number of instances of the class.
-- static methods:
These are those methods that can be called without the need for creating the objects of the class i.e. they are class level methods. They can call only static methods. They cannot refer to "this" as they are not associated with any particular instance.
-- static block: These are called before the main is called and are called only once. Subsequent invocation of the java program containing static block would not call it again. Hence, they can be used to load libraries say in native function call.
-- Only Inner class could be declared as a "static". This declaration suppress the generation of the reference to the outer class object. 这意味着:1)为创建一个static内部类的对象,我们不需要一个外部类对象;2)不能从static内部类对象访问一个外部类对象。


9. How many different types of JDBC drivers are present? Discuss them.
There are four JDBC driver types.

Type 1: JDBC-ODBC Bridge plus ODBC Driver:
The first type of JDBC driver is the JDBC-ODBC Bridge. It is a driver that provides JDBC access to databases through ODBC drivers. The ODBC driver must be configured on the client for the bridge to work. This driver type is commonly used for prototyping or when there is no JDBC driver available for a particular DBMS.

Type 2: Native-API partly-Java Driver:
The Native to API driver converts JDBC commands to DBMS-specific native calls. This is much like the restriction of Type 1 drivers. The client must have some binary code loaded on its machine. These drivers do have an advantage over Type 1 drivers because they interface directly with the database.

Type 3: JDBC-Net Pure Java Driver:
The JDBC-Net drivers are a three-tier solution. This type of driver translates JDBC calls into a database-independent network protocol that is sent to a middleware server. This server then translates this DBMS-independent protocol into a DBMS-specific protocol, which is sent to a particular database. The results are then routed back through the middleware server and sent back to the client. This type of solution makes it possible to implement a pure Java client. It also makes it possible to swap databases without affecting the client.

Type 4: Native-Protocol Pure Java Driver
These are pure Java drivers that communicate directly with the vendor's database. They do this by converting JDBC commands directly into the database engine's native protocol. This driver has no additional translation or middleware layer, which improves performance tremendously.
10. Does Java have "goto"?
Yes and No. There is no "goto" operator used in Java, but it is a reserved keyword, and one can use break statements to branch to a labelled statement, exactly as one would use a goto.


11. Why "bytecode"? Can you reverse-engineer the code from bytecode?
yes, with some tools.


12. How does exception handling work in Java?

1.It separates the working/functional code from the error-handling code by way of try-catch clauses.

2.It allows a clean path for error propagation. If the called method encounters a situation it can't manage, it can throw an
exception and let the calling method deal with it.

3.By enlisting the compiler to ensure that "exceptional" situations are anticipated and accounted for, it enforces powerful coding.

4.Exceptions are of two types: Compiler-enforced exceptions, or checked exceptions and Runtime exceptions, or unchecked exceptions. Compiler-enforced (checked) exceptions are instances of the Exception class or one of its subclasses -- excluding the RuntimeException branch. The compiler expects all checked exceptions to be appropriately handled. Checked exceptions must be declared in the throws clause of the method throwing them -- assuming, of course, they're not being caught within that same method. The calling method must take care of these exceptions by either catching or declaring them in its throws clause. Thus, making an exception checked forces us to pay heed to the possibility of it being thrown. An example of a checked exception is java.io.IOException. As the name suggests, it throws whenever an input/output operation is abnormally terminated.


13. Does Java have destructors?
Java does not have destructors. Garbage collector does this job periodically depending upon the memory requirements of the machine and on the fact that a particular object is no longer needed.
But it has finalizers that does a similar job. The syntax is
public void finalize() { }
If an object has a finalizer, the method is invoked before the system garbage collects the object, but using finalize() does not guarantee that it would be called b4 garbage collector is invoked.


14. What does the "final" keyword mean in front of a variable? A method? A class?
A final variable cannot be reassigned, but it is not constant. For instance,
final StringBuffer x = new StringBuffer();
x.append("hello");

is valid. X cannot have a new value in it, but nothing stops operations on the object that it refers, including destructive operations.
Also, a final method cannot be overridden or hidden by new access specifications. This means that the compiler can choose to in-line the invocation of such a method. (I don't know if any compiler actually does this, but it's true in theory.)
The best example of a final class is String, which defines a class that cannot be derived.


15. Access specifiers: "public", "protected", "private", nothing?
Public?    Any other class from any package can instantiate and execute the classes and methods
Protected? Only subclasses and classes inside of the package can access the classes and methods
Private? The original class is the only class allowed to execute the methods.
And in case if there is no modifier specified, it means, only the classes inside the package can access this class and its methods, it is also called "Friendly".

你可能感兴趣的:(java,面试,jdbc,Security,Access)