分享
 
 
 

面试时最经常被问到的问题(Frenquently asked interview questions)之Java篇

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Java Questions & Answers

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有