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。
闽公网安备 35060202000074号