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;

}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值