错题

1. 从下列函数原形看,用GCC编译时,返回值类型为int的函数有?

  • char F1(int n);
  • int F2(char n);
  • F3(int n);
  • int *F4(int n);
答案: BC,(函数的默认返回值类型为int)


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();
};
那么sizeof(C)的数值是()
  • 9
  • 17
  • 32
  • 24
答案:D(类的静态成员变量是不占用内存,类的成员函数,虚函数不占用内存,占内存的只有虚函数指针)


5.全局变量可不可以定义在可被多个.C文件包含的头文件中?

  • 可以
  • 不可以
答案:A(可以,在不同的C文件中以static形式来声明同名全局变量。

可以在不同的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 {};
那么sizeof(B)的数值是()
  • 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)
  • 以上都是
答案:B(题目中问的是能否通过编译,只有B会捕获到异常,进行异常处理,而A和C由于和throw抛出的异常类型不匹配,导致这个异常不被捕获,从而成为未捕获的异常,调用terminate函数结束程序。)


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
答案:D(该题的关键是要认清楚强制类型转换后指针的类型。
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--)
  • 都是未定义行为
  • 都不是未定义行为
答案:C

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
  • 程序错误
答案:B(第一次执行完itor = array.erase(itor)后,第一个300被删除,itor指向被删除的下一个位置,也就是300那个位置,然后执行for循环里的itor++,指向了500那个位置,所以只删除了一个300)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值