下面是我凭记忆想到的几个题目,有需要的同学就拿去吧,我也算做了点善事.
中体骏彩C++笔试题
2013-11-18
1.指针的含义是:B
A.名字 B.地址 C.名称 D.符号
2.给出下面的程序输出:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <locale>
using namespace std;
int main(int argc,char** argv){
char str[] = "陶";
char str1[] = "ztjcy中体骏彩";
wchar_t str2[] = L"ztjcy中体骏彩";
cout << strlen(str) << "," << sizeof(str) <<endl;
cout << strlen(str1) << "," << sizeof(str1) <<endl; 17,18
wcout << wcslen(str2)<< "," << sizeof(str2) <<endl; 9,40
return 0;
}
注意汉字占3个字节,是utf8编码时,宽字符大小是2字节,
3.重载(overload)和重写(overwrite)的区别?
重载overload:是函数名相同,参数列表不同 重载只是在类的内部存在。但是不能靠返回类型来判断。
重写override:也叫做覆盖。子类重新定义父类中有相同名称和参数的虚函数。函数特征相同。但是具体实现不同,主要是在继承关系中出现的 。
4.指出下面程序的错误?
class A{
public:
A(){ p = this; }
~A(){ if(p){
delete p;
p = null;
}
}
private:
A* p;
}
5.线程A在无限制地循环运行,线程B采用什么方式能让线程A安全退出?
有很多种退出线程的方法,这些方法的被称做线程之间的消息传递、同步、共享等。
终止线程,可以向线程发送消息/信号,或者一个互斥量,或者改变内存中的变量等,来通知线程,当线程收到这些消息/信号后,先做一些处理,比如释放掉线程内的资源,保存线程的运行结果等,然后自己退出线程,并返回一个UINT值。
6.TCP和UDP的区别?
TCP---传输控制协议,提供的是面向连接、可靠的字节流服务。当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之后才能传输数据。TCP提供超时重发,丢弃重复数据,检验数据,流量控制等功能,保证数据能从一端传到另一端。
UDP---用户数据报协议,是一个简单的面向数据报的运输层协议。UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但是并不能保证它们能到达目的地。由于UDP在传输数据报前不用在客户和服务器之间建立一个连接,且没有超时重发等机制,故而传输速度很快
7.PeekMessage()和GetMessage()的区别?
相同点:
PeekMessage函数与GetMessage函数都用于查看应用程序消息队列,有消息时将队列中的消息派发出去。
不同点:
无论应用程序消息队列是否有消息,PeekMessage函数都立即返回,程序得以继续执行后面的语句(无消息则执行其它指令,有消息时一般要将消息派发出去,再执行其它指令)。GetMessage函数只有在消息队列中有消息时返回,队列中无消息就会一直等,直至下一个消息出现时才返回。在等的这段时间,应用程序不能执行任何指令。
(从他们的不同点上来看,PeekMessage函数有点像“乞丐行乞”,有你就施舍点,没有也不抢。GetMessage函数有点像“强盗打劫”,有你得给,没有我就等你什么时候有了再给,这段时间我什么都不干,我就等你。)
8.输入一个整数,将它输出为以逗号分隔的字符串,比如1234567890,输出为"1,234,567,890"?
#include <iostream>
#include <string>
using namespace std;
int main()
{
//此程序用来格式化数字为“逗号分隔千位”的数值表示法,如:23,343,434,436
string str_number;
int str_length;
int swap_number;
cout<<"please enter an integer >= 1000:"<<endl;
cin>>str_number;
str_length = str_number.length();
swap_number = str_length % 3;
if(swap_number != 0)
{
cout<<str_number.substr(0,swap_number);
for (int i=swap_number;i<=str_length-3;i=i+3)
{
cout<<","<<str_number.substr(i,3);
}
}
else
{
for (int i=0;i<=str_length-3;i=i+3)
{
cout<<str_number.substr(i,3);
if(i != str_length-3)
{
cout<<",";
}
}
}
cout<<endl;
return 0;
}
9.给定一个大小为100的整数数组,要求随机地插入1......100中的数,要求不能插入重复的数,给出最高效的解法?
这题主要是借助stl中的set数据结构和rand()来实现高效的解法.set数据结构的一个特点是,键值合一,自动排序.
这里注意rand()的头文件是stdlib.h文件.下面是实现的参考代码
#include <stdlib.h>
#include <iostream>
#include <set>
using namespace std;
void create_rand(int len){
if(len <= 0){
cout << "please input a invalid value: " << endl;
return;
}
set<int> random_set;
do{
int val = rand()%len;
if(val == 0)
val = len;
if(!random_set.count(val))
random_set.insert(val);
}while(random_set.size()<len);
cout << "random array between 1 and" << len << ": " << endl;
set<int>::iterator set_it = random_set.begin();
while(set_it != random_set.end()){
cout << *set_it << endl;
++set_it;
}
}
int main(int argc, char** argv){
create_rand(100);
return 0;
}
这个是php的实现版本:
<?php
function create_rand(){
$arr = array();
do{
$rand = rand(1,100);
if(!in_array($rand))
$arr[] = $rand;
}while(count($arr)<100);
return $arr;
}
echo "create random integers between 1 and 100:\n";
var_dump(create_rand());
?>