C語言中的常見設(shè)計模式運用試題及答案_第1頁
C語言中的常見設(shè)計模式運用試題及答案_第2頁
C語言中的常見設(shè)計模式運用試題及答案_第3頁
C語言中的常見設(shè)計模式運用試題及答案_第4頁
C語言中的常見設(shè)計模式運用試題及答案_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

C語言中的常見設(shè)計模式運用試題及答案姓名:____________________

一、單項選擇題(每題2分,共10題)

1.在C語言中,以下哪個設(shè)計模式主要用于實現(xiàn)創(chuàng)建型模式中的對象創(chuàng)建過程?

A.工廠方法模式

B.單例模式

C.原型模式

D.建造者模式

2.下面哪個函數(shù)不是結(jié)構(gòu)體指針作為參數(shù)的例子?

A.voidfunc(structStudent*s);

B.voidfunc(structStudents);

C.voidfunc(structStudent*s[],intn);

D.voidfunc(structStudents[],intn);

3.以下哪個函數(shù)不是C語言中的設(shè)計模式之一?

A.Observer

B.Iterator

C.Command

D.Array

4.以下哪個函數(shù)是C語言中的工廠方法模式的一個實現(xiàn)?

A.voidcreateStudent();

B.structStudent*createStudent();

C.structStudent*createStudent(intage);

D.voidcreateStudent(intage);

5.以下哪個函數(shù)是C語言中的單例模式的一個實現(xiàn)?

A.structSingleton*getInstance();

B.structSingletongetInstance();

C.structSingleton*getInstance();

D.structSingletongetInstance();

6.以下哪個函數(shù)是C語言中的原型模式的一個實現(xiàn)?

A.structPrototype*clone();

B.structPrototypeclone();

C.structPrototype*clone(structPrototype*prototype);

D.structPrototypeclone(structPrototypeprototype);

7.以下哪個函數(shù)是C語言中的建造者模式的一個實現(xiàn)?

A.structBuilder*createBuilder();

B.structBuildercreateBuilder();

C.structBuilder*createBuilder(structBuilder*builder);

D.structBuildercreateBuilder(structBuilderbuilder);

8.以下哪個函數(shù)是C語言中的迭代器模式的一個實現(xiàn)?

A.structIterator*createIterator();

B.structIteratorcreateIterator();

C.structIterator*createIterator(structContainer*container);

D.structIteratorcreateIterator(structContainercontainer);

9.以下哪個函數(shù)是C語言中的觀察者模式的一個實現(xiàn)?

A.voidnotify();

B.voidupdate();

C.voidobserver();

D.voidnotify(structObserver*observer);

10.以下哪個函數(shù)是C語言中的命令模式的一個實現(xiàn)?

A.voidexecute();

B.voidcommand();

C.voidexecute(structCommand*command);

D.voidcommand(structCommandcommand);

二、填空題(每空2分,共5題)

1.在C語言中,單例模式通常通過_________來實現(xiàn)對象的唯一性。

2.在C語言中,工廠方法模式通常通過_________來實現(xiàn)對象的創(chuàng)建。

3.在C語言中,原型模式通常通過_________來實現(xiàn)對象的復(fù)制。

4.在C語言中,建造者模式通常通過_________來實現(xiàn)對象的構(gòu)建。

5.在C語言中,迭代器模式通常通過_________來實現(xiàn)對象的遍歷。

三、簡答題(每題5分,共5題)

1.簡述C語言中單例模式的作用和實現(xiàn)方法。

2.簡述C語言中工廠方法模式的作用和實現(xiàn)方法。

3.簡述C語言中原型模式的作用和實現(xiàn)方法。

4.簡述C語言中建造者模式的作用和實現(xiàn)方法。

5.簡述C語言中迭代器模式的作用和實現(xiàn)方法。

四、編程題(共10分)

編寫一個C語言程序,實現(xiàn)一個簡單的工廠方法模式,創(chuàng)建不同類型的交通工具對象,并使用這些對象進行操作。

```c

//工具類接口

structVehicle{

void(*drive)();

};

//汽車類實現(xiàn)

structCar{

structVehiclebase;

};

voidcarDrive(){

printf("Carisdriving.\n");

}

//摩托車類實現(xiàn)

structMotorcycle{

structVehiclebase;

};

voidmotorcycleDrive(){

printf("Motorcycleisdriving.\n");

}

//工廠類實現(xiàn)

structVehicleFactory{

structVehicle*(*createVehicle)(constchar*type);

};

structVehicle*createCar(){

structCar*car=(structCar*)malloc(sizeof(structCar));

car->base.drive=carDrive;

return(structVehicle*)car;

}

structVehicle*createMotorcycle(){

structMotorcycle*motorcycle=(structMotorcycle*)malloc(sizeof(structMotorcycle));

motorcycle->base.drive=motorcycleDrive;

return(structVehicle*)motorcycle;

}

structVehicle*createVehicleFactory(){

structVehicleFactory*factory=(structVehicleFactory*)malloc(sizeof(structVehicleFactory));

factory->createVehicle=createCar;

return(structVehicle*)factory;

}

intmain(){

structVehicleFactory*factory=createVehicleFactory();

structVehicle*vehicle=factory->createVehicle("Car");

vehicle->drive();

free(vehicle);

vehicle=factory->createVehicle("Motorcycle");

vehicle->drive();

free(vehicle);

free(factory);

return0;

}

```

二、多項選擇題(每題3分,共10題)

1.在C語言中,以下哪些設(shè)計模式屬于創(chuàng)建型模式?

A.工廠方法模式

B.單例模式

C.原型模式

D.建造者模式

E.觀察者模式

2.以下哪些函數(shù)參數(shù)是結(jié)構(gòu)體指針類型的?

A.voidfunc(structStudent*s);

B.voidfunc(structStudents);

C.voidfunc(structStudent*s[],intn);

D.voidfunc(structStudents[],intn);

E.voidfunc(structStudent*s);

3.在C語言中,以下哪些函數(shù)可以用于實現(xiàn)單例模式?

A.structSingleton*getInstance();

B.structSingletongetInstance();

C.structSingleton*getInstance();

D.structSingletongetInstance();

E.structSingleton*getInstance();

4.在C語言中,以下哪些函數(shù)可以用于實現(xiàn)工廠方法模式?

A.structVehicle*createStudent();

B.structVehicle*createStudent();

C.structVehicle*createStudent(intage);

D.structVehicle*createStudent();

E.structVehicle*createStudent();

5.在C語言中,以下哪些函數(shù)可以用于實現(xiàn)原型模式?

A.structPrototype*clone();

B.structPrototypeclone();

C.structPrototype*clone(structPrototype*prototype);

D.structPrototypeclone(structPrototypeprototype);

E.structPrototype*clone();

6.在C語言中,以下哪些函數(shù)可以用于實現(xiàn)建造者模式?

A.structBuilder*createBuilder();

B.structBuildercreateBuilder();

C.structBuilder*createBuilder(structBuilder*builder);

D.structBuildercreateBuilder(structBuilderbuilder);

E.structBuilder*createBuilder();

7.在C語言中,以下哪些函數(shù)可以用于實現(xiàn)迭代器模式?

A.structIterator*createIterator();

B.structIteratorcreateIterator();

C.structIterator*createIterator(structContainer*container);

D.structIteratorcreateIterator(structContainercontainer);

E.structIterator*createIterator();

8.在C語言中,以下哪些函數(shù)可以用于實現(xiàn)觀察者模式?

A.voidnotify();

B.voidupdate();

C.voidobserver();

D.voidnotify(structObserver*observer);

E.voidobserver(structObserverobserver);

9.在C語言中,以下哪些函數(shù)可以用于實現(xiàn)命令模式?

A.voidexecute();

B.voidcommand();

C.voidexecute(structCommand*command);

D.voidcommand(structCommandcommand);

E.voidcommand(structCommand*command);

10.在C語言中,以下哪些設(shè)計模式屬于行為型模式?

A.工廠方法模式

B.單例模式

C.觀察者模式

D.迭代器模式

E.策略模式

三、判斷題(每題2分,共10題)

1.在C語言中,單例模式確保一個類只有一個實例,并提供一個全局訪問點。(對)

2.工廠方法模式通過在運行時動態(tài)指定創(chuàng)建對象的方法來創(chuàng)建對象。(對)

3.原型模式通過復(fù)制現(xiàn)有對象來創(chuàng)建新的對象。(對)

4.建造者模式通過逐步構(gòu)建一個復(fù)雜對象來創(chuàng)建對象。(對)

5.迭代器模式允許遍歷集合對象,而不暴露其內(nèi)部表示。(對)

6.觀察者模式允許對象在狀態(tài)變化時通知其他依賴對象。(對)

7.命令模式將請求封裝為一個對象,從而允許用戶使用不同的請求、隊列或日志請求。(對)

8.在C語言中,結(jié)構(gòu)體指針參數(shù)比值傳遞參數(shù)更節(jié)省內(nèi)存。(對)

9.在C語言中,使用函數(shù)指針作為參數(shù)可以減少函數(shù)調(diào)用的開銷。(對)

10.在C語言中,工廠方法模式和抽象工廠模式是同一種模式的不同實現(xiàn)。(錯)

四、簡答題(每題5分,共6題)

1.簡述C語言中單例模式的作用和實現(xiàn)方法。

2.簡述C語言中工廠方法模式的作用和實現(xiàn)方法。

3.簡述C語言中原型模式的作用和實現(xiàn)方法。

4.簡述C語言中建造者模式的作用和實現(xiàn)方法。

5.簡述C語言中迭代器模式的作用和實現(xiàn)方法。

6.簡述C語言中觀察者模式的作用和實現(xiàn)方法。

試卷答案如下

一、單項選擇題(每題2分,共10題)

1.A

解析思路:工廠方法模式是創(chuàng)建型模式之一,主要用于對象的創(chuàng)建過程。

2.B

解析思路:選項B中的參數(shù)是結(jié)構(gòu)體本身,而非指針。

3.D

解析思路:選項D中的Array函數(shù)不是C語言中的設(shè)計模式。

4.B

解析思路:工廠方法模式中,創(chuàng)建對象的函數(shù)通常返回指向?qū)ο蟮闹羔槨?/p>

5.A

解析思路:單例模式中,getInstance函數(shù)通常返回指向唯一實例的指針。

6.A

解析思路:原型模式中,clone函數(shù)用于創(chuàng)建對象的副本。

7.A

解析思路:建造者模式中,createBuilder函數(shù)用于創(chuàng)建建造者對象。

8.A

解析思路:迭代器模式中,createIterator函數(shù)用于創(chuàng)建迭代器對象。

9.D

解析思路:觀察者模式中,notify函數(shù)通常接受一個觀察者指針作為參數(shù)。

10.E

解析思路:命令模式中,execute函數(shù)通常接受一個命令指針作為參數(shù)。

二、多項選擇題(每題3分,共10題)

1.A,C,D

解析思路:工廠方法模式、原型模式和建造者模式屬于創(chuàng)建型模式。

2.A,C,E

解析思路:選項A,C,E中的參數(shù)是結(jié)構(gòu)體指針類型。

3.A,C,D

解析思路:選項A,C,D中的函數(shù)可以用于實現(xiàn)單例模式。

4.A,B,D

解析思路:選項A,B,D中的函數(shù)可以用于實現(xiàn)工廠方法模式。

5.A,C,D

解析思路:選項A,C,D中的函數(shù)可以用于實現(xiàn)原型模式。

6.A,B,C,D

解析思路:選項A,B

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論