题目要求 本期题目: (单选题) 1、若有定义:byte[]x={11,22,33,﹣66};其中0≤k≤3,则对x数组元素错误的引用是() A x[5-3] B x[k] C x[k+5] D x[0]
(单选题)2、以下程序段的输出结果为: publicclassEqualsMethod { publicstaticvoidmain(String[] args) { Integer n1 = newInteger(47); Integer n2 = newInteger(47); System.out.print(n1 == n2); System.out.print(","); System.out.println(n1 != n2); } } A false,false B false,true C true,false D true,true
(单选题)3、有以下类定义: abstractclassAnimal{ abstractvoidsay(); } publicclassCat extendsAnimal{ publicCat(){ System.out.printf("Iam a cat"); } publicstaticvoidmain(String[] args) { Catcat=newCat(); } } 运行后: A I am a cat B Animal能编译,Cat不能编译 C Animal不能编译,Cat能编译 D 编译能通过,但是没有输出结果
上期练习题答案公布栏
(单选题) 1、对于上面这段代码,以下说法正确的是:
publicclassTest
{
publicintx;
publicstaticvoidmain(String []args)
{
System.out. println("Value is"+ x);
}
}
A 程序会打出 "Value is 0"
B 程序会抛出 NullPointerException
C 非静态变量不能够被静态方法引用
D 编译器会抛出 "possible reference before assignment"的错误
正确答案是:C
解析:非静态成员只能被类的实例化对象引用,因此这里在静态方法中访问x会造成编译出错
(单选题) 2、下面有关java基本类型的默认值和取值范围,说法错误的是?
A 字节型的类型默认值是0,取值范围是-2^7—2^7-1
B boolean类型默认值是false,取值范围是true\false
C 字符型类型默认是0,取值范围是-2^15 —2^15-1
D long类型默认是0,取值范围是-2^63—2^63-1
正确答案是:C
解析:
java中,char占16bit、byte占8bit、short占16bit、int占32bit、long占64bit、float占32bit、double占64bit。
只有char是无符号类型的。
字符型类型占用的是16位,2字节,但是字符型类型是无符号类型的,所以其取值范围是0 ~ 2^16-1 而不是 -2^15 ~ 2^15-1。
(单选题) 3、What will happen when you attempt to compile and run the
following code?
public class Test{
static{
int x=5;
}
static int x,y;
public static void main(String args[]){
x--;
myMethod( );
System.out.println(x+y+ ++x);
}
public static void myMethod( ){
y=x++ + ++x;
}
}
A compiletime error
B prints:1
C prints:2
D prints:3
E prints:7
F prints:8
正确答案是:D
解析:
1.静态语句块中x为局部变量,不影响静态变量x的值
2.x和y为静态变量,默认初始值为0,属于当前类,其值得改变会影响整个类运行。
3.java中自增操作非原子性的
main方法中:执行x--后 x=-1 调用myMethod方法,x执行x++结果为-1(后++),但x=0,++x结果1,x=1 ,则y=0
x+y+ ++x,先执行x+y,结果为1,执行++x结果为2,得到最终结果为3
(不定项选择题) 4、java中下面哪些是Object类的方法()
A notify()
B notifyAll()
C sleep
D wait()
正确答案是:ABD
解析:在根类Object中包含一下方法: