一.单选题

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

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

  1. java作业 温度换算,Java基础题目(温度转换、时间换算、信号报告)

    Java基础题目 题目主要来自浙江大学翁凯教授的零基础学习Java课程的配套习题. 1. 温度转换 题目内容: 写一个将华氏温度转换成摄氏温度的程序,转换的公式是: °F = (9/5)*°C + 3 ...

  2. Java 基础之类集

    平时我们用 List . Map 用得挺 Happy 的,只知道可以用它来保存数据,最近面试的时候问到这个 List 和 那个 List 的区别是什么呀, Vector 和 ArrayList 的区别 ...

  3. 昨晚看《天才知道》3进2的一个java基础题目有感:java是一种跨平台语言,它组成的2个部分是什么

    昨晚无意中看天才知道发现了这么个题目大概是这样: java是一种跨平台语言,它组成的2个部分是什么? 思考了一下,又看一次基础: java语言是一种基于"面向对象"思想的程序设计语 ...

  4. PTA里面怎么寻找JAVA题目_PTA基础题目集

    Java代码在上面,下面空两行是python 3 A/ _5 T$ |# c- t4 `% @, S9 I7-1 厘米换算英尺英寸 (15分)0 y; n0 J% u7 p 如果已知英制长度的英尺fo ...

  5. Java题目集-Chapter 10 Object-Oriented Thinking

    一.单选题 1. ______A_____ is attached to the class of the composing class to denote the aggregation rela ...

  6. python凯撒密码详解_Python基础题目集--课堂案例

    这些案例均为在校期间学习<python基础编程>课程的案例,适合初学者练手~ 1.输入分数,返回等级 ''' 分数区间为0~100,等级:A(>=90).B(>=80).C(& ...

  7. 【基础题目集】7-8 超速判断 (10分)

    题目链接:https://pintia.cn/problem-sets/14/problems/788 模拟交通警察的雷达测速仪.输入汽车速度,如果速度超出60 mph,则显示"Speedi ...

  8. PTA基础题目集 7-25 念数字 (15 分)

    输入一个整数,输出每个数字对应的拼音.当整数为负数时,先输出fu字.十个数字对应的拼音如下: 0: ling 1: yi 2: er 3: san 4: si 5: wu 6: liu 7: qi 8 ...

  9. Java-基础题目集-Chapter 2,3 Elementary Programming, Selections

    一.单选 1.Math.pow(2, 3) returns _____(D)_____. A.9 B.8 C.9.0 D.8.0 2.Are the following four statements ...

最新文章

  1. 常见的面试算法题:创建几个线程按顺序打印数字或者字母(多线程编程)(套用该模板即可)
  2. java反射泛型类型,【Java反射】Java 泛型基础
  3. Chrome 0 day漏洞利用链
  4. arm linux 添加ntp服务,嵌入式linux添加NTP服务
  5. c语言结构体编程,[编程] C语言的结构体详解
  6. python递归调用详解_Python递归调用自己的函数
  7. 3 FI配置-企业结构-分配-给公司分配公司代码
  8. Tcpdump抓包命令使用
  9. ios wifi 定位_iOS 中的三种定位方式
  10. 华为机试HJ100:等差数列
  11. LDAP密码认证例子
  12. Android -- Property Animation
  13. python isinstance()方法的使用
  14. java 新手入门电子书_Java基础入门电子书.pdf
  15. python安装pyaudio
  16. 穿透还原卡和还原软件的代码
  17. Ncurses学习经历(九)屏幕操作
  18. 大话设计模式:第27章 解释器模式
  19. 逆向PPTV视频真实播放地址全过程(Java版)
  20. voip 语音质量[摘]

热门文章

  1. 【Grasshopper基础11】如何在GH电池上增加一个自己的按钮
  2. 《Same Representation,Different Attentions:Shareable Sentence Representation》阅读笔记
  3. 复选框的全选、全不选,选中所有分项,全选自动选中
  4. 第六届中国云计算大会现场
  5. python枪_大疆机甲大师教育机器人Python API中文化之一:枪亮枪暗
  6. WinPhone学习笔记(一)——页面导航与页面相关
  7. springCloud的了解(四)—分布式事务问题
  8. 川大的计算机类和电气工程,四川大学网络教育学院电气工程及其自动化专业怎么样...
  9. mozilla 源码_每日新闻摘要:Mozilla的Firefox Premium计划
  10. 【文件系统制作】制作jffs文件系统