Java面試題

*Q1.How could a Java program redirect error messages to a file while sending other messages to the system console?

A. We can use multiple thread:one thread is for redirect.the anothor is for sending messages to the system console;At the same time,We have to use synchronized modifer to limit our action when we send messages.

 

*Q2.What's the difference between an interface and an abstract class?

A.    Interface is absolute abstract class..All motheds in interface are abstract and we don not need public static modifer to modify abstract methods.All constant in interface are public static final.

B.    Abstract class may have non abstract method.But if there is a method which is abstract,We need add abstract modifer to modify

     This class;

 

*Q3. Why would you use a synchronized block vs. synchronized method?

A. We use a synchronized block to synchronize a whole object.As you know some time We may reuse the same class which need not include a synchronized method.While we provided a class in which we implemented synchronized,we can not change it .So the first one is more flexible.

 

**Q4. You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?

A.Partly we can.But Inheritence for java is single.We realize multiple inheritances through interface.For example,we only can extends one abstract class but we can implements more than one interfaces.

 

*Q5. How can you force garbage collection?

A.We can not force garbage collection.Garbage collection is daemon thread which runs at backround.

 

*Q6. How do you know if an explicit object casting is needed?

A. Most of runtime exception are needed to cast  explicitly.Such as FilenotFoundException.But I get some information from IDE;

 

*Q7. What's the difference between the methods sleep() and wait()

A. The method sleep is to halt a thread in a limited time.When the time is up,then this thread is alive.While wait() method have to be invoked by other thread  with notify method or notifyAll method.

 

*Q8. Can you write a Java class that could be used both as an applet as well as an application?

A.

 

*Q9. What's the difference between constructors and other methods?

A. Constructors are used to initialize some values.When we did not surrply any customize constructors.The jvm can provide us a default

Construct without parameter.In java,in order to encapsulate,we need surrply some methods to access some private values.The constructors do not have return value and their name are the same with the class name and construct can not be overrided.

 

*Q10. Can you call one constructor from another if a class has multiple constructors

A. ok,that is no problem.We can use this(…) method to call other constructors.

 

*Q11. Explain the usage of Java packages.

A.Java package is for to archive some files in order to manage our class.We can use import XXXpackage.someclass to use class in this package if this class has public modifier.If these classes are in the same package,we need not use import …

 

*Q12. If a class is located in a package, what do you need to change in the OS environment to be able to use it?

A. We don not need.We can use import XXXpackage.someclass to use class in this package if this class has public modifier.If these classes are in the same package,we need not use import …

 

*Q13. What's the difference between J2SDK 1.5 and J2SDK 5.0?

A.I think they are the same.

 

**Q14. What's the difference between a queue and a stack?

A. Queue’s character is first in first out. Stack’s character is last in first

   Out.

*Q15. Explain the usage of the keyword transient?

A. When we do not want to serialize some values,we can declare some valuable with  transient modifer.

 

**Q16. What comes to mind when you hear about a young generation in Java?

A. I think it may bring us more functions and we can develop more convinent.

 

**Q17. What comes to mind when someone mentions a shallow copy in Java?

A. I think most object are referenced.If we want to copy a object,we should override the clone method.If there is another object included in this object.We also need to orverride the clone method for another.Through this,we can realize deep copy.

 

**Q18. If you're overriding the method equals() of an object, which other method you might also consider?

A. I might condider hashCode() method;

 

**Q19. You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?

A. I will use ArrayList because it is fast in access.While LinkedList is fast in modify and delete.

 

*Q20. What's the main difference between a Vector and ArrayList

A. The vector is a safe class while arraylist is not and vector should be initialize its capacity.such as Vector v = new Vector(10);

 

*Q21. When should the method invokeLater()be used?

A. If we want to improve our response efficient,we can use invokeLater to do something else at background.

 

**Q22. How would you make a copy of an entire Java object with its state?

A. . we should override the clone method.If there is another object included in this object.We also need to orverride the clone method for another.Through this,we can realize deep copy.

 

*Q23. What would you use to compare two String variables - the operator == or the method equals()?

A.I will use equals() method if I need to know if their values are equal.

If I need to know the two strings object are the same object ,I will use operator ==.

 

**Q24. How can you minimize the need of garbage collection and make the memory use more effective?

A. we can use single-pattern factory to create a object and try my best to avoid using new operator.

 

*Q25. How can a subclass call a method or a constructor defined in a superclass?

A. We can use super().XXX method to call another method.

 

**Q26. There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?

A. A class should include a reference to b class.

 

*Q27. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?

A. We need not any declaration.

 

*Q28. Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?

A. no matter.

 

*Q29. Can an inner class, declared inside of a method, access local variables of this method?

A. no problem.

 

*Q30. What can go wrong if you replace && with & in the following code:
String a=null; if (a!=null && a.length()>10) {...}

A. It will throw nullpointrefferenceerexception.

 
發佈了63 篇原創文章 · 獲贊 24 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章