分类:
2006-07-06 15:14:36
类和对象试卷
一、专业英语翻译
编译( ) 声明( ) 包 ( )
赋值( ) 导入( ) 重置( )
重载( ) 公共的( ) 接口( )
实现( ) J2SE( ) atstract( )
constructor method( ) parameter( )
二、选择题(多选)
1. 对于构造函数,下列叙述正确的是( )。
A. 构造函数是类的一种特殊函数,它的方法名必须与类名相同。
B. 构造函数的返回类型只能是void型。
C. 构造函数的主要作用是完成对类的对象的初始化工作。
D.一般在创建新对象时,系统会自动调用构造函数。
2. 下面是关于类及其修饰符的一些描述,正确的是( )。
A. abstract类只能用来派生子类,不能用来创建abstract类的对象。
B. final类不但可以用来派生子类,也可以用来创建final类的对象。
C. abstract不能与final同时修饰一个类。
D.abstract方法必须在abstract类中声明,但abstract类定义中可以没有abstract方法。
3. 为AB类的一个无形式参数、无返回值的方法method()书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为( )。
A. static void method()
B. public void method()
C. final void method()
D.abstract void method()
4. 以下程序中不合法的调用语句是( )。
class A{
void show1(){
System.out.println(“a”);
}
class B extends A{
void show2(){
System.out.println(“b”);
}
public class Cmy{
public static void main(String args[]){
A obj1=new A();
B obj2=new B();
……
}
}
A. obj1.show1();
B. obj2.show1();
C. obj1.show2();
D.obj2.show2();
5. 关于类的继承,下面叙述不正确的是( )。
A. 子类可以继承父类中的所有变量和方法。
B. 一个子类只能是一个父类。
C. 子类可以继承父类的父类中的变量和方法。
D.所有类都是通过直接或间接地继承Object类而得到的。
6. 关于构造方法的叙述正确的是( )。
A. 子类不能继承父类的构造方法。
B. 子类不能重载父类的构造方法。
C. 子类中必须定义自己的构造方法。
D.以上说法都不对。
7. 下列关于静态初始化器的叙述中,哪些是正确的?( )。
A. 静态初始化器是在构造函数之前加上static修饰符。
B. 静态初始化器是对类自身进行初始化。
C. 静态初始化器在同一个类中只能有一个。
D.静态初始化器是在其所属的类加载内存时由系统自动调用执行的。
8. 不使用static修饰符限定的方法称为对象(或实例)方法,下列说法中哪些是正确的?( )。
A. 实例方法可以直接调用父类的实例方法。
B. 实例方法可以直接调用父类的类方法。
C. 实例方法可以直接调用其他类的实例方法
D.实例方法可以直接调用本类的类方法。
三、选择题(不定选)
1. What will be the output when you compile and execute the following program.
class Base
{
static void test(){
System.out.println(“Base.test()”);
}
}
public class Chind extends Base{
static void test(){
System.out.println(“Child.test()”);
super.test();
}
static public void main(String[] a){
test()
}
}
Base.test()
Child.test()
Base.test()
Select the one correct answer
2. What will be the output when you compile and execute the following program.
public class Base{
Private void test(){
boolean f;
System.out.println(“Value is:”+f);
}
static public void main(String[] a){
new Base().test();
}
}
Select the one correct answer.
3. What happens when the following program is compiled and run.
public class example{
int i=0;
public static volid main(String[] a){
Chang_i(i);
System.out.println(i);
}
public static volid chang_i(int i){
i=2;
i*=2;
}
}
Select the one correct answer
4. Which of the following are legal declaration and definition of method
A. void method(){};
B. volid method(int) {};
C. method(){};
D. method(ok) {}
E. void method() {};
Select the ont correct answer
5. What is the result of compile and running the following class.
class Test{
public void methodA(int i) {
System.out.println(i);
}
public int methodA(int i) {
System.out.println(I+1);
Return i+1;
}
public static void main(String args[]) {
Test X = new Test();
X.methodA(5);
}
}
Select the one correct answer
6. What gets displayed on the screen when the following program is compiled and run.
protected class example{
public static void main(String args[]) {
String test=”abc”;
Test=test+test;
System.out.println(test);
}
}
Select the one correct answer
7. What is the correct ordering for the import,class and package declarations when found in a single file?
A. package,import,class
B. class,import,package
C. import,package,class
D. package,class,import
Select the one correct answer
8. what is correct declaration of an abstract method that is intended to be public
A. public abstract void add();
B. public abstract void add() {}
C. public abstract add();
D. public virtual add();
Select the one correct answe
9. Given the following code
public class Test{
…
}
Which of the following can be used to define a constructor for this class:
A. public viod Test(){..}
B. public Test(){…}
C. public stratic Test() {…}
D. public stratic void Test() {}
Select the one correct answer
10. Which of the following is a legal return type of a method overloading the following method:
public void add(int a) {…}
A. void
B. int
C. Can be anything
Select the correct answer
11. Which of the following statement is correc for a method which is overriding the following method
public void add(int a){…}
A. the overriding method must return void
B. the overriding method must return int
C. the overrinding method can return whatever it likes
Select the correct answer
12. Given the following classes defined in separste files:
class Vehicle {
public void drive() {
System.out.println(“Vehicle:drive”);
}
}
class Car extends Vehicle {
public void drive() {
System.out.println(“Car:drive”);
}
}
public class Test{
public static void main (String args[]){
Vehicle v;
Car c;
v=new Vehicle();
c=new Car();
v.drive();
c.drive();
v=c;
v.drive();
}
}
What will be the effect of compileing and running this class Test?
A. Generates a Compiler error on the statement v=c;
B. Generates runtime error on the statement v=c;
C. Prints out:
Vehicle:drive
Car:drive
Car:drive
E. prints out:
Vehicle:drive
Car:drive
Vehicle:drive
Select the ont right answer
13. Whicle variables can inner class access from the class whicle encapsulates it?
A. All static variables
B. All final variables
C. All instance variables
D. Only final instance variables
E. Only finel static variables
Select three correct answers
14..Carefully examine the following code:
public class StraticTest{
static{
System.out.println(“Hi there”);
}
public void print(){
System.out.println(“Hello”);
}
public static void main (String args[]) {
StaticTest st1=new StaticTest();
St1.print();
StaticTest st2=new StaticTest();
st2.print();
}
}
When will the string”Hi there”be printed?
A. Never.
B. Each time a new instrance ids created.
C. Once when the class is first loaded into the Java virtual machine
D. Only when the static method is call explicitly
Select one correct answer
15. Given the variable declarations below:
Byte myByte;
Int myInt;
Long myLong;
Char myChar;
Float myFloat;
Double myDouble;
Whicle one of the following assignments would need an explicite cast
A. myint=myByte
B. myInt=myLoad
C. myByte=3;
D. myInt=myChar
E. myFload=myDouble
F. myFolad=3
G. myDouble=3.0
Select two correct answer
16.if you want subclasses to access,but not to override a superclass member method,what Keyword should precede the name of superclass method?
Write down your answer______.
17.Consider the code below:
public static void main(String args[]){
int a=5;
System.out.println(cude?(a));
}
int cude(int theNum) {
return theNum*theNum*theNum;
}
What will happen when you attempt to compile and run this code?
A. It will not compile because cube is already define in the java.lang.Math class
B. It will not compile because cube is not static
C. It will compile,but throw an arithmetic exception
D. It will run perfectly and print”
Select one correct answer
18.What is the output of the following program?
class Test{
void show(){
System.out.println(“non-static method in Test”);
}
}
public class Q3 extends Test{
static void show(){
Sytem.out.println(“overridden non-static method in Q
}
public static void main(String [] args) {
Q
}
}
A. Compilation error at line 2
B. Compilation error at line 7
C. Not compilation error,but runtime exception at line 2
D. Not compilation error,but runtime exception at line 7
Select one correct answer
19.Given the following class definition,which of the following methods could be leally place after comment//Here
public class Rid{
public void amethod(int i,boolean b){}
//Here
}
A. public void amethod(boolean b,int i){}
B. public void amethod(int i ,boolean b1){}
C. public void amethod(int i ,boolean b2){}
D. public void amethod(int i ,boolean b){}
Select ont correct answer
20.Given the following class definetion,which of the following statement would be legal after the comment//Here
class InOut {
int s=2;
public void amethod(final int iArgs) {
int iam;
class Bicycle{
public void sayHello() {
//Here
}
}
}
public void another() {
int iOther;
}
}
A. System.out.println(s);
B. System.out.println(iOther);
C. System.out.println(iam);
D. System.out.println(iArgs);
Select two correct answer
21.Given following code;
final class First{
private int a=1;
int b=2;
}
class Second extends First{
public void method() {
System.out.println(a+b);
}
}
A. You cannot invoke println() without passing it a string.
B. Because a is private,no classes other than First can access it
C. Second cannot extend First
D. Final is not a valid keyword for a class
Select the one answer
22.Which keyword,when used in front of a method,must also appear in front of the class.
A. synchronized
B. abstract
C. public
D. private
Select one correct answer
23.What are the proper ways to initialize the static variables SIZE and MIN_VALUE?
class Base{}
static final int SIZE;
static folat MIN_VALUE;
Base(); {}
Void test() {
System.out.println(“Base.test()”);
}
}
A. . Add the following lines to Base Class
static{
SIZE=10;
MIN_VALUE=
}
B. Add the following lines to Base Class
{
SIZE=10;
MIN_VALUE=
}
C. add the following lines to Base Class constructor
SIZE=10;
MIN_VALUE=
static final int SIZE=10;
static float MIN_VALUE=
Slect two correct answer
24.Given these class definitions:
class Superclass {}
class Subclass1 extends Superclass{}
and these objects:
Superclass a=new Superclass();
Subclass1 b=new Subclasss1();
Which of the following explains the result of the statement:
B=(Subclass1)a:
A. Illegal at compile time
B. Legal at compile time but possibly illegal at runtime
C. Definitely legal at runtime
System one correct answer
25.What access contral keyword should you use to allow other classes to access a method freely within its package,but to restrict classes outside of the package from accessing that method
A. public
B. protected
C. private
D. do not supply an access control keyword
26.How can you force an object to be garbage collected?
A. Invoke its finalized() method
B. Remove all references to the object
C. User all memory that is available to the program
D. You cannot force an object to be garbage collected
Select ont correct answer
27.In the following code,which is the earliest statement,where the object originally held ine,may be garbage collected:
public class Test{
public static void main(String args[]) {
Employee e=new Employee(“Bob”,48);
e.calculatePay();
System.out.println(e.printDetails());
e=null;
e=new Emploee(“Denise”,36);
e,calculatePay();
System.out.println(e.printDtails());
}
}
A. Line 10
B. Line 11
C. Line 7
D. Line 8
E. Never
Select the ont answer
28.Here is a method which creates a number of String objects in the course of printing a count down sequence.
public void countdown() {
for(int i=10;i>=0;i--){
String tmp=Integer.toString(i);
System.out.println(tmp);
}
System.out.println(“BOOM!”);
}
When the program reaches lines 6, ow many of the String objects created in line 3 are eligible for garbage collection?Assume that the System.out object is not keeping a reference
A. none
B. 1
C. 10
D. 11
Select the ont answer.
编程题:
写一个学生类Student,要求如下。
a) 学生类Student的属性有:
i. id long型,代表学号。
ii. name String类对象,代表姓名。
iii. age int型,代表年龄。
iv. sex boolean型,代表性别(其中,true表示男,false表示女)。
v. phone String类对象,代表联系电话。
b) 学生类Student的方法有:
i. Student(long I,String n,int a,Boolean s,long p) 有参数的构造方法,形参表中的参数分别初始化学号、姓名、年龄、性别和联系电话。
ii. int getAge() 获取年龄作为方法的返回值。
iii. boolean getSex() 获取性别作为方法的返回值。
iv. long getPhone() 获取联系电话作为方法的返回值。
public String toString() 以姓名、联系电话的形式作为方法的返回值。