笔试题集锦(1)

1)  下面代码的结果是什么?(台湾某cpu公司05年笔试题)

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

None.gif #include  < iostream >
None.gif
using   namespace  std;
None.gif
None.gif
#define  product(x) (x*x)
None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int i=3,j,k;
InBlock.gif    j 
= product(i++);
InBlock.gif    k 
= product(++i);
InBlock.gif    cout
<<"j="<<j<<",k="<<k<<endl;
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

ContractedBlock.gifExpandedBlockStart.gif            
答案 #region 答案
InBlock.gif                j
=9,k=49,因为product(i++)=(i++*i++),所以j等于,此时i为,product(++i)  要求先累加i,则i为,所以,k结果为
ExpandedBlockEnd.gif            
#endregion

None.gif

2)  下面代码的结果是什么?(国内公司05年笔试题)

None.gif      int  a  =   5 ,b  =   3 ;
None.gif
None.gif
! a && b ++ ;
None.gif


执行后

a b 的值是 ?

ContractedBlock.gif ExpandedBlockStart.gif      答案 #region 答案
InBlock.gif             a为 
5,b为 3,因为!a使表达式为假,则不必再去计算后面的了。
ExpandedBlockEnd.gif            
#endregion

None.gif

3)  下面的代码两种写法,你认为哪种更好?(美国某嵌入式公司0510月笔试题)

 

写法1

None.gif          if ( ' A ' == a)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif       a
++;
ExpandedBlockEnd.gif}

None.gif

   写法2

None.gif if (a  == ' A ' )
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif       a
++;
ExpandedBlockEnd.gif}

None.gif
ContractedBlock.gifExpandedBlockStart.gif            
答案 #region 答案
InBlock.gif           第1种更好,因为如果不小心把“
==”写成了“=”,则是对常量赋值,编译器会报错的,而第2种会通过编译。
ExpandedBlockEnd.gif            
#endregion

None.gif
None.gif

4)  下面代码的结果是什么?(台湾某cpu公司05年笔试题)

None.gif char  foo()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif     unsigned 
int a = 6;
InBlock.gif     
int b = -20;
InBlock.gif     
char c;
InBlock.gif     (a
+b>6)?(c=1):(c=0);
InBlock.gif     
return c;
ExpandedBlockEnd.gif}

None.gif
ContractedBlock.gifExpandedBlockStart.gif            
答案 #region 答案
InBlock.gif           返回值为1,因为unsigned int的数与int类型的数进行运算,后者会自动转化为unsigned int型,因此a
+b的值大于6
ExpandedBlockEnd.gif            
#endregion

None.gif

5)  如何不使用中间变量进行两个数的交换?

ContractedBlock.gif ExpandedBlockStart.gif        答案 #region 答案
InBlock.gif        方法一:
InBlock.gif    a
=a+b;
InBlock.gif    b
=a-b;
InBlock.gifa
=a-b;
InBlock.gif但这个方法缺点是:如果a,b两个数很大,a
+b会超界
InBlock.gif
InBlock.gif方法二:    
InBlock.gif    a
=a^b;
InBlock.gif    b
=a^b;
InBlock.gif    a
=a^b;
InBlock.gif这里不用担心超界的问题
ExpandedBlockEnd.gif            
#endregion

None.gif

6)  如何判断一段程序是由C编译器还是C++编译器编译的?(美国某网络公司05年笔试题)

ContractedBlock.gif ExpandedBlockStart.gif            答案 #region 答案
InBlock.gif    #include 
<iostream>
InBlock.gif
using namespace std;
InBlock.gif
InBlock.gif
int main()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
char ch;
InBlock.gif    
string word;
InBlock.gif    #ifdef __cplusplus
InBlock.gif           cout
<<"hello,c++"<<endl;
InBlock.gif    
#endif
InBlock.gif    cin
>>ch;
InBlock.gif    
return 0;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif#include <stdio.h>
InBlock.gif
int main()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    #ifdef __STDC__
InBlock.gif           printf("
hello,c program");
InBlock.gif    
#endif
InBlock.gif    
return 0;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif另外,再附加一些有用的常量定义
InBlock.gif#include 
<iostream>
InBlock.gif
using namespace std;
InBlock.gif
InBlock.gif
int main()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
char ch;
InBlock.gif    
string word;
InBlock.gif    cout 
<<__FILE__<< " : line " << __LINE__<<endl;
InBlock.gif    cin
>>ch;
InBlock.gif    
return 0;
ExpandedSubBlockEnd.gif}

InBlock.gif__LINE__记录文件已经被编译的行数,__FILE__包含正在被编译的文件的名字
InBlock.gif另外两个预定义名字分别包含当前被编译文件的编译时间__TIME__ 和日期
InBlock.gif__DATE__ 时间格式为hh:mm:ss
InBlock.gif#include 
<iostream>
InBlock.gif
using namespace std;
InBlock.gif
InBlock.gif
int main()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
char ch;
InBlock.gif    
string word;
InBlock.gif    cout 
<<__TIME__<< " : " << __DATE__<<endl;
InBlock.gif    cin
>>ch;
InBlock.gif    
return 0;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif            
#endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值