1. 从下列函数原形看,用GCC编译时,返回值类型为int的函数有?
- char F1(int n);
- int F2(char n);
- F3(int n);
- int *F4(int n);
2. 二叉树是一种树形结构,每个节点至多有两颗子树,下列一定是二叉树的是()
- 红黑树
- B树
- AVL树
- B+树
答案:A(红黑树)
3. What is the result of the following program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
char
*f( char *str , char ch) { char *it1 = str; char *it2 = str; while (*it2 != '\0' ) { while (*it2 == ch) { it2++; } *it1++ = *it2++; } return str; } int
main( void )
{ char *a = new char [10]; strcpy (a , "abcdcccd" ); cout<<f(a,
'c' ); return 0; } |
- abdcccd
- abdd
- abcc
- abddcccd
- Access violation
答案:D(注意循环结束后,it1后面的值是不变的!)
4. 在64位系统中,有如下类:
1
2
3
4
5
6
7
8
9
10
|
class
C { public : char a; static char b; void *p; static int *c; virtual void func1(); virtual void func2(); }; |
- 9
- 17
- 32
- 24
5.全局变量可不可以定义在可被多个.C文件包含的头文件中?
- 可以
- 不可以
可以在不同的C文件中声明同名的全局变量,前提是其中只能有一个C文件中对此变量赋初值,此时连接不会出错)
6.在64位系统中,有如下类:
1
2
3
4
5
6
7
8
9
10
|
class
A { public : void *p1; private : void *p2; protected : void *p3; }; class
B: public A {}; |
- 8
- 16
- 24
- 32
答案:C(继承中的父类的私有变量是在子类中存在的,不能访问是编译器的行为,是可以通过指针操作内存来访问的。)
7.如何捕获异常可以使得代码通过编译?
1
2
3
4
5
6
7
|
class A { public : A(){} }; void
foo(){ throw new A; } |
- catch (A && x)
- catch (A * x)
- catch (A & x)
- 以上都是
8.
下面程序运行后的结果为:
1
2
3
4
5
6
7
|
char
str[] = "glad to test something" ; char
*p = str; p++; int
*p1 = reinterpret_cast< int
*>(p); p1++; p = reinterpret_cast< char *>(p1); printf( "result is %s\n" , p); |
- result is glad to test something
- result is ad to test something
- result is test something
- result is to test something
p的类型为char *,p++后p指向str数组的第2个元素即字母“l”的位置。
p1的类型为int *,p1++后p1指向的位置增加4个字节,指向str数组中的第6个元素即字母“t”的位置。
因此最后p的内容为“to test something”)
9.下列 C 代码中,不属于未定义行为的有:______。
- int i=0;i=(i++);
- char *p=”hello”;p[1]=’E’
- char *p=”hello”;char ch=*p++
- int i=0;printf(“%d%d\n”,i++ i--)
- 都是未定义行为
- 都不是未定义行为
10.
有以下程序
1
2
3
4
5
6
7
8
9
10
|
#include <stdio.h > main() { double x= 2.0 ,y; if (x< 0.0 )y= 0.0 ; else if ((x< 5.0 )&&(! x)) y= 1.0 /(x+ 2.0 ); if (x< 10.0 )y= 1.0 /x; else if y= 10.0 ; printf( "%f\n" ,y); } |
程序运行后的输出结果是
- 0.000000
- 0.250000
- 0.500000
- 1.000000
答案:C
11.
下列代码的输出为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#include<iostream> #include<vector> using namespace std; int
main( void ) { vector< int >array; array.push_back( 100 ); array.push_back( 300 ); array.push_back( 300 ); array.push_back( 500 ); vector< int >::iterator itor; for (itor = array.begin(); itor != array.end(); itor++) {
if (*itor == 300 )
{
itor = array.erase(itor);
} } for (itor = array.begin(); itor != array.end(); itor++) {
cout << *itor << " " ; } return 0 ; } |
- 100 300 300 500
- 100 300 500
- 100 500
- 程序错误