class Waiting implements Runnable { boolean flag = false; public synchronized void run() { if (flag) { flag = false; System.out.print("1 "); try { this.wait(); } catch (Exception e) { } System.out.print("2 "); } else { flag = true; System.out.print("3 "); try { Thread.sleep(2000); } catch (Exception e) { } System.out.print("4 "); notify(); } } public static void main(String [] args) { Waiting w = new Waiting(); new Thread(w).start(); new Thread(w).start (); } } 以下哪两项是正确的?()
第1题:
本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入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
第2题:
public class Test{public static void main(String[]args){intx=5;boolean b1=true;boolean b2=false;if((x==4)&&!b2)System.out.print("l");System.out.print("2");if((b2=true)&&b1)System.out.print("3");}}What is the result?()
A.2
B.3
C.12
D.23
E.123
F.Compilation fails.
G.An exceptional ist hrown at runtime.
第3题:
以上程序段运行的结果是:______. Dim a(-1 To 5) As Boolean Dim flag As Boolean flag = False Dim i As Integer Dim j As Integer Do Until flag = True For i = -1 To 5 j=j+1 if a(i)= False Then a(i)= True Exit For End If If i = 5 Then flag = True End If Next Loop Print j
A.20
B.7
C.35
D.8
第4题:
以下程序段运行的结果是______。Dim a(-1 To 5) As BooleanDim flag As Booleanflag = FalseDim i As IntegerDim j As IntegerDo Until flag = True For i=-1 To 5 j=j+1 If a(i)= False Then a(i)= True Exit For End If If i = 5 Then flag = True End If NextLoopPrint j
A.20
B.7
C.35
D.8
第5题:
如果一个正整数从高位到低位上的数字依次递减,则称其为降序数(如:9632是降序数,而8516则不是降序数)。现编写如下程序,判断输入的正整数是否为降序数。 Private Sub Command1 Click( ) Dim n As Long Dim flag As Boolean n=InputBox("输入一个正整数") S=Trim(Str(n)) For i=2 To Len(s) If Mid(s,i-1,1)<Mid(S,i,1)Then Exit For Next i If i=Len(S)Then flag=True Else flag=False If flag Then Print n;"是降序数" Else Print n;"不是降序数" End If End Sub 运行以上程序,发现有错误,需要对给flag变量赋值的If语句进行修改。以下正确的修改是( )。
A.If i=Len(s)+1 Then flag=False Else flag=True
B.If i=Len(s)+1 Then flag=True Else flag=False
C.If i=Len(s)-1 Then flag=False Else flag=True
D.If i=Len(s)-1 Then flag=True Else flag=False
第6题:
classWaitingimplementsRunnable{
booleanflag=false;
publicsynchronizedvoidrun(){
if(flag){
flag=false;
System.out.print("1");
try{this.wait();}catch(Exceptione){}
System.out.print("2");
}
else{
flag=true;
System.out.print("3");
try{Thread.sleep(2000);}catch(Exceptione){}
System.out.print("4");
notify();
}
}
publicstaticvoidmain(String[]args){
Waitingw=newWaiting();
newThread(w).start();
newThread(w).start();
}
}
以下哪两项是正确的?()
第7题:
A.000000000
B.0123456789
C.Compilationfails.
D.Thecoderunswithnooutput.
E.Thecodeentersaninfiniteloop.
F.Anexceptionisthrownatruntime.
第8题:
( 18 )阅读下列程序
Public class Test implements Runnable{
Private int x=0;
Private int y=o;
boolean flag=true;
Public static void main(string[ ] args) {
Test r =new Test( );
Thead t1=new Thead(r);
Thead t2=new Thead(r);
t1.start( );
t2.start( );
}
Public void run(){
While(flag) {
x++;
y++;
system.out.println( “ ( ” +x_ “ , ” +y+ ” ) ” );
if (x>=10)
flag=false;
}
}
}
下列对程序运行结果描述的选项中,正确的是
A)每行的( x,y )中,可能有;每一对( x,y )值都出现两次。
B)每行的( x,y )中,可能有;每一对( x,y )值仅出现一次。
C)每行的( x,y )中,可能有 x=y ;每一对( x,y )值都出现两次。
D)每行的( x,y )中,可能有 x=y ;每一对( x,y )值都出现一次。
第9题:
下列变量定义不合法的是( )。
A、boolean flag=false;
B、int k=1+'k';
C、char ch="c";
D、float r=1/2;
第10题:
阅读下列程序: Public class Test implements Runnable{ Private int x=0l Private int y=0; boolean flag=true; Public static void main(string[]args){ Test r=new Test; Thead tl=new Thead(r); Thead t2=new Thead(r); tl.start; t2.start; } Public void run{ While(flag){ x++; y++; system.out.println("("+x-","+y+")"); if(x>=10) flag=false; } } } 下列对程序运行结果描述的选项中,正确的是( )。
A.每行的(x,y)中,可能有x≠y;每一对(x,y)值都出现两次
B.每行的(x,y)中,可能有x≠y;每一对(x,y)值仅出现 一次
C.每行的(x,y)中,可能有x=y;每一对(x,y)值都出现两次
D.每行的(x,y)中,可能有x=y;每一对(x,y)值都出现