您的位置:首页 > 编程语言 > C语言/C++

C++笔试题(正在更新......)

2016-04-16 22:03 603 查看
1、以下程序的运行结果是(B)

int main(void)

{

printf("%s , %5.3s\n","computer","computer");

return 0;

}

A、computer , puter                                   B、computer , com

C、computer , computer                                D、computer , compu.ter  

解析:%m.ns 输出占m列,但只取字符串中左端n个字符。这n个字符输出在m列的右侧,左补空格。

 

2、以下程序的功能是(A)

#include<stdio.h>

int main(void)

{

FILE *fp;

long int n;

fp = fopen("wj.txt","rb");

fseek(fp , 0 , SEEK_END);

n = ftell(fp);

fclose(fp);

printf("%ld",n);

}

A、计算文件wj.txt内容的字节数
B、计算文件wj.txt的终止地址
C、计算文件wj.txt的起始地址
D、将文件指针定位到文件末尾

解析:int fseek(FILE *stream, long offset, int fromwhere);函数设置文件指针stream的位置。

如果执行成功,stream将指向以fromwhere为基准,偏移offset(指针偏移量)个字节的位置,函数返回0。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置,函数返回一个非0值。

函数 ftell 用于得到文件位置指针当前位置相对于文件首的偏移字节数。

3、以下程序段的输出结果是()

int p = 1234;

printf("%2d\n",p);

A、12    B、34           C、1234                D、提示出错、无结果

解析:%d表示是按照整数格式输出,中间2表示数字不足2位则补齐两位,不足位置用空格。-表示数字的对齐方式为左对齐,默认为右对齐的。

4、若二叉树中有n个度为2的结点,则该二叉树中的叶子结点数为(B)
A、n-1
B、n+1
C、2n
D、n/2

解析:对任意二叉树都有: n0 = n2 +1 ,其中n0是度为0的节点个数(即叶节点),n2是度为2的节点个数。

5、如下程序:

#include<iostream>

using namespace std;

 

class MyClass

{

public:

MyClass(int i = 0)

{

cout<<i;

}

MyClass(const MyClass &x)

{

cout<<2;

}

MyClass& operator=(const MyClass &x)

{

cout<<3;

return *this;

}

~MyClass()

{

cout<<4;

}

};

 

int  main() 

{

MyClass obj1(1) , obj2(2) , obj3(obj1);

return 0;

}

运行时的输出结果是(C)
A、11214444
B、11114444
C、122444
D、11214445
E、121444

解析:我将程序改写成下面这样,结构没变,只是输出更容易让人看懂:

注意:一般的类中可以有多个构造函数,只要构造函数的参数的个数或者类型不同(基于c++的重载函数原理),构造函数没有返回值。

析构函数既没有参数,也没有返回值。

#include"stdafx.h"

#include<iostream>

using namespace std;

 

class MyClass

{

public:

//构造函数1

MyClass(int i )   //int i=0;相当于对变量i进行初始化

{

cout << i<<"构造函数";

}

//构造函数2

MyClass(const MyClass &x)

{

cout << 2;

}

MyClass& operator=(const MyClass &x)

{

cout << 3;

return *this;

}

~MyClass()

{

cout << 4<<"析构函数";

}

};

 

int  main()

{

MyClass obj1(1);

cout << endl;

MyClass obj2(2);

cout << endl;

MyClass obj3(obj1);

cout << endl;

 

return 0;

}

 

6、以下程序段的输出结果是()

#include"stdafx.h"

#include<iostream>

using namespace std;

 

class A

{

public:

A(int i)

{

cout << "A ";

}

~A()  { }

};

 

class B

{

public:

B(int j)

{

cout << "B ";

}

~B()  { }

};

 

class C

{

public:

C(int k)

{

cout << "C ";

}

~C()  { cout << "~C "; }

};

 

class D : public C

{

public:

D(int i, int j, int k) : a(i), b(j), C(k)   //继承顺序C——B——A

{

cout << "D ";

}

~D()  { cout << "~D "; }

private:

B b;

A a;

};

 

 

int  main()

{

C *pc = new D(1, 2, 3);

delete pc;

 

return 0;

}

A、A  B  C  D ~D
B、A  B  C  D ~C
C、C  B  A  D ~D
D、C  B  A  D ~C

解析:

对象初始化顺序: c++搞了个成员初始化列表, 并确明确区分初时化跟赋值的区别. c++对象的初始化顺序是:
    (a) 基类初始化
    (b) 对象成员初时化
    (c) 构造函数的赋值语句
假设 class C : public A, public B {
        D d;//
     }
则初始化的顺序是A, B, D, C的构造函数. 这里基类的初始化顺序是按照声明的顺序, 成员对象也是按照声明的顺序.
 因此 c(int i, int j) : B(i), A(j) {} //这里成员初始化列表的顺序是不起作用的
析构函数的顺序则刚好是调过来, 构造/析构顺序可看作是一种栈的顺序
 
7、下面的程序段的输出结果是(D)

void main()

{

char *x = "abcd";

x += 2;

cout<<x;

}

A、指针变量x的地址
B、字符c的地址
C、c
D、cd

解析:看看下面的程序就可以找到答案了:

#include"stdafx.h"

#include<iostream>

using namespace std;

 

void main()

{

char *x = "abcd";

x += 2;

cout << x << endl;//cd

cout << x[0] << endl;//c

cout << x[1] << endl;//d

}

8、有如下程序

class Name

{

char name[20];

public:

Name()

{

strcpy(name , "");

cout<<'?';

}

Name(char *fname)

{

strcpy(name , fname);

cout<<'?';

}

};

void main()

{

Name names[3] = {Name("张三") , Name("李四") };

//数组长度为3,但是只有两个元素

}

运行此程序输出符号?的个数是(D)
A、1
B、2
C、0
D、3

解析:我将程序改写为下面的样子,结构不变!

#include"stdafx.h"

#include<iostream>

using namespace std;

class Name

{

char name[20];

public:

Name()

{

strcpy(name, "");

cout << '?';

cout << "构造函数" << endl;

}

Name(char *fname)

{

strcpy(name, fname);

cout << '?';

}

};

int main()

{

Name names[3] = { Name("张三"), Name("李四") };

return 0;

}

输出为:???构造函数

当这样写时:Name names[2] = { Name("张三"), Name("李四") };

输出为:??

分析:由于最后一个元素为空,所以程序选择调用构造函数。

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++