技术开发 频道

SCJP认证套题解析(5)

  46、The variable "result" is boolean. Which expressions are legal?
 
  A. result = true;
 
  B. if ( result ) { // do something... }
 
  C. if ( result!= 0 ) { // so something... }
 
  D. result = 1
 
  答案 AB
 
  题目:变量"result"是一个boolean型的值,下面的哪些表达式是合法的。
 
  Java的boolean不同于c或者c++中的布尔值,在java中boolean值就是boolean值,不能将其它类型的值当作boolean处理。
 
  47、Class Teacher and Student are subclass of class Person.
 
  Person p;
  Teacher t;
  Student s;
  p, t and s are all non-null.
  if(t instanceof Person) { s = (Student)t; }
  What is the result of this sentence?
 
  A. It will construct a Student object.
 
  B. The expression is legal.
 
  C. It is illegal at compilation.
 
  D. It is legal at compilation but possible illegal at runtime.
 
  答案 C
 
  题目:类Teacher和Student都是类Person的子类
 
  …
  p,t和s都是非空值
  …
  这个语句导致的结果是什么
 
  A. 将构造一个Student对象。
 
  B. 表达式合法。
 
  C. 编译时非法。
 
  D. 编译时合法而在运行时可能非法。
 
  instanceof操作符的作用是判断一个变量是否是右操作数指出的类的一个对象,由于java语言的多态性使得可以用一个子类的实例赋值给一个父类的变量,而在一些情况下需要判断变量到底是一个什么类型的对象,这时就可以使用instanceof了。当左操作数是右操作数指出的类的实例或者是子类的实例时都返回真,如果是将一个子类的实例赋值给一个父类的变量,用instanceof判断该变量是否是子类的一个实例时也将返回真。此题中的if语句的判断没有问题,而且将返回真,但是后面的类型转换是非法的,因为t是一个Teacher对象,它不能被强制转换为一个Student对象,即使这两个类有共同的父类。如果是将t转换为一个Person对象则可以,而且不需要强制转换。这个错误在编译时就可以发现,因此编译不能通过。
 
  48、Given the following class:
 
  public class Sample{
  long length;
  public Sample(long l){ length = l; }
  public static void main(String arg[]){
  Sample s1, s2, s3;
  s1 = new Sample(21L);
  s2 = new Sample(21L);
  s3 = s2;
  long m = 21L;
  }
  }
 
  Which expression returns true?
 
  A. s1 == s2;
 
  B. s2 == s3;
 
  C. m == s1;
 
  D. s1.equals(m).
 
  答案 B
 
  题目:给出下面的类哪个表达式返回true。
 
  前面已经叙述过==操作符和String的equals()方法的特点,另外==操作符两边的操作数必须是同一类型的(可以是父子类之间)才能编译通过。
 
  49、Given the following expression about List.
 
  List l = new List(6,true);
 
  Which statements are ture?
 
  A. The visible rows of the list is 6 unless otherwise constrained.
 
  B. The maximum number of characters in a line will be 6.
 
  C. The list allows users to make multiple selections
 
  D. The list can be selected only one item.
 
  答案 AC
 
  题目:给出下面有关List的表达式哪些叙述是对的。
 
  A. 在没有其它的约束的条件下该列表将有6行可见。
 
  B. 一行的最大字符数是6
 
  C. 列表将允许用户多选。
 
  D. 列表只能有一项被选中。
 
  List组件的该构造方法的第一个参数的意思是它的初始显式行数,如果该值为0则显示4行,第二个参数是指定该组件是否可以多选,如果值为true则是可以多选,如果不指定则缺省是不能多选。
 
  50、Given the following code:
 
  class Person {
  String name,department;
  public void printValue(){
  System.out.println("name is "+name);
  System.out.println("department is "+department);
  }
  }
  public class Teacher extends Person {
  int salary;
  public void printValue(){
  // doing the same as in the parent method printValue()
  // including print the value of name and department.
  System.out.println("salary is "+salary);
  }
  }
 
  Which expression can be added at the "doing the same as..." part of the method printValue()?
 
  A. printValue();
 
  B. this.printValue();
 
  C. person.printValue();
 
  D. super.printValue().
 
  答案 D
 
  题目:给出的代码下面的哪些表达式可以加入printValue()方法的"doing the same as..."部分。

  子类可以重写父类的方法,在子类的对应方法或其它方法中要调用被重写的方法需要在该方法前面加上super.进行调用,如果调用的是没有被重写的方法,则不需要加上super.进行调用,而直接写方法就可以。这里要指出的是java支持方法的递归调用,因此答案a和b在语法上是没有错误的,但是不符合题目代码中说明处的要求:即做和父类的方法中相同的事情打印名字和部门,严格来说也可以选a和b。

0
相关文章