关于数组的使用,请阅读以下代码: 其正确的输出结果为()
A.aa
B.bb
C.aabb
D.bbaa
A.aa
B.bb
C.aabb
D.bbaa
第1题
阅读以下说明和C++代码,将应填入(n)处的字句写在对应栏内。
【说明】
C++标准模板库中提供了vector模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空间为std。vector模板类的部分方法说明如下表所示:
【C++代码】
include <iostream>
include <vector>
using namespace (1);
typedef vector< (2) > INTVECTOR;
const int ARRAY_SIZE = 6;
void ShowVector (INTVECTOR &theVector);
int main() {
INTVECTOR theVector;
// 初始化 theVector, 将theVector的元素依次设置为0至5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++}
theVector.push_back((3));
ShowVector(theVector); // 依次输出theVector中的元素
theVector.erase (theVector.begin () + 3};
ShowVector(theVector);
}
void ShowVector (INTVECTOR &theVector) {
if (theVector.empty ()) {
cout << "theVector is empty." << endl; return;
}
INTVECTOR::iterator (4);
for (theIterator=theVector.begin(); theIterator !=theVector.end(); theIterator++) {
cout << *theIterator;
if (theIterator != theVector.end()-1) cout << ", ";
}
cout << end1;
}
该程序运行后的输出结果为:
0,1,2,3,4,5
(5)
第2题
试题六(共 15 分)
阅读以下说明和 C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
C++标准模板库中提供了 vector 模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空间为 std。vector模板类的部分方法说明如下表所示:
[C++代码]
include <iostream>
include <vector>
using namespace (1) ;
typedef vector< (2) > INTVECTOR;
const int ARRAY_SIZE = 6;
void ShowVector(INTVECTOR &theVector);
int main(){
INTVECTOR theVector;
// 初始化 theVector,将 theVector的元素依次设置为 0 至 5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.push_back((3) );
ShowVector(theVector); // 依次输出 theVector中的元素
theVector.erase(theVector.begin() + 3);
ShowVector(theVector);
}
void ShowVector(INTVECTOR &theVector) {
if (theVector.empty()) {
cout << "theVector is empty." << endl; return;
}
INTVECTOR::iterator (4) ;
for (theIterator = theVector.begin(); theIterator != theVector.end(); theIterator++){
cout << *theIterator;
if (theIterator != theVector.end()-1) cout << ", ";
}
cout << endl;
}
该程序运行后的输出结果为:
0, 1, 2, 3, 4, 5
(5)
第3题
阅读以下说明和Java代码,填补空缺。
[说明]
java.util库中提供了Vector模板类,可作为动态数组使用,并可容纳任意数据类型。
该类的部分方法说明如下所示:
方法名 含义
add(k) 向vector对象的尾部添加一个元素k
removeElementAt(i) 删除序号为i的元素(vector元素序号从0开始)
isEmpty() 判断vector对象是否含有元素
size() 返回vector对象中所包含的元素个数
[Java代码]
Import ________;
public class JavaMain {
static private final int ________ =6;
public static void main(String[]args) {
Vector theVector=new Vector< _______ >();
//初始化theVector,将theVector的元素设置为0至5
for(int cEachItem=0; cEachItem<ARRAY_SIZE; cEachItem++)
theVector. add(________ );
showVector(theVector); //依次输出theVector巾的元素
theVector. removeElementAt(3);
showVector(theVector);
}
public static void showVector(Vector theVector){
if(theVector. isEmpty()){
System.out.printin("theVector is empty.");
return;
}
for(int loop=0; loop<theVector.size(); loop++) {
System.out.print(theVector.get(loop));
System.out.print(",");
}
System.out.printin();
}
}
该程序运行后的输出结果为:
0, 1, 2, 3, 4, 5
___________
第4题
阅读以下技术说明和C语言代码,根据要求回答问题1至问题6。
【说明】
有两个进程(编号分别为0和1)需要访问同一个共享资源。为了解决竞争条件(race condition)的问题,需要实现一种互斥机制,使得在任何时刻只能有一个进程访问该共享资源。以下【C代码1】给出了一种实现方法。
【C代码1】
int flag[2]; /+flag数组,初始化为FALSE*/
Enter_Critical_Section(int my_task_id, int other_task_id)
{ while (flag[other_task_id]==TRUE); /*空循环语句*/
flag[my_task_id]=TRUE;
}
Exit_Critical_Section(int my_task_id, int other_task_id)
{ flag[my_task_id]=FALSE;
}
当一个进程要访问临界资源时,就可以调用【C代码1】给出的这两个函数。【C代码2】给出了进程0的一个例子。
【C代码2】
Enter_Critical_Section(0,1);
……使用这个资源……
Exit_Critical_Section(0,1);
……做其他的事情……
什么是临界资源(critical resource)?请用100字以内的文字简要说明。
第5题
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
java.util库中提供了Vector模板类,可作为动态数组使用,并可容纳任意数据类型。该类的部分方法说明如下表所示:
【Java代码】
import (1);
public class JavaMain {
static private final int (2)= 6;
public static void main(String[] args){
Vector<Integer> theVector = new Vector< (3) >();
// 初始化 theVector, 将theVector的元素设置为0至5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.add((4));
showVector(theVector); // 依次输出theVector中的元素
theVector.removeElementAt(3);
showVector(theVector);
}
public static void showVector(Vector<Integer> theVector
if (theVector.isEmpty()) {
System.out.println("theVectcr is empty.");
return;
}
for (int loop = 0; loop < theVector.size(); loop++)
System.out.print(theVector.get(loop));
System.out.print(", ");
}
System.out.println();
}
}
该程序运行后的输出结果为:
0,1,2,3,4,5
(5)
第6题
A.程序第二行出错,因为没有指定下标
B.值为’bb’的元素的下标为0
C.值为’bb’的元素的下标为1
D.值为’bb’的元素的下标为3
第7题
试题七(共 15 分)
阅读以下说明和 Java 代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
java.util 库中提供了 Vector 模板类,可作为动态数组使用,并可容纳任意数据类型。该类的部分方法说明如下表所示:
[Java 代码]
import (1) ;
public class JavaMain {
static private final int (2) = 6;
public static void main(String[] args){
Vector<Integer> theVector = new Vector< (3) >();
// 初始化 theVector,将 theVector的元素设置为 0 至 5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.add((4) );
showVector(theVector); // 依次输出 theVector中的元素
theVector.removeElementAt(3);
showVector(theVector);
}
public static void showVector(Vector<Integer> theVector){
if (theVector.isEmpty()) {
System.out.println("theVector is empty.");
return;
}
for (int loop = 0; loop < theVector.size(); loop++) {
System.out.print(theVector.get(loop));
System.out.print(", ");
}
System.out.println();
}
}
该程序运行后的输出结果为:
0, 1, 2, 3, 4, 5
(5)
第8题
试题四(共20 分)
阅读以下说明和 C 语言代码, 回答问题 1 至问题3, 将解答填入答题纸的对应栏内。
【说明】
在实时系统中, 许多控制软件需要将数据封装到一个数据结构中, 以节省存储空间。对于位操作,使用汇编语言实现其访问比较容易,但会增加编程难度,因此现在普遍采用 C 语言实现。使用高级语言编程要特别注意结构的存储格式以及编译器的特性。本题所使用的编译器对变量按声明顺序分配地址。分析图 4-1所示的 C 语言代码,回答以下问题。
【问题 1】 (8 分)
如果处理机按 16 位以大端方式(big_endian)编址,请在图 4-2 所示的存储器图表中填入 myRadarData 数据的存储内容(十六进制表示)。
【问题 2】 (8分)
在图 4-1 所示的程序中,第 22 行的语句执行完成后,下列语句的结果是多少?请将应填入(n)处的内容写在答题纸的对应栏中。
q->X = (1)
q->Y = (2)
q->Z = (3)
q->U = (4)
若再执行一次q++,则下列语句的结果又是多少?请将应填入(n)处的内容写在答题纸的对应栏中。
q->X = (5)
q->Y = (6)
q->Z = (7)
q->U = (8)
【问题 3】 (4 分)
内存空间常划分为代码段(text) 、数据段(data) 、bss 段(bss) 、堆区(heap)和栈区(stack) ,那么图 4-1 中 myRadarData 数组的存储空间应分配在哪个段中?指针变量 p、q 应分配在哪个段中?
第9题
阅读以下说明和C语言代码,回答问题1至问题4,将解答填入答题纸的对应栏内。
[说明]
有两个任务(编号分别为0和1)需要访问同一个共享资源,为了解决竞争条件(race condition)的问题,需要实现一种互斥机制,使得在任何时刻只能有一个任务访问该共享资源。代码一给出了一种实现方法。
[代码一]
1: int flag[2]; /* flag 数组,初始化为FALSE */
2: Enter_Critical_Section(int my_task_id, int other_task_id)
3: {
4: while (flag[other_task_id] == TRUE); /* 空循环语句 */
5: flag[my_task_id] = TRUE;
6: }
7: Exit_Critical_Section(int my_task_id, int other_task_id)
8: {
9: flag[my_task_id] = FALSE;
10: }
当一个任务要访问临界资源时,就可以调用代码一给出的这两个函数。代码二给出了任务0的一个例子。
[代码二]
Enter_Critical_Section(0,1);
…使用这个资源…
Exit_Critical_Section(0,1);
…做其他事情…
什么是临界资源(critical resource)?请用100字以内文字简要说明。
第10题
阅读下列函数说明和C++代码,回答问题
[说明]
对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(Iterator)。模式以下程序模拟将书籍(Book)放到书架(BookShelf)上并依次输出书名。这样就要涉及到遍历整个书架的过程。使用迭代器Iterator实现。图5-1显示了各个类间的关系。以下是C++语言实现,能够正确编译通过。
[图5-1]
[C++代码]
template (1) >
class Iterator{
public:
virtual bool hasNext() = 0;
(2) Object* next() = 0;
};
class Book{
//省略具体方法和属性
};
class BookShelf{
private:
vector books;
public:
BookShelf(){
}
Book* getBookAt(int index){
return &booksindex;
}
int getLength(){
return books. size();
}
};
template
class BookshelfIterator : public (3) {
private:
BookShelf * bookShelf;
int index;
public:
BookshelfIterator(BookShelf *bookShelf){
this->bookShelf = bookShelf;
index = 0;
}
bool hasNext(){//判断是否还有下一个元素
if(index < bookShelf->getLength()){
return true;
}else{
return false;
}
}
Objeot* next(){//取得下一个元素
return bookShelf->getBookAt(index++);
}
};
int main()
{
BookShelf bookShelf;
//将书籍上架,省略代码
Book *book;
Iterator *it = new BookShelfIterator((4) );
while((5) ){//遍历书架,输出书名
book=(Book*)it->next();
/*访问元素*/
}
return 0;
}
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!