文档库 最新最全的文档下载
当前位置:文档库 › 试题面

试题面

Java面向对象程序设计
考试卷
班级:
姓名:
时间: 90分钟

一、选择题(没有注明多选,则为单选)
1、下列变量定义错误的是 
A.int a;
B.double b=4.5;
C.boolean b=true;
D.float f=9.8; (9.8f)

2、6+5%3+2的值是
A.2
B.1
C.9
D.10

3、对于一个三位的正整数 n,取出它的十位数字k(k为整型)的表达式是
A.k = n / 10 % 10 //先得到百位和十位 192 19 9
B.k = ( n - n / 100 * 100 )%10
C.k = n % 10
D.k = n / 10

4、下列语句序列执行后,k 的值是
1.int x=6, y=10, k=5;
2.switch( x % y )
3.{
4. case 0: k=x*y;
5. case 6: k=x/y;
6. case 12: k=x-y;
7. default: k=x*y-x; (default 位置可以改变)
8.}
A.60
B.5
C.0
D.54

5、下列语句序列执行后,i的值是:
1.int i = 10;
2.do { i/=2; } while( i-- > 1 ); 10542->1->0-1
A.1
B.5
C.2
D.-1

6、在某个类中存在一个方法:void getSort(int x),以下能作为这个方法的重载的声明的是:(同一个方法中参数不同,返回值类型可以不同也可以相同)
A.public getSort(float x) 没有返回类型 一定是构造函数 不能重载
B.int getSort(int y)(参数一样不是重载)
C.double getSort(int x,int y) (参数不一样是重载)
D.void get(int x, int y)

7、下列哪个是合法的Java标识符:(两个答案) B C
A.Tree&Glasses
B.FirstJavaApplet
C._$theLastOne
D.273.5

8、设 a = 8,则表达式 a >>> 2 的值是:C (无符号右移动)左移是乘 右是除
1000->/2->/2 将一个数8除2,运算最快的方式
A.1
B.2
C.3
D.4

9、下面的程序名为Student.java
1.public class Student
2.{
3.private String name;
4.public Student(String s_name) //1 构造函数
5.{
6. name = s_name; //2
7.}
8.public static void main(String args[])
9.{
10. Student s = new Student(); //3
11.}
12.}
使用如下指令编译:javac Student.java将会得到什么结果?
A.将会顺利通过编译,并将产生一个Student.class的类文件
B.编译时在//3处出错
C.编译时在//2处出错
D.编译时在//1处出错

10、下面选项中能把字符串转换成float类型的是?: B
A.float value = new Float(str); 创建一个对象
B.float value = Float.parseFloat(str);
C.float value = Float.floatValue(str);
D.float value = (new Float()).parseFloat(str);

11、下边程序运行的结果是? 实例化子类对象要看父类
1.class Base { //父类
2. Base() { System.out.print("Base"); } //父类中构造函数
3.}
4.public class Alpha extends Base {
5. public static void main( String[] args ) {
6. new Alpha(); //实例化自己首先要实例化其父类
7. new Base(); //实例化父类
8. }
9. }
A.Base
B.BaseBase
C.程序编译失败.
D.程序运行但没有任何输出

12、下面程序运行的结果是? A 值传递
1.

public class X {
2. private static int a;
3. public static void main(String [] args) {
4. modify(a);
5. System.out.println(a);
6.}
7. public static void modify(int a) {
8. a++;
9. }
10.}
A.0
B.1
C.程序编译失败
D.程序抛出异常

13、下面程序运行的结果是?
1.String s = "Hello" + 9 + 1;+字符连接(9+1+”hello”=10hello(string类型))
2.System.out.println(s);
A.Hello10
B.Hello91
C.Hello10.0
D.程序编译失败

14、下列说法正确的是? C
A.一个子类可以有多个父类,一个父类也可以有多个子类
B.一个子类可以有多个父类,但一个父类只可以有一个子类
C.一个子类可以有一个父类,但一个父类可以有多个子类
D.上述说法都不对

15、下面程序运行的结果是?
1. abstract class AbstrctIt {
2. abstract float getFloat ();
3. }
4. public class AbstractTest extends AbstractIt {
5. private float f1= 1.0f;
6. private float getFloat () {return f1;} //权限只能扩大不能缩小
7. }
A.编译成功
B.在第6行产生一个运行时异常
C.在第6行产生一个编译错误
D.在第2行产生一个编译错误

16、下面的程序输出的结果是:
1.public class A implements B {
2.public static void main(String args[]){
3. int i;
4. A c1 = new A();
5. i= c1.k;
6. System.out.println("i="+i);
7.}
8.}
9.interface B {
10. int k = 10;接口(抽象方法和静态常量的结合)里的静态常量 public static final
11.}
A.i=0
B.i=10
C.程序有编译错误
D.i=true

17、方法methodA返回类型是:
1. public class returnIt{
2. returnType methodA(byte x, double y) {
3. return (short) x/y * 2;
4. }
5. }
A.int
B.byte
C.long
D.double

18、下面的程序输出的结果是:
1. public class IfTest {
2. public static void main(string[]args) {
3. int x = 3;
4. int y = 1;
5. if (x = y){ // =:赋值运算符 ==:比较运算符
6. System.out.println(“Not equal”);
7. }else
8. System.out.println(“Equal”);
9. }
10.}
A.Equal
B.Not Equal
C.编译失败
D.程序没有任何输出结果

19、在下面程序中,变量i可以使用的数据类型是:(两个答案)
1. switch (i) { //i可以是byete、char、short、int四种
2. default:
3. System.out.println(“Hello”);
4. }
A.char
B.byte
C.float 不行
D.double 不行
E.object

20、应用程序的main方法中有以下语句,则输出的结果是:
1.int[] x={122,33,55,678,-987};
2.int y=x[0];
3.for(int i=1;i4.if(x[i]>y)
5.y =x[i];
6.}
7.System.out.println(y);
A.678
B.122
C.-987
D.33

21、程序输出的结果是?
1. public class X {
2. public static void main(String [] args) {
3. try {
4. badMethod(); //调用静态方法,无意义。
5. System.o

ut.print(“A”);
6. }
7. catch (Exception ex) { // 未捕获到,不输出。
8. System.out.print(“B”);
9. }
10. finally {
11. System.out.print(“C”);
12. }
13. System.out.print(“D”);
14. }
15. public static void badMethod() {
System.out.println(“E”)
}
17. }
A.AB
B.BD
C.E
ACD
D.ABCD

22、程序输出的结果是?
System.out.println(4 | 3); // 二进制 按位或操作
0100 | 0011 0111
A.0
B.1
C.5
D.7

23、关于下述程序哪个是正确的构造器重载(两个正确)
public class ConstOver {
public ConstOver (int x, int y, int z) { }
}
A.ConstOver ( ) { }
B.protected int ConstOver ( ) { } //构造函数不能有返回类型
C.private ConstOver (int z, int y, byte x) { }
D.public Object ConstOver (int x, int y, int z) { }
E.public void ConstOver (byte x, byte y, byte z) { }

24、下述程序编译运行后在1和2处分别打印出什么值?
1.public class Test9
2.{ static int i = 1; //静态变量和静态块只会初始化一次
3. static{
4. i++; // i=2;
5. }
6. public Test9(){
7. i++;
8. }
9.public static void main(String[] args){
10. Test9 t1 = new Test9();
11. System.out.println(t1.i); //1
12. Test9 t2 = new Test9();
13. System.out.println(t2.i); //2
14. }
15.}
A.2和2
B.3和3
C.3和4
D.4和3

25、下列答案正确的是:两个答案
int[] arr = new int[10];
A.arr[0] 是null
B.arr[10]是0
C.arr[9] 是0
D.arr[0] 是0

26、编译器能够为类A分配一个默认构造器的是?(两个答案)
A.class A {}
B.class A {
public A() { }
}
C.class A {
public A(int x) { }
}
D.class Z { }
class A extends Z {
void A() { }
}

27、下面程序运行的结果是:
1.public class Foo {
2.public int i = 3;
3. public static void main(String args[]) {
4. Object o = new Foo();
5. Foo foo = (Foo)o;
6. System.out.println("i = " + foo.i);
7. }
8.}
A.i=3
B.i = 0.
C.程序编译错误
D.程序运行时抛出异常

28、下面程序运行的结果是:
1. class Exc0 extends Exception { }
2. class Exc1 extends Exc0 { }
3. public class Test {
4. public static void main(String args[]) {
5. try {
6. throw new Exc1();
7. } catch (Exc0 e0) {
8. System.out.println("Ex0 caught");
9. } catch (Exception e) {
10. System.out.println("exception caught");
11. }
12. }
13. }
A.Ex0 caught
B.exception caught
C.编译失败,错误在第2行
D.编译失败,错误在第6行

29、下面表达式计算的结果和返回值类型分别是?
Math.ceil(0.1 + Math.floor(Math.random())); 0-1之间的小数
A.0.0
B.1.0
C.float
D.double
E.一个随机数

30、下面程序运行的结果是:
1.public interface Test {
2. int frood = 42;
3.}
4.c

lass TestImpl implements Test {
5. private static int frood;
6. public static void main(String[] args) {
7. System.out.println(++frood);
8. }
9.}
A.0
B.1
C.42
D.43

答题卡
1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

26 27 28 29 30


二、编程题
注意:书写清晰,看不清楚不给分,注意字体大小,写不下可以写在背面,标清题号。
1、输出n行n列的空心矩形(要求使用嵌套循环),当n=5时,运行结果显示如下:
#####
# #
# #
# #
#####
程序如下:
public class Test {
public static void main(String[] args) {
int n=5;
for (int i=1; i<=n; i++) {
for (int j=1;j<=n;j++){
if(i==1||i==n||j==1||j==n){ //只有正方形的边缘都为“#”
System.out.print("#");
}
else{ //其他地方都是空
System.out.print(" ");
}
}
System.out.println();
}
}
}
2、设计Java程序
假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
程序如下:
public class Test {
public static void main(String[] args){
int sum=0;
int empty=1;
for(int i=50; i>0; i--){
if(empty ==3){
empty =1;
i++;
}else{
empty++;
}
sum++;
}
System.out.println("总共喝了"+ sum +"瓶饮料。");
}
}

3、设计Java程序,实现如下功能:
获取50个0至300之间的随机整数,并输出到屏幕上;
取出上面50个整数中的偶数,倒序排列输出到屏幕上。
程序如下:
public class Test {
public static void main(String[] args){
int[] ary = {};
int even = 0;
for (int i = 0; i <50; i++) {
int num = (int)(Math.random()*300);
System.out.println("num:"+num);
if (num%2==0) {
ary = Arrays.copyOf(ary, ary.length+1);
ary[even++] = num;
}
}
Arrays.sort(ary);
for (int i = ary.length-1; i >=0; i--) {
System.out.println("偶数:"+ary[i]);
}
}
}
4、编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。
程序如下:
class Vehicle{
public Vehicle(){}

int wheels;
int weight;

public void print(){
System.out.println("汽车车轮个数是:"+wheels+",汽车车重为:"+weight+"。");
}
}
class Car extends Vehicle{

public Car(){
super();
}

int loader;
public void print(){
System.out.println("小车有载人数是:"+loader+"。");
}
}
class Truck extends Car{

public Truck(){
super();
}

int payload;
public void print(){
System.out.println("卡车有载重量是:"+loader+"。");
}


}
public class Test {
public static void main(String[] args){
Vehicle v1 = new Vehicle();
v1.print();
Vehicle v2 = new Car();
v2.print();
Vehicle v3 = new Truck();
v3.print();
}
}

相关文档
相关文档 最新文档