Java面试问题 011-020
原文来自http://www.allapplabs.com/interview_questions/java_interview_questions.htm
翻译:Zeng Yuetian
转载请注明出处。
Q011: |
|
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. |
Q012: |
|
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. |
Q013: |
|
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. |
Q014: |
|
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). |
Q015: |
What if the main method is declared as private? |
A: |
The program compiles properly but at runtime it will give "Main method not public." message. |
Q016: |
What if the static modifier is removed from the signature of the main method? |
A: |
Program compiles. But at runtime throws an error "NoSuchMethodError". |
Q017: |
What if I write static public void instead of public static void? |
A: |
Program compiles and runs properly. |
Q018: |
What if I do not provide the String array as the argument to the method? |
A: |
Program compiles but throws a runtime error "NoSuchMethodError". |
Q019: |
What is the first argument of the String array in main method? |
A: |
The String array
is empty. It does not have any element. This is unlike C/C++ where the
first element by default is the program name. |