Java面向对象习题

  1. 有关Java中的类和对象,以下说法错误的是 (B)
    A. 同一个类的所有对象都拥有相同的特征和行为
    B. 类和对象一样,只是说法不同
    C. 对象是具有属性和行为的实体
    D. 类规定了对象拥有的特征和行为

  2. 在java中,以下程序的运行结果是 (A)
    package lianxi;
    class Perosn {
    public String name;
    }
    public class Test {
    public static void main(String[] args) {
    Perosn person = new Perosn();
    System.out.println(person.name);
    }
    }

A. 输出:null
B. 正常运行,但不会输出任何内容
C. 编译出错,不能运行
D. 能运行,但运行时会出现异常

  1. 下面代码运行的正确结果是 (D)
    class Phone {
    String message;
    }
    public class PhoneTest {
    public static void main(String[] args) {
    Phone one = new Phone();
    one.message = “hello”;
    Phone two = one;
    two.message = “world”;
    System.out.println(one.message);
    }
    }

A. 编译错误,无法正常运行
B. 编译正确,但运行时产生错误
C. hello
D. world

  1. 哪个空间用于存储使用new关键字所创建的对象 (B)
    A. 堆
    B. 栈
    C. 方法区
    D. 实例区

  2. 分析下面的Java代码,编译运行结果是 ©
    public class Test{
    int number;
    String strname;
    Test(int num, String name) {
    number = num;
    strname = name;
    System.out.println(“学号:” + num + “姓名:” + name);
    }
    public static void main(String[] args) {
    Test obj1 = new Test();
    Test obj2 = new Test(1, “张三”);
    }

A. 运行结果为:学号:1 姓名:张三
B. 运行结果为:学号:null 姓名:张三
C. 程序出现编译错误
D. 程序出现运行时异常

  1. 下面的哪几项是合法的构造方法重载(多选)(A C)
    public class Display{
    //…
    }

A. public Display(String s){}
B. public int display(int n1,int n2){}
C. Display (int …a){}
D. public display(Strnig s,int a){}

  1. 运行结果为() (A)
    public class Test{
    String mainboard,cpu;
    public Test(String s1, String s2) {
    mainboard = s1;
    cpu = s2;
    }
    public static void main(String[] args) {
    Test c = new Test(“华硕”,“intel”);
    System.out.println(“mainboard:” + c.mainboard + “,cpu:” +c.cpu);
    }
    }

A. mainboard:华硕,cpu:Intel
B. mainboard:s1,cpu:s2
C. mainboard:Intel,cpu:华硕
D. 华硕,Intel

  1. 在Java中,以下程序编译运行后的输出结果为( ) (D)
    public class Test {
    int x, y;
    Test(int x, int y) {
    this.x = x;
    this.y = y;
    }
    public static void main(String[] args) {
    Test pt1, pt2;
    pt1 = new Test(3,3);
    pt2 = new Test(4,4);
    System.out.println(pt1.x + pt2.x);
    }
    }

A. 6
B. 3 4
C. 8
D. 7

  1. 在Java中,下列关于this的说法错误的选项是(多选))(B C)
    A. 在构造方法中如果使用this调用其他构造方法,只能是第一条语句
    B. 不能在构造方法中调用同一个类的其他构造方法
    C. 在构造方法在中如果使用this调用其他构造方法,语句可以放在任意位置
    D. 可以使用“this.方法名()”或“this.属性名”来引用当前对象的成员

二、编程

  1. 创建Person类和测试类
    属性:名字(name),年龄(age),年级( grade)
    方法:
    无参无返回值的student方法,描述为:我是一名学生!
    带参数(性别sex)的方法,描述为:我是一个**孩!(其中,**为传入参数)
    无参无返回值的mySelf方法,介绍自己的姓名、年龄、年级(参数参考效果图)

public class Test {
public static void main(String[] args) {
//使用new关键字实例化对象

//传入name、age、grade的参数值
//分别调用student、sex、mySelf方法 } }
在这里插入图片描述
在这里插入图片描述

  1. 编写自定义猴子类

public class Monkey {
//属性:姓名(name)、特征(feature)

//无参的构造方法(默认初始化name和feature的属性值,属性值参考效果图)

//带参的构造方法(接收外部传入的参数,分别向 name 和 feature 赋值) }
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/hgfhjtff/article/details/105951992