C++程序设计英文版试卷含答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《C++程序设计英文版》试题
DEPARTMENT CLASS
1.Fill in the blanks (20 points) 1. What value is stored into the int variable b in each of the
following? Assume that b and n are int variables with b
containing 12 and n containing 5.
b*=2+3b=_____________________.
b%=(n%=2)b=____________________.
2.The getline function is a member function of the string
class.(True or False?) Answer is:___________________.
3.Given the following declarations,
enum colour { red, yellow, blue, green, white};
colour sample;
indicate whether each statement below is valid or
invalid.
a. sample ++; Answer is:_________.
b. sample = colour(sample+1); Answer is:_________.
4.You want to compute the square roots and absolute values
of some floating_point numbers.
a.Which C++ library functions would you use?
b.Which header file(s) must you #include in order to use
these functions?
Answers are:a.____________________________b.____________
5. If the string variable str contains the string
“Programming in C++”,what is output by the following
statement?
cout<<str.length()<<’ ’<<str.substr(1,2)<<endl;
Answer is:____________________________________.
6. In the statement
Display(gamma,delta);
Would you conclude that Display is a value-returning function or a void function?
Answer is:______________________.
7. Write the first line of a While statement that loops until the value of the Boolean variable done becomes true.
Answer is:________________________________________.
8. Given these values for the Boolean variables x,y,and z:
x=tr ue y=fa ls e z=t ru e
evaluate the following logical expression. Write a T if the result is true or a F if the result is false.
(x||!y)&&(!x||z) Answer is:____________.
9.Every C++ compiler guarantees that sizeof(int) < sizeof(long). (True or False?) Answer is:____________ 10.Write the type declaration for a struct data type named
PersonRec with three members: age, height, and weight.
All three members are intended to store integer values, with height and weight representing height in inches and wight in pounds,respectively.
Answer is:_________________________________. 11.Fill in the blanks in the following program so that it reads a value from the file stream inData and writes it to the file stream outData.
#include <iostream>
#include _______________________
using namespace std;
int main( )
{
int n;
ifstream inData;
_____________________________________
inData.open(“myfile.dat”);
___________________(“results.dat”);
_____________________________________
outData << n <<endl;
_____________________________________
}
2.Choose the right one (2×10=20)
( )1.Every C++ program consists of at least how many functions?
(A) 1 (B)2 (C)3 (D)4
( )2.Which of the following words is reserved words in C++?
(A) abs (B)char (C)str (D)sqrt
( )3.Assign the value “Ok”to the string variable street. Which of the following statements is valid?
(A)street=”Ok”; (B)street=’Ok’;
(C)street=Ok; (D)street Ok;
( )4.Which of the following identifiers is invalid?
(A) n (B)PAY_DAY (C)6Set (D)num5 ( )5.When the following code is executed, how many iterations of the loop are performed?
number = 2;
done = false;
while (!done)
{
number = number * 2;
if (number > 64)
done = true;
}
(A)3 (B)4 (C)5 (D)6
( )6.What is the value of the C++ expression?
7 % 2
(A)1 (B)2 (C)2.5 (D)3
( )7.Write a C++ expression for the following algebraic
expression. Which of the following expression is valid?
3<y<9
(A)3<y && y<9 (B)3<y || y<9
(C)3<y and y<9 (D)3<y or y<9
( )8.If a design has one level 0 module and two level 1 modules, how many C++ functions(including main) is the program
likely to have?
(A)1 (B)2 (C)3 (D)4
( )9.Which of the following function prototypes is valid?
(A)int Function(void a); (B)void Function(int a);
(C)int Function(a); (D)void int(char a);
( )10.What would the heading for a value-returning function named Mul look like if it had two int parameters,num1
and num2, and returned a int result?
(A)i nt Mul(int num1,int num2)
(B)i nt Mul(num1,num2);
(C)v oid Mul(int num1,int num2)
(D)i nt Mul(int,int);
3、True or False(2×5=10)
1、A reference parameter has the same scope as a global variable. ( )
2、Function names have global scope. ( )
3、A struct cannot have another struct as a member.( )
4、A union is a struct that can hold just one of its members at a time. ( )
5、Dot notation is used only by class clients to refer
to members. Other class members do not need to use dot notation.( )
4、Show the outputs of the following programs.
(5×6=30)
(1)1.#include<iostream.h>
void main()
{
int a[6]={12,4,17,25,27,16};
int b[6]={27,13,4,25,23,16};
for(int i=0; i<6; i++)
{ for (int j=0;j<6; j++)
if (a[i]==b[j]) break;
if(j<6) cout<<a[i]<<“”;
}
}
(2)#include〈iostream.h〉
void main( )
{ int i=1;
while (i<=15)
if (++i%3!=2) continue;
else cout <<”i=”<<i<<endl;
}
(3)
#include <iostream>
using namespace std;
void Test( );
int main()
{
Test( );
Test( );
Test( );
return 0;
}
void Test( )
{
int i=0;
static int j=0;
i++;
j++;
cout<<i<<’’<<j<<endl;
}
(4)#include <iostream>
using namespace std;
int main()
{
int a[8]={36,25,48,14,55,40,32,66};
int b1,b2;
b1=b2=a[0];
for (int i=1;i<8;i++)
if(a[i]<b1)
{
if (b1<b2)
b2=b1;
b1=a[i];
}
cout<<b1<<’ ’<<b2<<endl;
return 0;
}
(5)#include <iostream.h>
class A
{int a,b,c;
public:
A() { a=b=0;}
A(int aa,int bb)
{ a=aa; b=bb;
cout<<a<< ’’<<b<<endl;}
};
void main()
{ A x,y,z(5,6); }
(6) #include <iostream.h>
void fun(int a,int b,int *c );
void main()
{
int x,y,z;
fun (2,3,&x);
fun (4,x,&y);
fun (x,y,&z);
cout<<x<<','<<y<<','<<z<<endl;
}
void fun(int a,int b,int *c )
{
b=b*a;
*c=b-a;
}
5、(10)Write a program that prints out the approximate number of words in a text. For our purposes, this is the same as the number of gaps following words. A gap is defined as one or more spaces in a row, so a sequence of spaces counts as just one gap. The newline character
also counts as a gap. Anything other than a space or
newline is considered to be part of a word.
(Use the character‘$’ as the sentinel to control
the loop.)
6、(10)Write a C++ program that inputs an integer larger
than 1 and calculates the sum of the squares from 1 to that
integer. For example, if the integer equals 4,the sum of the
squares is 30(1+4+9+16). The output should be the value of
the integer and the sum, properly labeled. The program
should repeat this process for several input values. A
negative(负数) input value signals the end of the data.
《C++程序设计英文版》试题A答案
1.Fill in the blanks (20 points)
1. b= 60 b= 0
2. False
3. a: invalid b: valid
4. a.sqrt( ),fabs( ) b. <cmath>
5. 18 ro
6. a void function
7. while (!done)
8.T 9. True
10. struct PersonRec
{
int age;
int height;
int weight;
};
11. <fstream>
ofstream outData;
outData.open
inData>>n;
return 0;
2.Choose the right one (2×10=20)
1. A
2. B
3. A
4. C
5. D
6. A
7. A
8. C
9. B 10.A
3.True or False (2×5=10)
1、F
2、T
3、F
4、T
5、T
4.Show the outputs of the following programs.(30)
(1) 4 25 27 16
(2) i=2
i=5
i=8
i=11
i=14
(3) 1 1
1 2
1 3
(4) 14 25
(5) 5 6
(6) 4,12,44 5.(10) 6.(10)。