笔试题集锦(1)
来源:互联网 发布:powerdesigner 转sql 编辑:程序博客网 时间:2024/05/24 07:32
1) 下面代码的结果是什么?(台湾某cpu公司05年笔试题)
#include <iostream>
using namespace std;
#define product(x) (x*x)
int main()
{
int i=3,j,k;
j = product(i++);
k = product(++i);
cout<<"j="<<j<<",k="<<k<<endl;
return 0;
}
答案#region 答案
j=9,k=49,因为product(i++)=(i++*i++),所以j等于,此时i为,product(++i) 要求先累加i,则i为,所以,k结果为
#endregion
using namespace std;
#define product(x) (x*x)
int main()
{
int i=3,j,k;
j = product(i++);
k = product(++i);
cout<<"j="<<j<<",k="<<k<<endl;
return 0;
}
答案#region 答案
j=9,k=49,因为product(i++)=(i++*i++),所以j等于,此时i为,product(++i) 要求先累加i,则i为,所以,k结果为
#endregion
2) 下面代码的结果是什么?(国内公司05年笔试题)
int a = 5,b = 3;
!a&&b++;
!a&&b++;
执行后
答案#region 答案
a为 5,b为 3,因为!a使表达式为假,则不必再去计算后面的了。
#endregion
a为 5,b为 3,因为!a使表达式为假,则不必再去计算后面的了。
#endregion
3) 下面的代码两种写法,你认为哪种更好?(美国某嵌入式公司05年10月笔试题)
写法1:
if('A'==a)
{
a++;
}
{
a++;
}
写法2:
if(a =='A')
{
a++;
}
答案#region 答案
第1种更好,因为如果不小心把“==”写成了“=”,则是对常量赋值,编译器会报错的,而第2种会通过编译。
#endregion
{
a++;
}
答案#region 答案
第1种更好,因为如果不小心把“==”写成了“=”,则是对常量赋值,编译器会报错的,而第2种会通过编译。
#endregion
4) 下面代码的结果是什么?(台湾某cpu公司05年笔试题)
char foo()
{
unsigned int a = 6;
int b = -20;
char c;
(a+b>6)?(c=1):(c=0);
return c;
}
答案#region 答案
返回值为1,因为unsigned int的数与int类型的数进行运算,后者会自动转化为unsigned int型,因此a+b的值大于6
#endregion
{
unsigned int a = 6;
int b = -20;
char c;
(a+b>6)?(c=1):(c=0);
return c;
}
答案#region 答案
返回值为1,因为unsigned int的数与int类型的数进行运算,后者会自动转化为unsigned int型,因此a+b的值大于6
#endregion
5) 如何不使用中间变量进行两个数的交换?
答案#region 答案
方法一:
a=a+b;
b=a-b;
a=a-b;
但这个方法缺点是:如果a,b两个数很大,a+b会超界
方法二:
a=a^b;
b=a^b;
a=a^b;
这里不用担心超界的问题
#endregion
方法一:
a=a+b;
b=a-b;
a=a-b;
但这个方法缺点是:如果a,b两个数很大,a+b会超界
方法二:
a=a^b;
b=a^b;
a=a^b;
这里不用担心超界的问题
#endregion
6) 如何判断一段程序是由C编译器还是C++编译器编译的?(美国某网络公司05年笔试题)
答案#region 答案
#include <iostream>
using namespace std;
int main()
{
char ch;
string word;
#ifdef __cplusplus
cout<<"hello,c++"<<endl;
#endif
cin>>ch;
return 0;
}
#include <stdio.h>
int main()
{
#ifdef __STDC__
printf("hello,c program");
#endif
return 0;
}
另外,再附加一些有用的常量定义
#include <iostream>
using namespace std;
int main()
{
char ch;
string word;
cout <<__FILE__<< " : line " << __LINE__<<endl;
cin>>ch;
return 0;
}
__LINE__记录文件已经被编译的行数,__FILE__包含正在被编译的文件的名字
另外两个预定义名字分别包含当前被编译文件的编译时间__TIME__ 和日期
__DATE__ 时间格式为hh:mm:ss
#include <iostream>
using namespace std;
int main()
{
char ch;
string word;
cout <<__TIME__<< " : " << __DATE__<<endl;
cin>>ch;
return 0;
}
#endregion
#include <iostream>
using namespace std;
int main()
{
char ch;
string word;
#ifdef __cplusplus
cout<<"hello,c++"<<endl;
#endif
cin>>ch;
return 0;
}
#include <stdio.h>
int main()
{
#ifdef __STDC__
printf("hello,c program");
#endif
return 0;
}
另外,再附加一些有用的常量定义
#include <iostream>
using namespace std;
int main()
{
char ch;
string word;
cout <<__FILE__<< " : line " << __LINE__<<endl;
cin>>ch;
return 0;
}
__LINE__记录文件已经被编译的行数,__FILE__包含正在被编译的文件的名字
另外两个预定义名字分别包含当前被编译文件的编译时间__TIME__ 和日期
__DATE__ 时间格式为hh:mm:ss
#include <iostream>
using namespace std;
int main()
{
char ch;
string word;
cout <<__TIME__<< " : " << __DATE__<<endl;
cin>>ch;
return 0;
}
#endregion
- 笔试题集锦(1)
- 笔试题集锦(1)
- C++ 笔试题集锦(1)
- 单片机笔试题集锦1
- 笔试题集锦(2)
- 笔试题集锦(2)
- java笔试题集锦(一)
- C++ 笔试题集锦(2)
- Java笔试题集锦
- Java笔试题集锦
- 金山笔试题集锦
- Java笔试题集锦
- Java笔试题集锦
- Java笔试题集锦
- 计算机笔试题集锦
- Java笔试题集锦
- Java笔试题集锦
- Java笔试题集锦
- 2007浙大复试上机考试题目1--最小长方形
- 大数计算常用函数
- C#制作简易播放器
- Ajax初感想
- 笔试题集锦(2)
- 笔试题集锦(1)
- 第一次面试
- 数据结构复习笔记(6)
- WebCast听课录(11)
- WebCast听课录(10)
- WebCast听课录(9)
- WebCast听课录(8)
- WebCast听课录(7)
- WebCast听课录(6)