151、写出判断ABCD四个表达式的是否正确, 若正确, 写出经过表达式中 a的值 int a = 4; (A)a += (a++); (B) a += (++a) ;(C) (a++) += a;(D) (++a) += (a++); a = ? 答:C错误,左侧不是一个有效变量,不能赋值,可改为(++a) += a; 改后答案依次为9,10,10,11
152、某32位系统下, C++程序,请计算sizeof 的值 char str[] = “http://www./” char *p = str ; int n = 10; 请计算 (1)sizeof (str ) = ? (2)sizeof ( p ) = ? (3)sizeof ( n ) = ? void Foo ( char str[100]){ 请计算 sizeof( str ) = ?(4) } void *p = malloc( 100 ); 请计算 sizeof ( p ) = ?(5) 答:(1)25 (2)4 (3) 4 (4)4 (5)4
153、 回答下面的问题 (1).Void GetMemory(char **p, int num){ *p = (char *)malloc(num); } void Test(void){ char *str = NULL; GetMemory(&str, 100); strcpy(str, “hello”); printf(str); } 请问运行Test 函数会有什么样的结果? 答:输出“hello”
154、 void Test(void) { char *str = (char *) malloc(100); strcpy(str, “hello”); free(str); if(str != NULL){ strcpy(str, “world”); printf(str); } } 请问运行Test 函数会有什么样的结果? 答:输出“world”
155、 char *GetMemory(void){ char p[] = “hello world”; return p; } void Test(void){ char *str = NULL; str = GetMemory(); printf(str); } 请问运行Test 函数会有什么样的结果? 答:无效的指针,输出不确定
156、编写strcat函数 已知strcat函数的原型是char *strcat (char *strDest, const char *strSrc); 其中strDest 是目的字符串,strSrc 是源字符串。 (1)不调用C++/C 的字符串库函数,请编写函数 strcat 答: VC源码: char * __cdecl strcat (char * dst, const char * src) { char * cp = dst; while( *cp ) cp++; while( *cp++ = *src++ ) ; return( dst ); }
157、strcat能把strSrc 的内容连接到strDest,为什么还要char * 类型的返回值? 答:方便赋值给其他变量
158、MFC中CString是类型安全类么? 答:不是,其它数据类型转换到CString可以使用CString的成员函数Format来转换
159.C++中什么数据分配在栈或堆中? 答:栈: 存放局部变量,函数调用参数,函数返回值,函数返回地址。由系统管理 堆: 程序运行时动态申请,new 和malloc申请的内存就在堆上
160、函数模板与类模板有什么区别? 答:函数模板的实例化是由编译程序在处理函数调用时自动完成的,而类模板的实例化 必须由程序员在程序中显式地指定。
161、 int i=10, j=10, k=3; k*=i+j; k最后的值是? 答:60,此题考察优先级,实际写成: k*=(i+j);,赋值运算符优先级最低
162、do……while和while……do有什么区别? 答 、前一个循环一遍再判断,后一个判断以后再循环
163、请写出下列代码的输出内容 #i nclude main() { int a,b,c,d; a=10; b=a++; c=++a; d=10*a++; printf(“b,c,d:%d,%d,%d”,b,c,d); return 0; } 答 、10,12,120
164.在c语言库函数中将一个字符转换成整型的函数是atol()吗,这个函数的原型是什么? 答 、函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *nptr); 程序例: #include #include int main(void) { long l; char *str = “98765432″; l = atol(lstr); printf(“string = %s integer = %ld\n”, str, l); return(0); }
165. 以下三条输出语句分别输出什么? 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; // 输出什么? 答:分别输出false,false,true。str1和str2都是字符数组,每个都有其自己的存储区,它们的值则是各存储区首地址,不等;str3和str4同上,只是按const语义,它们所指向的数据区不能修改。str5和str6并非数组而是字符指针,并不分配存储区,其后的“abc”以常量形式存于静态数据区,而它们自己仅是指向该区首地址的指针,相等。
166 以下代码中的两个sizeof用法有问题吗? void UpperCase( char str[] ) // 将 str 中的小写字母转换成大写字母 { for( size_t i=0; i<sizeof(str)/sizeof(str[0]); ++i ) 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;
答:函数内的sizeof有问题。根据语法,sizeof如用于数组,只能测出静态数组的大小,无法检测动态分配的或外部数组大小。函数外的str是一个静态定义的数组,因此其大小为6,函数内的str实际只是一个指向字符串的指针,没有任何额外的与数组相关的信息,因此sizeof作用于上只将其当指针看,一个指针为4个字节,因此返回4。
167 非C++内建型别 A 和 B,在哪几种情况下B能隐式转化为A? 答: 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& ); // 赋值操作,虽不是正宗的隐式类型转换,但也可以勉强算一个 168.以下代码有什么问题? struct Test { Test( int ) {} Test() {} void fun() {} }; void main( void ) { Test a(1); a.fun(); Test b(); b.fun(); }
答:变量b定义出错。按默认构造函数定义对象,不需要加括号。
169 以下代码有什么问题? cout << (true?1:”1″) << endl; 答:三元表达式“?:”问号后面的两个操作数必须为同一类型。
170 以下代码能够编译通过吗,为什么? unsigned int const size1 = 2; char str1[ size1 ]; unsigned int temp = 0; cin >> temp; unsigned int const size2 = temp; char str2[ size2 ]; 答:str2定义出错,size2非编译器期间常量,而数组定义要求长度必须为编译期常量。
171. 以下代码中的输出语句输出0吗,为什么? struct CLS { int m_i; CLS( int i ) : m_i(i) {} CLS() { CLS(0); } }; CLS obj; cout << obj.m_i << endl;
答:不能。在默认构造函数内部再调用带参的构造函数属用户行为而非编译器行为,亦即仅执行函数调用,而不会执行其后的初始化表达式。只有在生成对象时,初始化表达式才会随相应的构造函数一起调用。
172 C++中的空类,默认产生哪些类成员函数? 答: class Empty { public: Empty(); // 缺省构造函数 Empty( const Empty& ); // 拷贝构造函数 ~Empty(); // 析构函数 Empty& operator=( const Empty& ); // 赋值运算符 Empty* operator&(); // 取址运算符 const Empty* operator&() const; // 取址运算符 const };
173 以下两条输出语句分别输出什么? 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; // 输出什么?
答:分别输出false和true。注意转换的应用。(int)a实际上是以浮点数a为参数构造了一个整型数,该整数的值是1,(int&)a则是告诉编译器将a当作整数看(并没有做任何实质上的转换)。因为1以整数形式存放和以浮点形式存放其内存数据是不一样的,因此两者不等。对b的两种转换意义同上,但是0的整数形式和浮点形式其内存数据是一样的,因此在这种特殊情形下,两者相等(仅仅在数值意义上)。 注意,程序的输出会显示(int&)a=1065353216,这个值是怎么来的呢?前面已经说了,1以浮点数形式存放在内存中,按ieee754规定,其内容为0x0000803F(已考虑字节反序)。这也就是a这个变量所占据的内存单元的值。当(int&)a出现时,它相当于告诉它的上下文:“把这块地址当做整数看待!不要管它原来是什么。”这样,内容0x0000803F按整数解释,其值正好就是1065353216(十进制数)。 通过查看汇编代码可以证实“(int)a相当于重新构造了一个值等于a的整型数”之说,而(int&)的作用则仅仅是表达了一个类型信息,意义在于为cout<<及==选择正确的重载版
174、请简述以下两个for循环的优缺点(5分)
for (i=0; i<N; i++) { if (condition) DoSomething(); else DoOtherthing(); } if (condition) { for (i=0; i<N; i++) DoSomething(); } else { for (i=0; i<N; i++) DoOtherthing(); }
优点:程序简洁 缺点:多执行了N-1次逻辑判断,并且打断了循环“流水线”作业,使得编译器不能对循环进行优化处理,降低了效率。
优点:循环的效率高 缺点:程序不简洁
175 void GetMemory(char *p) { p = (char *)malloc(100); } void Test(void) { char *str = NULL; GetMemory(str); strcpy(str, “hello world”); printf(str); } 请问运行Test函数会有什么样的结果? 答:程序崩溃。 因为GetMemory并不能传递动态内存, Test函数中的 str一直都是 NULL。 strcpy(str, “hello world”);将使程序崩溃。
176 char *GetMemory(void) { char p[] = “hello world”; return p; } void Test(void) { char *str = NULL;
str = GetMemory(); printf(str); } 请问运行Test函数会有什么样的结果? 答:可能是乱码。 因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不可知。
177 void GetMemory2(char **p, int num) { *p = (char *)malloc(num); } void Test(void) { char *str = NULL; GetMemory(&str, 100); strcpy(str, “hello”); printf(str); } 请问运行Test函数会有什么样的结果? 答: (1)能够输出hello (2)内存泄漏
178 void Test(void) { char *str = (char *) malloc(100); strcpy(str, “hello”); free(str); if(str != NULL) { strcpy(str, “world”); printf(str); } } 请问运行Test函数会有什么样的结果? 答:篡改动态内存区的内容,后果难以预料,非常危险。 因为free(str);之后,str成为野指针, if(str != NULL)语句不起作用。
179、请阅读以下一段程序,并给出答案。 class A { public: A(){ doSth(); } virtual void doSth(){printf(“I am A”);} }; class B:public A { public: virtual void doSth(){ printf(“I am B”);} }; B b; 执行结果是什么?为什么? 答:执行结果是I am A 因为b对象构造时调用基类A的构造函数A(),得此结果。
180 实现双向链表删除一个节点P,在节点P后插入一个节点,写出这两个函数; 答:双向链表删除一个节点P template<class type> void list<type>::delnode(int p) { int k=1; listnode<type> *ptr,*t; ptr=first;
while(ptr->next!=NULL&&k!=p) { ptr=ptr->next; k++; } t=ptr->next; cout<<”你已经将数据项 “<<t->data<<”删除”<<endl;
ptr->next=ptr->next->next; length–; delete t; }
在节点P后插入一个节点: template<class type> bool list<type>::insert(type t,int p) { listnode<type> *ptr; ptr=first;
int k=1; while(ptr!=NULL&&k<p) { ptr=ptr->next; k++; } if(ptr==NULL&&k!=p) return false; else { listnode<type> *tp; tp=new listnode<type>; tp->data=t; tp->next=ptr->next; ptr->next=tp; length++; return true; } }
181.完成下列程序
*
*.*.
*..*..*..
*…*…*…*…
*….*….*….*….*….
*…..*…..*…..*…..*…..*…..
*……*……*……*……*……*……*……
*…….*…….*…….*…….*…….*…….*…….*…….
#i nclude<iostream> using namespace std;
const int n = 8;
main() { int i; int j; int k;
for(i = n; i >= 1; i–) { for(j = 0; j < n-i+1; j++) { cout<<”*”; for(k=1; k < n-i+1; k++) { cout<<”.”; } } cout<<endl; } system(“pause”); }
182完成程序,实现对数组的降序排序
#include <iostream> using namespace std;
void sort(int* arr, int n);
int main()
{ int array[]={45,56,76,234,1,34,23,2,3}; sort(array, 9); for(int i = 0; i <= 8; i++)//曾经在这儿出界 cout<<array[i]<<” “; cout<<endl; system(“pause”); }
void sort(int* arr, int n) { int temp; for(int i = 1; i < 9; i++) { for(int k = 0; k < 9 – i; k++)//曾经在这儿出界 { if(arr[k] < arr[k + 1]) { temp = arr[k]; arr[k] = arr[k + 1]; arr[k + 1] = temp; } } } }
183. 以下两条输出语句分别输出什么?[C++难] 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; // 输出什么? 1 1065353216 boolalpha0 0 0 boolalpha1
51. 以下反向遍历array数组的方法有什么错误?[STL易] vector array; array.push_back( 1 ); array.push_back( 2 ); array.push_back( 3 ); for( vector::size_type i=array.size()-1; i>=0; –i ) // 反向遍历array数组 { cout << array[i] << endl; }
184 写一个函数,完成内存之间的拷贝。[考虑问题是否全面] 答: 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; }
185 对于C++中类(class) 与结构(struct)的描述正确的为: A,类中的成员默认是private的,当是可以声明为public,private 和protected, 结构中定义的成员默认的都是public; B,结构中不允许定义成员函数,当是类中可以定义成员函数; C,结构实例使用malloc() 动态创建,类对象使用new 操作符动态分配内存; D,结构和类对象都必须使用new 创建; E,结构中不可以定义虚函数,当是类中可以定义虚函数. F,结构不可以存在继承关系,当是类可以存在继承关系. 答:A,D,F
186.两个互相独立的类:ClassA 和 ClassB,都各自定义了非静态的公有成员函数 PublicFunc() 和非静态的私有成员函数 PrivateFunc(); 现在要在ClassA 中增加定义一个成员函数ClassA::AdditionalPunction(ClassA a,ClassB b);则可以在AdditionalPunction(ClassA x,ClassB y)的实现部分(函数功能体内部) 出现的合法的表达是最全的是: A,x.PrivateFunc();x.PublicFunc();y.PrivateFunc();y.PublicFunc(); B,x.PrivateFunc();x.PublicFunc();y.PublicFunc(); C,x.PrivateFunc();y.PrivateFunc();y.PublicFunc(); D,x.PublicFunc();y.PublicFunc(); 答:B
186.C++程序下列说法正确的有: A,对调用的虚函数和模板类都进行迟后编译. B,基类与子类中函数如果要构成虚函数,除了要求在基类中用virtual 声名,而且必须名字相同且参数类型相同返回类型相同 C,重载的类成员函数都必须要:或者返回类型不同,或者参数数目不同,或者参数序列的类型不同. D,静态成员函数和内联函数不能是虚函数,友员函数和构造函数也不能是虚函数,但是析构函数可以是虚函数. 答:A ***************************************************************************
187 头文件的作用是什么? 答:一、通过头文件来调用库功能。在很多场合,源代码不便(或不准)向用户公布,只要向用户提供头文件和二进制的库即可。用户只需要按照头文件中的接口声明来调用库功能,而不必关心接口怎么实现的。编译器会从库中提取相应的代码。 二、头文件能加强类型安全检查。如果某个接口被实现或被使用时,其方式与头文件中的声明不一致,编译器就会指出错误,这一简单的规则能大大减轻程序员调试、改错的负担。
188、以下为Windows NT下的32位C++程序,请计算sizeof的值(10分)
char str[] = “Hello” ; char *p = str ; int n = 10; 请计算 sizeof (str ) = 6 (2分) sizeof ( p ) = 4 (2分) sizeof ( n ) = 4 (2分)void Func ( char str[100]) { 请计算 sizeof( str ) = 4 (2分) }
void *p = malloc( 100 ); 请计算 sizeof ( p ) = 4 (2分)
3写出下列程序的运行结果。
unsigned int i=3; cout<<i * -1;
189.写出下列程序所有可能的运行结果。
int a; int b; int c;
void F1() { b=a*2; a=b; }
void F2() { c=a+1; a=c; }
main() { a=5; //Start F1,F2 inparallel F1(); F2(); printf(“a=%d\n”,a); }a=11
190一个链表的操作,注意代码的健壮和安全性。要求: (1)增加一个元素; (2)获得头元素; (3)弹出头元素(获得值并删除)。
191.unsigned short array[]={1,2,3,4,5,6,7}; int i = 3; *(array + i) = ? 答: 4
192 class A { virtual void func1(); void func2(); } Class B: class A { void func1(){cout << “fun1 inclass B” << endl;} virtual void func2(){cout << “fun2 inclass B” << endl;} } A, A中的func1和B中的func2都是虚函数. B, A中的func1和B中的func2都不是虚函数. C, A中的func2是虚函数.,B中的func1不是虚函数. D, A中的func2不是虚函数,B中的func1是虚函数.
答: A
193输出下面程序结果。 #include <iostream.h> class A { public: virtual void print(void) { cout<<”A::print()”<<endl; } }; class B:public A { public: virtual void print(void) { cout<<”B::print()”<<endl; }; }; class C:public B { public: virtual void print(void) { cout<<”C::print()”<<endl; } }; void print(A a) { a.print(); } void main(void) { A a, *pa,*pb,*pc; B b; C c; pa=&a; pb=&b; pc=&c; a.print(); b.print(); c.print(); pa->print(); pb->print(); pc->print(); print(a); print(b); print(c); }
A::print() A::print() B::print() C::print() A::print() B::print() C::print() A::print() A::print() A::print()
————————————————————————– 194.程序改错 class mml { private: static unsigned int x; public: mml(){ x++; } mml(static unsigned int &) {x++;} ~mml{x–;} pulic: virtual mon() {} = 0; static unsigned int mmc(){return x;} ……
}; class nnl:public mml { private: static unsigned int y; public: nnl(){ x++; } nnl(static unsigned int &) {x++;} ~nnl{x–;} public: virtual mon() {}; static unsigned int nnc(){return y;} …… };
代码片断: mml* pp = new nnl; ………. delete pp;
A: 基类的析构函数应该为虚函数 virtual ~mml{x–;}
————————————————————————– 195.101个硬币100真、1假,真假区别在于重量。请用无砝码天平称两次给出真币重还是假币重的结论。 答: 101个先取出2堆, 33,33 第一次称,如果不相等,说明有一堆重或轻 那么把重的那堆拿下来,再放另外35个中的33 如果相等,说明假的重,如果不相等,新放上去的还是重的话,说明假的轻(不可能新放上去的轻)
第一次称,如果相等的话,这66个肯定都是真的,从这66个中取出35个来,与剩下的没称过的35个比 下面就不用说了
方法二: 第3题也可以拿A(50),B(50)比一下,一样的话拿剩下的一个和真的比一下。 如果不一样,就拿其中的一堆。比如A(50)再分成两堆25比一下,一样的话就在 B(50)中,不一样就在A(50)中,结合第一次的结果就知道了。
196.写出程序结果: void Func(char str[100]) { printf(“%d\n”, sizeof(str)); }
答: 4 分析: 指针长度
197.int id[sizeof(unsigned long)]; 这个对吗?为什么?? 答: 对 这个 sizeof是编译时运算符,编译时就确定了 可以看成和机器有关的常量。
198、 sizeof应用在结构上的情况 请看下面的结构: struct MyStruct { double dda1; char dda; int type }; 对结构MyStruct采用sizeof会出现什么结果呢?sizeof(MyStruct)为多少呢?也许你会这样求: sizeof(MyStruct)=sizeof(double)+sizeof(char)+sizeof(int)=13 但是当在VC中测试上面结构的大小时,你会发现sizeof(MyStruct)为16。你知道为什么在VC中会得出这样一个结果吗? 其实,这是VC对变量存储的一个特殊处理。为了提高CPU的存储速度,VC对一些变量的起始地址做了”对齐”处理。在默认情况下,VC规定各成员变量存放的起始地址相对于结构的起始地址的偏移量必须为该变量的类型所占用的字节数的倍数。下面列出常用类型的对齐方式(vc6.0,32位系统)。 类型 对齐方式(变量存放的起始地址相对于结构的起始地址的偏移量) Char 偏移量必须为sizeof(char)即1的倍数 int 偏移量必须为sizeof(int)即4的倍数 float 偏移量必须为sizeof(float)即4的倍数 double 偏移量必须为sizeof(double)即8的倍数 Short 偏移量必须为sizeof(short)即2的倍数 各成员变量在存放的时候根据在结构中出现的顺序依次申请空间,同时按照上面的对齐方式调整位置,空缺的字节VC会自动填充。同时VC为了确保结构的大小为结构的字节边界数(即该结构中占用最大空间的类型所占用的字节数)的倍?
199 #include “stdafx.h” Y n P }2{&k O v H `,o0 #define SQR(X) X*X int main(int argc, char* argv[]) { int a = 10; int k = 2; int m = 1; a /= SQR(k+m)/SQR(k+m); printf(“%d\n”,a); return 0; } 这道题目的结果是什么啊? define 只是定义而已,在编择时只是简单代换X*X而已,并不经过算术法则的 a /= k+m*k+m/k+m*k+m; =>a /= (k+m)*1*(k+m); =>a = a/9; =>a = 1;
200.下面的代码有什么问题?
void DoSomeThing(…) { char* p; p = malloc(1024); // 分配1K的空间 2y x if (NULL == p) return; p = realloc(p, 2048); // 空间不够,重新分配到2K if (NULL == p) return; } A: p = malloc(1024); 应该写成: p = (char *) malloc(1024); 没有释放p的空间,造成内存泄漏。 |
|