Frenquently asked interview questions

来源:互联网 发布:手机淘宝一元夺宝在哪 编辑:程序博客网 时间:2024/06/07 02:36

 1.    what is the difference between an applet and an application? 【程序编程相关:利用Reflection API访问类的】

 

java questions & answers 【推荐阅读:软件开发尽量采用成熟的FrameWork】

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 users web browser, and the browsers security rules, (or suns appletviewer, which has fewer restrictions). 【扩展信息:先贴段jsp代码试试】

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.

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 dont 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.

3. what is rmi? 【程序编程相关:利用Reflection API访问类的】

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. 【推荐阅读:软件开发尽量采用成熟的FrameWork】

4. what gives java its "write once and run anywhere" nature? 【扩展信息:先贴段jsp代码试试】

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.

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.

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. 【程序编程相关:利用Reflection API访问类的】

5. how does java inheritance work? 【推荐阅读:软件开发尽量采用成熟的FrameWork】

native methods are used when the implementation of a particular method is present in language other than java say c, c++. 【扩展信息:先贴段jsp代码试试】

6. what are native methods? how do you use them?

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 methods: 【程序编程相关:利用Reflection API访问类的】

-- static variables: these are class level variable whose value remain same irrespective of the number of instances of the class. 【推荐阅读:软件开发尽量采用成熟的FrameWork】

-- 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. 【扩展信息:先贴段jsp代码试试】

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.

 

-- 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 3: jdbc-net pure java driver: 【程序编程相关:利用Reflection API访问类的】

 

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. 【推荐阅读:软件开发尽量采用成熟的FrameWork】

 

type 4: native-protocol pure java driver 【扩展信息:先贴段jsp代码试试】

 

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.

 

these are pure java drivers that communicate directly with the vendors database. they do this by converting jdbc commands directly into the database engines 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?

12. how does exception handling work in java? 【程序编程相关:利用Reflection API访问类的】

yes, with some tools. 【推荐阅读:软件开发尽量采用成熟的FrameWork】

2.it allows a clean path for error propagation. if the called method encounters a situation it cant manage, it can throw an exception and let the calling method deal with it. 【扩展信息:先贴段jsp代码试试】

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

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, theyre 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.

public void finalize() { } 【程序编程相关:利用Reflection API访问类的】

 

but it has finalizers that does a similar job. the syntax is 【推荐阅读:软件开发尽量采用成熟的FrameWork】

14. what does the "final" keyword mean in front of a variable? a method? a class? 【扩展信息:先贴段jsp代码试试】

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.

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 dont know if any compiler actually does this, but its 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".

原创粉丝点击