最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

JAVA基础 第3章类与对象_练习题

来源:动视网 责编:小OO 时间:2025-09-29 06:39:49
文档

JAVA基础 第3章类与对象_练习题

第3章类与对象一.选择题1.下列不属于面向对象编程的特性是(D)。A.封装性B.继承性C.多态性D.编译执行2.下列类的声明中不合法的是(C)。A.classPeople{…}B.class植物{…}C.ClassA{…}D.publicclass共有类{…3.下列方法的声明中不合法的是(C)。A.floatarea(){…}B.voidarea(){…}C.doublearea(d){…}D.intarea(intr){…}4.下列构造方法(构造器)的调用中正确的是(C)。A.按照一般的方法
推荐度:
导读第3章类与对象一.选择题1.下列不属于面向对象编程的特性是(D)。A.封装性B.继承性C.多态性D.编译执行2.下列类的声明中不合法的是(C)。A.classPeople{…}B.class植物{…}C.ClassA{…}D.publicclass共有类{…3.下列方法的声明中不合法的是(C)。A.floatarea(){…}B.voidarea(){…}C.doublearea(d){…}D.intarea(intr){…}4.下列构造方法(构造器)的调用中正确的是(C)。A.按照一般的方法
第3章  类与对象

一.选择题

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

A.封装性          B. 继承性          C. 多态性          D. 编译执行

2.下列类的声明中不合法的是(C)。

A. class People{…}             B. class 植物{…} 

C. Class A{…}                  D. public class 共有类{…

3.下列方法的声明中不合法的是(C)。

A. float area(){…}              B. void area(){…} 

C. double area(d){…}           D. int area(int r){…}

4. 下列构造方法(构造器)的调用中正确的是(C)。

A. 按照一般的方法调用             B. 由用户直接调用 

C. 只能通过new自动调用           D. 被系统调用

5.下列程序运行的结果是(A)。

     class Book{

        int width;

        int length;

    }

public class A{

        static void f(Book p){

            p.width=20;

            p.length=40;

        }

        public static void main(String args[]){

            Book b=new Book();

            b.width=10;

            b.length=20;

            f(b);

            System.out.print(" "+b.width);

            System.out.print(" "+b.length);

        }

}

A. 20 40         B. 10 40         C. 10 20        D. 以上都不对

6.下列程序运行的结果是(D)。

public class A{     

        static void f(int y){

            y=y+10;

        }

        public static void main(String args[]){

             double x=10;

             f(x);

             System.out.println(x);

        }

}

A. 10           B. 20        C. 10.0               D. 程序编译错误

7.下列程序运行的结果是(C)。

public class A{

        int z=20;

        static void f(int y){

            y=z;

            System.out.println(y);

        }

        public static void main(String args[]){

             f(10);    

        }

}

A. 10           B. 20       C. 程序编译错误       D. 以上都不对

8. 以下代码的输出结果为(C)。

public class Pass{

          static int j=20;

          public static void main(String args[]){

              int i=10;

              Pass p = new Pass();

              p.amethod(i);

              System.out.println(i+" and "+j);

           }

           public void amethod(int x){

              x=x*2;

              j=j*2;

        }

     }

A.错误: 方法参数与变量不匹配

B. 20 and 40

C. 10 and 40

D. 10 and 20

9. 编译和运行程序会出现什么样的结果(A)。

public class Ref{

     public static void main(String args[]){

         Ref r = new Ref();

         r.amethod(r);

      }

           public void amethod(Ref r){

               int i=99;

               multi(r);

               System.out.println(i);

            }

           public void multi(Ref r){

               r.i = r.i * 2;

           }

}

A.编译出错            B. 输出:99          C. 输出:198           D. 运行出错

10. 关于以下程序代码的说明正确的是(D)。

1.class  HasStatic{

2.    static  int  x=100; int y=0;

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

11. 有如下代码:

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);

           }

}

其输出结果是什么(C)。

A. The value is 8               B. The value is 9

C. The value is 10              D. The value is 11

12. 以下代码的调试结果为(D)。

    1. public class Q21

    2. {

    3.      int maxElements;

    4.   

    5.      void  Q21()

    6.      {

    7.          maxElements = 100;

    8.          System.out.println(maxElements);

    9.      }

    10.    

    11.     Q21(int i)

    12.     {

    13.          maxElements = i;

    14.          System.out.println(maxElements);

    15.     }

    16.

    17.     public static void main(String[] args)

    18.     {

    19.          Q21 a = new Q21();

    20.          Q21 b = new Q21(999);

    21.      }

    22.  }

A. 输出 100 和 999

B.输出 999 和 100

C.第3行出现编译错误, 变量 maxElements 未初始化

D. 第19行出现编译错误

13. 给出如下类定义:

public class test {

           test(int k) {…}

}

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

A. test obj1=new test();                  B. test obj1=new test(5);

C. test obj1=new test("5 ");              D. test obj1=new test(3.4);

14. 指出下列哪个方法不能与方法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)

15.下面程序的输出结果是什么(C)。

class J_Test{

        int m_i = 2;

        String m_s = null;

        J_Test(){

            m_i = 3;

            m_s = "constructor";

        }

        public static void main(String args[]){

            J_Test  app = new  J_Test();

            System.out.println(app.m_i+app.m_s);

        }

}

A. 2null           B. 3null          C. 3constructor          D. 以上都不对

16. 下面程序的输出结果是什么(A)。

class J_Test{

        int m_i = 2;

        String m_s = null;

        void J_Test(){

            m_i = 3;

            m_s = "constructor";

        }

        public static void main(String args[]){

            J_Test app = new J_Test();

            System.out.println(app.m_i+app.m_s);

        }

}

A. 2null           B. 3null          C. 3constructor          D. 以上都不对

17.下列代码的运行结果是(D)。

public class J_Test{

        static short m_index;

        static int m_age;

        public static void mb_setData(long n){

            m_index = n;

        }

        public static void main(String args[]){

            mb_setData(98);

            System.out.println("Index=" + m_index + ";Age="+m_age);

        }

}

A. Index = 98 ;Age=0                 B.  Index = 0 ;Age=0

C. Index = 0;Age=98                  D. 以上都不对

18.下列程序的运行结果为(B)。

     public class J_Test{

        static char m_name;

        static int m_age;

public static void mb_setData(char n){

            m_name = n;

        }

        public static void mb_setData(int n){

            m_age = n;

        }

        public static void main(String args[]){

            mb_setData((int)’a’);

mb_setData((char)98);

            System.out.println("Name=" + m_name + ";Age="+m_age);

        }

}

A.Name=98;Age=97                   B. Name=b;Age=97  

C. Name=97;Age=B                    D. 以上都不对

19. 程序的运行结果为(A)。

     public class J_Test{

        static short m_index;

        static int m_age;

public static void mb_setData(short n){

            m_index = n;

        }

        public static void mb_setData(int n){

            m_age = n;

        }

        public static void main(String args[]){

mb_setData(98);

            System.out.println("Index=" + m_index + ";Age="+m_age);

        }

}

A. Index=0;Age=98                   B. Index=98;Age=0

C. Index=0;Age=0                    D. 以上都不对

二.编程题(温馨提示:编程时尽量不要使用中文标识符。)

1. 编写一个Java应用程序,该程序中有3个类:Lader、Circle和主类A。

具体要求如下:

1)Lader类具有类型为double的上底、下底、高、面积属性,具有返回面积的功能,

包括一个构造方法对上底、下底、高进行初始化。

2)Circle类具有类型为double的半径、周长和面积属性,具有返回周长、面积的

功能,包括一个构造方法对半径进行初始化。

3)创建主类,用来测试类Lader和类Circle的功能。

1. class  Lader {

        double 上底, 下底, 高, 面积;

        Lader(double a, double b, double h) {

            上底 = a;

            下底 = b;

            高 = h;    

}

        double getArea(){

    //将计算结果存放在对象的成员变量里面,且作为函数值返回。

            面积 = (上底 + 下底) * 高 / 2;

            return 面积;

        }

}

class  Circle {

        double 半径, 周长, 面积;

        Circle(double r) {

    半径 = r;

         }

        double getLength() {

          周长 = 3.14 * 2 * 半径;

            return  周长; 

        }    

        double getArea() {

            面积 = 3.14 * 半径 * 半径;

return 面积;

        }

}

class One {

    public static void main(String[] args) {

        Lader la = new Lader(3,4,2);    //参数类型相同或相容

        la.getArea();      //必须先调用该方法,面积属性才有相应值。

        System.out.println("梯形的面积为:" + la.面积);

        Circle cr = new Circle(1);

        System.out.println("圆的周长为:" + cr.getLength());

        System.out.println("圆的面积为:" + cr.getArea());

    }

}

2. 建立一个名叫Cat的类,具有属性:姓名、毛色、年龄;具有行为:显示姓名、喊叫。

编写主类,在主类中创建一个对象猫,姓名为“妮妮”,毛色为“灰色”,年龄为

2岁,在屏幕上输出该对象的毛色和年龄,让该对象调用显示姓名和喊叫两个方法。

2.class Cat {

        String name;

        String color;

        int age;

        void showName(){

            System.out.println(name);

        }

        void cry(){

            System.out.println("喵喵");

        }

}

public class Two {

        public static void main(String[] args) {

            Cat c = new Cat();

            c.name="妮妮";

            c.color="灰色";

            c.age=2;

            System.out.println("color = " + c.color);

            System.out.println("age = " + c.age);

            c.showName();

            c.cry();

        }

}

3. 创建一个叫做People的类,具有属性:姓名、年龄、性别、身高;具有行为:说话、

计算加法、改名;还有能为所有属性赋值的构造方法。

创建主类,在主类中创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80;

让该对象调用成员方法:说出“你好!”,计算23+45的值,并将名字改为“李四”。

3. class People {

        String name;

        int age;

        char sex;

        float height;

    

        People(String n, int a, char s, float h){

            name = n;

            age = a;

            sex = s;

            height = h;

        }

        void speak(String s){

            System.out.println(s);

        }

        double add(double x, double y){

            return x+y;

        }

        void rename(String m){

            name = m;

        }

}

public class Three {

        public static void main(String[] args) {

            People zhang3 = new People("张三",18,'男',1.80f);

            zhang3.speak("你好!");

            System.out.println("23+45=" + zhang3.add(23,45));

            zhang3.rename("李四");

            System.out.println(zhang3.name);

        }

}

4. 创建一个叫做机动车的类:

属性:车牌号(String),车速(int),载重量(double)。

功能:加速(车速自增)、减速(车速自减)、修改车牌号,查询车的载重量。

编写两个构造方法:一个没有形参,在方法中将车牌号设置“XX1234”,速度设置

为100,载重量设置为100;另一个能为对象的所有属性赋值。

创建主类:

创建两个机动车对象。创建第一个时调用无参数的构造方法,调用成员方

法使其车牌为“辽A9752”,并让其加速。

创建第二个时调用有参数的构造方法,使其车牌为“辽B5086”,车速为

150,载重为200,并让其减速。输出两辆车的所有信息。

4. class Vehicle{

    String id;

    int speed;

    double load;

    void upSpeed(){

        ++speed;

    }

    void downSpeed(){

        --speed;

    }

    void idChanging(String i){

        id = i;

    }

    double showLoad(){

        return load;

    }

    Vehicle(){

        id = "XX1234";

        speed = 100;

        load = 100;

    }

    Vehicle(String i, int s, double l){

        id = i;

        speed = s;

        load = l;

    }

}

public class Four {

    public static void main(String[] args) {

        Vehicle v1 = new Vehicle();

        v1.idChanging("辽A9752");

        v1.upSpeed();

        Vehicle v2 = new Vehicle("辽B5086",150,200);

        v2.downSpeed();

        System.out.println(v1.id+

        System.out.println(v2.id+

    }

}

5. 创建一个point类,有成员变量x、y,方法getX()、setX(),还有一个构造方法用于初

始化x和y。创建主类来测试它。

5. class Point {

        double x,y;

    

        Point(double x, double y){

            this.x=x;

            this.y=y;

        }        

        public double getX() {

            return x;

        }

        public void setX(double x) {

            this.x = x;

        }

}

public class Five {

        public static void main(String[] args) {

            Point p = new Point(1,2);

            p.setX(3);

            System.out.println(p.getX());

        }

}

6. 创建一个三角形类,有成员变量三条边,有方法求周长,创建主类来测试它。

6. class Triangle{

        double a,b,c;

        double getLength(){

            return a+b+c;

        }

}

public class Six {

        public static void main(String[] args) {

            Triangle t = new Triangle();

            t.a = 2;

            t.b = 3;

            t.c = 4;

            System.out.println("周长=" + t.getLength());

        }

}

7. 编写Java应用程序。首先,定义一个Print类,它有一个方法void output(int x),

1)如果x的值是1,在控制台打印出大写的英文字母;

2)如果x的值是2,在控制台打印出小写的英文字母。

其次,再定义一个主类,在主类的main方法中创建Print类的对象,使用这个对象调

用方法output ()来打印出大小写英文字母。

7. class Print{

    void output(int x){

        char c;

        if (x==1){

         for(c='A';c<='Z';c++)

                System.out.print(c);

        }

        if (x==2){

         for(c='a';c<='z';c++)

                System.out.print(c);

        }

        System.out.println();

    }

}

public class Seven {

    public static void main(String[] args) {

        Print p = new Print();

        p.output(1);

        p.output(2);

    }

}

8. 编写Java应用程序。首先定义一个描述银行账户的Account类,包括成员变量“账号”

   和“存款余额”,成员方法有“存款”、“取款”和“余额查询”。其次,编写一个主类,

在主类中测试Account类的功能。

class Account {

    String id;

    double money;

    void save(double x) {

        money = money + x;

    }

    void get(double y) {

        money = money - y;

    }

  void select() {

      System.out.println("余额=" + money);

  }

}

public class Eight {

        public static void main(String[] args) {

            Account kaien = new Account();

            kaien.save(10000);

            kaien.get(200);

            kaien.select();    

        }

}

9. 编写Java应用程序。首先,定义一个时钟类——Clock,它包括三个int型成员变量

分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒)进行初始化,

还有一个成员方法show()用于显示时钟对象的时间。其次,再定义一个主类,在主类

的main方法中创建多个时钟类的对象,使用这些对象调用方法show()来显示时钟的时

间。

9. class Clock{

        int hour,second,minute;

        Clock(int h, int s, int m){

            hour = h;

            minute = m;

            second = s;

        }

        void show(){

            System.out.println(hour + ":" + minute + ":" + second);

        }

}

public class Nine {

        public static void main(String[] args) {

            Clock c1 = new Clock(21,16,30);

            c1.show();

            Clock c2 = new Clock(12,12,12);

            c2.show();

            Clock c3 = new Clock(3,3,3);

            c3.show();

        }

}

10.编写Java应用程序。首先,定义描述学生的类——Student,包括学号(int)、姓名

(String)、年龄(int)等属性;二个方法:Student(int stuNo,String name,int age)

用于对象的初始化,outPut()用于输出学生信息。其次,再定义一个主类,在主类的

main方法中创建多个Student类的对象,使用这些对象来测试Student类的功能。

10.class Student{

int id;

String name;

int age;

Student(int i, String n, int a){

id = i;

      name = n;

      age = a;

}

void outPut(){

      System.out.println("学号=" + id + "; 姓名=" + name + "; 年龄=" + age);

}    

}

public class Ten {

        public static void main(String[] args) {

            Student s1 = new Student(1,"Li",18);

            s1.outPut();

            Student s2 = new Student(2,"Wang",19);

            s2.outPut();

            Student s3 = new Student(3,"Jiang",20);

            s3.outPut();

        }

}

文档

JAVA基础 第3章类与对象_练习题

第3章类与对象一.选择题1.下列不属于面向对象编程的特性是(D)。A.封装性B.继承性C.多态性D.编译执行2.下列类的声明中不合法的是(C)。A.classPeople{…}B.class植物{…}C.ClassA{…}D.publicclass共有类{…3.下列方法的声明中不合法的是(C)。A.floatarea(){…}B.voidarea(){…}C.doublearea(d){…}D.intarea(intr){…}4.下列构造方法(构造器)的调用中正确的是(C)。A.按照一般的方法
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top