Java课程考试的复习要点,觉得老师的命题点抓住了Java这门语言的基础,挺好的。分享下。答案是查的课程PPT。

Java复习要点

语言:English

题型:

1. 判断(1’ ×10)

2. 选择(3’ ×13)注:为多选题,少选或错选均不给分

3. 简答(4道共20分)

4. 编程(共10分)

5. 设计(共21分)

判断题:

1. 一个特定类型的所有对象是不是可以接收同样的消息?(是)

All objects of a particular type can receive the same messages.

2. Java有没有sizeof()函数?(没有)

Java has no “sizeof”

In C and C++, the sizeof( ) operator satisfies a specific need: it tells you the number of bytes allocated for data items.

Java does not need a sizeof( ) operator for this purpose, because all the data types are the same size on all machines.

3. 静态方法需不需要创建该类的对象才能访问?(不需要)

If you want to have a piece of data or a method that isn’t associated with any particular object of this class, you can declared them as static. Then you can access the data or call the method even if no objects are created.

4. 子类可不可以访问父类的protected方法?(可以)

To inherit from an existing class, you say that your new class extends an existing class, like this:

class Foo extends Bar {

……

}

Then class Foo can access public elements and protected elements in class Bar.

5. 同时使用组合和继承是很常见的方法?(是)

It is very common to use composition and inheritance together.

6. 参数前加final意味着什么?

With a primitive, final makes the value a constant, but with an object reference, final makes the reference a constant. Once the reference is initialized to an object, it can never be changed to point to another object. However, the object itself can be modified.

Java allows you to make arguments final by declaring them as such in the argument list. This means that inside the method you cannot change what the argument reference points to.

7. 可不可以创建一个处理器捕获所有的异常?(可以)

8. 数组对象一旦创建以后,其大小能不能改变?(对)

9. 私有的方法是不是一定是final的?(是)

Any private methods in a class are implicitly final.

10. 转型是不是安全的?(向上转型安全,向下则不安全)

Upcasting is always safe because you’re going from a more specific type to a more general type.

11. 在一个静态方法里能不能使用非静态变量?(不能)

Static method can only access static data.

12. 垃圾回收器只负责收集哪部分?(使用new方法分配的内存)

The GC only knows how to release memory allocated with new. A method called finalize( ) is used to handle memory which is allocated without using new.

When the GC is ready to release the storage used for your object, it will first call finalize( ), and only on the next garbage-collection pass will it reclaim the object’s memory.

13. 覆盖方法是不是必须保证参数相同?(是)

14. 接口是不是类?(是)

15. 三元操作符的使用(A?B:C)

boolean-exp ? value0 : value1

If boolean-exp evaluates to true, value0 is evaluated, and its result becomes the value produced by the operator.

If boolean-exp is false, value1 is evaluated and its result becomes the value produced by the operator.

16. final使引用总指向同一对象。

17. A computer has a CPU.

选择题:

1. Java中存储区包括哪些?(寄存器、栈、堆、静态存储器、常量存储器、非随机存取存储器)

Registers. The stack. The heap. Static storage. Constant storage. Non-RAM storage.

2. Java中回车怎么表示?(\r)换行怎么表示?(\n)

3. for循环、do/while循环的特点?

4. 组合和继承分别在哪种情况下使用?(复用功能时用组合,复用接口时用继承)

Composition is generally used when you want the features of an existing class inside your new class, but not its interface.

When you inherit, you take an existing class and make a special version of it. In general, this means that you’re taking a general-purpose class and specializing it for a particular need.

A general guideline is “Use inheritance to express differences in behavior, and fields to express variations in state.”

5. 子类对象能不能作为一个基类对象来看待?(能)子类能访问基类的所有方法吗?(private方法不能)

Taking an object reference and treating it as a reference to its base type is called upcasting because of the way inheritance trees are drawn with the base class at the top.

6. StringBuffer类的容量和长度的关系?

StringBuffer类是用来生成字符串缓冲区,这种字符串称为可变字符串。StringBuffer对象的默认容量是16个字符。

StringBuffer无法像String对象一样直接将字符串指定给StringBuffer对象,例如StringBuffer myString3="this is worong"。必须使用new生成StringBuffer对象。

StringBuffer对象有容量和长度之分,容量是不变的,长度是可变的。长度小于等于容量,表示实际存储的字符数目。

int capacity():返回StringBuffer对象的容量。

int length():返回StringBuffer对象的长度。

当存储的字符串长度超过其容量,容量会改为原来容量的两倍加上2。预先指定足够的容量效率较高。

7. 区别基于字节的和Unicode兼容的、基于字符的不同的I/O类。

Reader and Writer classes provide Unicode-compliant, character-based I/O.

8. 系统提供的标准I/O类:System.in,System.out,System.err 分别是什么类的对象?(System.in是InputStream,后两者是PrintStream)

9. 关键字:哪些是基本类型?(首字母小写的)哪些是引用类型?(首字母大写的)

Primitive type Size Minimum Maximum Wrapper type

boolean — — — Boolean

char 16-bit Unicode 0 Unicode 216- 1 Character

byte 8-bit -128 +127 Byte

short 16-bit -215 +215—1 Short

int 32-bit -231 +231—1 Integer

long 64-bit -263 +263—1 Long

float 32-bit IEEE754 IEEE754 Float

double 64-bit IEEE754 IEEE754 Double

void — — — Void

10. new一个对象总是在什么区域创建?(堆heap)

The primitive variable holds the value, and it’s placed on the stack, so it’s much more efficient.

All numeric types are signed, so don’t look for unsigned types.

The “wrapper” classes for the primitive data types allow you to make a nonprimitive object on the heap to represent that primitive type.

11. 判断标识符是否合法。

12. 多态的关键因素是什么?动态绑定。

Connecting a method call to a method body is called binding.

For late binding (dynamic binding, or run-time binding), the compiler still doesn’t know the object type, but the method-call mechanism finds out and calls the correct method body.

13. 包的概念对应操作系统中的文件夹。

Package name must appear as the first noncomment in the file, and the package name must be the same as the directory (fold) name in which the file is located.

14. 为什么要把类的方法定义为final?

There are two reasons for final methods.

a) The first is to put a “lock” on the method to prevent any inheriting class from changing its meaning.

b) The second reason for final methods is efficiency. If you make a method final, you are allowing the compiler to turn any calls to that method into inline calls.

15. 转型(有些是不允许的,长转为短可能会造成数据丢失)

Java allows you to cast any primitive type to any other primitive type, except for boolean, which doesn’t allow any casting at all.

In Java, casting is safe, with the exception that when you perform a so-called narrowing conversion (that is, when you go from a data type that can hold more information to one that doesn’t hold as much), you run the risk of losing information.

16. 接口里的方法必须都是public

接口里如果定义了字段,不写修饰符则默认为public static final

An interface contains method without body. It can also contain fields, but these are automatically static , final and public.

You can choose to explicitly declare the method declarations in an interface as public, but they are public even if you don’t say it.

简答题:

1. 什么是多态?什么是动态绑定?

Connecting a method call to a method body is called binding.

For late binding (dynamic binding, or run-time binding), the compiler still doesn’t know the object type, but the method-call mechanism finds out and calls the correct method body.

2. 原子操作为什么不需要被同步?原子操作是不可分的,不能被线程调度器中断。

3. class/method/成员变量等前面加个final有什么含义?

With a primitive, final makes the value a constant, but with an object reference, final makes the reference a constant. Once the reference is initialized to an object, it can never be changed to point to another object. However, the object itself can be modified.

Java allows you to make arguments final by declaring them as such in the argument list. This means that inside the method you cannot change what the argument reference points to.

There are two reasons for final methods.

a) The first is to put a “lock” on the method to prevent any inheriting class from changing its meaning.

b) The second reason for final methods is efficiency. If you make a method final, you are allowing the compiler to turn any calls to that method into inline calls.

When you say that an entire class is final (by preceding its definition with the final keyword), you state that you don’t want to inherit from this class or allow anyone else to do so.

4. 什么是序列化?序列化一个对象意味着什么?

Java’s object serialization allows you to take any object that implements the Serializable interface and turn it into a sequence of bytes that can later be fully restored to regenerate the original object.

Serializable object means:

a) Write a object to a disk and then read from disk to restore it.

b) Transfer a object through a network and then read from stream and restore it.

5. OO(面向对象)有哪五个特性?

Five basic characteristics of OOP by Alan Kay:

 Everything is an object.

 A program is a bunch of objects telling each other what to do by sending messages.

 Each object has its own memory made up of other objects.

 Every object has a type.

 All objects of a particular type can receive the same messages.

An object has state, behavior and identity. -By Booch

6. 重载和覆盖有什么区别?

Often, the same word expresses a number of different meanings—it’s overloaded.

A method is a name for an action, and you may use the methods with same name to take different actions.

Each overloaded method must take a unique list of argument types. The list of arguments can differ in:

 Types of arguments

 Number of arguments

 Ordering of arguments

Note: Methods only differ in return type are not allowed.

7. 静态方法(或成员变量)和非静态方法(或成员变量)有什么区别?

If you want to have a piece of data or a method that isn’t associated with any particular object of this class, you can declared them as static. Then you can access the data or call the method even if no objects are created.

Static method can only access static data.

A static method means that there is no this for that particular method. You can call a static method for the class itself, without any object.

The static method inside a class can only access to other static methods and to static fields.

编程题:

1. 判断一个数是不是素数

2. 实现文件的拷贝

设计题:

1. UDP协议

2. TCP协议

Java复习要点