Given the following class definition:
class A{
protected int i; A(int i){ this.i=i;
} }
which of the following would be a valid inner class for this class? Select all valid answers:
A. class B{ }
B. class B extends A{ }
C. class B extends A{
B(){System.out.println(“i=”+i);} }
D. class B{
class A{} }
E. class A{} 解答:A
点评:此题考查内部类及关键字“super”的用法。内部类不能与外部类同名。另外,当B继承A时,A中的构造函数是带参数的,B中缺省构造函数的函数体为空;而JAVA编译器会为空构造函数体自动添加语句“super();”调用父类构造函数,更进一步是调用父类的参数为空的构造函数。而父类中没有参数为空的构造函数。
例题22:
Which modifier should be applied to a method for the lock of object this to be obtained prior to excution any of the method body?
A. synchronized
B. abstract
C. final
D. static
E. public 解答:A
点评:此关键字可以在两个线程同时试图访问某一数据时避免数据毁损。
例题23:
The following code is entire contents of a file called Example.java,causes precisely one error during compilation:
1) class SubClass extends BaseClass{ 2) }
3) class BaseClass(){ 4) String str;
5) public BaseClass(){
6) System.out.println(“ok”);} 7) public BaseClass(String s){ 8) str=s;}}
9) public class Example{ 10) public void method(){
11) SubClass s=new SubClass(“hello”);
12) BaseClass b=new BaseClass(“world”); 13) } 14) }
Which line would be cause the error?
A. 9 B. 10 C. 11 D.12 解答:C
点评:当一个类中未显式定义构造函数时,缺省的构造函数是以类名为函数名,参数为空,函数体为空。虽然父类中的某一构造函数有字符串参数s,但是子类继承父类时并不继承构造函数,所以它只能使用缺省构造函数。故在第11行出错。 例题24:
Which statement is correctly declare a variable a which is suitable for refering to an array of 50 string empty object?
A. String [] a
B. String a[]
C. char a[][]
D. String a[50]
F. Object a[50] 解答:A,B
点评:注意,题中问的是如何正确声明一个一维数组,并非实例化或者初始化数组。
例题25:
Give the following java source fragement: //point x
public class Interesting{ //do something }
Which statement is correctly Java syntax at point x?
A. import java.awt.*;
B.package mypackage
C. static int PI=3.14
D. public class MyClass{//do other thing…} E. class MyClass{//do something…} 解答:A,E
点评:X处可以是一个输入,包的定义,类的定义。由于常量或变量的声明只能在类中或方法中,故不能选择C;由于在一个文件中只能有一个public类,故不能选择D。
例题26:
Give this class outline: class Example{ private int x;
//rest of class body… }
Assuming that x invoked by the code java Example, which statement can made x be directly accessible in main() method of Example.java?
A. Change private int x to public int x
B. change private int x to static int x
C. Change private int x to protected int x
D. change private int x to final int x 解答:B
点评:静态方法除了自己的参数外只能直接访问静态成员。访问非静态成员,必须先实例化本类的一个实例,再用实例名点取。
例题27:
the piece of preliminary analsis work describes a class that will be used frequently in many unrelated parts of a project
“The polygon object is a drawable, A polygon has vertex information stored in a vector, a color, length and width.”
Which Data type would be used?
A. Vector
B. int
C. String
D. Color
E. Date
解答:A,B,D
点评:polygon的顶点信息存放在Vector类型的对象内部,color定义为Color,length和width定义为int。
注意,这是考试中常见的题型。
例题28:
A class design requires that a member variable should be accessible only by same package, which modifer word should be used?
A. protected
B. public
C. no modifer
D. private
解答:C
点评:此题考点是高级访问控制。请考生查阅高级访问控制说明表格。
例题29:
Which declares for native method in a java class corrected?
A. public native void method(){}
B. public native void method();
C. public native method();
D. public void method(){native;}
E. public void native method(); 解答:B
点评:native关键字指明是对本地方法的调用,在JAVA中是只能访问但不能写的方法,它的位置在访问权限修饰语的后面及返回值的前面。
例题30:
Which modifer should be applied to a declaration of a class member variable for the value of variable to remain constant after the creation of the object? 解答:final
点评:定义常量的方法是在变量定义前加final关键字。
例题31:
Which is the main() method return of a application?
A. String
B. byte
C. char
D. void
解答:D
点评:main()方法没有返回值,所以必须用void修饰。main()方法的返回值不能任意修