c/c++筆試題選(一)

1.  What does the following program print?

#include <iostream>

Using namespace std;

int main()

{

       int x=2,y,z;

x*=(y=z=5);   cout<<x<<endl ;

       z=3 ;

       x= =(y=z) ; cout<<x<<endl ;

       x=(y= =z) ; cout<<x<<endl ;

       x=(y&z) ; cout<<x<<endl ;

       x=(y&&z) ; cout<<x<<endl ;

       y=4 ;

x=(y|z) ; cout<<x<<endl ;

       x=(y||z) ; cout<<x<<endl ;

       return 0 ;

}

主要考查賦值,比較,與,或等運算符

答案:10,10,1,3,1,7

2.下面兩段代碼輸出結果有什麼不同?

1:#include <iostream>

using namespace std;

main()

{

       int a,x;

       for(a=0,x=0;a<=1&&!x++;a++)a++

cout<<a<<x<<endl;

}

2: #include <iostream>

using namespace std;

main()

{

int a,x;

       for(a=0,x=0;a<=1&&!x++;)a++;

       cout<<a<<x<<endl;

 

}

2,1        1,2

3用一個表達式,.判斷一個數X是否是 次方(24816…..,不可用循環語句。

!(X&(X-1))

4.下面程序的結果是多少?

#include <iostream>

#include <string>

using namespace std;

main()

{

       int count;

int m=999;

while(m)

{

count++;

m=m&(m-1);

}

cout<<count;

}

m二進制中1的個數

5ab交換

如何將ab的值進行交換,不用任何中間變量。

a=a+b;

b=a-b;

a=a-b;

 

a=ab;

b=ab;

a=ab;

6.在c++程序中調用C編譯器後的函數,爲什麼要加extern “C”?

C++語言支持函數重載,C語言不支持函數重載。函數被C++編譯後在庫中的名字與C語言的不同。C++提供了C連接交換指定符號extern “C”解決名字匹配問題。

7、頭文件中的ifndef/define/endif是幹什麼用的?

防止該頭文件被重複引用。

8、一個5位數字ABCDE*4=EDCBA,5個數字不重複,請編程求出來這個數字是多少?

#include <iostream>

using namespace std;

int main()

{

       for(int i=10000;i<100000;i++)

{

       int j=0;

int t=I;

while(t!0)

{

              j=j*10+t%10 ;

t/=10 ;

}

if(i<<2)==j){

cout<<i ;

break ;

}

}

9用預處理命令#define聲明一個常數,用以表明1年中有多少秒?

#define SECONDS_PER_YEAR (60*60*24*365)UL

10.const #define相比有什麼不同?

C++語言可以用const定義常量,也可以用#define定義常量,但是前者比後者又更多的優點:

(1)       const常量有數據類型,而宏常量沒有數據類型。編譯器可以對前者進行類型安全檢查,而對後者只進行字符替換,沒有類型安全檢查,並且在字符替換中可能會產生意料不到的錯誤。

(2)       有些集成化的調試工具可以對const常量進行調試,但是不能對宏常量進行調試。在C++程序中只使用const常量不使用宏常量,即const常量完全取代宏常量。

10.代碼輸出結果。

#include<iostream>

#include<stdio.h>

#include<string.h>

using namespace std;

struct{

short a1;

short a2;

short a3;

}A;

struct{

long a1;

       short a2;

}B;

int main()

{

char* ss1 = “0123456789”;

char ss2[] = “0123456789”;

char ss3[100]= “0123456789”;

int ss4[100];

char q1[]=”abc”;

char q2[]= ”a/n”;

char* q3=”a/n”;

char* str1=(char*)malloc(100) ;

void* str2=(void*)malloc(100) ;

 

cout<<sizeof(ss1)<<    ”;     4

cout<<sizeof(ss2)<<    ”;     11

cout<<sizeof(ss3)<<    ”;     100

cout<<sizeof(ss4)<<    ”;     400

cout<<sizeof(q1)<<    ”;      4

cout<<sizeof(q2)<<    ”;      3

cout<<sizeof(q3)<<    ”;      4

cout<<sizeof(A)<<    ”;       6

cout<<sizeof(B)<<    ”;              8

cout<<sizeof(str1)<<    ”;    4

cout<<sizeof(str2)<<    ”;    4

 return 0;

}

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章