`
zyslovely
  • 浏览: 231631 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

基础题

阅读更多
1 哪些是check异常,哪些是uncheck异常?


The Java Language Specification calls any exception that derives from the class Error or
the class RuntimeException an unchecked exception. All other exceptions are called checked
exceptions.


这道题的答案肯定是知道的,只是当时一时发慌,又是第一个问题,答得可能有点乱。


-------------------------------------------------------------------------------------------------------------------------


2 LinkedList和ArrayList有什么区别,在什么场合上用?


1) A linked list store each object in a separate link, each link also stoe a reference to the next link in the sequence. In the Java programing language, all linked lists are actually double linked; that is, each link also store a reference to its predecessor.


Removing an element from the middle of a linked list is an inexpensive operation—only the links around the element to be removed need to be updated.


You should never use get method to random access an object to step through a linked list. This method is inefficient. Each time you lookup anohter element, the search starts from the beginning of the list.


2) The List interface describes an ordered collection in which the position of elements matters. An ArrayList encapsulates a dynamically reallocated array object.


Removing an element from the middle of an ArrayList  is expensive since all array elements beyond the
removed one must be moved toward the beginning of the array.


In a word, if you offten update a list, use LinkedList. If you offten random access a list, use ArrayList.


这道题当时回答的时候我应该已经把两者的区别说出来了,但提及到使用场合和效率问题时,我的回答面试官好像不太满意。


-------------------------------------------------------------------------------------------------------------------------


3 HashSet和TreeSet的区别?


1) In Java, hash tables are implemented as arrays of linked lists. Each list is called a bucket. To find the place of an object in the table, compute its hash code and reduce it modulo the total number of buckets. A HashSet class that implements a set based on the hash tabel. You add elements with the add method. The contains method is redefined to make a fast lookup to find if an element is already present in the set. It checks only the elements in one bucket and not all elements in the collection. You would only use a HashSet if you don’t care about the ordering of the elements in the collection.


2) The TreeSet class is similar to the hash set, with one added improvement. A tree set is a sorted collection. You insert elements into the collection in any order. The sorting is accomplished by a red-black tree data structure. Adding a element to a tree is slower than adding it to a hash table, but it is still much faster than adding it into the right place in an array or linked list.


-------------------------------------------------------------------------------------------------------------------------


4 抽象类和接口的区别,什么时候用抽象类,什么时候用接口?


这个问题是老生常谈了,但很惭愧的说,两者的区别谁都能说上来,但是使用场合我确实不太清楚,也请各位指点了。


-------------------------------------------------------------------------------------------------------------------------


5 Synchronized必须需要一个对象锁吗?如果定义了一个Synchronized方法,这时是对哪个谁加锁?


A Synchronized method automatically performs a lock action when it is invoked; its body is not executed until the lock action has successfully completed. If the method is an instance method, it locks the monitor associated with the instance for which it was invoked (that is, the object that will be known as this during execution of the body of the method). If the method is static, it locks the monitor associated with the Class object that represents the class in which the method is defined.


对监视器加锁和对Class对象加锁我到是都答出来了,希望当时我表达的还算清楚。


-------------------------------------------------------------------------------------------------------------------------


6 wait和sleep的区别?


这个问题网上都是,我也答了一些最基本的东西。但面试官问了一些是使用上的不同,老实说多线程做的比较少,具体使用还真没什么经验。



-------------------------------------------------------------------------------------------------------------------------


7 简述一下生产者和消费者。


我简单的阐述了一下,其中提到了一句“当任务序列达到最大时,生产者就开始等待,直到有消费者消耗掉一个任务再进行生产”,然后面试官问了一句“为什么要有最大数量的任务限制?如果没有会怎么样?”。我当时只能凭个人感觉答了一些,而且看过网上的列子,好像所有写生成者和消费者程序的人都对最大任务数做了限制,我个人理解就是节省cpu的资源,不要造成内存的消耗过大,不知道是否正确。


-------------------------------------------------------------------------------------------------------------------------


8 死锁,为什么会产生死锁,Java中如何避免死锁?


互斥 持有与等待 非抢占性 循环等待,这个四个应该是产生死锁的条件,当时肯定不是按照这么专业的名词答得,大多说了一下自己的理解,大意上应该是对的,但对于Java中如何避免死锁还真没遇到过类似问题。还是待人指点了。


-------------------------------------------------------------------------------------------------------------------------


9 Java中有内存溢出吗? 列举几个例子。


Java是肯定有内存溢出了,当时我举的是HashMap的例子,如果一个对象的hashCode 方法和这个对象本身的属性有关,而且当这个对象的一个实例作为key值存到一个HashMap中之后,实例的属性发生了变化。这样再用这个key值去get存储的对象,就获得不了我们当初存储的对象了。不知道我我的理解是否对,请各位指正。当时面试官反问了一句:“如果用values 方法不就可以在找到所有的值对象吗?” 以前遇到过类似的问题,但就是一时想不起来如何回答了,还有一个内存溢出的例子,就是死活想不起来了,晕。


-------------------------------------------------------------------------------------------------------------------------


10 String StringBuffer StringBuilder的区别?


String a final class which can't be extended and updated. Every time you concatenate strings, a new String object is constructed. This is time consuming and it wastes memory. Using the StringBuilder class avoids this problem. StringBuffer, is slightly less efficient, but it allows multiple threads to add or remove characters. If all string editing happens in a single thread (which is usually the case), you should use StringBuilder.


面试官又问了一句StringBuffer是如何实现的,没答上来,后来看了看JDK的源码,就是对一个简单的字符数组的操作。哎,当时应该想到的,可能还是因为太紧张加上被问了很久了吧。


-------------------------------------------------------------------------------------------------------------------------


11 非阻塞I/O如何实现的?简单设计一个非阻塞I/O。


老实回答,没用过NIO,更没设计过。但以前看过一些文章,就是对采用类似观察者模式,循环的监视所有I/O流,当观察到一个I/O流处于非阻塞状态时才去对它采取操作,如果是阻塞状态就让其他线程去执行其他任务,不要等待这个被阻塞的I/O流。


-------------------------------------------------------------------------------------------------------------------------


12 Observer模式和Command模式的好处以及解决的问题。


因为我说这在现在的项目中用到了这两种模式,所以面试官才问。


1) Observer模式解决了多次传递参数句柄的问题,利用观察者通知对象发生了改变,就不用每次都将对象传递到用到这个对象的地方了。


2) Command模式实现了线性操作,统一管理各个认为。可以方便的操作哪个任务想要,哪个任务需要重做。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics