笔试题目收集(4)

笔试题目搜集系列推荐:

(1)笔试题目搜集1

(2)笔试题目收集2

(3)笔试题目搜集3

(4)笔试题目搜集4

(5)笔试题目搜集5





1.求下列程序结果

#include"iostream" 
using namespace std; 
 
class A{ 
public:
   void funA();
};
class B{ 
public:
   void funB();
};
 
class C:public A,public B{
 
};
int main() 
{ 
    cout<<sizeof(C)<<endl;
    system("pause");
    return 0; 
}

输出结果:1。 在没有虚函数的时候,类的继承关系不分配虚表,也就没有指向虚表的指针,至于为什么是1。 本来求 sizeof 应该是 0 。但当我们声明该类型的实例的时候,它必须在内存中占有一定的空间,否则无法使用这些实例。可以参考 http://blog.csdn.net/u010064842/article/details/9222005 第七题。


2.求下列程序输出结果

#include"iostream"  
using namespace std;  

class A{  
public:
     A(int num=0){this->num = num;}
     virtual void fun1(){
	     cout<<"a="<<num<<endl;;
	     cout<<"A::fun1"<<endl;
     }
     virtual void fun2(){
	     cout<<"A::fun2"<<endl;
     }
private:
     int num;
};
class B{  
public:
     void funB(){};

};

class C:public A,public B{
};

int main()  
{  
     typedef void(*FUN)(); 
     A a(5);
     C c,c1;
     cout<<sizeof(A)<<endl;                                 //1
     cout<<"a虚表的位置"<<(int *)*(int *)&a<<endl;           //2
     cout<<"a中的num的值"<<*(int *)(((int *)&a)+1)<<endl;    //3
     cout<<endl;
     cout<<sizeof(C)<<endl;                                 //4
     cout<<"c虚表的位置"<<(int *)*(int *)&c<<endl;           //5
     cout<<"c1虚表的位置"<<(int *)*(int *)&c1<<endl;         //6
     FUN f = (FUN)*(int *)*(int *)&a;              
     f();                                                   //7
     system("pause");
     return 0;  
}  
运行结果:

下面对结果分析:

//1:输出8,因为类中有虚函数,所以每个对象包含一个虚指针(4字节),再加上int类型(4字节)。

//2:(int *)*(int *)&a,(int *)&a取出a对象的地址并转换为int*指针,*(int *)&a取出第一个元素的值也就是虚指针的                值,(int *)*(int *)&a,(int *)&a 转换为(int *)打印。具体结构如下:


//3:分析跟2是一样的,详细如上面的图解。

//4:一个虚指针+num的存储空间。

//5:对象c的虚指针,同时也看到对象a和对象c的虚指针不是一个地方,即父类和子类的虚指针不一样。

//6:对象c1的虚指针,发现c和c1的指针是一样的,同类子对象虚指针是一样的。

//7:  调用虚函数,但是参数没传入,打印的是随机值。

注:(int *)&a,指针转换为相应的类型


3.下列程序程序输出结果

#include"iostream"  
using namespace std;  

class A{  
public:
     virtual void fun1(){}
};
class B{  
public:
     virtual void fun2(){};

};

class C:public A,public B{
     char ch;
     int num;
};

class D:public A{
public:long long data;
};

int main()  
{  
     cout<<sizeof(A)<<endl;      //1
     cout<<sizeof(C)<<endl;      //2
     cout<<sizeof(D)<<endl;      //3
     D d;
     d.data = 25;
     cout<<*(long long *)((int *)&d+2)<<endl; //4
     system("pause");
     return 0;  
}  

输出结果:


//1:A中含有一个虚指针,所以大小为4

//2:C继承自B、C,因为B和C都有虚函数,所以此时C中有两个虚指针,分别指向两个虚表。同时C还有char和int类型两个成员,这里同样需要考虑对界问题。且这个这个对界,从0开始对的。具体如下:


//3:D中只包含一个虚指针,但是含有long long类型元素(8个字节),这是也要考虑对界问题,具体如下:


//4:((int *)&d+2)将地址加到下面红色的地方,然后转换成long long*指针类型,再取出指针所指内容。验证了下面结构的正确性。


4.选择题

#include <stdio.h>
int main(){
    int s;
    scanf(“%d”,&s);
    while(s >0)
    {
         Switch(s)
         {
             case 1:printf(“%d”,s+5);
             case 2:printf(“%d”,s+4);break;           
             case 3:printf(“%d”,s+3);
             default: printf(“%d”,s+2);break;
          }
         scanf(“%d”,&s);
    }
}

这段代码输入1 2 3 4 5 0回车,输出结果是(A)

A、6566567

B、65665672

C、66667

D、666672


5、linux的cron后台常驻程序(daemon)用于(D)

A、负责文件在网络找的共享

B、管理打印子系统

C、跟踪管理系统信息和错误

D、管理系统日常任务的调度


说明:在Linux中,我们经常用到 cron 服务器来完成这项工作。cron服务器可以根据配置文件约定的时间来执行特定的作务。比如我们可以在配置文件中约定每天早上4点,对httpd 服务器重新启动,这就是一个计划任务;在Linux系统中,计划任务一般是由cron承担,我们可以把cron设置为开机时自动启动。cron启动后,它会读取它的所有配置文件(全局性配置文件/etc/crontab,以及每个用户的计划任务配置文件),然后cron会根据命令和执行时间来按时来调用度工作任务。


6.求程序输出的结果

int main()
{
   const int size = 3; 
   int ia[ size ] = { 1, 2, 3 }; 
   vector< int > ivec( size ); 
 
   for ( int ix = 0; ix < size; ++ix ) 
      ivec.push_back( ia[ ix ]); 
   for ( int ix = 0; ix < size; ++ix ) 
      cout<<ivec[ix]<<" "; 
   return 0;
}
输出结果:0 0 0 1 2 3

说明:前面三个0是定义的时候,初始化为默认初始值。后面1,2,3是后面加上去的。该题输出结果不是预期的1 2 3,这点特别注意。(曾经犯的错误,记录下来)

7.10、Assume both x and y are integers,which one of the followings returns the minimum of the two integers?(微软的好题)
A、y^((x^y) & -(x<y))
B、y^(x^y)
C、x^(x^y)
D、(x^y)^(y^x)

E、None of above

x<y的时候,-(x<y)即-1的补码形式就是全1(111111),(x^y)&-(x<y)== x^y。x>y的时候,-(x<y)即0的补码形式就是全0(000000),(x^y)&-(x<y)== 0

8、The Orchid Pavilion(兰亭集序) is well known as the top of “行书”in history of Chinese literature. The most fascinating sentence is "Well I know it is a lie to say that life and death is the same thing, and that longevity and early death make no difference Alas!"(固知一死生为虚诞,齐彭殇为妄作).By counting the characters of the whole content (in Chinese version),the result should be 391(including punctuation). For these characters written to a text file,please select the possible file size without any data corrupt.

A、782 bytes in UTF-16 encoding
B、784 bytes in UTF-16 encoding
C、1173 bytes in UTF-8 encoding

D、1176 bytes in UTF-8 encoding

E、None of above

UTF-16每个字符采用两个字节,而UTF-8用1到6个字节编码UNICODE字符,一般是三个字符


9. A,B,C,D都为32位整型,基于以下给定的C,D能否得出A,B
A、C=A+B,D=A-B
B、C=A+2*B,D=A+B
C、C=A+B,D=B

D、C=A-B,D=(A+B)>>1
E、C=A*B,D=A/B 
该题主要是考虑越界问题
对于A选项假设A>0,B>0;C可能越界使得C=A+B-2^32举个反例:A=B=2^31-1 C=-2,D=0;
A=B=-1,C=-2,D=0
对于C选项不管C是否越界总能得到A=C-D, B=D
对于B选项我们可以考虑Q=A+B, C=Q+B ,D=Q跟C的那个一样,就能求出Q与B Q=A+B,B又已知A可求
D选项:A=B=-1 A=B=2^31-1
E选项:A=B=2^15, A=B=2^31


10.请写出strcpy 和 memcpy 的区别(5分)
答:strcpy和memcpy都是标准C库函数,它们有下面的特点。
strcpy提供了字符串的复制。即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符。
strcpy函数的原型是:char* strcpy(char* dest, const char* src);
memcpy提供了一般内存的复制。即memcpy对于需要复制的内容没有限制,因此用途更广。
memcpy函数的原型是:void *memcpy( void *dest, const void *src, size_t count );
strcpy和memcpy主要有以下3方面的区别。
1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。
2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。
3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy。


11.下面程序运算的结果

#include<iostream>

using namespace std;

class A{
public:
     void print(){cout<<"A"<<endl;}
};

class	B:public A{
public:
     void print(){cout<<"B"<<endl;}
};

int main()
{
    A *p = new B();
    p->print();
    system("pause");
    return 0;
}

运行结果:A


12.快速求出数组中的最后一个数

  1. // 得到数组的最后一个数 - 使用指向数组的指针来完成  
  2. #include <stdio.h>  
  3. int main()  
  4. {  
  5.   printf("    得到数组的最后一个数的趣味实现 \n");    
  6.   printf(" - http://blog.csdn.net/morewindows/article/details/10022147 -\n");    
  7.   printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");  
  8.   
  9.   const int MAXN = 8;  
  10.   int a[MAXN] = {1, 2, 4, 8, 16, 32, 64, 128}; 
  11.   //这里关键是&a将数组名转换为了行指针 
  12.   int *p = (int*)(&a + 1);  //&a 是一个指针,指向大小为4的数组 int(*)[MAXN]  
  13.   printf("%d\n", *(p - 1)); //为128  
  14.   return 0;  

待续。。。。。。。


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值