C/C++笔试题(6)

 1、 完成程序,实现对数组的降序排序

#include <stdio.h>

void sort(              );

int main()

{

   int array[]={4556762341342323} //数字任//意给出

   sort(                 );

   return 0;

}

void sort(                  )

{ ____________________________________

 

  |                                   |

 

  |                                   |

 

  |-----------------------------------------------------|

 

}

 

2、 费波那其数列,11235……编写程序求第十项。可以用递归,也可以用其他方

法,但要说明你选择的理由。

#include <stdio.h>

int Pheponatch(int);

int main()

{ printf("The 10th is %d",Pheponatch(10));

  return 0;

}

int Pheponatch(int N)

{

--------------------------------

|                     |

 

|                     |

--------------------------------

}

 

3、下列程序运行时会崩溃,请找出错误并改正,并且说明原因。

#include <stdio.h>

#include <malloc.h>

typedef struct{

   TNode* left;

   TNode* right;

   int value;

} TNode;

TNode* root=NULL;

void append(int N);

int main()

{ append(63);

  append(45);

  append(32);

  append(77);

  append(96);

  append(21);

  append(17); // Again, 数字任意给出

}

void append(int N)

{ TNode* NewNode=(TNode *)malloc(sizeof(TNode));

NewNode->value=N;

  if(root==NULL)

  {

   root=NewNode;

   return;

  }

 else

 {

   TNode* temp;

   temp=root;

   while((N>=temp.value && temp.left!=NULL) || (N<temp. value && temp. right!=NULL))

   {

      while(N>=temp.value && temp.left!=NULL)

             temp=temp.left;

      while(N<temp.value && temp.right!=NULL)

              temp=temp.right;

   }

   if(N>=temp.value)

         temp.left=NewNode;

   else

         temp.right=NewNode;

   return;       

 }

}

4. A class B network on the internet has a subnet mask of 255.255.240.0, what

 is the maximum number of hosts per subnet.

B类地址的子网掩码是255.255.240.0,问每个子网内的最大主机数

子网掩码与iP相与得到子网号,255.255.240.0化成二进制11111111.11111111.11110000.00000000,即有12"0"2^12-2=4096-2=4094

a. 240    b. 255     c . 4094   d. 6553

 

5、. What is the difference: between o(log n) and o(log n^2), where both logarithems(对数) have base 2.

a. o(log n^2) is bigger   b. o(log n) is bigger   c. no difference 

 

6、. For a class what would happen if we call a class’s constructor from with

the same class’s constructor .

a. compilation error   b. linking error   c. stack overflow    d. none of the above

 

7、. “new”in c++ is a:

A. library function like malloc in c B. key word  C. operator  D. none of the above

malloc是库函数,不在编译器控制范围之内;new是运算符,在编译器控制范围之内。  
调用malloc时,从堆中申请内存;调用new时,从堆中申请内存并为内存调用构造函数。

 

8、 Which of the following information is not contained in an inode  .

a. file owner    b. file size  c. file name   d. disk address

 

9、 What’s the number of comparisons in the worst case to merge two sorted li

sts containing n elements each .

a. 2n           b.2n -1                  c .2n+1                d.2n-2

 

10、 Time complexity of n algorithm T(n), where n is the input size ,is T(n)=T(

n-1)+1/n if n>1 otherwise 1 the order of this algorithm is       .

 

a. log (n)   b. n                     c. n^2                  d. n^n

 

11、The number of 1’ s in the binary representation of 3*4096+ 15*256+5*16+ 3 a

re   .

a. 8                      b. 9                       c . 10           d. 12

 

12.设计函数 int atoi(char *s)

13int i=(j=4,k=8,l=16,m=32); printf(“%d”, i); 输出是多少?

14.解释局部变量、全局变量和静态变量的含义。

15.解释堆和栈的区别。

在传统的C中堆和栈实际是一块物理内存,堆主要用来动态分配内存,从堆栈内存的低端向上分配;而栈主要用来传递函数参数、返回值和局部参数内存分配,是从堆栈内存的高端向下分配,俗称压栈和出栈;堆是动态分配,比如用new,malloc分配,需要手工释放,不然会导致memory  leak,  
栈是静态分配,比如函数调用是需要分配堆栈,但堆栈能自动释放.

 

16.论述含参数的宏与函数的优缺点。

宏是编译期的,函数是运行期的;宏不是实体,而函数是一个可寻址的实体;宏只是编译期替换,在程序里每遇到S(a,b),就用a*b代替,ab两个实体并没有由宏实际产生,而函数S会在栈中定义两个对象ab。宏没有生存期、作用域之类的概念,而函数就有。

 

17. 以下三条输出语句分别输出什么?[C]

char str1[]       = "abc";

char str2[]       = "abc";

const char str3[] = "abc";

const char str4[] = "abc";

const char* str5  = "abc";

const char* str6  = "abc";

cout << boolalpha << ( str1==str2 ) << endl; // 输出什么?

cout << boolalpha << ( str3==str4 ) << endl; // 输出什么?

cout << boolalpha << ( str5==str6 ) << endl; // 输出什么?

答:分别输出false,false,truestr1str2都是字符数组,每个都有其自己的存储区,它们的值则是各存储区首地址,不等;str3str4同上,只是按const语义,它们所指向的数据区不能修改。str5str6并非数组而是字符指针,并不分配存储区,其后的“abc”以常量形式存于静态数据区,而它们自己仅是指向该区首地址的指针,相等。

 

18. C++内建型别 A B,在哪几种情况下B隐式转化A[C++中等]

答:

a. class B : public A { ……} // B公有继承自A,可以是间接继承的

b. class B { operator A( ); } // B实现了隐式转化为A的转化

c. class A { A( const B& ); } // A实现了non-explicit的参数为B(可以有其他带默认值的参数)构造函数

d. A& operator= ( const A& ); // 赋值操作,虽不是正宗的隐式类型转换,但也可以勉强算一个

19. 以下代码中的两个sizeof用法有问题吗?[C]

void UpperCase( char str[] ) // str 中的小写字母转换成大写字母

{

    for( size_t i=0; i<sizeof(str)/sizeof(str[0]); ++i )

        if( 'a'<=str[i] && str[i]<='z' )

            str[i] -= ('a'-'A' );

}

char str[] = "aBcDe";

cout << "str字符长度为: " << sizeof(str)/sizeof(str[0]) << endl;

UpperCase( str );

cout << str << endl;

答:函数内的sizeof有问题。根据语法,sizeof如用于数组,只能测出静态数组的大小,无法检测动态分配的或外部数组大小。函数外的str是一个静态定义的数组,因此其大小为6,函数内的str实际只是一个指向字符串的指针,没有任何额外的与数组相关的信息,因此sizeof作用于上只将其当指针看,一个指针为4个字节,因此返回4

 

20 以下代码有什么问题?[C]

void char2Hex( char c ) // 将字符以16进制表示

{

    char ch = c/0x10 + '0'; if( ch > '9' ) ch += ('A'-'9'-1);

    char cl = c%0x10 + '0'; if( cl > '9' ) cl += ('A'-'9'-1);

    cout << ch << cl << ' ';

}

char str[] = "I love 中国";

for( size_t i=0; i<strlen(str); ++i )

    char2Hex( str[i] );

cout << endl;

 

21 以下代码有什么问题?[C++]

struct Test

{

    Test( int ) {}

    Test() {}

    void fun() {}

};

void main( void )

{

    Test a(1);

    a.fun();

    Test b();

    b.fun();

}

答:变量b定义出错。按默认构造函数定义对象,不需要加括号。

22. 以下代码有什么问题?[C++]

cout << (true?1:"1") << endl;

答:三元表达式:”问号后面的两个操作数必须为同一类型。

 

23、. 以下代码能够编译通过吗,为什么?[C++]

unsigned int const size1 = 2;

char str1[ size1 ];

unsigned int temp = 0;

cin >> temp;

unsigned int const size2 = temp;

char str2[ size2 ];

答:str2定义出错,size2非编译器期间常量,而数组定义要求长度必须为编译期常量

24. 以下代码中的输出语句输出0吗,为什么?[C++]

struct CLS

{

    int m_i;

    CLS( int i ) : m_i(i) {}

    CLS()

    {

        CLS(0);

    }

};

CLS obj;

cout << obj.m_i << endl;

答:不能。在默认构造函数内部再调用带参的构造函数属用户行为而非编译器行为,亦即仅执行函数调用,而不会执行其后的初始化表达式。只有在生成对象时,初始化表达式才会随相应的构造函数一起调用。

25. C++中的空类,默认产生哪些类成员函数?[C++]

答:

class Empty

{

public:

    Empty();                          // 缺省构造函数

    Empty( const Empty& );            // 拷贝构造函数

    ~Empty();                         // 析构函数

    Empty& operator=( const Empty& ); // 赋值运算符

    Empty* operator&();               // 取址运算符

    const Empty* operator&() const;   // 取址运算符 const

};

 

26. 以下两条输出语句分别输出什么?[C++]

float a = 1.0f ;

cout << (int)a << endl;

cout << (int&)a << endl;

cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什么?

float b = 0.0f ;

cout << (int)b << endl;

cout << (int&)b << endl;

cout << boolalpha << ( (int)b == (int&)b ) << endl; // 输出什么

 

27. 以下反向遍历array数组的方法有什么错误?[STL]

vector array;

array.push_back( 1 );

array.push_back( 2 );

array.push_back( 3 );

for( vector::size_type i=array.size()-1; i>=0; --i ) // 反向遍历array数组

{

    cout << array[i] << endl;

}

答:首先数组定义有误,应加上类型参数:vector<int> array。其次vector::size_type被定义为unsigned int,即无符号数,这样做为循环变量的i0时再减1就会变成最大的整数,导致循环失去控制

 

28. 以下代码有什么问题?[STL]

typedef vector IntArray;

IntArray array;

array.push_back( 1 );

array.push_back( 2 );

array.push_back( 2 );

array.push_back( 3 );

// 删除array数组中所有的2

for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )

{

    if( 2 == *itor ) array.erase( itor );

}

答:同样有缺少类型参数的问题。另外,每次调用“array.erase( itor );”,被删除元素之后的内容会自动往前移,导致迭代漏项,应在删除一项后使itor--,使之从已经前移的下一个元素起继续遍历。

 

29. 写一个函数,完成内存之间的拷贝。[考虑问题是否全面]

答:

void* mymemcpy( void *dest, const void *src, size_t count )

{

    char* pdest = static_cast<char*>( dest );

    const char* psrc = static_cast<const char*>( src );

    if( pdest>psrc && pdest<psrc+cout ) 能考虑到这种情况就行了

    {

        for( size_t i=count-1; i!=-1; --i )

                pdest[i] = psrc[i];

    }

    else

    {

        for( size_t i=0; i<count; ++i )

            pdest[i] = psrc[i];

    }

    return dest;

}

int main( void )

{

    char str[] = "0123456789";

    mymemcpy( str+1, str+0, 9 );

    cout << str << endl;

 

    system( "Pause" );

    return 0;

}

 

 

30 线程与进程的区别

31:请你分别划划OSI的七层网络结构图,和TCP/IP的五层结构图?

32:请你详细的解释一下IP协议的定义,在哪个层上面,主要有什么作用? TCPUDP呢?

33:请问交换机和路由器分别的实现原理是什么?分别在哪个层次上面实现的?

34:请问C++的类和C里面的struct有什么区别?

35:请讲一讲析构函数和虚函数的用法和作用?

36:全局变量和局部变量有什么区别?实怎么实现的?操作系统和编译器是怎么知道的?

37:一些寄存器的题目,主要是寻址和内存管理等一些知识。

38、:8086是多少位的系统?在数据总线上是怎么实现的?

39、 对于C++中类(class) 与结构(struct)的描述正确的为:

  A,类中的成员默认是private,当是可以声明为public,private protected,结构中定

义的成员默认的都是public;

  B,结构中不允许定义成员函数,当是类中可以定义成员函数;

  C,结构实例使用malloc() 动态创建,类对象使用new 操作符动态分配内存;

  D,结构和类对象都必须使用new 创建;

  E,结构中不可以定义虚函数,当是类中可以定义虚函数.

  F,结构不可以存在继承关系,当是类可以存在继承关系.

:A,D,F

***************************************************************************

***************************************************************************

40、两个互相独立的类:ClassA ClassB,都各自定义了非景泰的公有成员函数 PublicFu

nc() 和非静态的私有成员函数 PrivateFunc();

   现在要在ClassA 中增加定义一个成员函数ClassA::AdditionalPunction(ClassA a,Cl

assB b);则可以在AdditionalPunction(ClassA x,ClassB y)的实现部分(函数功能体内部

)

    出现的合法的表达是最全的是:

    A,x.PrivateFunc();x.PublicFunc();y.PrivateFunc();y.PublicFunc();

    B,x.PrivateFunc();x.PublicFunc();y.PublicFunc();

    C,x.PrivateFunc();y.PrivateFunc();y.PublicFunc();

    D,x.PublicFunc();y.PublicFunc();

:B

***************************************************************************

***************************************************************************

41,C++程序下列说法正确的有:

  A,对调用的虚函数和模板类都进行迟后编译.

  B,基类与子类中函数如果要构成虚函数,除了要求在基类中用virtual 声名,而且必须名

字相同且参数类型相同返回类型相同

  C,重载的类成员函数都必须要:或者返回类型不同,或者参数数目不同,或者参数序列的类

型不同.

  D,静态成员函数和内联函数不能是虚函数,友员函数和构造函数也不能是虚函数,但是析

构函数可以是虚函数.

:A

***************************************************************************

42,C++中的类与结构的区别?

43,构造函数和析构函数是否可以被重载,为什么?

44,一个类的构造函数和析构函数什么时候被调用,是否需要手工调用?

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值