技术开发 频道

SCJP认证套题解析(3)

【IT168 技术文档】

  21、Which of the following assignment is not correct?
 
  A. float f = 11.1;
 
  B. double d = 5.3E12;
 
  C. double d = 3.14159;
 
  D. double d = 3.14D.
 
  答案 A
 
  题目:下面的哪些赋值语句是对的。
 
  浮点数的赋值是带有小数点的数字缺省是double型的,如果在浮点数后面加f或者F则是float,后面加d或者D则是double,科学计数法形式的浮点数也是double型的,而double的精度比float高,将一个高精度的double赋值给一个低精度的float时需要进行强制类型转换,反之则不需要。
 
  22、Given the uncompleted code of a class:
 
  class Person {
  String name, department;
  int age;
  public Person(String n){ name = n; }
  public Person(String n, int a){ name = n; age = a; }
  public Person(String n, String d, int a) {
  // doing the same as two arguments version of constructor
  // including assignment name=n,age=a
  department = d;
  }
  }
 
  Which expression can be added at the "doing the same as..." part of the constructor?
 
  A. Person(n,a);
 
  B. this(Person(n,a));
 
  C. this(n,a);
 
  D. this(name,age).
 
  答案 C
 
  题目:给出下面的不完整的类代码,下面的哪些表达式可以加到构造方法中的"doing the same as..."处?
 
  在同一个类的不同构造方法中调用该类的其它构造方法需要使用this(…)的形式,而且必须是在构造方法的第一行调用,这个和普通的方法重载调用的方式不同,普通的方法可以直接使用方法名加参数来调用,而且调用位置没有限制,因此答案A是不行的,B的语法就是错误的,D的错误在于在父类型的构造函数被调用前不能引用类的成员。构造方法是一个类对象实例化的起点(虽然严格来说首先执行的并不是构造方法的第一个语句,而是内存的分配),因此在构造方法中不能将成员作为参数引用。
 
  23、Which of the following statements about variables and their scopes are true?
 
  A. Instance variables are member variables of a class.
 
  B. Instance variables are declared with the static keyword.
 
  C. Local variables defined inside a method are created when the method is executed.
 
  D. Local variables must be initialized before they are used.
 
  答案 ACD
 
  题目:下面关于变量及其范围的陈述哪些是对的。
 
  A. 实例变量是类的成员变量。
 
  B. 实例变量用关键字static声明。
 
  C. 在方法中定义的局部变量在该方法被执行时创建
 
  D. 局部变量在使用前必须被初始化。
 
  类中有几种变量,分别是:局部变量(英文可以为:local\automatic\temporary\stack variable)是定义在方法里的变量;实例变量(英文为:instance variable)是在方法外而在类声明内定义的变量,有时也叫成员变量;类变量(英文为:class variable)是用关键字static声明的实例变量,他们的生存期分别是:局部变量在定义该变量的方法被调用时被创建,而在该方法退出后被撤销;实例变量在使用new Xxxx()创建该类的实例时被创建,而其生存期和该类的实例对象的生存期相同;类变量在该类被加载时被创建,不一定要用new Xxxx()创建,所有该类的实例对象共享该类变量,其生存期是类的生存期。任何变量在使用前都必须初始化,但是需要指出的是局部变量必须显式初始化,而实例变量不必,原始类型的实例变量在该类的构造方法被调用时为它分配的缺省的值,整型是0,布尔型是false,而浮点型是0.0f,引用类型(类类型)的实例变量的缺省值是null(没有进行实际的初始化,对它的使用将引起NullPointException),类变量的规则和实例变量一样,不同的是类变量的初始化是在类被加载时。
 
  24、
  public void test() {
  try { oneMethod();
  System.out.println("condition 1");
  } catch (ArrayIndexOutOfBoundsException e) {
  System.out.println("condition 2");
  } catch(Exception e) {
  System.out.println("condition 3");
  } finally {
  System.out.println("finally");
  }
  }
  Which will display if oneMethod run normally?
 
  A. condition 1
 
  B. condition 2
 
  C. condition 3
 
  D. finally
 
  答案 AD
 
  题目:在oneMethod()方法运行正常的情况下将显示什么?
 
  如果try块中的语句在执行时发生异常,则执行从该处中断而进入catch块,根据异常的类型进行匹配,最前面的优先进行匹配比较,只要该异常是catch中指定的异常的子类就匹配成功进而执行相应的catch中的内容,而finally块中的内容无论是否发生异常都将被执行。
 
  25、Given the following code:
 
  public class Test {
  void printValue(int m){
  do { System.out.println("The value is"+m);
  }
  while( --m > 10 )
  }
  public static void main(String arg[]) {
  int i=10;
  Test t= new Test();
  t.printValue(i);
  }
  }
 
  Which will be output?
 
  A. The value is 8
 
  B. The value is 9
 
  C. The value is 10
 
  D. The value is 11
 
  答案 C
 
  题目:给出下面的代码输出将是什么?
 
  此题考察的是do… while循环和 -- 操作符的知识,do…while最少被执行一次,在执行完do中的内容后判断while中的条件是否为true,如果为true的话就再执行do中的内容,然后再进行判断,以此类推直到while的判断为false时退出循环执行循环后面的内容,而操作符的规则是在变量右边的-- 将先进行运算,然后才是使变量的值减一,而在变量左边的是先将变量的值减一再运算。
0
相关文章