3下列代码中,将引起一个编译错误的行是()。1)public class Test{2)int m,n;3)public Test(){}4)pu

题目

3下列代码中,将引起一个编译错误的行是( )。 1)public class Test{ 2)int m,n; 3)public Test(){} 4)public Test(int a){m=a;} 5)public static void main(String args[]){ 6)Test t1,t2; 7)int j,k; 8)j=0; k=0; 9) t1=new Test(); 10)t2=new Test(j,k); 11)} 12)}

A.第3行

B.第5行

C.第6行

D.第10行

参考答案和解析
正确答案:D
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

有如下程序: include using namespace std; class Test{ public: Tes

有如下程序: #include<iostream> using namespace std; class Test{ public: Test() {n+=2;} ~Test() {n-=3;} static int getNum(){return n;} private: static int n; }; int Test::n=1; int main() { Test*p=new Test; delete p; cout<<"n="<<Test::getNum()<<endl; return 0; } 执行后的输出结果是( )。

A.n=0

B.n=1

C.n=2

D.n=3


正确答案:A
解析:语句Test*p=new Test;会调用类的构造函数Test() {n+=2;},使n的值由原来的1变为3,然后delete p调用类的析构函数~Test() {n-=3;},因为n是static型变量,所以会在3的基础上减 3,使得输出结果为0。

第2题:

有如下程序:include usingnamespacestd:class Test{public: Test(){n+=2; ~Test(){n-

有如下程序:#include <iostream>using namespace std:class Test{public: Test() {n+=2; ~Test() {n-=3; ; static int getNum() {return n;}privaue: static int n:};int Test::n=1;int main(){ Test* p=new Test; delete p; cout<<"n="<<Test::getNum()<<end1; return 0;} 执行后的输出结果是

A.n=0

B.n=1

C.n=2

D.n=3


正确答案:A
解析:本题考核静态数据成员与静态成员函数的定义与使用方式。静态数据成员是类中所有对象共享的成员,而不是某个对象的成员.题中变量n是静态数据成员,对象对其操作的结果具有叠加作用,main函数中先定义了Test的对象*p,然后又delete p,所以对静态数据n进行了两次操作,分别是“n+=2”和“n-=3”,n的初始值是1,那么n最后的值变为0。main函数最后通过调用静态函数getNum得到n的值,并输出。

第3题:

有如下程序:includeusing namespace std;Class Test{public:Test(){n+=2;}~Test(){n-

有如下程序: #include<iostream> using namespace std; Class Test { public: Test() {n+=2;} ~Test() {n-=3;} static int getNum() {return n;} private: static int n; }; int Test::n=1; int main() { Test*p=new Test;

A.n=0

B.n=1

C.n=2

D.n=3


正确答案:A
解析: 静态数据成员是类中所有对象共享的成员,而不是某个对象的成员。题中变量n是静态数据成员,对象对其操作的结果具有叠加作用,main函数中先定义了Test的对象*p,然后又delete p,所以对静态数据n进行了两次操作,分别是”n+=2”和”n+=3”,n的初始值是1,那么n最后的值变为0。main函数最后通过调用静态函数getNum得到n的值,并输出。

第4题:

有下列程序:includeusing namespace Std;class Test{public:Test(){n+=2;}~Test(){n-

有下列程序: #include<iostream> using namespace Std; class Test { public: Test() {n+=2;} ~Test() {n-=3;} static int getNum(){return n;} private: static int n; }; int Test∷n=1; int main()

A.n=0

B.n=l

C.n=2

D.n=3


正确答案:A
解析: 此题考查的是静态数据成员和静态成员函数。静态数据成员是类中所有对象共享的成员,而不是某个对象的成员。题目中的静态数据成员n的运算具有叠加性,执行“n+=2”和“n-=3”后n的值为0。

第5题:

有下列程序: include using namespace std; classTest{ public: Test(){n+=2;} ~Test

有下列程序: #include<iostream> using namespace std; classTest{ public: Test() {n+=2;} ~Test() {n-=3;} static int getNum(){return n;} private: static int n; }; int Test::n=1; int main() { Test*p=new Test

A.n=0

B.n=l

C.n=2

D.n=3


正确答案:A
解析: 语句Test*p=new Test;会调用类的构造函数Test() {n+=2;},使n的值由原来的1变为3,然后delete p调用类的析构函数~Test() {n-=3;},因为n是static型变量,所以会在3的基础上减3使得输出结果为0。

第6题:

如下程序的输出结果是includeusing namespace std;class Test{public:Test( ){n+=2;}

如下程序的输出结果是 #include<iostream> using namespace std; class Test{ public: Test( ){n+=2;} ~Test( ){n-=3;} static int getNum( ){return n;} private: static int n; }; int Test::n=1; int main( ){ Test*P=new Test: delete P; cout<<"n="<<Test::getNum( )<<endl; return 0; }

A.n=0

B.n=1

C.n=2

D. n=3


正确答案:A
解析:静态数据成员的初始值n=1,执行Test*p=new Test;,调用构造函数后,n= 3,deletep;调用析构函数,n-=3,所以最终n=0。

第7题:

有如下程序:include using namespace std;class Test{public:Test(){n+=2; }~Test(){

有如下程序: #include <iostream> using namespace std; class Test { public: Test() {n+=2; } ~Test() {n-=3; } static int getNum() {return n; } private: static int n; }; int Test::n=1; int main() { Test* p=new Test; delete p; cout<<"n="<<Test::getNum()<<endl; return 0; } 执行该程序的输出结果是( )。

A.n=0

B.n=1

C.n=2

D.n=3


正确答案:A
解析:此题考查的是静态数据成员和静态成员函数。静态数据成员是类中所有对象共享的成员,而不是某个对象的成员。题目中的静态数据成员n的运算具有叠加性,执行“n+=2”和“n-=3”后n的值为0。

第8题:

有如下程序: #include(iostream> using namespace std; Class Test { public: Test() {n+=2;} ~Test() {n-=3;) static int getNum() {return n;} private: static int n; }; int Test::n=1; int main() { Test*p=new Test;

A.n=0

B.n=1

C.n=2

D.n=3


正确答案:A
解析: 本题考查的是静态数据成员和静态成员函数。静态数据成员是类中所有对象共享的成员,而不是某个对象的成员。题目中的静态数据成员n的运算具有叠加性,执行“n+=2”和“n-=3”后n的值为0。

第9题:

有如下程序: include using namespace std;class Test {public: Test() {n+=2;} ~Tes

有如下程序: #include <iostream> using namespace std; class Test { public: Test() {n+=2;} ~Test() {n-=3;} static int getNum(){retum n;} private: static int n; }; int Test:: n=1; int main() { Test*p=new Test; delete p; cout<<"n="<<Test:: getNum()<<end1; return 0; };执行后的输出结果是______.

A.n=0

B.n=1

C.n=2

D.n=3


正确答案:A
解析:经过一次构造函数和析构函数的调用后,执行后的输出结果是A。

第10题:

如下程序编译时发生错误,错误的原因是show函数实现语句错误,则正确的语句应该为______。

include<iostream.h>

class test

{

private:

int hum;

public:

test(int);

void show( );

};

test::test(int n){num=n;}

test::show( ){cout<<num<<endl;}

void main( )

{

test T(10):

T.show( );

}


正确答案:void test::show( ){coutnumendl;}
void test::show( ){coutnumendl;} 解析:show成员函数的声明和实现不一致,即实现部分应有void修饰符,这样才能编译通过。