public
不使用修饰符
private
protected
第1题:
( 12 )请将下面的程序补充完整,使得程序输出 “ 飘是张娜的书 ” 。
#include <iostream>
using namespace std;
class Book{
public:
Book(char *str) {strcpy(title,str);}
【 12 】 void PrintInfo() {cout<<title<<endl;}
protected:
char title[50];
};
class MyBook:public Book{
public:
MyBook(char *s1,char *s2= " 张娜 " ):
【 13 】 {strcpy(owner,s2);}
virtual void PrintInfo() {cout<<title<< " 是 " owner<< " 的书 " <<endl;}
private:
char owner[10];
};
int main(){
Book *prt=new MyBook( " 飘 " );
prt->PrintInfo();
return 0;
}

第2题:
如下程序的输出结果是______。
include<iostream>
using namespace std;
class Pet{
char name[10];
public:
Pet(char*nanle){strcpy(this->name,name);}
const char*getName( )const{return name;}
virtual void call( )eonst=0;
};
class Dog:public Pet{
public:
Dog(char*name):Pet(name){ }
void call( )eonst{cout<<"汪汪叫";}
};
class Cat:public Pet{
public:
Cat(char*name):Pet(name){ }
void call( )const{eout<<"喵喵叫";}
};
int main( ){
Pet*petl=new Dog("哈克"),*pet2=new Cat("吉米");
eout<<petl->getName( );petl->call( );eout<<endl;
cout<<pet2->getName( );pet2->call( );eout<<endl;
return 0;
}
第3题:
本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入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
第4题:
阅读下面一个支持多线程并发操作的堆栈类代码 public class MyStack { private int idx=0; private int[] data=new int[8]; public ______ Void push(int i) { data[idx]=i; idx++; } … } 在下画线处应填入的是
A.synchronized
B.wait
C.blocked
D.interrupt
第5题:
阅读下面实现堆栈类并发控制的部分代码 public class DataStack{ private int idx=0; private int[]data=new int[8]; public void push(int i){ . ________________{ data[idx]=i; idx + +; } } } …… } 在程序下画线处填入正确选项是
A.synchronized
B.synchronized(this)
C.synchronized()
D.synchronized(idx)
第6题:
以下程序运行后的输出结果是______。
include <iostream>
include <string>
using namespace std;
class Y;
class X
{
int x;
char *strx;
public:
X(int a, char *str)
{
x=a;
strx=new char[strlen(str)+1]
strcpy (strx,str);
}
void show(Y &ob);
};
class Y
{
prlvate:
int y;
char *stry;
public:
Y(int b,char *str)
{
y=b;
stry=new char[strlen(str)+1];
strcpy(stry,str);
}
friend void X::show(Y &ob);
};
void X::show{Y &ob)
{
cout<<strx<<",",
cout<<ob.stry<<endl;
}
int main{
{
X a (10, "stringX");
Y b (20, "stringY");
a. show (b);
renurn 0;
}
第7题:
为了支持压栈线程与弹栈线程之间的交互与同步,应 在下画线处填入的选项是( )。 public class StackTest{ private int idx=0; private int[]data=new int[8] public void push(int i){ synchronized(this){ ; data(idx)=i: idx++: } } }……
A.this.notify
B.interrupt
C.this.wait
D.sleep
第8题:
为了支持压栈线程与弹栈线程之间的交互与同步,在程序的下画线处依次填入的语句是( )。 public class IntStack{ private int idx=0; private int[]data=new int[8]; public void push(int i){ data[idx]=i: idx++; … … }
A.synchronized notify
B.synchronized this.wait
C.synchronized this.notify
D.Serializable sleep
第9题:
阅读下面实现堆栈类并发控制的部分代码 public class DataStack } private int idx=0; private int[] data=new int[8]; public void push(int i) { ______ { data[idx]=I: idx++; } } … } 程序中下画线处应填入的正确选项是
A.synchronized
B.synchronized(this)
C.synchronized()
D.synchronized(idx)
第10题:
以下哪个方法可以用来获得进度条的当前进度值?()
第11题:
public class NamedCounter { private final String name; private int count; public NamedCounter(String name) { this.name = name; } public String getName() { return name; } public void increment() { coount++; } public int getCount() { return count; } public void reset() { count = 0; } } Which three changes should be made to adapt this class to be used safely by multiple threads? ()
第12题:
public
不使用修饰符
private
protected
第13题:
阅读下列一个支持多线程并发操作的堆栈类代码段 public class MyStack{ private int idx=0; private int[]data=new int[8]; public______void push(int i){ data[idx]=i; idx + +; } …… } 在下画线处应填入的是
A.synchronized
B.wait
C.blocked
D.interrupt
第14题:
在堆栈类SharedStack的定义中,为了保证堆栈在并发操作中数据的正确性,应在下划线处填入的修饰符是(两个下划线的填写内容相同)
public class SharedStack(
______int idx=0;
______char[]data=new char[10];
public synchronized void push(char C) {......}
public synchronized void pop{}{......}
}
A) public
B) 不使用修饰符
C) private
D) protected
A.
B.
C.
D.
第15题:
在堆栈类Sharedstack的定义中,为了保证堆栈在并发操作中数据的正确性,应在下画线处填人的修饰符是(两个下画线的填写内容相同)( )。
A.puhlic
B.不使用修饰符
C.private
D.protected
第16题:
有如下程序:
include<iostream>
using namespace std;
class Pet{
char name[10];
public:
Pet(char*name){strcpy(this->name,name);}
const char*getName()const {return name;}
virtual void call()const=0;
};
class Dog:public Pet{
public:
Dog(char*name):Pet(name){}
void call()const{cout<<"汪汪叫":}
};
class Cat:public Pet{
public:
Cat(char*name):Pet(name){}
void call()const{cout<<"喵喵叫";}
};
int main(){
Pet*pet1=new Dog("哈克"),*pet2=new Cat("吉米");
cout<<pet1->getName();pet1->call();cout<<end1;
cout<<pet2->getName();pet2->call();cout<<end1;
return 0;
}
程序的输出结果是______。
第17题:
为了支持压栈线程与弹栈线程之间的交互与同步,在下画线处依次填入的语句是 public class IntStack { private int idx=0; private int[]data=new int[8]; public ______ void push(int i) { data[idx]=i; idx++; ______ } … }
A.synchronized() notify()
B.synchronized() this.wait()
C.synchronized() this.notify()
D.synchronized() sleep()
第18题:
有以下程序: #include <iostream> #include <string> using namespace std; class Y; class X { private: int x; char *strx; public: X(int a, char *str) { x=a; strx=new char[strlen(str)+1]; strcpy(strx,str); } void show(Y &ob) ; }; class Y { private: int y; char *stry; public: Y(int b,char *str) { y=b; stry=new char[strlen(str)+1]; strcpy(stry, str); } friend void X: :show(Y &ob) ; }; void X: :show(Y &ob) { cout<<strx<<", "; cout<<ob, stry<<end1; } int main ( ) { X a(10,"X"); Y b (20, "Y"); a. show(B) ; return 0; } 执行后的输出结果是( )。
A.X,Y
B.a,b
C.X,X
D.Y,Y
第19题:
为了支持压栈线程与弹栈线程之间的交互与同步,应在下画线处填入的选项是( )。 public class StackTest{ private int idx=0; private int[]data=new int[8] public void push(int i){ synchronized(this)( ; data(idx)=i: idx++: } } }… …
A.this.notify
B.interrupt
C.this.wait
D.sleep
第20题:
在堆栈类Sharedstack的定义中,为了保证堆栈在并发操作中数据的正确性,应在下画线处填入的修饰符是(两个下画线的填写内容相同)( )。 public class SharedStack{ int idx=0; char[]data=new char[10]; public synchtonized void push(char c){……} publR,synchronized void pop{……} }
A.Dublic
B.不使用修饰符
C.private
D.protected
第21题:
( 31 ) 为了支持压栈线程与弹栈线程之间的交互与同步 , 在程序的下划线处依次填入的语句是
public class IntStack{
private int idx=0;
private int[] data=new int[8];
public void push(int i){
data[idx]=i;
idx++;
}
__________
......
}
A ) synchronized()
notify()
B ) synchronized()
this.wait()
C ) synchronized()
this.notify()
D ) synchronized()
sleep()
第22题:
class Account { private int balance; public void setBalance(int b) { balance = b; } public int getBalance() { return balance; } public void clearBalance() { balance = 0; } } 哪一个改变可以使 Account 类线程安全?()
第23题:
public class TestFive { private int x; public void foo() { int current = x; x = current + 1; } public void go() { for(int i=0;i<5;i++) { new Thread() { public void run() { foo(); System.out.print(x + “, “); } }.start(); }}} Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ?()
第24题:
在第2行加 synchronized 修饰符。
在第3行加 synchronized 修饰符。
在第3行、第4行和第6行加 synchronized 修饰符。
在第4行、第6行和第8行加 synchronized 修饰符。