尾部都有答案
第四章练习题(封装)
(1)下列有关类、对象和实例的叙述,正确的是哪一项?
A.类就是对象,对象就是类,实例是对象的另一个名称,三者没有差别
B.对象是类的抽象,类是对象的具体化,实例是对象的另一个名称
C.类是对象的抽象,对象是类的具体化,实例是类的另一个名称
D.类是对象的抽象,对象是类的具体化,实例是对象的另一个名称
(2)下列类Account的构造方法中,声明正确的是?
A. Account (String name){)
B. Account (String name)
C. Account (name)()
D. NewAccount (String name){)
(3)类Account中字段声明正确的是哪项?
A.
class Account {
name;
Amount;
)
B.
class Account {
String name=l.0;
Double amount="Mike";
)
C.
class Account {
String name;
Double amount;
)
D.
class Account {
String name= "Mike,,, double amount=1000.0;
)
(4)类Account中方法声明正确的是哪一项?
A.
class Account {
Deposit();
)
B.
class Account {
void deposit();
}
C.
class Account {
void deposit(){)
)
D.
class Account {
void deposit{)
)
(5)下列有关类声明的代码片段,哪一项是正确的?
A.
package school;
import java. sql.*;
Class Student {
}
B.
import java.sql.*;
package school;
Class Student {
)
C.
package school;
Class Student{
}
import java. sql.*j
D.
package school;
import java. sql.*;
private String name;
Class Student {
}
(6)有关new关键字的描述正确的是哪项?
A. 创建对象实例的时候可以不使用new关键字
B. new所创建的对象不占用内存空间
C. new会调用类的构造器来创建对象
D. new所创建的对象一定存在引用变量
(7)下列哪些是方法public int add (int a)的重载方法?(选三项)
A. public int add (long a);
B. public void add (int a)j
C. public void add (long a);
D. public int add (float a);
(8)我们定义一个Account类来描述银行账户,银行账户有账户名、金额等属性特征,同时有存款、取款等行为特征,下述代码适合描述的是哪项?
A.
class Account {
String name; //账户
String amount; //金额
Account (String name)( )
void deposit (double mount){ //存款
)
void withdraw (double mount){ //取款
}
)
B.
class Account {
String name;//账户
Doujole amount; //金额
Account (double amount){ )
void deposit (double mount){ //存款
)
void withdraw (double mount){ //取款
)
)
C.
class Account {
String name;//账户
Double amount; //金额
Account (String name){ )
void deposit (double mount){//存款
)
void withdraw (double mount){ //取款
)
)
D.
class Account {
String name;//账户
Double amount;//金额
Account (String name){ )
void deposit(){//存款
)
void withdraw(){//取款
)
)
(9)现有
class Banana2 {
static int X=2;
public static void main (String [] args) {
int X=2;
Banana2 b=new Banana2();
go(x);
}
static {x+=x; )
void go (int x) {
++x;
System. out.println (x);
}
}
结果为
A. 7
B. 5
C. 3
D. 2
(10)现有
class TestFoo {
int x;
String y;
int getX() { return x; }
String getY() {return y; }
void setX (int x) {
int Z=7:
this.x=x;
}
}
可以添加多少个修饰符来封装此类?
A. 5
B. 4
C. 3
D. 2
(11)定义枚举如下
public enum Direction{
EAST,SOUTH,WEST,NORTH
)
列正确使用该枚举类型的语句是哪项?
A. Direction Direction=EAST;
B. Direction direction=Direction.WEST;
C. int a- Direction.NORTH;
D. Direction direction=2;
(12)定义类
package utils;
public class Rep {
public static String twice (String s){return s+s ;)
}
//再定义另一个类Demo:
//insert code here
public class Demo{
public static void main (String[] args){
System. out .println( twice( "Hello"));
}
}
在第一行插入哪项代码,可以使程序正常编译和执行?
A.import utils.*;
B. import utils.Rep.*;
C. import static utils.Rep.twice;
D. static import utils.Rep.twice;
(13)现有
public class TestDemo {
private int X-2;
static int y=3;
public void method() {
final int i=100;
int j =10;
Class Cinner {
public void mymethod() {
//Here
}
}
}
}
在Here处可以访问的变量是哪些?(选三项)
A. X
B. y
C. j
D. i
(14)现有如F包结构
Com
|一一X
| |一一Alpha.class
| |
| |一一y
I |一一Beta.class
|
|l-- Gamma.class
//和类
Class Test { Alpha a; Beta b; Gamma c; }
哪三个必须加入到类Test中,以使其通过编译?(选三项)
A. package y;
B. package com;
C. import com.x.y.*;
D. import com.x.*;
(15)现有2个文件
package x;
public class X{
public static void doX() {System.out.print("doX");}
}
//和
class Find{
public static void main(String [] args) {
//insert code here
}
}
哪两行分别插入到类Find的第3行将编译并产生输出“doX”?(选两项)
A. doX();
B. X.doX();
C. x.X.doX();
D. x.X myX = new x.X();myX.doX();
参考答案
1、D
2、A
3、C
4、C
5、A
6、C
7、ACD
8、C
9、C
10、D
11、B
12、C
13、ABD
14、BCD
15、CD