C++笔试题

 

1. 以下三条输出语句分别输出什么? [C ]
char str1[]
       = "abc";
char str2[]
       = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char* str5
   = "abc";
const char* str6
   = "abc";
cout << boolalpha << ( str1==str2 ) << endl; //
输出什么? false
cout << boolalpha << ( str3==str4 ) << endl; //
输出什么? false
cout << boolalpha << ( str5==str6 ) << endl; //
输出什么? true

13.
C++ 内建型别 A B ,在哪几种情况下 B 能隐式转化为 A [C++ 中等 ]
答:
a. class B : public A {
…… } // B 公有继承自 A ,可以是间接继承的
b. class B { operator A( ); } // B
实现了隐式转化为 A 的转化
c. class A { A( const B& ); } // A 实现了 non-explicit 的参数为 B (可以有其他带默认值的参数)构造函数
d. A& operator= ( const A& ); //
赋值操作,虽不是正宗的隐式类型转换,但也可以勉强算一个

12.
以下代码中的两个 sizeof 用法有问题吗? [C ]
void UpperCase( char str[] ) //
str 中的小写字母转换成大写字母
{
     for( size_t i=0; i<sizeof(str)/sizeof(str[0]); ++i )//str 在这里是指针,长度为4
         if( 'a'<=str[i] && str[i]<='z' )
             str[i] -= ('a'-'A' );
}
char str[] = "aBcDe";
cout << "str
字符长度为 : " << sizeof(str)/sizeof(str[0]) << endl;
UpperCase( str );
cout << str << endl;

7.
以下代码有什么问题? [C ]
void char2Hex( char c ) //
将字符以 16 进制表示
{
     char ch = c/0x10 + '0'; if( ch > '9' ) ch += ('A'-'9'-1);
     char cl = c%0x10 + '0'; if( cl > '9' ) cl += ('A'-'9'-1);
     cout << ch << cl << ' ';
}
char str[] = "I love
中国 ";
for( size_t i=0; i<strlen(str); ++i )
     char2Hex( str[i] );
cout << endl;

4.
以下代码有什么问题? [C++ ]
struct Test
{
     Test( int ) {}
     Test() {}
     void fun() {}
};
void main( void )
{
     Test a(1);
     a.fun();
     Test b();
     b.fun();
}


5.
以下代码有什么问题? [C++ ]
cout << (true?1:"1") << endl;

// 编译不能通过,类型不一致, 需要改为cout << (true? "1":"1") << endl;

8.
以下代码能够编译通过吗,为什么? [C++ ]
unsigned int const size1 = 2;
char str1[ size1 ];
unsigned int temp = 0;
cin >> temp;
unsigned int const size2 = temp;//
初始化为变量后,后面编译有问题
char str2[ size2 ];// 静态分配,size2 必须是常量。

9.
以下代码中的输出语句输出 0 吗,为什么? [C++ ]
struct CLS
{
     int m_i;
     CLS( int i ) : m_i(i) {}
     CLS()
     {
         CLS(0);
     }
};
CLS obj;
cout << obj.m_i << endl;
//
输出未知数,编译器只会调用一次构造函数,执行CLS(0); 不会再次调用


10. C++
中的空类,默认产生哪些类成员函数? [C++ ]
答:
class Empty
{
public:
     Empty();                            // 缺省构造函数
     Empty( const Empty& );              // 拷贝构造函数
     ~Empty();                          // 析构函数
     Empty& operator=( const Empty& ); // 赋值运算符
     Empty* operator&();                // 取址运算符
     const Empty* operator&() const;    // 取址运算符 const
};

3.
以下两条输出语句分别输出什么? [C++ ]
float a = 1.0f;
cout << (int)a << endl;//1
cout << (int&)a << endl;//
未知数

cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什么 false
float b = 0.0f;
cout << (int)b << endl;//0
cout << (int&)b << endl;//0
cout << boolalpha << ( (int)b == (int&)b ) << endl; //
输出什么 true

2.
以下反向遍历 array 数组的方法有什么错误? [STL ]
vector<int> array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 3 );
for( vector<int> ::size_type i=array.size()-1; i>=0; --i ) //
反向遍历 array 数组
{
     cout << array[i] << endl;
}
// vector<int>::size_type
类型属于usigned int 的,死循环


6.
以下代码有什么问题? [STL ]
typedef vector IntArray;
IntArray array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 2 );
array.push_back( 3 );
//
删除 array 数组中所有的 2
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
     if( 2 == *itor ) array.erase( itor );// 删除后指针丢失
}

11.
写一个函数,完成内存之间的拷贝。 [ 考虑问题是否全面 ]
答:
void* mymemcpy( void *dest, const void *src, size_t count )
{
     char* pdest = static_cast<char*>( dest );
     const char* psrc = static_cast<const char*>( src );
     if( pdest>psrc && pdest<psrc+cout ) 能考虑到这种情况就行了
     {
         for( size_t i=count-1; i!=-1; --i )
                 pdest[i] = psrc[i];
     }
     else
     {
         for( size_t i=0; i<count; ++i )
             pdest[i] = psrc[i];
     }
     return dest;
}
int main( void )
{
     char str[] = "0123456789";
     mymemcpy( str+1, str+0, 9 );
     cout << str << endl;

     system( "Pause" );
     return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值