Java-基础题目集-Chapter 8,9 Multidimensional Arrays ,Objects and Classes

一.单选题

1.What is the output of the following program?(D

public class Test {
  public static void main(String[] args) {
    int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};

    for (int row = 0; row < values.length; row++) {
      System.out.print(m(values[row]) + " ");
    }
  }

  public static int m(int[] list) {
    int v = list[0];
    for (int i = 1; i < list.length; i++)
      if (v < list[i])
        v = list[i];
    return v;
  }
}

A.3 33

B.1 1

C.5 6

D.5 33

E.33 5

PS:不要误选B

2.What is the output of the following program?(E

public class Test {
  public static void main(String[] args) {
    int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};

    int v = values[0][0];
    for (int row = 0; row < values.length; row++)
      for (int column = 0; column < values[row].length; column++)
        if (v < values[row][column])
          v = values[row][column];

    System.out.print(v);
  }
}

A.1

B.3

C.5

D.6

E.33

PS:不要误选A

3.What is the output of the following program?(A

public class Test {
  public static void main(String[] args) {
    int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};

    int v = values[0][0];
    for (int[] list : values)
      for (int element : list)
        if (v > element)
          v = element;

    System.out.print(v);
  }
}

A.1

B.3

C.5

D.6

E.33

PS:不要误选E

4._____(B)_____ represents an entity in the real world that can be distinctly identified.

A.A class

B.An object

C.A method

D.A data field

答:对象表示现实世界中可以明确标识的实体。

5.___(A)____ is a construct that defines objects of the same type.

A.A class

B.An object

C.A method

D.A data field

答:类是定义相同类型的对象的构造。

6.An object is an instance of a ____(B)______.

A.program

B.class

C.method

D.data

答:对象是类的实例。

7.The keyword _____(C)_____ is required to declare a class.

A.public

B.private

C.class

D.All of the above.

答:class是声明类必需的。

8.____(A)____ is invoked to create an object.

A.A constructor

B.The main method

C.A method with a return type

D.A method with the void return type

答:调用构造函数来创建一个对象。

9.You should add the static keyword in the place of ? in Line ____(A)____ in the following code:

1 public class Test {
2   private int age;
3
4   public ? int square(int n) {
5     return n * n;
6   }
7
8   public ? int getAge() {
9  }
10}

A.in line 4

B.in line 8

C.in both line 4 and line 8

D.none

10.Analyze the following code:(B

class Circle {
  private double radius;

  public Circle(double radius) {
    radius = radius;
  }
}

A.The program has a compile error because it does not have a main method.

B.The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.

C.The program has a compile error because you cannot assign radius to radius.

D.The program does not compile because Circle does not have a default constructor.

答:程序将编译,但您无法创建具有指定半径的圆形对象。对象的半径将始终为 0。

11.Analyze the following code.(B)

class TempClass {
  int i;
  public void TempClass(int j) {
    int i = j;
  }
}

public class C {
  public static void main(String[] args) {
    TempClass temp = new TempClass(2);
  }
}

A.The program has a compile error because TempClass does not have a default constructor.

B.The program has a compile error because TempClass does not have a constructor with an int argument.

C.The program compiles fine, but it does not run because class C is not public.

D.The program compiles and runs fine.

答:程序有编译错误,因为 TempClass 没有具有 int 参数的构造函数。

12.Given the declaration Circle x = new Circle(), which of the following statement is most accurate.(C

A.x contains an int value.

B.x contains an object of the Circle type.

C.x contains a reference to a Circle object.

D.You can assign an int value to x.

答:x 包含对Circle对象的引用。

13.Analyze the following code.(E

public class Test {
  int x;

  public Test(String t) {
     System.out.println("Test");
  }

  public static void main(String[] args) {
    Test test = null;
    System.out.println(test.x);
  }
}

A.The program has a compile error because test is not initialized.

B.The program has a compile error because x has not been initialized.

C.The program has a compile error because you cannot create an object from the class that defines the object.

D.The program has a compile error because Test does not have a default constructor.

E.The program has a runtime NullPointerException because test is null while executing test.x.

答:该程序具有运行时空点异常,因为在执行 test.x 时测试为空。

14.Analyze the following code.(D

public class Test {
  int x;

  public Test(String t) {
     System.out.println("Test");
  }

  public static void main(String[] args) {
    Test test = new Test();
    System.out.println(test.x);
  }
}

A.The program has a compile error because System.out.println method cannot be invoked from the constructor.

B.The program has a compile error because x has not been initialized.

C.The program has a compile error because you cannot create an object from the class that defines the object.

D.The program has a compile error because Test does not have a default constructor.

答:程序出现编译错误,因为 Test 没有默认构造函数。

15.Variables that are shared by every instances of a class are _____(D)_____.

A.public variables

B.private variables

C.instance variables

D.class variables

答:由类的每个实例共享的变量是类变量。

16.A method that is associated with an individual object is called _____(C)_____.

A.a static method

B.a class method

C.an instance method

D.an object method

答:与单个对象关联的方法称为实例方法。

17.To declare a constant MAX_LENGTH as a member of the class, you write ___(E)____.

A.final static MAX_LENGTH = 99.98;

B.final static float MAX_LENGTH = 99.98;

C.static double MAX_LENGTH = 99.98;

D.final double MAX_LENGTH = 99.98;

E.final static double MAX_LENGTH = 99.98;

答:类中的常量是被类的所有对象所共享的。注意要有关键词final static和变量数据类型double 

18.Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate?(C

A.x contains an array of ten int values.

B.x contains an array of ten objects of the Circle type.

C.x contains a reference to an array and each element in the array can hold a reference to a Circle object.

D.x contains a reference to an array and each element in the array can hold a Circle object.

答:x 包含对数组的引用,数组中的每个元素都可以保存对 Circle 对象的引用。

19.What code may be filled in the blank without causing syntax or runtime errors:(A)

public class Test {
  java.util.Date date;

  public static void main(String[] args) {
    Test test = new Test();
    System.out.println(_________________);
  }
}

A.test.date

B.date

C.test.date.toString()

D.date.toString()

答:不能使用toString()的原因是没有创建一个指向date对象的引用。

20.Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _____(C)____ in the class.

public MyClass() {
  xMethod();
}

A.a static method

B.an instance method

C.a static method or an instance method

21.Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be?(C

A.public void getFinished()

B.public boolean getFinished()

C.public boolean isFinished()

D.public void isFinished()

答: getter 和 setter 称为 访问器方法 

22.Analyze the following code and choose the best answer:(A

public class Foo {
  private int x;

  public static void main(String[] args) {
    Foo foo = new Foo();
    System.out.println(foo.x);
  }
}

A.Since x is private, it cannot be accessed from an object foo.

B.Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code.

C.Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code.

D.You cannot create a self-referenced object; that is, foo is created inside the class Foo.

答:由于 x 是私有的,因此无法从对象 foo 访问它。

23.Analyze the following code:(C

public class Test {
  private int t;

  public static void main(String[] args) {
    int x;
    System.out.println(t);
  }
}

A.The variable t is not initialized and therefore causes errors.

B.The variable t is private and therefore cannot be accessed in the main method.

C.t is non-static and it cannot be referenced in a static context in the main method.

D.The variable x is not initialized and therefore causes errors.

E.The program compiles and runs fine.

答:t 是非静态的,不能在 main 方法中引用它。

二.多选题

1.Which of the following statements are true?(ABCD

A.Local variables do not have default values.

B.Data fields have default values.

C.A variable of a primitive type holds a value of the primitive type.

D.A variable of a reference type holds a reference to where an object is stored in the memory.

E.You may assign an int value to a reference variable.

答:局部变量没有默认值。

数据字段具有默认值。

基元类型的变量保存基元类型的值。

引用类型的变量保存对对象在内存中存储位置的引用。

2.To obtain the distance between the points (40, 50) and (5.5, 4.4), use ____(BCDE)_____.

A.distance(40, 50, 5.5, 4.4)

B.new Point2D(40, 50).distance(5.5, 4.4)

C.new Point2D(40, 50).distance(new Point2D(5.5, 4.4))

D.new Point2D(5.5, 4.4).distance(40, 50)

E.new Point2D(5.5, 4.4).distance(new Point2D(40, 50))

3.Which of the following statements are true about an immutable object?(ABDE

A.The contents of an immutable object cannot be modified.

B.All properties of an immutable object must be private.

C.All properties of an immutable object must be of primitive types.

D.An object type property in an immutable object must also be immutable.

E.An immutable object contains no mutator methods.

答:

不可变对象的所有属性必须是私有的。
不可变对象不包含任何更改器(mutator)方法。
不可变对象的内容不能修改。
不可变对象中的对象类型属性也必须是不可变的。

4.Which of the following statements are true?(ABD

A.Use the private modifier to encapsulate data fields.

B.Encapsulating data fields makes the program easy to maintain.

C.Encapsulating data fields makes the program short.

D.Encapsulating data fields helps prevent programming errors.

答:封装数据字段会使程序变得简短(X);

三.编程题

1.Central city

Given a set of cities, the central city is the city that has the shortest total distance to all other cities.

Write a program that reads the number of the cities and the locations of the cities (coordinates), and finds the central city and its total distance to all other cities.

输入格式:

第一行输入城市的个数;

第二行依次输入每个城市的位置坐标,以空格隔开。

输出格式:

第一行输出中心城市的坐标,格式为:(x, y)

第二行输出中心城市到其他城市的总距离。保留小数点后2位。

输入样例:

例如,5个城市和5组坐标值:

5
2.5 5 5.1 3 1 9 5.4 54 5.5 2.1

输出样例:

输出中心城市的坐标和中心到其他城市的总距离:

(2.5, 5.0)
60.81

重要提示:

程序主类的名字必须为Main。

解:

import java.util.Scanner;
import javafx.geometry.Point2D;

public class Main {
    public static void main(String[] args) {
        centralCity();
    }
    public static void centralCity() {
       Scanner sc = new Scanner(System.in);
       Point2D[] cities = new Point2D[sc.nextInt()];
       for (int i = 0; i < cities.length; i++)
           cities[i] = new Point2D(sc.nextDouble(), sc.nextDouble());
       Point2D centralCity = getCentralCity(cities);
       System.out.println("(" + centralCity.getX() + ", " + centralCity.getY() + ")");
       System.out.printf("%.2f",getDistance(cities, centralCity));
    }
    public static Point2D getCentralCity(Point2D[] cities) {
        double[][] distance = new double[cities.length][cities.length];
        double sum = 0, minDistances = 0;
        Point2D centralCity = cities[0];
        for (int i = 0; i < distance.length; i++)
            for (int j = 0; j < distance[i].length; j++) {
                distance[i][j] = cities[j].distance(cities[i]);
                if(i == 0) minDistances += distance[i][j];
            }

        for (int i = 0; i < distance.length; i++) {
            sum = 0;
            for (int j = 0; j < distance[i].length; j++)
                sum += distance[i][j];
            if(sum < minDistances) {
                minDistances = sum;
                centralCity = cities[i];
            }
        }
        return centralCity;
    }
    public static double getDistance(Point2D[] cities, Point2D centralCity) {
        double distance = 0;
        for (int i = 0; i < cities.length; i++)
            if(!centralCity.equals(cities[i]))
                distance += centralCity.distance(cities[i]);
        return distance;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxx_xiyuyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值