在有N个缓冲区的生产者消费者的问题中,下列叙述中哪些是错误的?producer() { int item; while(TRUE) { item = produce_item(); P(empty); P(mutex); insert_item(item); V(mutex) V(full); } } consumer() { int item; while(TRUE) { P(full); P(mutex); item = remove_item(); V(mutex); V(mutex); consume_item(item); } }
A.信号量empty的初值为N
B.信号量full的初值为0
C.信号量mutex的初值为0
D.P(full)和P(mutex)两条语句可以颠倒顺序
E.V(mutex)和V(mutex)两条语句可以颠倒顺序
第1题:
#include<iostream.h> void main( ) { int n=9; while(n>6) { n--; cout < < n;} } 该程序的输出结果是
A.987
B.876
C.8765
D.9876
第2题:
下列程序是死循环的是
A.for(;;);
B.int s=36; while(S)--S;
C.int m=10; do { m- - }while(m>0);
D.int n=5; while(n>1) { n- -; if(n<0)break; }
第3题:
下列程序是死循环的是 ( )
A.for(;;)
B.int s=36; while(s)=s;
C.int m=10; do{m- -;}while(m>0);
D.int n=5; while(n>1) { n- -; if(n<0)break; }
第4题:
下面程序的运行结果是( )。 include<stdio.h> main() {int a,b,c,n; a=2;b=0;c=1;n=1; while(n<=3) {c=c*a;b=_b+c;++n;} printf("b=%d",B); }
第5题:
下列给定程序中,函数fun()的功能是:依次取出字符串中所有的数字字符,形成新的字符串,并取代原字符串。
请改正函数fun()中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <stdio.h>
include <conio.h>
void fun(char *s)
{int i,j;
for(i=0,j=0; s[i]!= '\0'; i++)
if(s[i]>= '0'&&s[i]<= '9')
/*************found**************/
s[j]=s[i];
/*************found**************/
s[j]="\0";
}
main()
{char item[80];
clrscr();
printf("\nEnter a string: ");gets(item);
printf("\n\nThe string is:\%s\n", item);
fun(item);
printf("\n\nThe string of changing is :
\%s\n",item);
}
第6题:
下面程序的输出结果是( )。 #include<iostream> using namespace std; int main() { int n=10; while(n>7){ n--; cout<<11<<','; } cout<<endl; }
A.10,9,8,
B.9,8,7,
C.10,9,8,7,
D.9,8,7,6,
第7题:
av9功能是求整形数组中的前若干个元素的平均值,设数组元素个数最多不超过则下列函数说明语句错误的是( )。
A.int avg(int*a,int n);
B.int avg(int a[lO],int n);
C.int avg(int a,int n);
D.int avg(int a[],int n);
第8题:
定义如下变量: int n=10; 则下列循环的输出结果是( )。 while(n>7) { -n; printf("%d\n",n); }
A.1098
B.987
C.10987
D.9876
第9题:
下列选项中,不能输出100个整数的是( )。
A.for(int i=0;i<100;i++) System.out.println(i);
B.int i=0; do{ System.out.println(i); i++; }while(i<100);
C.int i=0; while(i<100){ System.out.println(i); i++; }
D.int i=0; while(i<100){ i++; if(i<100)continue; System.OUt.println(i); }
第10题:
()阅读下列说明和C语言程序,将应填入 (n)处的语句写在答题纸的对应栏内。[说明]设有定义 #define ITEM struct item #define SIZE sizeof(ITEM) ITEM { int num; ITEM *next; }; ITEM *head=NULL; 下述函数定义实现按插表尾形式(即每一新表元素插入至当前所生成链表的表尾之后)生成一个正向线性链表。最后指向所生成链表表头的指针作为函数值返回。为生成一个线性链表,要求输入一批整型数据,并以-1作为结束标志。请填空完善程序。 ITEM *gene(ITEM *head) { ITEM *temp,*tail;/*tail指向当前链表尾结点*/ int intno; printf("Enter integer NO.,-1 to stop:\n"); scanf("%d",&intno); while (intno!=-1) { temp=(____(1)_____)malloc(SIZE); temp->num=intno; if (head==_____(2)_____) /*空表*/ { head=temp; tail=______(3)______; } else /*非空表*/ { ____(4)_______=temp; tail=temp; } scanf("%d",&intno); } return (_______(5)_______);
第11题:
1
6
8
9
第12题:
int function(int m,int n);
int function(int,int);
int function(int m=3,int n);
hat function(int&m,int&n);
第13题:
A.1
B.6
C.8
D.9
第14题:
本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入DataPool类中,消费者从中取出数据。DataPool类一次只能存放一个数据。请更正题中带下划线的部分。
注意:不改变程序的结构,不得增行或删行。
class DataPool
{
private int data;
private boolean isFull;
public DataPool()
{
isFull=false;
}
public synchronized void putData(int d)
{
if(isFull= =true)
{
try
{
this.notify();
}
catch(InterruptedException e)
{}
}
data=d;
isFull=true;
System.out.println("生产了一个数据:"+data);
this.notify();
}
public synchronized int getData()
{
if(isFull= =false)
{
try
{
this.wait();
}
catch(InterruptedException e)
{}
}
isFull=false;
System.out.println("消费了一个数据"+data);
this.wait();
return this.data;
}
boolean getIsFull()
{
return isFull;
}
}
class Producer extends Thread
{
DataPool pool;
public Producer(DataPool pool)
{
this.pool=pool;
}
public void run()
{
for(int i=0; i<10; i++)
{
int data=(int) (Math.random()*1000);
try
{//用于生产数据
sleep(data);
}
catch(InterruptedException e)
{}
pool.putData(data);
}
}
}
class Consumer implements Runnable
{
DataPool pool;
public Consumer(DataPool pool)
{
this.pool=pool;
}
public void run()
{
for(int i=0; i<10; i++)
{
int data=pool.getData();
try
{//用于处理数据
sleep((int) (Math.random()*1000));
}
catch(InterruptedException e)
{}
}
}
}
public class advance
}
public static void main(String[] args)
{
Data Pool pool=new Data Pool();
Producer pro=new Producer(pool);
Runnable con=new Consumer(pool);
Thread conTh=new Thread(con);
&n
第15题:
下列函数原型声明中,错误的是
A.int function(int m,int n);
B.int function(int,int);
C.int function(int m=3,int n);
D.int function(int&m,int&n);
第16题:
avg函数的功能是求整型数组中的前若干个元素的平均值,设数组元素个数最多不超过10,则下列函数说明语句错误的是
A)int avg(jnt *a,int n.);
B)int avg(int a[10],int n);
C)int avg(int a,int n);
D)int avg(int a[ ],int n);
第17题:
int Calc(unsigned int x)
{
int count=0;
while(x)
{
printf("x=%i\n",x);
count++;
x=x&(x-1);
}
return count;
}
问 Calc(9999)的值是多少。
第18题:
下列给定程序中函数fun()的功能是计算1/n!的值。
例如:给n输入5,则输出0.0083330
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <stdio.h>
include <conio.h>
/******+****found*************/
int fun(int n)
{double result=1.0;
if(n==0)
return 1.0;
while(n>1 && n<170)
/**********found************/
result *=n++;
result=1/result;
return result;
}
main()
{
int n;
printf("Input N:");
scanf("%d",&n);
printf("\n1/%d!=%If\n",n,fun(n));
}
第19题:
下列给定程序中,函数fun()的功能是:依次取出字符串中所有的字母,形成新的字符串,并取代原字符串。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include<stdio.h>
include<conio.h>
void fun(char*S)
{
int i,j;
for(i=0,j=0;s[i]! ='\0';i++)
/*************found************/
if((s[i]>='A'&&s[i]<='Z')&&(s[i]>= 'a'&&S[i]<='z',))
s[j++]=s[i];
/*************found*************/
s[j]=“\0”;
}
main()
{
char item[80];
clrscr();
printf(“\nEnter a string:”);
gets(item);
printf(“\n\nThe string is:\%S\n",item);
fun (item);
printf("\n\nThe string of changing
is:\%S\n",item);
}
第20题:
有如下程序: #include<stdio.h> main() { int n=9; while(n>6){n--;printf("%d",n);} } 该程序的输出结果是( )。
A.987
B.876
C.8765
D.9876
第21题:
下列各程序段中,不是死循环的是( )。
A.int j=100;
B.for(;;) while(1) {j=j%100+1; if(j>1000) break; }
C.int n=0;
D.int m=36; do{ while(m)--m; ++n; }while(n>=0);
第22题:
有9个生产者,6个消费者,共享容量为8的缓冲区。在这个生产者-消费者问题中,互斥使用缓冲区的信号量mutex的初值应该为()。
第23题:
int avg(int *a,int n);
int avg(int a[10],int n);
int avg(int a,int n);
int avg(int a[],int n);