# Tentative Topical Coverages for Mid Term

Topic(s)Suggested Readings
Java SE EssentialsChapters 1-5 (review of essentials)
Object-Oriented Programming (OOP) conceptsChap. 6
Arrays and ArraylistsChap. 7
Abstraction, Inheritance, PolymorphismChap. 10
Stream I/O file processing, Exception HandlingChap. 9,11
Generics, Collections (intro only)Chap. 17,18

# Sample

  1. Which of these is an incorrect array declaration?

    • intarr[] = newint[5]
    • int[] arr = new int[5]
    • int arr[] = new int[5]
    • int arr[] = int [5] new

    哪一个是不正确的数组声明?

  2. What will this code print?

    int arr[] = new int [5];
    System.out.print(arr[0]);
    • 0
    • value stored in arr[0]
    • 00000
    • Class name@hashcode in hexadecimalform
  3. 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++;
    		}
    	}
    }
    • 0 2 4 6 8
    • 1 3 5 7 9
    • 0 1 2 3 4 5 6 7 8 9
    • 1 2 3 4 5 6 7 8 9 10
  4. Look at the following code.

    Integer myNumber = new Integer(5);
    int var = myNumber;

    What is true about the second statement?

    • The statement performs autoboxing.
    • It results in an error because you can't assign a wrapper class to a primitive variable.
    • The statement performs unboxing.
    • The statement performs unwrapping.

    详解 Java 的自动装箱与拆箱 (Autoboxing and unboxing)

    • 该语句执行自动装箱。
    • 由于无法将包装器类分配给原始变量,因此会导致错误。
    • 该语句执行拆箱。✔️
    • 该语句执行解包。
  5. Which is the valid declaration within an interface definition?

    • public double methoda();
    • public final double methoda();
    • static void methoda(double d1);
    • protected void methoda(double d1);

    在接口定义中,哪个是有效声明?

  6. A private base method can be overridden by some derived public method.

    私有基础方法可以被某些派生的公共方法覆盖。

  7. Which of these keywords is not a part of exception handling?

    • try
    • finally
    • thrown
    • catch

    这些关键字中的哪个不是异常处理的一部分?

  8. The process of converting one date type to another is called .

    • Translating
    • Casting
    • Compiling
    • Declaring

    将一种日期类型转换为另一种日期的过程称为

    • 翻译
    • 强制转换 ✔️
    • 编译
    • 声明
  9. Which of the following is the correct declaration statement in java program?

    • int num = int[5]
    • int num = new num[5]
    • int[] num = new int[5]
    • None

    以下哪项是 Java 程序中正确的声明语句?

  10. 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"); 
    	} 
    }
    • No statement required.
    • import java.io.*;
    • include java.io.*;
    • import java.io.PrintWriter;

    哪一行代码应替换丢失的语句以使该程序得以编译?

  11. method is used to find the nth no. of character of given string s1.

    • 1.index(n)
    • s1.substring(n)
    • s1.length()
    • s1.charAt(n)

    方法用于查找给定字符串 s1 的第 n 个字符。

  12. Which of these can be overloaded?

    • Methods
    • Constructors
    • both a and b
    • None can be overloaded

    哪些可以被重载?

  13. 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.

    • Only (II)
    • (I) and (II)
    • (II) and (III)
    • Only (I)
    • Only (III)

    Java 的多重继承概念是通过?实现的

    • 扩展两个或多个类。
    • 扩展一个类、实现一个或多个接口。 ✔️
    • 实现两个或多个接口。 ✔️
  14. Private members of a class are inherited in the sub class.

    类的私有成员在子类中被继承。

  15. 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。

  16. 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);
    	}
    }
    • ONE
    • TWO
    • THREE
    • ONE TWO
    • Error
  17. If a superclass does not have a default constructor, then a class that inherits from it,

    • must call one of the constructors that the superclass does have
    • must contain the default constructor for the superclass
    • does not inherit the data member fields from the superclass
    • must initialize the superclass values

    如果父类没有默认的构造函数,则继承自它的类

    • 必须调用父类确实具有的构造函数之一 ✔️
    • 必须包含父类的默认构造函数
    • 不继承父类的数据成员字段
    • 必须初始化超类值
  18. The binary search algorithm

    • will cut the portion of the array being searched in half each time the loop fails to locate the search value
    • will have an average of N/2 comparisons, where N is the number of elements in the array
    • is less efficient than the sequential search algorithm

    二进制搜索算法

    • 每次循环未能找到搜索值时,将被搜索数组的部分切成一半 ✔️
    • 将平均进行 N/2 次比较,其中 N 是数组中元素的数量
    • 效率较低比顺序搜索算法

# Coding and Definitions

  1. 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
    没有泛型 / 类型检查

    List<String> list = new ArrayList();
    	list.add("hey");
    	Object obj1 = list.get(0);
    	String obj2 =  list.get(0);
    	Integer obj3 = list.get(0);  //compile error! Can't convert String to Integer
  2. 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.
    使用泛型时,类型参数不允许为静态,因为静态变量在对象之间共享,因此编译器无法确定要使用哪种类型。

  3. 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 在内存中也是动态的,并且可以在运行时增长和缩小。

  4. 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.
    接口是一种契约,一种通用方法,带有签名,允许任何类以其希望的方式实现其方法。

  5. 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 概念,其中一个名称可以具有多种形式。 例如,您有用于通信的智能手机。 您选择的通信模式可以是任何一种。 它可以是呼叫,短信,图片消息,邮件等。因此,目标很普遍,那就是沟通,但他们的方法不同。 这称为多态。

    Error: Failed to launch the browser process! spawn /Applications/Google Chrome.app/Contents/MacOS/Google Chrome ENOENT
    
    
    TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
    
  6. 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);
    }
  7. 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

  1. Methods that operate on an object's fields are called

    • Private methods
    • Instance methods
    • Instance variables
    • Public methods

    操作对象字段的方法称为

    • 私人方法
    • 实例方法 ✔️
    • 实例变量
    • 公开方法
  2. A constructor

    • Has return type of void
    • Always accepts two arguments
    • Always has an access specifier of private
    • Has the same name as the class

    构造函数

    • 返回类型为 void
    • 一律接受两个参数
    • 始终具有私有的访问说明
    • 与类同名 ✔️
  3. To develop a program to solve a problem, you start by .

    • analyzing the problem
    • implementing the solution in Java
    • designing the algorithm
    • entering the solution into a computer system

    要开发解决问题的程序,您首先需要从 开始。

    • 分析问题 ✔️
    • 用 Java 实现解决方案
    • 设计算法
    • 将解决方案输入计算机系统
  4. Declaring a class level variable as static means .

    • access is obtainable only thru a class object
    • any method (static, instance) can have access
    • no methods can have access
    • none of the above

    将类级别变量声明为静态意味着

    • 访问只能通过类对象获得
    • 任何方法(静态,实例)都可以访问 ✔️
    • 没有方法可以访问
    • 以上都不是
  5. In an interface all methods have

    • public access
    • packaged access
    • private access
    • protected access

    在接口中,所有方法都具有

    • public 访问权限 ✔️
    • packaged 访问权限
    • private 访问权限
    • protected 访问权限
  6. The binary search algorithm

    • will cut the portion of the array being searched in half each time the loop fails to locate the search value
    • will have an average of N/2 comparisons, where N is the number of elements in the array
    • is less efficient than the sequential search algorithm
    • will have a maximum number of comparisons equal to the number of elements in the array

    二进制搜索算法

    • 每次循环未能找到搜索值时,将被搜索数组的一部分切成两半 ✔️
    • 将具有 N/2 个比较的平均值,其中 N 是数组中元素的数量
    • 比顺序搜索算法效率低
    • 将具有的最大比较数量等于数组中元素的数量
  7. The declaration

    ArrayList<int> aL = new ArrayList<int>();
    • compiles and runs correctly, but is not recommended
    • allows the programmer to create an ArrayList that holds integer types
    • causes a compile-time error
    • compiles correctly, but causes an exception to be thrown at run time
    • 正确编译并运行,但不建议
    • 允许程序员创建一个保存整数类型的 ArrayList
    • 导致编译时错误 ✔️
    • 正确编译,但导致在运行时引发异常
  8. Another term for an object of a class is:

    • access specifier
    • instance
    • member
    • method

    类的对象的另一个术语是:

    • 访问说明符
    • 实例 ✔️
    • 成员
    • 方法
  9. In a Binary Search routine, the array item in the middle of the array portion is at subscript position

    • (lower + upper)/2
    • (upper - lower)/2
    • (lower - upper)/2
    • lower + upper/2

    在二进制搜索例程中,中间的数组项位于下标位置?

  10. Which class is related to all the exceptions that cannot be caught?

    • Error
    • Exception
    • RuntimeException
    • All of the mentioned

    哪个类与所有无法捕获的异常相关?
    Runtime errors cannot be caught generally. Error class is used to catch such errors/exceptions.
    通常无法捕获运行时错误。 Error 类用于捕获此类错误 / 异常。

  11. For the following code, which statement is not true?

    public class Sphere {
    	private double radius;
    	public double x;
    	private double y;
    	private double z;
    }
    • z is available to code that is written outside the Sphere class
    • radius, x, y, and z are called members of the Sphere class
    • x is available to code that is written outside the Sphere class
    • radius is not available to code written outside the Sphere class

    对于以下代码,哪个语句不正确

    • z 可用于在 Sphere 类之外编写的代码 ✔️
    • radius,x,y 和 z 被称为 Sphere 类的成员
    • x 可用于在 Sphere 类之外编写的代码
    • radius 不适用于在 Sphere 类之外编写的代码
  12. The following statement creates an ArrayList object. What is the purpose of the <String> notation?

    ArrayList<String> arr = new ArrayList<String>();
    • It specfies that everything store for the arr object will be converted to a String;
    • It specfies that String objects may not be stored in the ArrayList object;
    • It specfies that only String objects may be stored in the ArrayList object;
    • It specfies that the get method will only return String ob.

    以下语句创建一个 ArrayList 对象。 <String> 表示法的目的是什么?

    • 确定将所有存储在 arr 对象中的东西都转换为 String
    • 确定 String 对象可能不存储在 ArrayList 对象中;
    • 规定只有 String 对象可以存储在 ArrayList 对象中; ✔️
    • 它指定 get 方法将仅返回 String ob
  13. 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];
    }
    • value contains the lowest value in array1
    • value contains the sum of all the values in array1
    • value contains the highest value in array1
    • This would cause the program to crash

    以下代码的结果是什么?

    • 值包含 array1 中的最小值
    • value 包含 array1 中所有值的总和
    • 值包含 array1 中的最大值
    • 这会导致程序崩溃 ✔️
      {.options}
      a <= array1.length - 1
  14. 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.
      数据隐藏有助于增强对象内部数据的完整性。
  15. 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
    例如 无参数构造函数程序员定义

    public Rectangle() {
    	length = width = 1.0;
    }
  16. 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

    • if (array[k] > array[max]) k = max;
    • if (array[k] > array[max]) max = k;
    • if (array[k] > max) return max;
    • if (array[k] > array[max]) return max;

    getMax 应该返回数组位置介于 0last 之间的最大值。

  17. 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 .

    • An abstract class
    • An interface
    • either A or B

    如果要在几个密切相关的类之间共享代码,则希望在一个类中具有通用功能,并希望将某些其他功能的实现推迟到派生类,则我们需要使用

  18. 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());
    }
    • 1
    • 2
    • 4
    • 8
  19. Which collection does not allow duplicates on its elements?

    • list
    • arrayList
    • set
    • linkedlist

    哪个集合不允许其元素重复?

  20. 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

    • if (top == s.length)
        	throw new RuntimeException("Overflow");
        else {
        	top ++;
        	s[top] = x;
        }
    • if (top < s.length) {
      	s[top] = x;
      	top++;
      }
      else
      	throw new RuntimeException("Overflow");
    • if (top < 0) throw new IndexOutBoundsException()
      	s[top] = x;
      	top ++;
    • s.add(top, x);

    假设一个类,将使用以下变量来实现基于数组的堆栈
    将项目 x 添加到堆栈的代码语法 / 逻辑可以写成?

  21. A constructor is a method that is automatically called when an object is created.

    构造函数是一种在创建对象时自动调用的方法。

  22. In Java, you do not use the new operator when you use a(n):

    • array size declarator
    • initialization list
    • two-dimensional array
    • all choices are correct

    在 Java 中,当使用 a(n) 时,请勿使用 new 运算符:

    • 数组大小声明符
    • 初始化列表 ✔️
    • 二维数组
    • 所有选择都是正确的
  23. When you are working with a , you are using a storage location that holds a piece of data.

    • primitive variable
    • reference variable
    • numeric literal
    • binary number

    使用 时,您使用的是一个存储数据的存储位置。

    • 基本变量 ✔️
    • 参考变量
    • 数字文字
    • 二进制数
  24. 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.

    • public, double
    • public, float
    • private, float
    • private, double

    UML 图条目是一个方法,其参数类型为数据类型,并且不返回值。

  25. Explain the feature of overload.

    解释重载的功能。
    Two or more methods in a class may have the same name and they have different parameter lists.
    一个类中的两个或多个方法可能具有相同的名称,并且具有不同的参数列表。

  26. What is wrong with the following code?

    public class ClassB extends ClassA {
    	public ClassB() {
    		int init = 10;
    		super(40);
    	}
    }
    • Nothing is wrong with the code.
    • The method super is not defined.
    • The call to the method super must be the first statement in the constructor.
    • No values may be passed to super.

    以下代码有什么问题?

    • 代码没有错。
    • 未定义方法 super。
    • 对方法 super 的调用必须是构造函数中的第一条语句。 ✔️
    • 不能将任何值传递给 super。
  27. 补充下列代码,要求返回最大值

    Public static int myList (ArrayList<Integer> arr){
    	//return the greatest integer value in the ArrayList
    	return Collections.max(arr);
    }
  28. 填写下列代码,实现参数值得交换。

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