Java面试问题 011-020

原文来自http://www.allapplabs.com/interview_questions/java_interview_questions.htm

翻译:Zeng Yuetian

转载请注明出处。 

 

Q011:

What is an Iterator?
什么是Interator

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.
一些collection类提供通过java.util.Iterator接口来遍历其中内容的途径。这个接口允许你遍历一个对象的集合,并且依次操作这些对象。记住,当你用Iterator的时候,它们是获得该Iterator时的快照。一般来说,不建议大家在遍历Iterator的时候改变collection

 

 

Q012:

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.
抽象类是必须在本扩展或继承后才能被用的。抽象类就像一个模板。抽象类无法被实例化(比如,你无法调用它的构造函数)。抽象类可能包含static数据。任何有抽象方法的类自动变成抽象类,而且必须被声明为抽象类。
即使一个类没有抽象方法,它依然可以被声明为抽象的。这样可以防止它被实例化。

 

 

Q013:

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.
Static意味着整个类只有一个,而不管该类有多少个具体实现。这意味着你不用创建该类的对象就可以使用该Static数据。Static方法隐式是final的,因为覆盖是基于对象的类型的,一个static方法是附属于类的,而不是附属于某个该类的对象。如果父类的static方法不被声明为final,父类的static方法将被子类的static所隐藏。然而你无法用一个非static的方法来覆盖同名的static方法。也就是说,你无法将一个类方法在子类中转换成实例方法。

 

 

Q014:

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).
final类无法被扩展,例如final类不能被继承。Final方法在该类被继承时无法被覆盖。你不能改变一个final变量的值(代表常量)。

 

 

Q015:

What if the main method is declared as private?
如果main方法被声明成private会怎样?

A:

The program compiles properly but at runtime it will give "Main method not public." message.
程序可以被编译,但是运行时会报"Main method not public."

 

 

Q016:

What if the static modifier is removed from the signature of the main method?
如果main函数的签名中的static被去掉会如何?

A:

Program compiles. But at runtime throws an error "NoSuchMethodError".
程序可以被编译。但是运行时会报"NoSuchMethodError"

 

 

Q017:

What if I write static public void instead of public static void?
如果把public static void写成static public void会怎样?

A:

Program compiles and runs properly.
程序能正确编译和运行。

 

 

Q018:

What if I do not provide the String array as the argument to the method?
如果我不提供String数组作为main函数的参数会怎样?

A:

Program compiles but throws a runtime error "NoSuchMethodError".
程序能编译,但是会抛出运行时错误"NoSuchMethodError"  

 

 

Q019:

What is the first argument of the String array in main method?
main
方法中String数组的第一个参数是什么?

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.
String数组是空的,它不含如何元素。这不像c或者c++,它们中的第一个元素默认是程序名。

posted @ 2015-10-14 23:14  剑芒  阅读(154)  评论(0编辑  收藏  举报