日期:2014-05-20  浏览次数:20868 次

J2SE面试题总结(英文)
最近在准备面试,把一些题目和自己大概归纳的答案贴出来,希望对大家有帮助,也请高人指证

1.Difference between Abstract class and Interface.

1) Abstract - a class which has abstract methods, but also instance methods;
  Interface - has only abstract methods.
2) Interface allows multiple implementation, thus easier to maintain;
  Abstract class does not allow multiple inheritance
3) Interface - Static final constants only;
  Abstract - Both instance and static constants are possible
4) Interface - slower;  
  Abstract - faster;
5)Abstract class must be extended to use, can not be instantiated, a class may be declared abstract even if it does not have abstract methods to prevent it from being instantiated.

2.
1)What’s the principle of garbage collection and when it is used? 2)The purpose of garbage collection? 3)Is there any method to notice JVM to collection garbage? 4) Does garbage collection guarantee that a program will not run out of memory?

1)When the object is created, garbage collector starts to monitor the address, size and usage of this object, manage and record all the objects in the heap,and distinguish which are reachable which are unreachable, once the object is unreachable, the memory will be taken back.

2)Identify and discard the objects no longer needed by a program to make the resources reclaimed and reused.

3)System.gc(), but not to a specific object

4)No.
a.Possible that program uses memory resource faster than it is collected
b.Possible that program creates objects that are not subject to garbage collection

3.Describe synchronization

1)The capability to control the access of multiple threads to the shared resources, ensure that only one thread is accessed to the resource at one time. e.g. Without synchronization, if one thread is modifying a shared variable while another thread is in the process of using or updating the same shared variable, this will lead to errors.

Both synchronized methods and synchronized statements can only be executed after the thread got the lock.





asynchronous

no need to wait the return, when one task starts, other tasks can still operate at the same time.

举一个例子,就是,打电话是同步,发短信是异步。同步的意思就是必须等到返回了才能进一步执行,而异步就是发送请求以后,不需要等待函数或者事件进行下去(like download),可以继续执行其它 的任务。

4.Explain different ways of using thread?

1) Implement the Runnable interface
2) Extend from Thread class

1) is more advantageous, cuz you can still inherit from other class or implement other interfaces



5. Why not stop() and suspend() for thread?

Might stop some operations and cause unexpected errors.

6.1)What’s wrapper classes? 2)Why they are needed?

1) Integer, Character, Double …

Wrapper classes are classes that allow primitive types to be accessed as objects.

Java provides wrapper class for every primitive data type

They exist in java.lang package

2) Sometimes it’s easier to deal with primitive data as an object. For example,

a.Collections only accept objects instead of primitive data types

b.Some utility methods are provided by wrapper classes

c.Some methods only accept object as a parameter

7. What’s the differences between error and exception?

1) Error is an irrecoverable condition occurring at runtime e.g. OutOfMemory error, can not be repaired at runtime.

2) Exception is condition occurring because of bad inputs e.g. NullPointerException, FileNotFoundException. It’s possible to recover.

8. Differences between Checked Exception and Unchecked(Runtime) Exception

1) Checked Exception is the exception Java compiler forces you to catch e.g. IOException from FileInputStream’s read() method.

if a checked exception may be thrown in the body of a method, the method must either catch it or declare it in the throws clause.