1. new
int *pia = new int[10]; // 10个未初始化int int *pia2 = new int[10](); // 10个值初始化为0的int
解析:
对于内置类型而言,new仅仅是分配内存,除非后面显示加(),相当于调用它的构造函数;
对于自定义类型而言,只要一调用new,那么编译器不仅仅给它分配内存,还调用它的默认构造函数初始化,即使后面没有加()
2. enum
enum string{ x1, x2, x3=10, x4, x5, } x;
unsigned char *p1; unsigned long *p2; p1=(unsigned char *)0x801000; p2=(unsigned long *)0x810000;
请问p1+5= 什么?p2+5= 什么?
解析:
801005 810014
p1指向字符型,一次移动一个字符型,1个字节;p1+5后移5个字节,16进制表示为5;
p2指向长整型,一次移动一个长整型,4个字节,p2+5后移20字节,16进制表示为14。
{ char每次移动1个字节;short移动2个字节 ;int , long ,float移动4个字节 ;double移动8个字节}
4.
void example(char acWelcome[]){ printf("%d",sizeof(acWelcome)); return; } void main(){ char acWelcome[]="Welcome to Huawei Test"; example(acWelcome); return; }
在32位机器中,输出是?
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 to test something
class CTest { public: CTest():m_chData(‘\0’),m_nData(0) { } virtual void mem_fun(){} private: char m_chData; int m_nData; static char s_chData; }; char CTest::s_chData=’\0’;
2 static的成员变量属于类域,不算入对象中 +0
3 神马成员都没有的类,或者只有成员函数(一旦类中有其他的占用空间成员,则这1个字节就不在计算之内,如一个类只有一个int则占用4字节而不是5字节) +1
4 对齐法则,对大家都没有问题
class A { public: void FuncA() { printf( "FuncA called\n" ); } virtual void FuncB() { printf( "FuncB called\n" ); } }; class B : public A { public: void FuncA() { A::FuncA(); printf( "FuncAB called\n" ); } virtual void FuncB() { printf( "FuncBB called\n" ); } }; void main( void ) { B b; A *pa; pa = &b; A *pa2 = new A; pa->FuncA(); ( 3) pa->FuncB(); ( 4) pa2->FuncA(); ( 5) pa2->FuncB(); delete pa2; }
程序的输出结果?
解析:
FuncA called FuncBB called FuncA called FuncB called
int FindSubString( char* pch ) { int count = 0; char * p1 = pch; while ( *p1 != '\0' ) { if ( *p1 == p1[1] - 1 ) { p1++; count++; }else { break; } } int count2 = count; while ( *p1 != '\0' ) { if ( *p1 == p1[1] + 1 ) { p1++; count2--; }else { break; } } if ( count2 == 0 ) return(count); return(0); } void ModifyString( char* pText ) { char * p1 = pText; char * p2 = p1; while ( *p1 != '\0' ) { int count = FindSubString( p1 ); if ( count > 0 ) { *p2++ = *p1; sprintf( p2, "%i", count ); while ( *p2 != '\0' ) { p2++; } p1 += count + count + 1; }else { *p2++ = *p1++; } } } void main( void ) { char text[32] = "XYBCDCBABABA"; ModifyString( text ); printf( text ); }
In the main() function, after ModifyString(text) is called, what’s the value of ‘text’?
解析:
XYBCDCBA1BAA
void perm(int list[], int k, int m) { if ( ) { copy(list,list+m,ostream_iterator<int>(cout," ")); cout<<endl; return; } for (int i=k; i<=m; i++) { swap(&list[k],&list[i]); ( ); swap(&list[k],&list[i]); } }
程序的功能是输出数组的全排列。请填空。
解析:
k==m 和 perm(list,k+1,m)
#include<iostream> using namespace std; class MyClass { public: MyClass(int i = 0) { cout << i; } MyClass(const MyClass &x) { cout << 2; } MyClass &operator=(const MyClass &x) { cout << 3; return *this; } ~MyClass() { cout << 4; } }; int main() { MyClass obj1(1), obj2(2); MyClass obj3 = obj1; return 0; }
运行时的输出结果是()
解析:
122444
C MyClass obj3 = obj1;
obj3还不存在,所以调用拷贝构造函数输出
2
,
如果obj3存在,obj3=obj,则调用复制运算符重载函数,输出
3
#include<stdio.h> char *myString() { char buffer[6] = {0}; char *s = "Hello World!"; for (int i = 0; i < sizeof(buffer) - 1; i++) { buffer[i] = *(s + i); } return buffer; } int main(int argc, char **argv) { printf("%s\n", myString()); return 0; }
如下代码输出结果是什么?
解析:
O(nm)
15.
折半查找法在查找成功时进行的关键字比较次数最多为ëlog2nû +1,即判定树的高度;
折半查找法在查找不成功时进行的关键字比较次数最多为ëlog2nû +1
16.
C语言的文件操作标准库函数中关于定位的函数只有两个一个是rewind 反绕函数,另一个fseek 随机定位函数;