61阅读

c语言程序设计试题-C语言程序设计试题

发布时间:2017-12-04 所属栏目:c#程序设计

一 : C语言程序设计试题

C语言程序设计试题

一、单项选择题(本题共25小题,每小题2分,共50分)

在每小题列出的四个备选项中只有一个是符合题目要求的,请将其正确答案涂写在答题卡上。[www.61k.com]错选、多选或未选均无分。

1. 以下正确的C语言自定义标识符是______。

A. _1a

C. do B. 2a_ D. a.12

2. 在C语言中,错误的常数表示是_______。

A. 0L B.-0x6aL

C. '6'

A. 1 D. 1.234E3.5 B. 2

D. 10 3. 设int a, x=2; 执行语句a=x>0?3*x:x=10;后,变量x的值是_______。 C. 6

4.设有以下程序段:

int x=2,y=2,z=0,a;

a=++x||++y&&z++;

printf("%d,%d,%dn",x,y,z);

执行后输出的结果是_________。

A. 2, 2, 0 B. 3, 3,1

C. 3, 2, 0 D. 3, 2, 1

5. 在C语言中,字符串"TOPt65""的长度是_______。

A. 5 B.7

C. 8 D. 12

6. 设float x,由键盘输入:12.45, 能正确读入数据的输入语句是_________。

A. scanf("%5f",&x) B. scanf("%5d",&x);

C. scanf("%f",x); D. scanf("%s",&x);

7.逗号表达式a=2*6,a*3,a+5的值是_________。

A. 12 B. 17

C.36 D. 41

8. C语言程序中,整型常量的书写形式不包括_________。

A. 二进制 B. 八进制

C. 十进制 D. 十六进制

9. 在C语言中,下列运算符中结合性属于自右向左的是_________。

A. && B. -

C.<< D. ->

10. 设int x;,则与计算︱x︱等价的表达式是_________。

A. x>0?-x:x B. x>0?x:-x

C. x<0?x:-x D. x<0?-x:-x

11. 执行下面的程序段后,k的值是_______。

int k=1,n=325;

do { k*=n%10;n/=10;}while(n);

A. 3 B. 30

C. 523 D. 325

12. 表达式的值为0的是_________。

c语言程序设计题库 C语言程序设计试题

A. 5/5%5 B. 5>2

C. !4 D. 0x7&&7

13. 设int a=11, b=2;执行下述程序段后,变量a和b的值分别是_______。[www.61k.com) do

{ a/=b++;

}while(a>b);

A. 1,3 B. 1,4

C. 2,3 D. 2,4

14. 在C语言中,下列错误的说法是_______。

A. 函数可以递归调用

B. 不允许在函数中再定义函数

C. 递归程序不可能用非递归算法实现

D. 对于continue语句,可以通过改变程序的结构而不使用它

15.int a[2][3]={{1},{2,3}};,则a[1][0]的值是_________。

A.0

C. 2 B. 1 D. 3

16. 以下对二维数组a的正确说明是_________。.

A. int a[3][]; B. float a[][4];

C. double a[3][4]; D. float a(3)(4);

17.设char s[10]= "abcd",t[]="12345";,则s和t在内存中分配的字节数分别是_______。

A.6和5 B. 6和6

C. 10和5 D. 10和6

18. switch(表达式)语句中的“表达式”,允许的类型是_________。

A.float, int B.float, int, char

C. int, char D. char, double

19. 在C语言中,形参的隐含存储类别是_________。

A. auto B. static

C. extern D. register

20. 下列属于文件包含的命令是_________。

A. #define N 25 B. #endif

C. #include "stdio.h"

21. 设int i,j;

for(i=5;i;i--)

for(j=0;j<4;j++) {?}

则循环体执行次数是________。

A. 5 B.4

C. 20 D.无限次

22.若有以下定义和语句:

char strl[]="string", str2[5];

则用以复制字符串的正确方法是_______。

A. strcpy(str2,"Hello");

B. strcpy(strl,"Hello");

C. str2=str1;

D. str1="Hello"; D. #else

c语言程序设计题库 C语言程序设计试题

23.正确的变量定义是________。(www.61k.com)

A. unsigned long d=1000;

B. float m1=m2=10.0;

C. char c1='A',c2=A;

D. double x=0.618,x=3.14;

24.下面程序的输出结果是_______。

#include <stdio.h>

void main()

{ int s,k;

for(s=1,k=2;k<5;k++)

s+=k;

printf("%dn",s);

}

A. 1 B. 9

C. 10 D. 15

25.下列描述中不正确的是_______。

A. 字符型数组中可以存放字符串

B. 可以对字符型数组进行整体输入、输出

C. 可以对实型数组进行整体输入、输出

D. 不能在赋值语句中通过赋值运算符“=”对字符型数组进行整体赋值

二、填空题(本题共10小题,每小题2分,共20分)

请在每小题空格中填上正确答案.错填、不填均无分。

26. 一个C语言程序的执行起点是___________。

27. 设int x=3,y=5; 则执行printf("%dn",x&&y);后输出_____。

1? 28.定出公式?x?2??x?y??对应的C语言表达式______。 5x??

29. 若函数调用语句为f(a,b,f(a+b,a-b,b));,则函数f的参数个数是_______。

30. 设int a=2;,则执行a+=a*=15;后,变量a的值是_______。

31. 设double y[4][5];,则数组y中元素的个数是_______。

32. 设int x=7,y=3,则表达式x%y+x/y的值是________。

33. 设char s[ ]={"E_mail"};,则执行printf("%dn",strlen(s));后输出的值是_____。

34. 当执行下面程序段后,变量i的值是____________。

int a,b,c,d,i,j,k;

a=10; b=c=d=5; i=j=k=0;

for( ; a>b; ++b) i++;

while(a>++c) j++;

do k++; while(a>d++);

35.设int a[3][4]={{1,2,5,3},{2,4,7,9},{3,6,5,8}};则表达式a[1][2]+a[2][3]的值是________。

三、程序分析题(本题共5小题,每小题2分,共10分)

c语言程序设计题库 C语言程序设计试题

36.写出下面程序的运行结果。(www.61k.com)

#include <stdio.h>

void main()

{

int x=15;

while(x<20)

{ x++;

if(x/3!=0) {x++;break;}

else continue;

}

printf("%dn",x);

}

37. 写出下面程序的运行结果。

#include <stdio.h>

void inv (int x[ ], int n)

{int t,i,j;

for (i=0; i<n/2; i++)

{j=n-1-i;

t=x[i]; x[i]=x[j]; x[j]=t;}

}

void main()

{int i,a[5]={3,7,9,11,0};

inv(a,5);

for(i=0;i<5;i++)

printf("%d ",a[i]);

printf("n");

}

38. 写出下面程序的运行结果。(程序运行时,输入一个整数5) #include <stdio.h>

void main()

{

int a,b[10],c,i=0;

printf("输入一个整数n");

scanf("%d",&a);

while(a!=0)

{ c=a%2;

a=a/2;

b[i]=c;

i++;

}

for(;i>0;i--) printf("%d", b[i-1]);

}

c语言程序设计题库 C语言程序设计试题

39. 写出下面程序的运行结果。[www.61k.com)

#include <stdio.h>

void main()

{

int a,b,i;

a=b=0;

for(i=0;i<=10;i+=2)

{ a+=i;

b+=i+1;

}

printf("a=%dn",a);

printf("b=%dn", b-11);

}

40. 写出下面程序的运行结果。

#include <stdio.h>

int a=100;

s()

{ static int a =20;

a++;

printf("%dn",a);

}

void main()

{

int i;

for(i=1;i<=3;i++)

{ a++;

printf("%d",a);

s();

}

}

四、程序填充题(本题共2小题,每个空格2分,共10分)

给出下列程序,将横线处缺少的部分补上,使其能正确运行。

41. 对含有10个元素的数组a先按元素的存储次序倒序输入,再按其正序输出。 #include <stdio.h>

#define _____________ /* 空格1 */

void main()

{

int i,a[N];

for(i=N-1;________;i--) /* 空格2 */

scanf("%d",__________); /* 空格3 */

for(i=0;i<N;i++) printf("%dt",a[i]);

printf("n");

c语言程序设计题库 C语言程序设计试题

}

42.学生成绩的字每等级和分数范围如下:

字母等级 分数范圈

A 90~100

B

C 80~89 70~79

D 60~69

E 0~59

其它字母

#include <stdio.h>

void main()

{

__________; /* 空格4 */

scanf("%c",&score);

switch(score)

{

case 'A': printf("A:90~100n"); break;;

case 'B': printf("B:80~89n");break;

case 'C': printf("C:70~79n");break;

case 'D': printf("D:60~69n");break;

case 'E': printf("E:0~59n");break;

__________: printf("输入错误n"); /* 空格5 */

}

}

五、程序修改题(本题共2小题,共10分)

请在注释“ERROR”处修改程序中的错误,使它能得出正确的结果。(www.61k.com]注意:不要改动main 函数,不得增行或删行,也不得更改程序结构!

43.用指针变量输出数值。

#include <stdio.h>

void main()

{

int a[ ]={1,2,3,4,5,6,7,8,9,10},*p;

*p=a; /* ERROR1 */

printf("%fn",*p+9); /* ERROR2 */

}

44.利用函数求两个整数的最大公约数。

#include <stdio.h>

void main()

{ int gcd(int a, int b);

int a,b,abd;

printf("intput data a b:n"); 输入错误 输入字母等级之后,输出相应的分数范围。

c语言程序设计题库 C语言程序设计试题

scanf("%d,%d",a,b); /* ERROR3 */ abd=gcd(a,b);

printf("abd=%dn",abd);

}

int gcd(int x,int y); /* ERROR4 */ { int t;

if(x<y) {t=x;x=y;y=t;}

while(y!=0)

{ t=x%y;x=y;x=t;} /* ERROR5 */ return x;

}

c语言程序设计题库 C语言程序设计试题

参考答案:

==========

一、单项选择题

1.A

10.B 2.D 11.B 3. B 12.C

21.C 4. C 13.B 22.B 5.B 14.C 23.A 6.A 15.C 24.C 7.B 16.C 25.C 8.A 17.D 9.B 18.C 19.A 20.C

二、填空题

26. 主函数

27. 1

28. 1.0/2*(x+sqrt(x+y)/(5*x))

29. 3

30. 60

31. 20

32. 3

33. 6

34. 5

35. 15

三、程序分析题

36. 17

37. 0 11 9 7 3

38. 101

39. a=30

B=25

40. 10121

10222

10323

四、程序填充题

41. N 10、 i>=0、 &a[i]

42. char score、 default

五、程序修改题

43. *p=a; ? p=a;

%f ? %d

44. scanf("%d,%d",a,b) ? scanf("%d,%d",&a,&b) int gcd(int x,int y); ? int gcd(int x,int y) x=t ? y=t

二 : C#程序设计笔试题

4.一个长度为10000的字符串,通过随机从a-z中抽取10000个字符组成。请用c#语言编写主要程序来实现。

答:

using System.Text;

StringBuilder sb = new StringBuilder(0, 10000);

string strABC = “a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z”;

string[] ABC = strABC.Split(‘,’);

int len = ABC.Length;

Random rd = new Random();

for (int i = 0; i < 10000; i++)

{

sb.Append(ABC[rd.Next(len)]);

}

5.产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。

int[] intArr=new int[100];

ArrayList myList=new ArrayList();

Random rnd=new Random();

while(myList.Count<100)

{

int num=rnd.Next(1,101);

if(!myList.Contains(num))

myList.Add(num);

}

for(int i=0;i<100;i++)

intArr[i]=(int)myList[i];

2.如何把一个Array复制到ArrayList里

答:

foreach( object o in array )arrayList.Add(o);

1. 用C#写一段选择排序算法,要求用自己的编程风格。

答:private int min;

public void xuanZhe(int[] list)//选择排序

{

for (int i = 0; i < list.Length – 1; i++)

{

min = i;

for (int j = i + 1; j < list.Length; j++)

{

if (list[j] < list[min])

min = j;

}

int t = list[min];

list[min] = list[i];

list[i] = t;

}

}

4.写一个函数计算当参数为N的值:1-2+3-4+5-6+7……+N

答:public int returnSum(int n)

{

int sum = 0;

for (int i = 1; i <= n; i++)

{

int k = i;

if (i % 2 == 0)

{

k = -k;

}

sum = sum + k;

}

return sum;

}

public int returnSum1(int n)

{

int k = n;

if (n == 0)

{

return 0;

}

if (n % 2 == 0)

{

k = -k;

}

return aaa(n – 1) + k;

}

7. 某一密码仅使用K、L、M、N、O共5个字母,密码中的单词从左向右排列,密码单词必须遵循如下规则 :

(1) 密码单词的最小长度是两个字母,可以相同,也可以不同

(2) K不可能是单词的第一个字母

(3) 如果L出现,则出现次数不止一次

(4) M不能使最后一个也不能是倒数第二个字母

(5) K出现,则N就一定出现

(6) O如果是最后一个字母,则L一定出现

问题一:下列哪一个字母可以放在LO中的O后面,形成一个3个字母的密码单词?

A) K B)L C) M D) N

答案:B

问题二:如果能得到的字母是K、L、M,那么能够形成的两个字母长的密码单词的总数是多少?

A)1个 B)3个 C)6个 D)9个

答案:A

问题三:下列哪一个是单词密码?

A) KLLN B) LOML C) MLLO D)NMKO

答案:C

62-63=1 等式不成立,请移动一个数字(不可以移动减号和等于号),使得等式成立,如何移动?

答案:62移动成2的6次方

17、列出常用的使用javascript操作xml的类包

答:

XML.prototype.xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);

XML.prototype.InitXML=InitXML;

XML.prototype.getFirstChild=getFirstChild;

XML.prototype.getLastChild=getLastChild;

XML.prototype.getChild=getChild; // 取得节点值

XML.prototype.getNodeslength=getNodeslength; // 最得节点下的子节点的个数

XML.prototype.getNode=getNode; // 取得指定节点

XML.prototype.delNode=delNode; // 删除指定节点,如果节点相同,则删除最前面的节点.

XML.prototype.getNodeAttrib=getNodeAttrib; // 取得节点的指定属性值.

XML.prototype.InsertBeforeChild=InsertBeforeChild; // 在指定节点之前插入一个节点.

XML.prototype.InsertChild=InsertChild; // 在指定节点下插入节点.

XML.prototype.setAttrib=setAttrib; // 设置指定属性的值.

XML.prototype.setNodeValue=setNodeValue; // 设置指定节点的值.

XML.prototype.CreateNodeS=CreateNodeS; // 创建一个指定名的节点.

XML.prototype.addAttrib=addAttrib; // 为指定节点添加指定属性,并设置初值.

XML.prototype.FindString=FindString; // 在指定节点下查找字符串.

给定以下XML文件,完成算法流程图<FileSystem>

< DriverC >

<Dir DirName=”MSDOS622”>

<File FileName =” Command.com” ></File>

</Dir>

<File FileName =”MSDOS.SYS” ></File>

<File FileName =” IO.SYS” ></File>

</DriverC>

</FileSystem>

请画出遍历所有文件名(FileName)的流程图(请使用递归算法)。

答:

void FindFile( Directory d )

{

FileOrFolders = d.GetFileOrFolders();

foreach( FileOrFolder fof in FileOrFolders )

{

if( fof is File )

You Found a file;

else if ( fof is Directory )

FindFile( fof );

}

}

6.C#代码实现,确保windows程序只有一个实例(instance)

///<summary>

///应用程序的主入口点。

///</summary>

[STAThread]

staticvoid Main()

{

//防止程序多次运行

if(!OneInstance.IsFirst(“GetPayInfo”))

{

MessageBox.Show (“警告:程序正在运行中! 请不要重复打开程序!可在右下角系统栏找到!”,”程序错误提

示:”,MessageBoxButtons.OK,MessageBoxIcon.Stop);

return;

}

Application.Run(new Form1());

}

// ******************* 防止程序多次执行 **************************

publicabstractclass OneInstance

{

///<summary>

///判断程序是否正在运行

///</summary>

///<param name=”appId”>程序名称</param>

///<returns>如果程序是第一次运行返回True,否则返回False</returns>

publicstaticbool IsFirst(string appId)

{

bool ret=false;

if(OpenMutex(0x1F0001,0,appId)==IntPtr.Zero)

{

CreateMutex(IntPtr.Zero,0,appId);

ret=true;

}

return ret;

}

[DllImport("Kernel32.dll",CharSet=CharSet.Auto)]

privatestaticextern IntPtr OpenMutex(

uint dwDesiredAccess, // access

int bInheritHandle, // inheritance option

string lpName // object name

);

[DllImport("Kernel32.dll",CharSet=CharSet.Auto)]

privatestaticextern IntPtr CreateMutex(

IntPtr lpMutexAttributes, // SD

int bInitialOwner, // initial owner

string lpName // object name

);

}

6.一列数的规则如下: 1、1、2、3、5、8、13、21、34…… 求第30位数是多少, 用递归算法实现。

答:public class MainClass

{

public static void Main()

{

Console.WriteLine(Foo(30));

}

public static int Foo(int i)

{

if (i <= 0)

return 0;

else if(i > 0 && i <= 2)

return 1;

else return Foo(i -1) + Foo(i – 2);

}

}

8.请编程实现一个冒泡排序算法?

答:

int [] array = new int ;

int temp = 0 ;

for (int i = 0 ; i < array.Length – 1 ; i++)

{

for (int j = i + 1 ; j < array.Length ; j++)

{

if (array[j] < array[i])

{

temp = array[i] ;

array[i] = array[j] ;

array[j] = temp ;

}

}

}

11.在下面的例子里

using System;

class A

{

public A()

{

PrintFields();

}

public virtual void PrintFields(){}

}

class B:A

{

int x=1;

int y;

public B()

{

y=-1;

}

public override void PrintFields()

{

Console.WriteLine(“x={0},y={1}”,x,y);

}

}

当使用new B()创建B的实例时,产生什么输出?

答:X=1,Y=0;

3.下面的例子中

using System;

class A

{

public static int X;

static A(){

X=B.Y+1;

}

}

class B

{

public static int Y=A.X+1;

static B(){}

static void Main(){

Console.WriteLine(“X={0},Y={1}”,A.X,B.Y);

}

}

产生的输出结果是什么?x=1,y=2

15.根据委托(delegate)的知识,请完成以下用户控件中代码片段的填写:

namespace test

{

public delegate void OnDBOperate();

public class UserControlBase : System.Windows.Forms.UserControl

{

public event OnDBOperate OnNew;

privatevoidtoolBar_ButtonClick(objectsender,System.Windows.Forms.ToolBarButtonClickEventArgs e)

{

if(e.Button.Equals(BtnNew))

{

//请在以下补齐代码用来调用OnDBOperate委托签名的OnNew事件。

}

}

}

答:if( OnNew != null )

OnNew( this, e );

16.分析以下代码,完成填空

string strTmp = “abcdefg某某某”;

int i= System.Text.Encoding.Default.GetBytes(strTmp).Length;

int j= strTmp.Length;

以上代码执行完后,i= j=

答:i=13,j=10

17.给定以下XML文件,完成算法流程图。

<FileSystem>

< DriverC >

<Dir DirName=”MSDOS622”>

<File FileName =” Command.com” ></File>

</Dir>

<File FileName =”MSDOS.SYS” ></File>

<File FileName =” IO.SYS” ></File>

</DriverC>

</FileSystem>

请画出遍历所有文件名(FileName)的流程图(请使用递归算法)。

答:伪代码:

void FindFile( Directory d )

{

FileOrFolders = d.GetFileOrFolders();

foreach( FileOrFolder fof in FileOrFolders )

{

if( fof is File )

You Found a file;

else if ( fof is Directory )

FindFile( fof );

}

}

C#:

Public void DomDepthFirst(XmlNode currentNode)

{

XmlNode node=currentNode.FirstChild;

while(node!=null)

{

DomDepthFirst(node);

node=node.NextSibling;

}

if(node.Name==”File”)

{

Console.Write(((XmlElement)node).GetAttribute(“FileName”)+”\r\n”);

}

}

29.根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。

public void test(int i)

{

lock(this)

{

if (i>10)

{

i–;

test(i);

}

}

}

答:不会发生死锁,(但有一点int是按值传递的,所以每次改变的都只是一个副本,因此不会出现死锁。但如果把int换做一个object,那么死锁会发生)

78、下面的程序执行结果是什么?

class Person

{

public int Age { get; set; }

}

int i1 = 20;

int i2 = i1;

i1++;

Console.WriteLine(i2);

Person p1 = new Person();

p1.Age = 20;

Person p2 = p1;

p1.Age++;

Console.WriteLine(p2.Age);

答案:

20 、21

2、不用中间变量交换两个变量

int i = 500;

int j = int.MaxValue – 10;

//int i = 10;

//int j = 20;

Console.WriteLine(“i={0},j={1}”, i, j);

i = i + j;//i=30

j = i – j;//j=10;

i = i – j;//i=20;

Console.WriteLine(“i={0},j={1}”,i,j);

有一个10个数的数组,计算其中不重复数字的个数。{3,5,9,8,10,5,3}

用HashSet

int[] values = { 3, 5, 9, 8, 10, 5, 3 };

HashSet<int> set = new HashSet<int>();

foreach (int i in values)

{

set.Add(i);

}

foreach (int i in set)

{

Console.WriteLine(i);

}

点击table获取行号列号?

function getRowAndColumn()

{

if(!document.getElementsByTagName || !document.createTextNode)return;

varrows=document.getElementById(‘MyTable’).getElementsByTagName(‘tr’);

varcols;

for(i = 0; i < rows.length; i++)

{

rows[i].onclick=function()

{

10.   alert(“行:”+eval(this.rowIndex + 1));

11.   }

12.   if(i=0)

13.   {

14.   colsTH =rows[i].getElementsByTagName(‘th’);

15.   alert(colsTH.length);

16.   for(k = 0; k< colsTH.length; k++)

17.   {

18.   colsTH[k].onclick =function()

19.   {

20.   alert(“列:”+eval(this.cellIndex +1));

21.   }

22.   }

23.   }

24.   else

25.   {

26.   cols =rows[i].getElementsByTagName(‘td’);

27.   for(j = 0; j < cols.length; j++)

28.   {

29.   cols[j].onclick =function()

30.   {

31.   alert(“列:”+eval(this.cellIndex + 1));

32.   }

33.   }

34.   }

35.   }

36.   }

//相传有一群猴子要选出大王,它们采用的方式为:所有猴子站成一个圈,然后从1开始报数,每当数到”.

//”N的那一只猴子就出列,然后继续从下一个猴子开始又从1开始数,数到N的猴子继续出列,一直到最后”.

//”剩的猴子就是大王了。假如现在有M只猴子,报数数为N,请问第几只猴子是大王?列出选大王的过程。

int M = 10;

int N = 3;

List<int> monkeys = new List<int>();

for (int i = 1; i <= M; i++)

{

monkeys.Add(i);

}

int currentIndex = 0;

while (true)

{

for (int i = 1; i <= N; i++)

{

if (i == N)

{

monkeys.RemoveAt(currentIndex);

if (monkeys.Count == 1)

{

Console.WriteLine(monkeys[0]);

return;

}

}

currentIndex++;

if (currentIndex >= monkeys.Count)

{

currentIndex = 0;

}

}

}

38:请编程遍历页面上所有TextBox控件并给它赋值为string.Empty?

答:

foreach (System.Windows.Forms.Control control in this.Controls)

{

if (control is System.Windows.Forms.TextBox)

{

System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control ;

tb.Text = String.Empty ;

}

}

10. 程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)

要求: 1.要有联动性,老鼠和主人的行为是被动的。

2.考虑可扩展性,猫的叫声可能引起其他联动效应。

要点:1. 联动效果,运行代码只要执行Cat.Cryed()方法。2. 对老鼠和主人进行抽象

评分标准: <1>.构造出Cat、Mouse、Master三个类,并能使程序运行

<2>从Mouse和Master中提取抽象

<3>联动效应,只要执行Cat.Cryed()就可以使老鼠逃跑,主人惊醒。

public interface Observer

{

void Response(); //观察者的响应,如是老鼠见到猫的反映

}

public interface Subject

{

void AimAt(Observer obs); //针对哪些观察者,这里指猫的要扑捉的对象—老鼠

}

public class Mouse : Observer

{

private string name;

public Mouse(string name, Subject subj)

{

this.name = name;

subj.AimAt(this);

}

public void Response()

{

Console.WriteLine(name + ” attempt to escape!”);

}

}

public class Master : Observer

{

public Master(Subject subj)

{

subj.AimAt(this);

}

public void Response()

{

Console.WriteLine(“Host waken!”);

}

}

public class Cat : Subject

{

private ArrayList observers;

public Cat()

{

this.observers = new ArrayList();

}

public void AimAt(Observer obs)

{

this.observers.Add(obs);

}

public void Cry()

{

Console.WriteLine(“Cat cryed!”);

foreach (Observer obs in this.observers)

{

obs.Response();

}

}

}

class MainClass

{

static void Main(string[] args)

{

Cat cat = new Cat();

Mouse mouse1 = new Mouse(“mouse1″, cat);

Mouse mouse2 = new Mouse(“mouse2″, cat);

Master master = new Master(cat);

cat.Cry();

}

}

//———————————————————————————————

设计方法二: 使用event — delegate设计..

public delegate void SubEventHandler();

public abstract class Subject

{

public event SubEventHandler SubEvent;

protected void FireAway()

{

if (this.SubEvent != null)

this.SubEvent();

}

}

public class Cat : Subject

{

public void Cry()

{

Console.WriteLine(“cat cryed.”);

this.FireAway();

}

}

public abstract class Observer

{

public Observer(Subject sub)

{

sub.SubEvent += new SubEventHandler(Response);

}

public abstract void Response();

}

public class Mouse : Observer

{

private string name;

public Mouse(string name, Subject sub) : base(sub)

{

this.name = name;

}

public override void Response()

{

Console.WriteLine(name + ” attempt to escape!”);

}

}

public class Master : Observer

{

public Master(Subject sub) : base(sub){}

public override void Response()

{

Console.WriteLine(“host waken”);

}

}

class Class1

{

static void Main(string[] args)

{

Cat cat = new Cat();

Mouse mouse1 = new Mouse(“mouse1″, cat);

Mouse mouse2 = new Mouse(“mouse2″, cat);

Master master = new Master(cat);

cat.Cry();

}

}

三 : 索爱笔试题程序笔试


索爱笔试分两个阶段,每阶段一个小时,一共两个小时,第一阶段,试卷为英文,考逻辑和智力题,四道逻辑大题,考的都是总结推理之类的,选择题。例如,一条路上住了七家房子,谁家住谁家东边,谁家住谁家西边谁家不住在头尾,谁家正好住在第几座房子等等,每个大题有6、7道小问题,问的无非也是谁家可能住在几号房子,哪两家不可能是邻居等,其它三道题目也类似。一个是三对夫妇点菜,一个是三层楼办车展。最后一题是关于飞行表演中的飞行员分配,我没看懂题目,瞎填了一气:(还有一道智力题,是九个九宫格,呈九宫方位排列,一共81个格,填入9组1~9,让每行每列以及每个小九宫格里边,1~9都只出现一次。已经给出了十几个数字,填剩下的。

第二阶段是技术笔试。我投的是软件测试,笔试试卷是中文描述的。考的都是c语言。

一、索爱笔试题:用变量a定义以下类型:(这道题目是中英文描述,我为了抄的简单就写英文了。)

a) an integer

b) a pointer to an integer

c) a pointer to a pointer to an integer

d) an array of 10 integers

e) an array of 10 pointers to integers

f) a pointer to an array of 10 integers

g) a pointer to a function that makes an integer as an argument and returns an integer

h) an array of 10 pointers to functions that makes an integer as an argument and returns an integer

二、索爱笔试题:关键字static的作用是什么?

三、索爱笔试题:这段代码有什么问题?

strunt s_a{int a[10];};void f(){int i;strunt s_a *s_ptr;for (i=0,i10,i )s_ptr-a[i]=i;}四、写一个字符串反转函数,比如把abcd变成dcba,要求在空间和时间上尽量优化。函数原型char* strrev(char* dest, char* src);

四、索爱笔试题:写链表操作函数

链表节点定义如:struct listnode{long id;struct listnode* next;}写一个返回下一个节点的函数,没有的话返回0struct listnode* listnext(struct listnode* cur);写一个在已经排序好的链表中插入一个节点的函数,返回插入位置的前一个节点如果节点已经在链表里边返回0struct listnode* listinsert(struct listnode* head,struct listnode* newnode);写一个删除节点的函数,返回删除后的前一个节点,如果节点不在链表里边返回0struct listnode* listremove(struct listnode* head,struct listnode* thenode)

本文标题:c语言程序设计试题-C语言程序设计试题
本文地址: http://www.61k.com/1067496.html

61阅读| 精彩专题| 最新文章| 热门文章| 苏ICP备13036349号-1