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.
2) Unchecked Exceptions
RuntimeException and any of its subclasses.
Compiler does not force the programmer to catch it or declare it in a throws clause.
Thrown at runtime because of wrong inputs or wrong business logic
9. How to create custom exceptions?
extend from class Exception
10. Different ways to handle exceptions
1) wrapping the code in a try block followed by a catch block to catch the exceptions
2) List the exceptions in the throws clause after the method name.
11. Is it necessary that each try block must be followed by a catch block?
No. finally block is ok also.
12.What are pass by reference and pass by value? (or how many ways can an argument be passed)
1) reference - passing the address instead of the value of an object.
2) value - passing a copy of the value.
!!! Java supports only pass-by-value
13. HashMap vs Map
Map is interface, HashMap is a class which implements Map interface.
14. HashMap vs HashTable
相同点
1) The both implement Map interface
不同点
1) HashTable is synchronized, HashMap is not
2) HashTable does not allow null object as a key or value for an entry. HashMap allows.
3) HashTable extends from abstract class Dictionary. HashMap extends from instance class AbstractMap.
通常多用HashMap而不是HashTable,多用List而不是Vector,因为需要同步的时候再同步就是了,不用预先同步好,这样会降低效率
15. Vector vs ArrayList vs LinkedList vs Array
1) Vector is synchronized, List is not. Vector has similar function as Arraylist, they are more like dynamic array but for objects. 如果当前的数据长度已经超出了当前分配好的尺寸,Vector增长100%,ArrayList增长50%。
2) List is more efficient than Vector
3) Array is a set of data types with fixed length. List and Vector are used to store objects, the length is changeable.
4) For random queries, ArrayList and Vector are more efficient than LinkedList.
16. Differences between a constructor and a method ?
Constructor :
1)Member function of a class used to create objects of the class.
2)Same name as the class
3)No return type
4)Invoked by using “new” keyword
Method:
1)Ordinary member function of a class
2)Own name
3)Own return type, maybe void
4)Invoked by using “dot” operator
17. Declaring vs defining a variable?
(Creation)Definition = Declaration + Initialization
18. Assignment vs Initialization
Initialization can be done only once
Assignment can be done as many times as desired
19. argument vs parameter
argument 就是变量,方法内的变量
parameter 是方法的参数
20. String vs StringBuffer
String
1)only supports constant strings, once created, can not be changed, modification mechanism is actually creating a new object and pass the new value.
2)immutable
StringBuffer
1)supports modifiable and growable strings.
2)provides convenient method like append to conjunct the strings.more efficient that String.
21. process vs thread
process is like a program, thread is a separate path of this program.
22. Collection vs Collections
Collection is an interface ,mainly implemented by Set and List.
Collections, as a wrapper class, provides several convenient methods to operate the Collection. such as sort(),
23. final vs finally vs finalize
final is a modifier, final class can not be extended, final method can not be overrided, final variable can not be changed.
finally is used as part of the try catch block, means operates anyways. (e.g. exception happens, but the file already open should be closed), it will execute even there is a return clause and no exception happens.
finalize, a method from Object class, used by garbage collection before the unreachable object is collected.
24. sleep() vs wait()
sleep() is from Thread class, does not release the lock, it gives an constant time and be wake up automatically. But can be interrupted by interrupt()
wait() is from Object class,releases the lock, it has to be wake up by notify() method or notifyAll() method. these 3 methods can only be used in the synchronized blocks.
27. What are the different scopes for java variables?
It depends where it is declared
1) Instance: Object level.initialized to default values at the creation time. It is accessible as long as the object is accessible.
2) Local: Method level, defined in a method, no default values. Accessible only when the method is in execution process.
3) Static: Class level. Initialized when the class is loaded in JVM for the first time. As long as the class is loaded, it is accessible, not tied to any object instance.
28. Set vs List
Set stores elements in an unordered way and does not allow duplicate elements.
List stores elements in an ordered way and elements can be duplicate.
29. How to serialize an object into a file?
The class whose instance needed to be serialized should implement Serializable interface.Pass the instance to ObjectOutputStream which wraps a FileOutputStream.
[解决办法]
LZ很是犀利呀! 都弄成英文的。顶一个~
[解决办法]
1.Difference between Abstract class and Interface.
1) Abstract - a class which has abstract methods, but also instance methods;
Interface - has only abstract methods.
Abstract - a class which may have abstract methods, but must be declared as abstract at the beginning.
[解决办法]
yes or no
1.Do I need to import java.lang
No, it’s loaded by JVM by default
2.Can I have multiple main methods in the same class?
No, there can be at most one main method in one class, compiler will report error “main method is already defined in the class”
可以重载
[解决办法]
7.Is empty .java file a valid source file ?
yes
empty .java 文件名有空格 文件名类名一样,类名不能有空格,所以 这个文件名不对