试题一
第一部分:以下转自
http://blog.vckbase.com/bruceteen/archive/2004/06/22/496.aspx
1. 以下三条输出语句分别输出什么?
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; // 输出什么?
cout << boolalpha << ( str3==str4 ) << endl; // 输出什么?
cout << boolalpha << ( str5==str6 ) << endl; // 输出什么?
2. 以下反向遍历array数组的方法有什么错误?
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;
}
3. 以下两条输出语句分别输出什么?
float a = 1.0f;
cout << (int)a << endl;
cout << (int&)a << endl;
cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什么?
float b = 0.0f;
cout << (int)b << endl;
cout << (int&)b << endl;
cout << boolalpha << ( (int)b == (int&)b ) << endl; // 输出什么?
4. 以下代码有什么问题?
struct Test
{
Test( int ) {}
Test() {}
void fun() {}
};
void main( void )
{
Test a(1);
a.fun();
Test b();
b.fun();
}
5. 以下代码有什么问题?
cout << (true?1:"1") << endl;
6. 以下代码有什么问题?
typedef vector<int> 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 );
}
7. 以下代码有什么问题?
void char2Hex( char c ) // 将字符以16进制
{
char ch = c/0x10 + '0'; if( ch > '9' ) ch += 7;
char cl = c%0x10 + '0'; if( cl > '9' ) cl += 7;
cout << ch << cl << ' ';
}
char str[] = "I love 中国";
for( size_t i=0; i<strlen(str); ++i )
char2Hex( str[i] );
cout << endl;
http://blog.vckbase.com/bruceteen/archive/2004/06/22/496.aspx
1. 以下三条输出语句分别输出什么?
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; // 输出什么?
cout << boolalpha << ( str3==str4 ) << endl; // 输出什么?
cout << boolalpha << ( str5==str6 ) << endl; // 输出什么?
2. 以下反向遍历array数组的方法有什么错误?
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;
}
3. 以下两条输出语句分别输出什么?
float a = 1.0f;
cout << (int)a << endl;
cout << (int&)a << endl;
cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什么?
float b = 0.0f;
cout << (int)b << endl;
cout << (int&)b << endl;
cout << boolalpha << ( (int)b == (int&)b ) << endl; // 输出什么?
4. 以下代码有什么问题?
struct Test
{
Test( int ) {}
Test() {}
void fun() {}
};
void main( void )
{
Test a(1);
a.fun();
Test b();
b.fun();
}
5. 以下代码有什么问题?
cout << (true?1:"1") << endl;
6. 以下代码有什么问题?
typedef vector<int> 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 );
}
7. 以下代码有什么问题?
void char2Hex( char c ) // 将字符以16进制
{
char ch = c/0x10 + '0'; if( ch > '9' ) ch += 7;
char cl = c%0x10 + '0'; if( cl > '9' ) cl += 7;
cout << ch << cl << ' ';
}
char str[] = "I love 中国";
for( size_t i=0; i<strlen(str); ++i )
char2Hex( str[i] );
cout << endl;
--------------------------------------------------------------------------------
第一题:以下程序的结果是什么?
第一题:以下程序的结果是什么?
Code:struct 2DPoint
{
int x, y;
};
struct 3DPoint: 2DPoint
{
int z;
};
{
int z;
};
int main()
{
3DPoint 3dp = { { 0, 1 }, 2 };
cerr<< 3dp.x + 3dp.y + 3dp.z << endl;
}
{
3DPoint 3dp = { { 0, 1 }, 2 };
cerr<< 3dp.x + 3dp.y + 3dp.z << endl;
}
第二题:以下程序的结果是什么?
Code:class I1
{
public:
virtual void func()
{
cerr << "I1" << endl;
}
};
class B1
{
public:
B1( int i )
{
cerr << "B1" << i << endl;
}
};
{
public:
B1( int i )
{
cerr << "B1" << i << endl;
}
};
class B2: public I1
{
public:
virtual void func()
{
cerr << "B2" << endl;
};
B2( int i )
{
cerr << "B2" << i << endl;
func();
}
};
{
public:
virtual void func()
{
cerr << "B2" << endl;
};
B2( int i )
{
cerr << "B2" << i << endl;
func();
}
};
class D1: public B1, public virtual B2
{
public:
D1() : B1( 1 ), b1( 2 ), B2( 3 ), b2( 4 )
{
}
B2 b2;
B1 b1;
};
{
public:
D1() : B1( 1 ), b1( 2 ), B2( 3 ), b2( 4 )
{
}
B2 b2;
B1 b1;
};
int main()
{
D1 d;
}
{
D1 d;
}
第三题:以下程序的结果是什么?
Code:int i=7;
int j = ( ++i ) + ( ++i ) + ( ++i );
cerr << j << endl;
第四题:以下程序的结果是什么?
Code:void func(void* p)
{
cerr << p << endl;
}
void func(double d)
{
cerr << d << endl;
}
{
cerr << d << endl;
}
int main()
{
func( 1 );
func( 0 );
}
{
func( 1 );
func( 0 );
}
第五题:以下程序的结果是什么?
Code:void func0( int )
{
// do sth.
}
void func1( int i )
{
int j=0;
int k=1;
throw i?i:j;
}
{
int j=0;
int k=1;
throw i?i:j;
}
void func2( int i )
{
throw i?i:cos(i);
}
{
throw i?i:cos(i);
}
void func3( int i )
{
int j=0;
throw i?i:func0(1);
}
{
int j=0;
throw i?i:func0(1);
}
int main()
{
try
{
func2(2);
}
catch(...)
{
cerr<<1<<endl;
}
catch(int)
{
cerr<<2<<endl;
}
}
{
try
{
func2(2);
}
catch(...)
{
cerr<<1<<endl;
}
catch(int)
{
cerr<<2<<endl;
}
}
第六题:这个程序中的//some code部分,如果是有效代码的话,会被执行到吗??
Code:...
j = ++i;
if ( i == j )
{
//some code
}
...
这个程序中的//some code部分,如果是有效代码的话,会被执行到吗??
Code:class Funny
{
public:
bool operator == ( const Funny& )
{
return true;
}
Funny& operator ++ ( void )
{
return *this;
}
//...
};
Funny i, j;
j = ++i;
if ( i == j )
{
//some code
}
if ( i == j )
{
//some code
}
如果你在第一部分选择不会,该好好想一想了
好,如果i和j不是用户定义的类型,而是C++内建的类型,那答案又是什么??
第七题:Singleton
如果你没使用过Singleton,请跳过这题。
下面是一个Singleton类,在单线程情况下,运行正常。可是在多线程情况下就会出现问题。请指出其问题。
Code:class SomeClass
{
SomeClass()
{}
public:
static SomeClass* Instance()
{
static SomeClass someClass;
return &someClass;
}
};
那下面两个实现在多线程情况下有没有问题呢??
Code:class SomeClass
{
SomeClass()
{}
public:
static SomeClass* Instance()
{
static SomeClass* someClass;
lock();
if ( someClass == NULL )
someClass = new SomeClass();
unlock();
return someClass;
}
};
class SomeClass
{
SomeClass()
{}
public:
static SomeClass* Instance()
{
static SomeClass* someClass;
if ( someClass == NULL )
{
lock();
someClass = new SomeClass();
unlock();
}
return someClass;
}
};
{
SomeClass()
{}
public:
static SomeClass* Instance()
{
static SomeClass* someClass;
if ( someClass == NULL )
{
lock();
someClass = new SomeClass();
unlock();
}
return someClass;
}
};
第八题:以下程序的结果是什么?
程序一:
Code:class Base
{
public:
virtual void Funny() = 0;
};
class Derived: public Base
{
public:
virtual void Funny()
{
cerr << "hey, don't catch me" << endl;
}
}
{
public:
virtual void Funny()
{
cerr << "hey, don't catch me" << endl;
}
}
void func()
{
try
{
Base* pBase = new Derived();
throw ( *pBase );
}
catch( Base& b )
{
b.Funny();
}
}
{
try
{
Base* pBase = new Derived();
throw ( *pBase );
}
catch( Base& b )
{
b.Funny();
}
}
程序二:
Code:class Base
{
public:
virtual void Funny() {};
};
class Derived: public Base
{
public:
virtual void Funny()
{
cerr << "hey, don't catch me" << endl;
};
}
{
public:
virtual void Funny()
{
cerr << "hey, don't catch me" << endl;
};
}
void func()
{
try
{
Base* pBase = new Derived();
throw ( *pBase );
}
catch( Derived& d )
{
d.Funny();
}
catch( Base& b )
{
b.Funny();
}
}
{
try
{
Base* pBase = new Derived();
throw ( *pBase );
}
catch( Derived& d )
{
d.Funny();
}
catch( Base& b )
{
b.Funny();
}
}
第九题:以下程序的结果是什么?
Code:int i = 0;
int j = 1;
++++i;
i++++;
i+++++j;
cerr << i << endl << j << endl;
第十题:以下程序的结果是什么?
Code:class T
{
public:
int mValue;
T( int i )
{
mValue = i;
}
T()
{
i = 0;
T( 1 );
}
};
int main()
{
T t();
cerr << t.mValue << endl;
}
{
T t();
cerr << t.mValue << endl;
}
第十一题:以下程序的结果是什么?
Code:// dual main.cpp
int main()
{
cerr << "int main" << endl;
}
{
cerr << "int main" << endl;
}
int main( int )
{
cerr << "int main" << endl;
}
{
cerr << "int main" << endl;
}
第十二题:以下程序有错吗?
Code:#include <stdio.h>
#define ABP a
#
#define PBA b
第十三题:如果你有一个类库,其中有一个替换字符串的类,要求为:
1,从一个字符串构造,这个字符串是由';'分割的,假设';'不可能出现在字符串中。
Code:CStringReplace strReplace( "abc;def" );
strcpy( s, "xasdasdasabcaaa" );
strReplace.replace( s );
//然后,s == "xasdasdasdefaaa";
2,构造字符串中,每两个部分为一个单位,其长度相同。"abc;def"合法。"abc;defg"非法,而且由外部程序保证不会传入这样的字符串。
这样写可以吗?
这样写可以吗?
Code:class CStringReplace
{
char* mString;
public:
CStringReplace( const char* filter ) //split by ';'
{
mString = new char[ strlen( filter ) + 1 ];
strcpy( mString, filter );
}
CStringReplace(const CStringReplace& that)
{
mString = new char[ strlen( that.mString ) + 1 ];
strcpy( mString, that.mString );
}
CStringReplace& operator=( const CStringReplace& that )
{
delete mString;
mString = new char[ strlen( that.mString ) + 1 ];
strcpy( mString, that.mString );
}
~CStringReplace()
{
delete mString;
}
void ReplaceString( char* strToReplace )
{
char* before = strtok( mString, ";" );
char* after = strtok( NULL, ";" );
// we needn't check before and after, since the caller ensures it.
for( int i = 0; i < strlen( strToReplace ) ; ++i )
if ( strncmp( strToReplace + i, before, strlen( before ) ) == 0 )
strncpy( strToReplace + i, after, strlen( after ) );
}
};
第十四题:以下程序有什么问题?
Code:class B
{
};
class D: public B
{
};
{
};
with RTTI enable, the code
D* d=new D();
B* b=d;
d=dynamic_cast<b>;
B* b=d;
d=dynamic_cast<b>;
第十五题:以下程序的结果是什么?
Code:class B
{
public:
B( const B& b )
{
cerr << 1 << endl;
}
};
class D: public B
{
public:
D()
{
}
D( const B& b )
{
cerr << 2 << endl;
}
};
{
public:
D()
{
}
D( const B& b )
{
cerr << 2 << endl;
}
};
D d1;
D d2(d1);
D d2(d1);
浙公网安备 33010602011771号