Java面試

Questions
                                               Java面試問題
                              
Q: What is the difference between an Interface and an Abstract class?
問:接口與抽象類間的區別?
A: An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.
A:抽象類可以有具體的實現方法,可以執行缺省行爲,接口僅僅聲明常量和實例      方法,但是不能執行缺省行爲,並且所有方法隱含是抽象的。接口有所有公共成員但沒有實現。抽象類是一個可以包括通常類成員(私有,保護,等)的類,但有一些抽象方法。
   
Q: What is the purpose of garbage collection in Java, and when is it used?  
問:在Java語言中垃圾回收機制的目的是什麼,它用在什麼時候?
A: The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.    A:垃圾回收機制的目的是爲了識別和拋棄不再需要由程序控制的對象,從而實現資源的可再生和重用.如果一個對象沒有在任何地方被引用,即沒有任何一個引用指向這個對象,那麼這個單元就是可以被回收的。

Q: Describe synchronization in respect to multithreading.
問:描述關於多線程的同步.
A: With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.  
A:對於多線程, 同步是有能力控制出入的多線程共享資源。未經同步,有可能一個線程修改共享變量,而另一個線程在使用過程中或更新同一共享變量.這通常會導致重大的失誤。

Q: Explain different way of using thread?
問:解釋一下使用不同線程的方法?
A: The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance..the only interface can help.   
A:線程可以執行調用可運行接口或從線程類繼承.前者是較爲有利。起因是當你要作多重繼承時.. 只有藉助於接口。

Q: What are pass by reference and passby value?
問:什麼是按參數傳遞和按值傳遞?
A: Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed.      A:按參數傳遞意味着是通過地址本身來傳遞而不是通過值來傳遞,按值傳遞就是指將值拷貝進行傳遞

Q: What is HashMap and Map?
問:什麼是HashMap和Map?
A: Map is Interface and Hashmap is class that implements that.
A:Map是接口而 HashMap是用來繼承它的類。

Q: Difference between HashMap and HashTable?
問:Hashtable和HashMap的區別
A: The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesnt allow). HashMap does not guarantee that the order of the map will remain constant over time. HashMap is unsynchronized and Hashtable is synchronized.
A:HashMap類大致相當於HashTable,除了它不同步並且它允許一個 null 鍵和多個 null值(HashMap允許空值作爲關鍵子和值,而HashTable不允許),隨着時間的推移hashmap並不保證Map順序保持不變,.HashMap不同步而HashTalue同步

Q: Difference between Vector and ArrayList?
問:Vector和ArrayList的區別?
A: Vector is synchronized whereas arraylist is not.
A:Vector同步但ArrayList不同步。

Q: Difference between Swing and Awt?
問:Swing和AWT的區別是什麼?
A: AWT are heavy-weight componenets. Swings are light-weight components. Hence swing works faster than AWT A:AWT是重量級組件,Swing是輕量級組件,因此Swing比AWT運行的更快。
   
Q: What is the difference between a constructor and a method?  
問:構造器和方法的區別:
A: A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.  A:構造器是被用來創建類對象的一個Function成員,它和類本身名字一樣,沒有返回類型,並且請求調用新的運算符。方法是類的一個普通Function成員,它有自己的名字,有返回類型(有可能是無效的),並且請求調用點運算符。

Q: What is an Iterator?
問:什麼是迭代器?
A: Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator. A:一些集合類通過java.util.iterator接口提供他們的目錄遍歷.這個接口允許你通過對象集反過來走訪每一個對象,記住當使用包含了一集合的快照的迭代器那時Iterators被獲得; 當遍歷Iterator時它一般不適合修改集合本身。
 
Q: State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers. 
問:獨一陳述一下公共類(public),私有類(private),保護類(protected),缺省(default)修飾符的有效性,並結合這些修飾符陳述一下在聲明項目條件時包關係間的作用?
A: public : Public class is visible in other packages, field is visible everywhere (class must be public too)
private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.
protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.
default :What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.
A:public:public類可以供其他包中的類訪問,在任何一處都可以訪問到它的域(類也必須是public類)
private:私有變量或方法只能在聲明瞭這個變量或方法的本類的實例中調用,私有屬性只能通過擁有和它一樣屬性的類才能訪問。
protected:在同樣的包中供所有的類使用並且也供那些擁有Protected屬性的類的所有子類使用,這個訪問提供給屬於在另外一個擁有Protected屬性的包的子類
default:你通過缺省ie得到的,沒有進行過任何訪問修改的(ie,public,private或者protected).這意味它可以在所有特殊的包內訪問到。

Q: What is an abstract class?
問:什麼是一個抽象類?
A: Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such.A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.
A:抽象類必須是extended/subclassed(是有用的)。它相當於模板。抽象的類不能被實例化(ie,您不可以告訴它的建設者),抽象類可以包含靜態數據。任一類以一個抽象方法自動地是抽象的本身,且必須被聲明,一個類可以被聲明是抽象的既使它沒有抽象方法。這防止它被實例化。

Q: What is static in java?
問:在JAVA中static的意思是什麼?
A: Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.
A:Static指代每一個類,而不是每一個對象,無論有多少類的實例可能存在.就是指你可以不用創建一個實例類就可以用他們。Static方法隱含了final,因爲基於對象類型的重要性,並且Static方法不符於類,沒有對象,一個Static方法在父類(superclass)中可能由其他Static方法隱含在子類中,只要原先的方法沒有被聲明final類,但是你不能忽略一個靜態方法以一個nonstatic出現,換句話說,在子類中你無法改變一個靜態方法到一個事例方法。

Q: What is final?
問:什麼是final類?
A: A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).
A:final類不能被ie繼承.,final類不可以被子類繼承。final方法在當它的類被繼承時不能重載,你不能改變final變量的值 (是一個恆值)。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章