尾部都有答案
第四章練習(xí)題(封裝)
(1)下列有關(guān)類荣恐、對(duì)象和實(shí)例的敘述芽唇,正確的是哪一項(xiàng)椒振?
A.類就是對(duì)象,對(duì)象就是類祥楣,實(shí)例是對(duì)象的另一個(gè)名稱,三者沒有差別
B.對(duì)象是類的抽象囊卜,類是對(duì)象的具體化耍铜,實(shí)例是對(duì)象的另一個(gè)名稱
C.類是對(duì)象的抽象,對(duì)象是類的具體化泪勒,實(shí)例是類的另一個(gè)名稱
D.類是對(duì)象的抽象昼蛀,對(duì)象是類的具體化,實(shí)例是對(duì)象的另一個(gè)名稱
(2)下列類Account的構(gòu)造方法中圆存,聲明正確的是叼旋?
A. Account (String name){)
B. Account (String name)
C. Account (name)()
D. NewAccount (String name){)
(3)類Account中字段聲明正確的是哪項(xiàng)?
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中方法聲明正確的是哪一項(xiàng)送淆?
A.
class Account {
Deposit();
)
B.
class Account {
void deposit()怕轿;
}
C.
class Account {
void deposit(){)
)
D.
class Account {
void deposit{)
)
(5)下列有關(guān)類聲明的代碼片段偷崩,哪一項(xiàng)是正確的?
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)有關(guān)new關(guān)鍵字的描述正確的是哪項(xiàng)诀紊?
A. 創(chuàng)建對(duì)象實(shí)例的時(shí)候可以不使用new關(guān)鍵字
B. new所創(chuàng)建的對(duì)象不占用內(nèi)存空間
C. new會(huì)調(diào)用類的構(gòu)造器來創(chuàng)建對(duì)象
D. new所創(chuàng)建的對(duì)象一定存在引用變量
(7)下列哪些是方法public int add (int a)的重載方法谒出?(選三項(xiàng))
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)我們定義一個(gè)Account類來描述銀行賬戶,銀行賬戶有賬戶名邻奠、金額等屬性特征笤喳,同時(shí)有存款、取款等行為特征碌宴,下述代碼適合描述的是哪項(xiàng)杀狡?
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)現(xiàn)有
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);
}
}
結(jié)果為
A. 7
B. 5
C. 3
D. 2
(10)現(xiàn)有
class TestFoo {
int x;
String y;
int getX() { return x; }
String getY() {return y; }
void setX (int x) {
int Z=7:
this.x=x;
}
}
可以添加多少個(gè)修飾符來封裝此類碑隆?
A. 5
B. 4
C. 3
D. 2
(11)定義枚舉如下
public enum Direction{
EAST,SOUTH,WEST,NORTH
)
列正確使用該枚舉類型的語句是哪項(xiàng)恭陡?
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 上煤;)
}
//再定義另一個(gè)類Demo:
//insert code here
public class Demo{
public static void main (String[] args){
System. out .println( twice( "Hello"));
}
}
在第一行插入哪項(xiàng)代碼休玩,可以使程序正常編譯和執(zhí)行?
A.import utils.*;
B. import utils.Rep.*;
C. import static utils.Rep.twice;
D. static import utils.Rep.twice;
(13)現(xiàn)有
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處可以訪問的變量是哪些牧抽?(選三項(xiàng))
A. X
B. y
C. j
D. i
(14)現(xiàn)有如F包結(jié)構(gòu)
Com
|一一X
| |一一Alpha.class
| |
| |一一y
I |一一Beta.class
|
|l-- Gamma.class
//和類
Class Test { Alpha a; Beta b; Gamma c; }
哪三個(gè)必須加入到類Test中遥赚,以使其通過編譯扬舒?(選三項(xiàng))
A. package y;
B. package com;
C. import com.x.y.*;
D. import com.x.*;
(15)現(xiàn)有2個(gè)文件
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行將編譯并產(chǎn)生輸出“doX”凫佛?(選兩項(xiàng))
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