CMS专题

单选题Consider the following class:     class Test(int i) {     void test(int i) {  System.out.println(“I am an int.”); }    void test(String s) {   System.out.println(“I am a string.”);     }          public static void main(String args) {    Test t=new Tes

题目
单选题
Consider the following class:     class Test(int i) {     void test(int i) {  System.out.println(“I am an int.”); }    void test(String s) {   System.out.println(“I am a string.”);     }          public static void main(String args) {    Test t=new Test();     char ch=“y”;    t.test(ch);     }      }     Which of the statements below is true?()
A

 Line 5 will not compile, because void methods cannot be overridden.

B

 Line 12 will not compile, because there is no version of test() that rakes a charargument.

C

 The code will compile but will throw an exception at line 12.

D

 The code will compile and produce the following output: I am an int.

E

 The code will compile and produce the following output: I am a String.

如果没有搜索结果,请直接 联系老师 获取答案。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

给出下列代码,可放在类A的横线位置作为A合理的内部类的是( )。 class A { protected int i; A(int i) { this.i = i; } ______ }

A.class B { }

B.class B extends A { }

C.class B implements A { }

D.class A { }


正确答案:A
解析:本题主要考查在定义内部类时,内外部类不能同名,不存在继承关系,可以把内部类当成类的成员。选项B存在继承关系,因此是错误的;选项D中内外部类同名,所以也是错误的。选项C中,implements表示一个类使用某个接口,而A是一个类,不是接口,因此选项C也是错误的。

第2题:

在如下源代码文件Test.java中, 哪个是正确的类定义?()

A.public class test { public int x = 0; public test(int x) { this.x = x; } }

B.public class Test{ public int x=0; public Test(int x) { this.x = x; } }

C.public class Test extends T1, T2 { public int x = 0; public Test (int x) { this.x = x; } }

D.public class


正确答案:BD

第3题:

下面程序的运行结果是【】。 inChlde using namespace std; class count { static int n;

下面程序的运行结果是【 】。

inChlde<iOStream>

using namespace std;

class count

{

static int n;

public:

count()

{

n++;

}

static int test()

{

for(int i=0:i<4;i++)

n++;

return n;

}

};

int count::n=0;

int main()

{

cout<<COUnt::test()<<" ";

count c1, c2;

cout<<count::test()<<end1;

return 0;

}


正确答案:4 10
4 10 解析:本题主要考查C++类中静态数据成员的使用。题目程序首先定义了类count,其内部含有private类型数据成员static int n;同时含有public类型构造函数count()和静态成员函数static int test(),这两个函数的功能分别是为对象申请系统资源并将静态数据成员n加1和将静态数据成员n加4。主函数前,程序将静态数据成员n初始化为0,该数据成员为所有类count的对象所共有的数据成员:主函数中程序首先执行静态成员函数test()(由于test声明为static,因此其调用时无需通过具体对象),其执行过程中,静态数据成员n应该加4变成n=4,因此此处输出为4:此后程序创建对象c1和c2,由于在每次创建过程中都要调用构造函数count(),而每次调用 count()函数后,静态数据成员n值都会加1因此,创建两个对象之后,n值变为n= 6:再次执行test()函数后,n的值再次加4,因此变为n=6+4=10。故程序全部执行后,变量n值变为10,而中间程序输出为“410”。

第4题:

使用VC6打开考生文件夹下的工程test37_1,此工程包含一个源程序文件test37_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为:

0149 16 25 36 49 64 81

源程序文件test37_1.cpp清单如下:

include<iostream.h>

template <class T, int N = 100> class Vector

{

T vec[N];

public:

void set(int pos, T val);

T get(iht pos);

/***************** found *****************/

}

template <class T, int N> void Vector<T, N>::set(int pos, T val)

{

vec[pos] = val;

}

/***************** found *****************/

template <class T, int N> Vector<T, N>::get(int pos)

{

return vec[pos];

}

int main ()

{

Vector<double, 10> v;

int i = 0;

double d = 0.0;

for (i = 0; i < 10; i++)

v.set(i, double(i * i));

for (i = 0; i < 10; i++)

cout<<v.get(i)<<" ";

cout<<end1;

/***************** found *****************/

}


正确答案:(1) 错误:} 正确:}; (2) 错误:templateclass Tint N>VectorTN>::get(int pos) 正确:templateclass Tint N>T VectorTN>::get(int pos) (3) 错误:缺少返回值 正确:加入 return 0;
(1) 错误:} 正确:}; (2) 错误:templateclass T,int N>VectorT,N>::get(int pos) 正确:templateclass T,int N>T VectorT,N>::get(int pos) (3) 错误:缺少返回值 正确:加入 return 0; 解析:(1)主要考查考生对于类定义的理解,即使使用了类模板,在类定义的结尾仍然需要使用分号,这是C++的规定;
(2)主要考查考生是否会掌握了模板类的定义,template是关键字,在0中间是类型的定义,题目中Vector是一个类的名称,前面应该有该模板的名称,即T,这样才是完整的定义;
(3)主要考查考生对于函数返回值的掌握,任何返回值类型不为int型的函数最后都必须使用returen语句返回对应类型的值,就算是main函数也不例外。

第5题:

给出下列的程序段,则哪个选项是类A合理的内部类? ( ) class A{ protected int i; A(int i){ this.i=i;

A.classB { }

B.class B extendsA { }

C.class B extends A{ B(){System.out.println("i="+1);} }

D.class A { }


正确答案:A

第6题:

下面代码段的输出结果为( )。 public class Test { public static void main(String sss[]) { int i=0xFFFFFFFl; int j=~i; } }

A.0

B.1

C.14

D.-15


正确答案:C
解析:本题考查对位运算符的理解和掌握。j的值是将i的值按位取反得到的,所以,将0xFFFFFFF1取反得到0x0000000E,十进制数值为14。故本题答案是C。

第7题:

下列程序的执行结果是 ( ) public class Test { public int aMethod() { satic int i=0; i++; System.out.println(i); } public static void.main(String args[]) { Test test=new Test(); test.aMethod(); }

A.编译错误

B.0

C.1

D.运行成功,但不输出


正确答案:A

第8题:

下列程序段的输出结果是【 】。

public class Test {

void printValue(int m) {

do {

System.out.println("The value is"+m);

}while (--m>10);

}

public static void main (String args[]) {

int i=10;

Test t= new Test();

t.printValue(i);

}

}


正确答案:Thevalue is 10
Thevalue is 10 解析:本题考查do-while循环的用法。do-while至少执行一次,在执行完do中的内容后,判断while中的条件是否为true。如果为true,就再执行do中的内容,然后再进行判断。依次类推,直到while的判断为false时退出循环,执行循环后面的内容。题目中m的值为10,当程序运行到do-while循环时,程序先执行一次循环然后再作判断,在判断条件--m>10时,其值为false,退出循环。因此只执行了一次输出操作,输出内容为:The value is 10。

第9题:

下列程序的运行结果是【 】。 include class test { private: int num; public: tes

下列程序的运行结果是【 】。

include <iostream. h>

class test

{

private:

int num;

public:

test()

int TEST() {return num+100;}

~test()

};

test::test(){num=0;}

test::~test(){cout<<"Destructor is active"<<endl;}

void main()

{

test x[3]

cout<<x[1]. TEST()<<endl;

}


正确答案:100
100 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

第10题:

下面程序段的输出结果是 class Base { int i; Base() { add(1); } void add(int v) { i+=v; } void print() { System.out.println(i); } } class Extension extends Base { Extension() { add(2); } void add(int v) { i+=v*2; } } public class Test { public static void main(String args[]) { bogo(new Extension()); } static void bogo(Baseb){ b.add(8); b.print(); } }

A.9

B.18

C.20

D.22


正确答案:D
解析:本题考查继承和构造函数的用法。首先要明确对一个类的继承是指在父类的基础上构建了一个子类,子类继承了父类的方法和状态。题目所给程序段创建的是Extension类的实例,在运行时,从main()函数进入程序,所有调用add()方法的过程将始终和Extension类的add()方法动态绑定。初始值:i=0;创建实例new Extension();先调用父类的默认构造函数Base(),并在父类的默认构造函数中执行add(1),i=0+1×2,所以i=2,再调用子类的默认构造函数Extension(),子类的默认构造函数中执行add(2),i=2+2×2,所以i=6;执行add(8);i=6+8×2,因此,最终i=22,正确答案为选项D。

更多相关问题