C/C++類
1、以下程序的輸出是(12)
- class Base
- {
- public:
- Base(int j) : i(j) { }
- virtual ~Base() { }
- void func1()
- {
- i *= 10;
- func2();
- }
- int getValue()
- {
- return i;
- }
- protected:
- virtual void func2()
- {
- i++;
- }
- protected:
- int i;
- };
-
- class Child : public Base
- {
- public:
- Child(int j) : Base(j) { }
- void func1()
- {
- i *= 100;
- func2();
- }
- protected:
- void func2()
- {
- i += 2;
- }
- };
-
- int main(void)
- {
- Base *pb = new Child(1);
- pb->func1();
- cout<<pb->getValue()<<endl;
- delete pb;
-
- return 0;
- }
2、請問程序的輸出結果是(30)
#define DOUBLE(x) x+x // x*2
int i = DOUBLE(5)*5;
cout<<i<<endl;
3、寫出一下程序的輸出(死循環)
- int main(void)
- {
- char num;
- for(num = 0; num < 255; )
- num += num;
- printf("%d\n",num);
- return 0;
- }
4、程序出錯在什麼階段?()
- int main(void)
- {
- http://www.sogou.com
- cout<<"welcome to sogou"<<endl;
-
- return 0;
- }
A、編譯階段出錯 B、運行階段出錯 C、編譯和運行都出錯 D、程序運行正常
因為http://www.sogou.com中//後面是注釋,前面的http:是標簽(類似於goto語句中的標簽)。(這個題目碉堡了)
5、下面程序執行結果為【說明:X86_64環境】(D)
- int main(void)
- {
- int a[4][4] = {
- {1,2,3,4},
- {50,60,70,80},
- {900,1000,1100,1200},
- {13000,14000,15000,16000} };
- int (*p1)[4] = a;
- int (*p2)[4] = &a[0];
- int *p3 = &a[0][0];
- printf("%d %d %d %d\n",*(*(a+1)-1),*(*(p1+3)-2)+1,*(*(p2-1)+16)+2,*(p3+sizeof(p1)-3));
-
- return 0;
- }
A、16000 1101 13002 2
B、4 2 3 60
C、16000 2 3 2
D、4 1101 13002 60
p1為指向一維數組的指針,所以a + 1指向{50,60,70,80}這一維的地址。減一則為4的地址;同理第二個輸出1101。同理,由於數組的列是4,所以*(p2 - 1) + 16就相當於*(p2) + 12,所以第三個輸出13002。
第四個由於p1是指針,所以sizeof(p1)為8(68位的系統),所以第四個輸出60。
6、在32位操作系統gcc編譯器環境下,下面的程序的運行結果是(A)
- class A
- {
- public:
- int b;
- char c;
- virtual void print()
- {
- cout<<"this is father's function!"<<endl;
- }
- };
-
- class B : A
- {
- public:
- virtual void print()
- {
- cout<<"this is children's function!"<<endl;
- }
- };
- int main(void)
- {
- cout<<sizeof(A)<<" "<<sizeof(A)<<endl;
-
- return 0;
- }
A、12 12
B、8 8
C、9 9
D、12 16
7、以下哪些做法是不正確或者應該極力避免的:【多選】(ACD)
A、構造函數聲明為虛函數
B、派生關系中的基類析構函數聲明為虛函數
C、構造函數調用虛函數
D、析構函數調用虛函數