# Tentative Topical Coverages for Mid Term
Topic(s) | Suggested Readings |
---|---|
Java SE Essentials | Chapters 1-5 (review of essentials) |
Object-Oriented Programming (OOP) concepts | Chap. 6 |
Arrays and Arraylists | Chap. 7 |
Abstraction, Inheritance, Polymorphism | Chap. 10 |
Stream I/O file processing, Exception Handling | Chap. 9,11 |
Generics, Collections (intro only) | Chap. 17,18 |
# Sample
Which of these is an incorrect array declaration?
哪一个是不正确的数组声明?
What will this code print?
int arr[] = new int [5];
System.out.print(arr[0]);
What is the output of this program?
class array_output {
public static void main(String args[]) {
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
Look at the following code.
Integer myNumber = new Integer(5);
int var = myNumber;
What is true about the second statement?
详解 Java 的自动装箱与拆箱 (Autoboxing and unboxing)
Which is the valid declaration within an interface definition?
在接口定义中,哪个是有效声明?
A private base method can be overridden by some derived public method.
私有基础方法可以被某些派生的公共方法覆盖。
Which of these keywords is not a part of exception handling?
这些关键字中的哪个不是异常处理的一部分?
The process of converting one date type to another is called .
将一种日期类型转换为另一种日期的过程称为
Which of the following is the correct declaration statement in java program?
以下哪项是 Java 程序中正确的声明语句?
What line of code should replace the missing statement to make this program compile?
/* Missing Statement ? */
public class foo {
public static void main(String[]args) throws Exceptionn {
java.io.PrintWriter out = new java.io.PrintWriter();
new java.io.OutputStreamWriter(System.out);
out.println("Hello");
}
}
哪一行代码应替换丢失的语句以使该程序得以编译?
method is used to find the nth no. of character of given string s1.
方法用于查找给定字符串 s1 的第 n 个字符。
Which of these can be overloaded?
哪些可以被重载?
The concept of multiple inheritance is implemented in Java by?
I. Extending two or more classes.
II. Extending one class and implementing one or more interfaces.
III. Implementing two or more interfaces.Java 的多重继承概念是通过?实现的
Private members of a class are inherited in the sub class.
类的私有成员在子类中被继承。
A method of a super class with a default access modifier can be overridden as protected or public but not as private.
具有默认访问修饰符的父类方法,可以覆盖为 protected 或 public,但不能是 private。
What will be the output of the following program?
class X {
void method(int a) {
System.out.println("ONE");
}
void method(double d) {
System.out.println("TWO");
}
}
class Y extends X {
@Override
void method(double d) {
System.out.println("THREE");
}
}
public class MainClass {
public static void main(String[] args) {
new Y().method(100);
}
}
If a superclass does not have a default constructor, then a class that inherits from it,
如果父类没有默认的构造函数,则继承自它的类
The binary search algorithm
二进制搜索算法
# Coding and Definitions
What are some advantages of using Generic types? List some example.
使用泛型类型有哪些优势?列举一些例子。
Advantages: use cases - Type safety at runtime + avoids costs (code duplication!).
优点:用例 - 运行时类型安全 + 避免成本(代码重复!)。
Ex. Casting issues give compile time errors if elements aren't proper possible generic datatypes.
例如,如果元素不是适当的泛型数据类型,则强制转换问题会导致编译时错误。
Generics work around this by typechecking at compile time to prevent ClassCastExceptions!
泛型通过在编译时进行类型检查,来避免 ClassCastExceptions,来解决此问题!
Without generics/typecheck
没有泛型 / 类型检查Why do you suppose a class level generic variable cannot be static?
为什么类级别的泛型变量不能为静态变量?
Using generics, type parameters are not allowed to be static, as the static variable is shared amongst objects so compiler can not determine which type to used.
使用泛型时,类型参数不允许为静态,因为静态变量在对象之间共享,因此编译器无法确定要使用哪种类型。What are some basic differences between an Array and ArrayList?
Array 和 ArrayList 之间有哪些基本区别?
The array represents a basic functionality whereas the ArrayList is part of collection framework in Java.
数组 Array 代表基本功能,而 ArrayList 是 Java 中 collection 框架的一部分。
Therefore array members are accessed using [], whilst an ArrayList has a set of methods to access elements and have the ability to modify them.
因此,数组成员使用[]
访问,而 ArrayList 具有一组访问元素,并具有修改元素的能力。
Arraylists are also dynamic in memory and can grow and shrink at runtime.
Arraylist 在内存中也是动态的,并且可以在运行时增长和缩小。What is the major difference between an abstract class and an interface?
抽象类和接口之间的主要区别是什么?
Abstract class is more expensive to use, as there is a look-up to do when you inherit from them. Can define a behavior for common classes.
抽象类的使用成本更高,因为从它们继承时需要进行查找。可以定义常见类的行为。
An interface is a contract, a generic pattern of methods with just signatures allowing any class implement its methods in a way that it wishes to do.
接口是一种契约,一种通用方法,带有签名,允许任何类以其希望的方式实现其方法。Define Polymorphism? What are some of its abilities/usefulness?
多态的定义?它的一些功能 / 用处是什么?
Means a state of having many shapes or the capacity to take on various forms (sub types). Ability to describe and process objects of various types and classes through a single, uniform interface (base object).
表示具有多种形状或具有采取多种形式(子类型)的能力的状态。通过单个统一的界面(基础对象)描述和处理各种类型和类的对象的能力。
Polymorphism is a OOPs concept where one name can have many forms. For example, you have a smartphone for communication. The communication mode you choose could be anything. It can be a call, a text message, a picture message, mail, etc. So, the goal is common that is communication, but their approach is different. This is called Polymorphism.
多态是一个 OOPs 概念,其中一个名称可以具有多种形式。 例如,您有用于通信的智能手机。 您选择的通信模式可以是任何一种。 它可以是呼叫,短信,图片消息,邮件等。因此,目标很普遍,那就是沟通,但他们的方法不同。 这称为多态。Create and define a method called popList that gets passed an arraylist called list that will accept any numeric value type passed in. The method definition should assign 5 integers to the arraylist via a loop storing the values of 1,2,3,4 and 5 respectively.
创建并定义一个名为 popList 的方法,该方法将传递一个名为 list 的 arraylist 数组列表,该列表将接受传入的任何数值类型。该方法定义应通过存储 1,2,3,4 和 5 的循环,分别将 5 个整数分配给 arraylist。
static void popList (ArrayList<Number> list) {
for( int i=1; i<=list.size(); ++i)
list.add(i);
}
Code the sort method below to sort an arraylist in ascending order? (DO NOT USE ANY PRE DEFINED METHODS)
编写下面的 sort 方法对数组列表进行升序排序?(请勿使用任何预先定义的方法)
public class aListClass{
public static void main(String[] args) {
ArrayList<Integer> values = new ArrayList<>();
values.add(1);
values.add(41);
values.add(3);
sort(values);
for (int i = 0; i < values.size(); i++)
System.out.println(values.get(i));
}
public static void sort(ArrayList<Integer> aList) {
int temp;
for (int i = 0; i < aList.size() - 1; i++) {
for (int j = i + 1; j < aList.size(); j++) {
if (aList.get(i) > (aList.get(j))) {
temp = aList.get(i);
aList.set(i, aList.get(j)); // swap
aList.set(j, temp); // values
}
}
}
}
}
# Review
Methods that operate on an object's fields are called
操作对象字段的方法称为
A constructor
构造函数
To develop a program to solve a problem, you start by .
要开发解决问题的程序,您首先需要从 开始。
Declaring a class level variable as static means .
将类级别变量声明为静态意味着 。
In an interface all methods have
在接口中,所有方法都具有
- public 访问权限 ✔️
- packaged 访问权限
- private 访问权限
- protected 访问权限
The binary search algorithm
二进制搜索算法
The declaration
ArrayList<int> aL = new ArrayList<int>();
Another term for an object of a class is:
类的对象的另一个术语是:
In a Binary Search routine, the array item in the middle of the array portion is at subscript position
在二进制搜索例程中,中间的数组项位于下标位置?
Which class is related to all the exceptions that cannot be caught?
哪个类与所有无法捕获的异常相关?
Runtime errors cannot be caught generally. Error class is used to catch such errors/exceptions.
通常无法捕获运行时错误。 Error 类用于捕获此类错误 / 异常。For the following code, which statement is not true?
public class Sphere {
private double radius;
public double x;
private double y;
private double z;
}
对于以下代码,哪个语句不正确
The following statement creates an
ArrayList
object. What is the purpose of the<String>
notation?ArrayList<String> arr = new ArrayList<String>();
以下语句创建一个
ArrayList
对象。<String>
表示法的目的是什么?What would be the results of the following code?
final int SIZE = 5;
int[] array1 = new int[SIZE];
// ... Code that will put values in array1
int value = 0;
for (int a = 0; a <= array1.length; a++) {
value += array1[a];
}
以下代码的结果是什么?
- 值包含 array1 中的最小值
- value 包含 array1 中所有值的总和
- 值包含 array1 中的最大值
- 这会导致程序崩溃 ✔️
{.options}a <= array1.length - 1
What is Data Hiding, how why is it important, in other words, why consider it for any instance fields of a class?
- What is Data Hiding:
什么是数据隐藏?
Allows direct access and any allowed changes to the objects internal data by the class object itself.
允许类对象本身直接访问,和允许对对象内部数据进行任何更改。
Create private fields to allow for data hiding.
创建私有字段,以允许隐藏数据。 - how why is it important?
为什么它如此重要,换句话说,为什么要为一个类的任何实例字段考虑它?
Data hiding is important because classes are typically used as components in large software systems, involving a team of programmers.
数据隐藏很重要,因为类通常在大型软件系统中用作组件,涉及一个团队。
Data hiding helps enforce the integrity of an object's internal data.
数据隐藏有助于增强对象内部数据的完整性。
- What is Data Hiding:
Distinguish between a no-argument constructor and a default constructor. How are constructors distinguished from normal methods of a class?
区分无参数构造函数和默认构造函数。构造函数如何与类的常规方法区分开?
A constructor that does not accept arguments is known as a no-arg constructor. It usually intializes class level variables.
不接受参数的构造函数称为无参数构造函数。 它通常初始化类级别的变量。
The default constructor is a constructor that the Java compiler adds to your code if no explicit constructor is available. The default constructor is consider a no-arg constructor.
默认构造函数是 Java 编译器在没有显式构造函数可用时,将其添加到您的代码中的构造函数。 默认构造函数被视为无参数构造函数。
Ex. no-arg constructor programmer defined
例如 无参数构造函数程序员定义The method getMax shown below is supposed to return the position of the largest value in the portion of an array between 0 and last, inclusive:
int getMax(int array[], int last) {
int max = 0;
for (int k = 1; k <= last; k++) {
// Code is Missing
}
return max;
}
The missing code is
getMax
应该返回数组位置介于0
到last
之间的最大值。If you want to share code among several closely related classes, you expect to have common functions in a class and want to defer implementations of some other functions to derived classes, then we need to use .
如果要在几个密切相关的类之间共享代码,则希望在一个类中具有通用功能,并希望将某些其他功能的实现推迟到派生类,则我们需要使用
What is the output of this program?
public static void main(String args[]) {
ArrayList obj = new ArrayList();
obj.add("F");
obj.ensureCapacity(8); //increase capacity of object
System.out.println(obj.size());
}
Which collection does not allow duplicates on its elements?
哪个集合不允许其元素重复?
Consider a class that uses the following variables to implement an array-based stack:
String [] s = new String[100];
int top = 0;
code syntax/logic for adding an item x to the stack can be written as
假设一个类,将使用以下变量来实现基于数组的堆栈
将项目 x 添加到堆栈的代码语法 / 逻辑可以写成?A constructor is a method that is automatically called when an object is created.
构造函数是一种在创建对象时自动调用的方法。
In Java, you do not use the new operator when you use a(n):
在 Java 中,当使用
a(n)
时,请勿使用new
运算符:When you are working with a , you are using a storage location that holds a piece of data.
使用 时,您使用的是一个存储数据的存储位置。
The following UML diagram entry
setBalance(h : double) : void
,this is a method with a parameter of data type and does not return a value.UML 图条目是一个方法,其参数类型为数据类型,并且不返回值。
Explain the feature of overload.
解释重载的功能。
Two or more methods in a class may have the same name and they have different parameter lists.
一个类中的两个或多个方法可能具有相同的名称,并且具有不同的参数列表。What is wrong with the following code?
public class ClassB extends ClassA {
public ClassB() {
int init = 10;
super(40);
}
}
以下代码有什么问题?
补充下列代码,要求返回最大值
Public static int myList (ArrayList<Integer> arr){
//return the greatest integer value in the ArrayList
return Collections.max(arr);
}
填写下列代码,实现参数值得交换。
public static void main(String[] args) {
List<Integer> list= new ArrayList<>();
list.add(66);
list.add(44);
swap(list, 0, 1);
list.add(22);
}
public static void swap(List<Integer> a, int i, int j) {
int tmp = a.get(i);
a.set(i, a.get(j));
a.set(j, tmp);
}