给出以下代码,请问如何声明secret使其为一个只读变量?public class Example{public int secret;}
A. 声明secret变量为private。
B. 声明Example类为private。
C. 声明secret变量为private,并且建立一个用于获取secret变量值的getSecret()方法。
D. 声明secret变量transient。
E. 声明secret变量为static。
查看答案
给出以下代码,下面程序的运行结果是什么?class A extends Integer{int x=0;}
A. 代码编译成功。
B. 代码编译错误。因为Iinteger类是final类,不能被继承。
C. 代码编译成功。因为类A没有方法和构造器。
D. 代码编译成功。但运行时会抛出ArithmeticException异常。
给出以下代码,请问该程序的运行结果是什么?1. public abstract class Example{2. public abstract void mth1();3. public static void mth2(){4. int mth2 = 30;5. System.out.printIn(“mth2=”+mth2);6. }7. public abstract void mth3();8. }
A. 程序编译通过。
B. 第1行发生编译错误。
C. 第3行发生编译错误。
D. 第7行发生编译错误。
给出以下代码,请问该程序的运行结果是什么?class Base{Base(){System.out.printIn(“Message 1:In the base class constructor”);}}abstract class Sub1 extends Base{Sub 1(){System.out.printIn(“Message 2:In the abstract class SubI’s constructor”);}}class Sub2 extends Sub1{public Sub2(){System.out.printIn(“Message 3:In the Sub2 class’s constructor”);}public static void main(String args[]){Sub2 d2 = new Sub2();}}
A. 代码编译失败,因为非抽象类不能被扩展为抽象类。
B. 代码编译失败,因为抽象类不能有构造器。
C. 打印输出Message 1:In the base class constructorMessage 3:In the derived2 class\’s constructor.
D. 打印输出Message 1:In the base class constructorMassage 2:In the abstract class Derived1\’s constructorMessage 3:In the derived2 class\’s constructor.
给出以下代码,请问该程序的运行结果是什么?1. public class Example{2. static int x;3. static public void main(String[]args){4. x=x+1;5. System.out.println(“Value is”+x);6. }7. }
A. 代码编译失败,因为变量x未被初始化。
B. 代码编译成功,打印输出Value is 1。
C. 代码编译成功,但在运行期第5行抛出一个NullPointerException异常。
D. 代码编译失败,因为main()方法形式不对。