试题1
下面各选项可以在A的子类中使用的是()
class A {
protected int method (int a, int b) {
return 0;
}
}
A.
public int method (int a, int b) { return 0; }
B.
private int method(int a, int b) { return 0; }
C.
private int method(int a, long b) { return 0; }
D.
public short method(int a, int b) { return 0; }
试题2
下列代码执行的结果是:
class Base
{
void test() {
System.out.println("Base.test()");
}
}
public class Child extends Base {
void test() {
System.out.println("Child.test()");
}
public static void main(String[] a) {
Child anObj = new Child();
Base baseObj = anObj;
baseObj.test();
}
}
A.
Child.test()
Base.test()
B.
Base.test()
Child.test()
C.
Base.test()
D.
Child.test()
试题3
关于以下程序代码的说明正确的是( ):
1.class HasStatic{
2. private static int x=100;
3. public static void main(String args[ ]){
4. HasStatic hs1=new HasStatic( );
5. hs1.x++;
6. HasStatic hs2=new HasStatic( );
7. hs2.x++;
8. hs1=new HasStatic( );
9. hs1.x++;
10. HasStatic.x--;
11. System.out.println(“x=”+x);
12. }
13.}
A.
5行不能通过编译,因为引用了私有静态变量
B.
10行不能通过编译,因为x是私有静态变量
C.
程序通过编译,输出结果为:x=103
D.
程序通过编译,输出结果为:x=102
试题4
关于java.lang.String类,以下描述正确的一项是( )
A.
String类是final类故不可以继承;
B.
String类是final类故可以继承;
C.
String类不是final类故不可以继承;
D.
String类不是final类故可以继承;
试题5
下列关于interface的说法正确的是:( )
A.
interface中可以有private方法
B.
interface中可以有final方法
C.
interface中可以有方法实现
D.
interface可以继承其他interface
试题6
以下程序正确的输出是( )
public class FatherClass {
public FatherClass() {
System.out.println("FatherClass Create");
}
}
public class ChildClass extends FatherClass {
public ChildClass() {
System.out.println("ChildClass Create");
}
public static void main(String[] args) {
FatherClass fc = new FatherClass();
ChildClass cc = new ChildClass();
}
}
A.
FatherClass Create
FatherClass Create
ChildClass Create
B.
FatherClass Create
ChildClass Create
FatherClass Create
C.
ChildClass Create
ChildClass Create
FatherClass Create
D.
ChildClass Create
FatherClass Create
FatherClass Create
试题7
请看如下代码
class Person {
private int a;
public int change(int m){return m;}
}
public class Teacher extends Person{
public int b;
public static void main(String arg[]){
Person p = new Person();
Teacher t = new Teacher();
int i;
// point x
}
}
下面哪些放在// point x行是正确的?
A.
i = b;
B.
i = p.a;
C.
i = p.change(30);
D.
i = t.b;
试题8
下列选项中放置于“《插入代码》”处会编译出错的是
interface IFace{}
class CFace implements IFace{}
class Base{}
public class ObRef extends Base{
public static void main(String argv[]){
ObRef ob = new ObRef();
Base b = new Base();
Object o1 = new Object();
IFace o2 = new CFace();
《插入代码》
}
}
A.
o1=o2;
B.
b=ob;
C.
ob=b;
D.
o1=b;
试题9
已知类Student的定义如下:
public class Student {
String name;
Student(String name) {
this.name = name;
}
《插入代码》
}
main方法代码:
Student stu1=new Student(new String(“tom”));
Student stu2=new Student(“tom”);
System.out.print(stu1==stu2);
System.out.println(stu1.equals(stu2));
如果控制台的输出结果是false true,那么《插入代码》处填入的代码正确的是:
A.
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student stu = (Student) obj;
return stu.name==this.name;
} else {
return false;
}
}
B.
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student stu = (Student) obj;
return stu.name.equals(this.name);
} else {
return false;
}
}
C.
public boolean equals(Object obj) {
if (obj instanceof Student) {
return obj.name.equals(this.name);
} else {
return false;
}
}
D.
public boolean equals(Object obj) {
if (obj instanceof Student) {
return obj.name==this.name;
} else {
return false;
}
}
试题10
指出下列程序运行的结果
public class Example{
String str=new String("good");
char[]ch={'a','b','c'};
public static void main(String args[]){
Example ex=new Example();
ex.change(ex.str,ex.ch);
System.out.print(ex.str+" and ");
System.out.print(ex.ch);
}
public void change(String str,char ch[]){
str="test ok";
ch[0]='g';
}
}
A.
good and abc
B.
good and gbc
C.
test ok and abc
D.
test ok and gbc
下面各选项可以在A的子类中使用的是()
class A {
protected int method (int a, int b) {
return 0;
}
}
A.
public int method (int a, int b) { return 0; }
B.
private int method(int a, int b) { return 0; }
C.
private int method(int a, long b) { return 0; }
D.
public short method(int a, int b) { return 0; }
试题2
下列代码执行的结果是:
class Base
{
void test() {
System.out.println("Base.test()");
}
}
public class Child extends Base {
void test() {
System.out.println("Child.test()");
}
public static void main(String[] a) {
Child anObj = new Child();
Base baseObj = anObj;
baseObj.test();
}
}
A.
Child.test()
Base.test()
B.
Base.test()
Child.test()
C.
Base.test()
D.
Child.test()
试题3
关于以下程序代码的说明正确的是( ):
1.class HasStatic{
2. private static int x=100;
3. public static void main(String args[ ]){
4. HasStatic hs1=new HasStatic( );
5. hs1.x++;
6. HasStatic hs2=new HasStatic( );
7. hs2.x++;
8. hs1=new HasStatic( );
9. hs1.x++;
10. HasStatic.x--;
11. System.out.println(“x=”+x);
12. }
13.}
A.
5行不能通过编译,因为引用了私有静态变量
B.
10行不能通过编译,因为x是私有静态变量
C.
程序通过编译,输出结果为:x=103
D.
程序通过编译,输出结果为:x=102
试题4
关于java.lang.String类,以下描述正确的一项是( )
A.
String类是final类故不可以继承;
B.
String类是final类故可以继承;
C.
String类不是final类故不可以继承;
D.
String类不是final类故可以继承;
试题5
下列关于interface的说法正确的是:( )
A.
interface中可以有private方法
B.
interface中可以有final方法
C.
interface中可以有方法实现
D.
interface可以继承其他interface
试题6
以下程序正确的输出是( )
public class FatherClass {
public FatherClass() {
System.out.println("FatherClass Create");
}
}
public class ChildClass extends FatherClass {
public ChildClass() {
System.out.println("ChildClass Create");
}
public static void main(String[] args) {
FatherClass fc = new FatherClass();
ChildClass cc = new ChildClass();
}
}
A.
FatherClass Create
FatherClass Create
ChildClass Create
B.
FatherClass Create
ChildClass Create
FatherClass Create
C.
ChildClass Create
ChildClass Create
FatherClass Create
D.
ChildClass Create
FatherClass Create
FatherClass Create
试题7
请看如下代码
class Person {
private int a;
public int change(int m){return m;}
}
public class Teacher extends Person{
public int b;
public static void main(String arg[]){
Person p = new Person();
Teacher t = new Teacher();
int i;
// point x
}
}
下面哪些放在// point x行是正确的?
A.
i = b;
B.
i = p.a;
C.
i = p.change(30);
D.
i = t.b;
试题8
下列选项中放置于“《插入代码》”处会编译出错的是
interface IFace{}
class CFace implements IFace{}
class Base{}
public class ObRef extends Base{
public static void main(String argv[]){
ObRef ob = new ObRef();
Base b = new Base();
Object o1 = new Object();
IFace o2 = new CFace();
《插入代码》
}
}
A.
o1=o2;
B.
b=ob;
C.
ob=b;
D.
o1=b;
试题9
已知类Student的定义如下:
public class Student {
String name;
Student(String name) {
this.name = name;
}
《插入代码》
}
main方法代码:
Student stu1=new Student(new String(“tom”));
Student stu2=new Student(“tom”);
System.out.print(stu1==stu2);
System.out.println(stu1.equals(stu2));
如果控制台的输出结果是false true,那么《插入代码》处填入的代码正确的是:
A.
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student stu = (Student) obj;
return stu.name==this.name;
} else {
return false;
}
}
B.
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student stu = (Student) obj;
return stu.name.equals(this.name);
} else {
return false;
}
}
C.
public boolean equals(Object obj) {
if (obj instanceof Student) {
return obj.name.equals(this.name);
} else {
return false;
}
}
D.
public boolean equals(Object obj) {
if (obj instanceof Student) {
return obj.name==this.name;
} else {
return false;
}
}
试题10
指出下列程序运行的结果
public class Example{
String str=new String("good");
char[]ch={'a','b','c'};
public static void main(String args[]){
Example ex=new Example();
ex.change(ex.str,ex.ch);
System.out.print(ex.str+" and ");
System.out.print(ex.ch);
}
public void change(String str,char ch[]){
str="test ok";
ch[0]='g';
}
}
A.
good and abc
B.
good and gbc
C.
test ok and abc
D.
test ok and gbc
A
D
D
A
D
A
C D
C
D
A
2 D
3 D
4 A
5 D
6 A
7 CD
8
9
10 B
2.d
3.d
4.a
5.d
6.a
7.cd
8.c
9.b
10.b
2.C 只是调用了Base类的test方法,别的都是障眼法
3.D private对类本身没有影响,静态属性用了实例调用.+3次-1次
4.A final类不能被继承
5.D
6.A 子类new对象的时候首先调用父类构造方法,如果有爷爷先调用爷爷的
7.CD b是static,不能直接=. a是private的.只能被类自己调用
8.C 不能向上转型.
9.B 重写equals方法
10.B 要记住String是不能改变的.
经过测试的答案.
2.D
3.D
4.A
5.D
6.A
7.CD
8.C
9.B
10.B
第二题baseObj为Base的实例不错,这个实际上就是多态的问题,父类的引用指向子类对象,如果有重写的方法会调用子类的函数
你可以再测试试下
2 D
3 D
4 A
5 D
6 A
7 CD
8 C
9 B
10 B
这个考察的应该是子类覆盖父类的方法吧,返回类型必须是父类或者父类的子类,比如父类是int,那么子类只能是int,或者int的子类,int没有子类,那么只能返回int,父类是protected的,子类必须不小于这个,所以不能是private,所以选A.
试题2这里Child anObj = new Child();是new了一个子类的对象,Base baseObj = anObj;这是把子类向上转型,也就是包装到了父类里面,但是调用方法的时候还是调用自己的方法.选D.
试题3
这是x 是静态变量,一个类只是共用一个x,所以HasStatic hs1=new HasStatic( );
5. hs1.x++; 就是100+1,HasStatic hs2=new HasStatic( );
7. hs2.x++; 就是101+1,hs1=new HasStatic( );
9. hs1.x++; 就是102+1,HasStatic.x--; 就是103-1,
这样就是102了.
选d.试题4这个是常识,一个final类是不可继承的,而String确实是final的.
选A.试题5interface本身就是用来继承和实现的,private是不可以的,同样,interface里面的方法体都是空的,如果是final,是没办法重写覆盖的,所以interface中可以有方法实现也是不对的,而interface可以继承其他interface 这个是可以的,所以选D.试题6 这个FatherClass fc = new FatherClass();会输出FatherClass Create,而ChildClass cc = new ChildClass();作为子类,new的时候会将父类先new,FatherClass Create
ChildClass Create 就是这样,所以选A.
试题7
A. i = b; ,这个b是类的普通成员,而i是在静态的main方法中定义的,静态方法不可以直接调用,要通过对象.属性来访问.i = p.a; ,这个a 是private属性,类外面是不可以访问的,也是不可以.出来的.i = p.change(30); 这个就是把i变成30,i = t.b; 这个是把b赋值给i,b是类里面的属性,会默认初始化为0,所以i就会变成0,如果从程序运行上,c,d都是对的.具体的是什么,我就不懂了,汗.
试题8
o1=o2;这是把一个CFace赋值给object,子类赋值给父类是允许的,b=ob; 就是ObRef 赋值给Base ,也是子类赋值给父类,也是允许的,ob=b; 这个就不可以了,父类好比一只猫,子类是波斯猫,波斯猫可以赋值给猫,因为波斯猫是猫,但猫不一定是波斯猫,所以父类赋值给子类要强制转型,o1=b;是把Base 赋值给object,也是子类赋值给父类.所以选C.
试题9stu1==stu2是比较两个对象的地址,Student stu1=new Student(new String(“tom”));
Student stu2=new Student(“tom”);这样两个对象肯定是不同的,比较地址必然是false.
System.out.println(stu1.equals(stu2));
这个如果是true,那么就是比较name了,由于是字符串比较,是不能用stu.name==this.name这种==来比较的,所以A,D都是不可以的,C里面return obj.name.equals(this.name);这个obj是object,要强制转型后才有name属性的,所以只能选B.试题10由于java里面都是值传递,将一个string传进去,实际上传的是一个string的拷贝字符串,改变的知识拷贝字符串的值,所以还是good,而将一个char[]传进去,实际上传的是char[]的首地址的拷贝,而操作的优势char[]数组本身,不是操作首地址,所以是会被改变的,所以是B.
1.C
2.D
3.D
4.A
5.D
6.A
7.CD
8.C
9.B
10.B