诚信应考,考试作弊将带来严重后果!
华南理工大学期末考试
《C++程序设计(I)》试卷 A
- Which of the following is true? ( A )
A)Structures may contain many data types.
B)Members of different structures must have unique names.
C)Keyword typedef is used to define new data types.
D)Structures are always passed to functions by reference.
1) 以下哪项是正确的?( )
A)结构体(Structures)可以包含多种数据类型。
B)不同结构体的成员必须具有唯一的名称。
C)关键字typedef用于定义新的数据类型。
D)结构体总是通过引用传递给函数。
答案:
A)结构体(Structures)可以包含多种数据类型。
- Which of the following is false? ( D )
A) char a[20]=”abcdefg”; B) char a[]=”x+y=55.”;
C) char a[15]={’1’,’2’}; D) char a[10]=’5’;
2) 以下哪项是错误的?( D )
A) char a[20]=”abcdefg”;
B) char a[]=”x+y=55.”;
C) char a[15]={’1’,’2’};
D) char a[10]=’5’;
答案:
D) char a[10]=’5’;
- Which of the following logical expression is equal to !(x>0 || y==5)( B )。
A) x<=0 || y!=5 B) x<=0 && y!=5
C) x>0 || y!=5 D) x>0 && y==5
3) 以下哪个逻辑表达式等价于 !(x>0 || y==5)( B )。
A) x<=0 || y!=5
B) x<=0 && y!=5
C) x>0 || y!=5
D) x>0 && y==5
答案:
B) x<=0 && y!=5
- How many errors in the following statements? ( )
struct d {
int a;
double a;
} a,d;
A)1 B)2 C)3 D)4
#include<iostream>
using namespace std;
struct d {
int a;
} ;
int main()
{
d d;
}
这是能运行的
答案好像是2个,似乎只能看到一个
- Assume i=2,j=2,the result of the expression (i++)+(++j) is( B )。
A)4 B)5 C)6 D) 7
- Assume a=1,b=2,c=3,d=4,the result of the expression a>b?a:c>d?c:d is( )
A)1 B)2 C)3 D)4
D
- After the following statements is performed, the value of x is ( )。
unsigned x=10; x<<=2;
A) 10 B)20 C)30 D)40
D
- Which of the following is false ( B )
- Recursive calls take time and consume additional memory.
B) Recursive functions are the functions that call themselves directly. - 递归可以间接调用directly错误了
C) One advantage of the recursive functions is easy to understand.
D) If no base case,the recursive function will infinite loops.
- Which operator has the highest precedence?( A )
A)! B)&& C)!= D)?:
- After the following statements is performed, the value of a[0],a[1],a[2] are ( 10.12.12 )
int a[ ]={10,11,12}, *p=&a[0];p++;(*p)++;
A)10,11,12 B)11,12,12
C)10,12,12 D)11,11,12
- Assume i=1,after the following statements is performed, the value of i is( 4 )。
switch(i)
{case 1:i++;
case 2:i++;
case 3:++i;break;
case 4:--i;
default:i++;
}
A) 1 B) 2 C) 3 D) 4
显而易见
- Which of the following is ture ( D )
A)The default case is required in the switch selection structure.
B)The break statement is required in the default case of switch selection structure to exit the structure properly.
C)The expression (x>y && a<b ) is true if either the expression x>y is true or a<b is true.
D)An expression containing the || operator is true if either or both of its operands are true.
显然
- Which of the following is ture ( D )
A)An array can store many different types of values.
B)An array subscript should normally be of data type float.
C)If there are fewer initializers in an initializer list than the number of elements in the array, the remaining elements are initialized to the last value in the list of initializers.
D)It is an error if an initializer list contains more initializers than the number of elements in the array.
翻译:
A)一个数组可以存储许多不同类型的值。
B)数组的下标通常应该是浮点型数据类型。
C)如果在初始化列表中的初始化器数量比数组中元素的数量少,剩余的元素会被初始化为初始化列表中的最后一个值。
D)如果初始化列表中包含的初始化器数量超过数组中元素的数量,这将是一个错误。
解答:
A)错误。数组可以存储相同数据类型的元素,但这些元素可以是任何合法的数据类型,包括基本数据类型和用户定义的数据类型。
B)错误。数组的下标应该是整数类型,通常是int。浮点型数据类型不是合法的数组下标。
C)错误。如果初始化列表中的初始化器数量少于数组中元素的数量,剩余的元素会被初始化为初始化列表中的最后一个值。
D)正确。C选项已经说明了如果初始化器列表中的初始化器数量少于数组中元素的数量,这是允许的。然而,如果初始化列表中的初始化器数量超过数组中元素的数量,这将是一个错误。
- Which of the following is ture ( )
A)Pointers of any type can be assigned to void pointers.
B)The address operator & can be applied only to constants and to expressions
C)A pointer that is declared to the type void can be dereferenced.
D)Pointers of different type may not be assigned to one another without a cast operation.
空指针就可以
The correct answer is:
A)Pointers of any type can be assigned to void pointers.
解释:
指向void
类型的指针本身是合法的,但无法直接对其进行解引用。void
指针通常用于通用的指针操作,但在使用时,必须将其转换为特定类型的指针才能进行解引用操作。以下是一个例子:
void *ptr; // 声明一个指向 void 的指针
// 此时无法直接解引用 ptr,因为不知道它指向的具体类型
// *ptr; // 这是错误的
int num = 10;
ptr = # // 将 int 类型的指针赋给 void 指针
// 现在可以通过将 void 指针转换为 int 指针来解引用
int *intPtr = (int *)ptr;
int value = *intPtr; // 现在可以正确地解引用
在上面的例子中,ptr
最初是一个void
指针,但在将其赋给int
指针后,我们可以使用intPtr
来解引用,获取num
的值。
A)正确。`void`指针是一种特殊类型的指针,可以用来存储任何类型的指针。因此,任何类型的指针都可以赋值给`void`指针。
B)错误。地址运算符`&`可以应用于变量和表达式,而不仅仅是常量。
- Assume
struct worker
{ int no ;
char name[ 20 ] ;
};
worker w,*p = &w ;
which of the following is false ( )。
A) w.no B) p -> no C) ( *p ).no D) *p.no
D
D一眼错
- Which of the following is false( C )
A)int a[2][3]={0} B)int a[][3]={{0,1},{0}}
C)int a[2][3]={{0,1},{2,3},{4,5}} D)int a[][3]={0,1,2,3,4,5} - The function prototype of fun3 is: void fun3(int &),which of the following is true:( )
A)int x=2.17; fun3(&x) B)int a =15; fun3(a*3.14)
C)int b=100; fun3(b) D)fun3(256) - C
- Assume the statement using std::cout; is used, which of the following is false ( D )
A)All variables must be declared before they are used.
B)All variables must be given a type when they are declared.
C)Declarations can appear almost any where in the body of a C++ function.
D)A C++ program that prints three lines of output must contain three out statements using cout.
- 2. Please write out the performed results of the following programs。(24 scores)
- #include <iostream>
using namespace std;
void main()
{ int i, j, k=0;
for(i=0;i<3;i++){
k++;
for(j=0;j<3;j++){
if(!(j%2)) continue;
k++;
}
if(!(j%2)) break;
}
cout<<"k="<<k<<endl;
}
显而易见
- #include <iostream>
using namespace std;
void main()
{ char *p, a[]= "I Love C++" ;
for(p=a; *p!='\0' ;)
{ cout<<p<<endl;
p++;
if(*p != '\0' ) p++ ;
else break;
}
}
I Love C++
Love C++
ve C++
C++
++
- #include <iostream>
using namespace std;
int f(int);
void main()
{ int x=4,i;
for(i=0;i<2;i++)
cout<<f(x)<<endl;
}
int f(int a)
{ int b=0;
static int c=3;
b*=a; c*=a;
return (a+b+c);
}
- #include<iostream>
using namespace std;
void main()
{
int temp,value,a,b,c,d;
value=temp=a=b=c=d=0;
do
{ cout<<"Enter a four-digit integer :";
a=cin.get()-48;
b=cin.get()-48;
c=cin.get()-48;
d=cin.get()-48;
if ((a>=0)&&(a<=9)&&(b>=0)&&(b<=9)&&(c>=0)&&(c<=9)&&(d>=0)&&(d<=9))
value=a*1000+b*100+c*10+d;
}while (!value);
a=(a+7)%10;b=(b+7)%10; c=(c+7)%10; d=(d+7)%10;
temp=a; a=c; c=temp;
temp=b; b=d; d=temp;
cout<<"The orginal number is : "<<value<<endl;
cout<<"The encrypt number is : "<<a<<b<<c<<d<<endl;
}
Input:1234
Output:
- #include <iostream>
using namespace std;
int function1(int, int);
int main()
{
cout<<"The result is:"<< function1(2,10) <<endl;
return 0;
}
int function1(int a, int b)
{ if ( b<=1)
return a;
else
return a* function1(a,b/2);
}
- 3.Fill in 【 】 according to the request of the subjects(20 scores).
- The following program finds the maximum in a matrix and gives the position of the maximum.
#include <iostream>
using namespace std;
【 】
【 】
void findmax ( int a[ ][n]);
void main()
{ int a[m][n]={1,2,3,4,5,6,7,8,9,10,1,2};
findmax(a);
}
void findmax( int b[][n])
{ int max,maxrow,maxcol;
【
】
for (int i=0;i<m;i++)
for (int j=0;j<n;j++)
if (b[i][j]>max)
{
【 】
}
cout<<"The maximum in the matrix is "<<max<<endl;
cout<<"The maximum position is row:"<<maxrow<<"column:"<<maxcol<<endl;
}
#include <iostream>
using namespace std;
const int m = 3;
const int n = 4;
void findmax(int a[][n]);
void main()
{
int a[m][n] = { 1,2,3,4,5,6,7,8,9,10,1,2 };
findmax(a);
}
void findmax(int b[][n])
{
int max, maxrow, maxcol;
max = 0;
maxrow = 0;
maxcol = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (b[i][j] > max)
{
maxrow = i;
maxcol = j;
}
cout << "The maximum in the matrix is " << max << endl;
cout << "The maximum position is row:" << maxrow << "column:" << maxcol << endl;
}
- The following program sorts the strings in descending order and prints out the result.
#include <iostream>
using std::cout;
using std:endl;
#include <cstring>
void sortstr(【 】, int n)
{ int i, j, k;
char * temp;
for(i=0; i<n-1; i++)
{ k=i;
for( j=i+1;j<n; j++)
if (【 】 )
k=j;
}
}
void main()
{ int i;
char *pname[4]={"pascal", "Visual Basic", "Visual C++", "lisp"};
sortstr(【 】);
for(i=0;i<4;i++)
cout<<pname[i]<<endl;
}
#include <iostream>
using std::cout;
using std::endl;
#include <cstring>
void sortstr(char **arr, int n)
{
int i, j, k;
char *temp;
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
// Compare strings in descending order
if (strcmp(arr[j], arr[k]) > 0)
k = j;
}
// Swap strings if needed
if (k != i)
{
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
}
又是一个有趣的选择排序(
int main()
{
int i;
char *pname[4] = {"pascal", "Visual Basic", "Visual C++", "lisp"};
// Call the sortstr function to sort the strings in descending order
sortstr(pname, 4);
// Print the sorted strings
for (i = 0; i < 4; i++)
cout << pname[i] << endl;
return 0;
}