面向对象测试题

1.下列不属于面向对象编程的特性的是(  C )。

A.封装                         B.继承

C.抽象                         D.多态

2.(*)下述概念中不属于面向对象的是( D )。

A.对象                       B.继承、多态

C.类、封装                     D.过程调用

3.(*)对象是Java中很重要的概念,下列说法中能准确地描述对象的是( A   )。

A.对象是类的具体实例,可以操作数据的方法

B.对象是抽象的,类可以通过对象来生成

C.对象只是方法的集合

D.对象是一组具有共同的结构和行为的类

4.(*)下面有关变量及其作用域的陈述哪一项是错误的?(D

A.在方法里面定义的局部变量在方法退出的时候被撤销

B.局部变量只在定义它的方法内有效

C.在方法外面定义的实例变量在对象被构造时创建

D.在方法中定义的方法的参变量只要该对象被需要就一直存在

5.下列方法的声明中不合法的是(B )。

A.float play(){ return 1; }

B.void play(int d,e) { }

C.double play(int d) { return 2.0; }

D.int play(int r) { return 1; }

6.下列哪个方法不能与方法public void add(int a){ }重载?(A

A.public int add(int b) { }

B.public void add(double b) { }

C.public void add(int a, int b) { }

D.public void add(float g) { }

7.类Test定义如下:

1.public class Test {

2.    float use(float a, float b) {

3.    }

4.  

5.}

将以下哪种方法插入第4行是不合法的?( B

A.float use(float a, float b, float c) {  }

B.float use(float c, float d) {  }

C.int use(int a,int b) {  }

D.float use(int a, int b, int c) {  }

 

 

8.为了区分重载多态中同名的不同方法,要求(   A )。

A.采用不同的参数列表

B.返回值类型不同

C.调用时用类名或对象名做前缀

D.参数名不同

9.下列有关构造方法描述正确的是(    D)。

A.所有类都必须定义一个构造方法

B.构造方法必须有返回值

C.构造方法必须访问类的非静态成员

D.构造方法可以初始化类的成员变量

10.下列关于构造方法的叙述中,错误的是(C   )。

A.构造方法名与类名必须相同

B.构造方法没有返回值,但不用void声明

C.构造方法不可以重载

D.构造方法只能通过new自动调用

11.设A为已定义的类名,下列声明对象a的语句中正确的是(C)。

A.public A a =new A( );

B.A a = A( );

C.A a = new A();

D.a A;

12.给出如下类定义:

public class Test {

Test(int i) {

}

}

如果要创建一个该类的对象,正确的语句是( B )。

A.Test t = newTest();

B.Test t = newTest(5);

C.Test t = newTest("5");

D.Test t = newTest(3.4);

13.以下代码的调试结果为(C  )。

class Square {

    int a;

    void Square() {

        a = 10;

    }

    public static void main(String[] args) {

        Square s = new Square();

        System.out.println(s.a);

    }

}

A.输出10                       B.编译错误

C.输出0                        D.运行错误

14.下面程序的输出结果是()。

class Test {

    int i = 2;

    String s = null;

    void Test() {

        i = 3;

        s = "days";

    }

    public static void main(String args[]) {

        Test t = new Test();

        System.out.println(t.i + t.s);

    }

}

A.2null                        B.3null

C.3days                        D.以上都不对

15.编译并运行下面的Java程序,将产生什么结果?(D  )

class A {

    int i = 1;

    int j;

    public static void main(String[] args) {

        int k;

        A a = new A();

        System.out.println(a.i + a.j + k);

    }

}

A.0                            B.1

C.2                            D.出现编译错误

16.如下代码的输出结果是(  C )。

public class Test {

    void printValue(int m) {

        do {

            System.out.println("The value is " + m);

        } while (--m > 10);

    }

    public static void main(String arg[]) {

        int i = 10;

        Test t = new Test();

        t.printValue(i);

    }

}

A.The value is8               B.Thevalue is 9

C.The value is10              D.Thevalue is 11

17.如下代码的输出结果是(  D )。

class Point {

    int x, y;

    Point(int a, int b) {

        x = a;

        y = b;

    }

}

class Test {

    public static void main(String[] args) {

        Point p1, p2;

        p1 = new Point(3, 6);

        p2 = new Point(8, 9);

        p1 = p2;

System.out.println("p1.x= " + p1.x + ", p1.y = " + p1.y);

    }

}

A.p1.x = 3, p1.y = 6           B.p1.x = 3, p1.y= 9

C.p1.x = 8, p1.y = 6           D.p1.x = 8, p1.y= 9

二、简答题

1.简述面向对象编程的三个主要特征。

封装:客观事物封装成抽象的类

继承:子类继承父类的所有特征和行为

多态:父类引用变量可以指向子类对象

2.什么是方法的重载?

在同一个类中定义多个同名的方法,

方法名一样,参数类型或者个数不一样

3.简述成员变量和局部变量的区别?

成员变量:作为类的成员直接存在类中,可以通过this引用

局部变量:在方法定义或声明上

内存位置不同:成员变量在堆,局部变量在栈

成员变量有默认值初始化

局部变量没有,必须定义赋值才能

4.在写好java文件后,编译时会编译java文件中的注释代码吗?

不会   只识别代码

5.什么是方法的重写?

子类继承父类方法,方法名一样,参数列表一样

而且只能重写父类非私有的方法

三、编程题

1.定义立方体类Cube,具有属性边长和颜色,具有方法设置颜色和计算体积,在该类的主方法中创建一个立方体对象,将该对象的边长设置为3,颜色设置为“green”,输出该立方体的体积和颜色。4

public class Cube {

     static double side;

     static String color;

 

     Cube(double side){

         this.side=side;

     }

     public static double bulk(){

         return side*side*side;

     }

     public static void setColor(String c){

         color=c;

     }

}

public class Test {
 

     public static void main(String[] args) {

         Cube c=new Cube(4);

         c.setColor("green");

         System.out.println("立方体的体积为:"+c.bulk());

         System.out.println(" 颜色为:"+c.color);

        

     }

 

}

2.编写Java应用程序,该程序中有梯形类和主类。要求如下:8

(1)梯形类具有属性上底、下底、高和面积,具有返回面积的功能,在构造方法中对上底、下底和高进行初始化。

(2)主类用来测试梯形类的功能。

 1 public class Ladder {
 2 
 3      double upper;
 4 
 5      double bottom;
 6 
 7      double height;
 8 
 9      double area;
10 
11      public Ladder(double upper, double bottom, double height) {
12 
13          super();
14 
15          this.upper = upper;
16 
17          this.bottom = bottom;
18 
19          this.height = height;
20 
21      }
22 
23      //返回面积
24 
25      public double getArea(){
26 
27          area=(upper+bottom)*height/2;
28 
29          return area;
30 
31      }
32 
33 }
34 
35 测试类:
36 
37 public class Zhu {
38 
39      public static void main(String[] args) {
40 
41          Ladder lad=new Ladder(2, 5, 9);
42 
43          System.out.println("梯形的面积为:"+lad.getArea());
44 
45      }
46 
47 }

3.按要求编写Java应用程序:8

(1)定义描述学生的类Student,有一个构造方法对属性进行初始化,一个outPut方法用于输出学生的信息。

(2)定义主类,创建两个Student类的对象,测试其功能。

 1 public class Student {
 2 
 3      String name;
 4 
 5      String sex;
 6 
 7      int age;
 8 
 9      public Student(String name, String sex, int age) {
10 
11          super();
12 
13          this.name = name;
14 
15          this.sex = sex;
16 
17          this.age = age;
18 
19      }
20 
21      public void outPut(){
22 
23          System.out.println("学生的姓名为:"+this.name+"	性别为:"+this.sex+"	年龄为:"+this.age);
24 
25      }
26 
27 }
28 
29 public class TestStu {
30 
31      public static void main(String[] args) {
32 
33          Student stu=new Student("夏目", "男", 20);
34 
35          stu.outPut();
36 
37          Student stu1=new Student("娘口三三", "男", 18);
38 
39          stu1.outPut();
40 
41      }
42 
43 }

4.(*)定义机动车类,具有属性:车牌号、车速和载重量,具有功能:加速、减速。一个构造方法没有形参,在方法中将车牌号设置为“辽A1234”,速度为100,载重量为50;另一个构造方法用形参为对象的所有属性赋值。5

在主类中创建两个机动车对象,创建第一个时调用无参数的构造方法,使其加速10。创建第二个时调用有参数的构造方法,使其车牌号为“辽B5678”,车速为80,载重量为20,并让其减速20。最后输出两辆车的所有信息。

 1 public class Car {
 2 
 3      String num;
 4 
 5      double speed;
 6 
 7      double weight;
 8 
 9      double speedUp;
10 
11      double speedDown;
12 
13      Car(){
14 
15          num="辽A1234";
16 
17          speed=100;
18 
19          weight=50;
20 
21      }
22 
23      Car(String n, double s, double w) {
24 
25          num=n;
26 
27          speed=s;
28 
29          weight=w;
30 
31      }
32 
33      public void speedUp(int s) {
34 
35          speed+=s;
36 
37         
38 
39      }
40 
41      public void speedDown(int d){
42 
43          speed-=d;
44 
45      }
46 
47     
48 
49 }
50 
51  
52 
53  
54 
55 public class TestCar {
56 
57      public static void main(String[] args) {
58 
59          Car c=new Car();
60 
61          c.speedUp(10);
62 
63          Car c1= new Car("辽B5678",80, 20);
64 
65          c1.speedDown(20);
66 
67          System.out.println(c.num + "的车速为" +c.speed + ", 载重量为" +c.weight);
68 
69          System.out.println(c1.num + "的车速为" +c1.speed + ", 载重量为" +c1.weight);
70 
71      }
72 
73 }

5.(*)按要求编写Java应用程序。9

(1)定义Teacher类,具有属性:教师的姓名、年龄、教授的课程以及是否担任班主任,方法printInfo输出教师的姓名、年龄和所教授的课程,构造方法为成员变量赋值。
(2)定义Team类,有属性:班级名称和班主任,有一个输出班级名称的方法,另一个是构造方法,为成员变量赋值。
(3)定义主类TestTeam,创建一个班级对象,输出班级名称和班主任的详细信息。

public class Teacher {
  
     String name;

     String coures;

     int age;

     boolean  Tutor;

     public Teacher(String name, String coures, int age,boolean Tutor) {

         super();

         this.name = name;

         this.coures = coures;

         this.age = age;

         this.Tutor=Tutor;

     }

     public void printlnfo(){

         System.out.println("老师的姓名为:"+this.name+"		年龄为:"+this.age+"	教授课程为:"+this.coures);

     }

}

 

 

public class Team {

     String classname;

     Teacher tutor;

     public Team(String classname, Teacher tutor) {

         super();

         this.classname = classname;

         this.tutor = tutor;

     }

     public void className(){

         System.out.println("班级名称为:"+this.classname);

     }

}

 

 

public class TestTeam {

     public static void main(String[] args) {

         Teacher tea=new Teacher("薛之谦", "音乐", 33, true);

         Team tea1=new Team("金曲捞", tea);

         tea1.className();

         tea1.getTutor().printlnfo();

     }

}
原文地址:https://www.cnblogs.com/2734156755z/p/9396422.html