public class X i

题目

public class X implements Runnable (   private int x;   private int y;    public static void main(String args) (   X that = new X();   (new Thread(that)) . start( );   (new Thread(that)) . start( );   )    public synchronized void run( ) (    for (;;) (    x++;    y++;    System.out.printIn(“x = “ + x + “, y = “ + y);    )   )    )   What is the result?()

  • A、 An error at line 11 causes compilation to fail.
  • B、 Errors at lines 7 and 8 cause compilation to fail.
  • C、 The program prints pairs of values for x and y that might not always be the same on the same line  (for example, “x=2, y=1”)
  • D、 The program prints pairs of values for x and y that are always the same on the same line (forexample, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by  “x=1, y=1”)
  • E、 The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by  “x=2s, y=2”)
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

以下程序输出结果为______。 include using namespace std; class TestClass 1 { publi

以下程序输出结果为______。

include<iostream>

using namespace std;

class TestClass 1

{

public:

TestClass1(){}

TestClass1(int i)

{x1=i;}

void dispa()

{cout<<"x1="<<x1<<",";}

private:

int x1;

}:

class TestClass2:public TestClass1

{

public:

TestClass2(){}

TestClass2(int i):TestClass1(i+10)

{

x2=i:

}

void dispb()

{

dispa();

cout<<"x2="<<x2<<end1;

}

private:

int x2:

}:

int main()

{

TestClass2 b(2):

b.dispb();

return 0;

}


正确答案:x1=12x2=2
x1=12,x2=2 解析:由主函数main入于,首先定义类TestClass2的对象b,成员数据为2。然后调用dispb函数。dispb中首先执行dispa函数,TestClass2为TestClass1的派生类,“TestClass2(int i):TestClass1(i+10)”所以TestClass1中的x1=12,所以dispa输出的结果为x1=12。在TestClass1中x2=2,所以dispb中的输出语句“cout"x2="x2end1;”中输出x2=2。即答案为“x1=12,x2=2”。

第2题:

下列类的定义中,有( ) 处语法错误。 class Base { public: Base(){} Base(int i) { data=i; } private: int data; }; class Derive: public Base { public: Derive(): Base(O) { } Derive(int x) { d=x; } void setvalue(int i) { data=i; } private: d; };

A.1

B.2

C.3

D.4


正确答案:B
解析:本题考核派生类的定义和成员的访问权限。第一处错误:在派生类的构造函数Derive(intx)中没有调用基类的构造函数对基类对象初始化:第二处错误:数据data是基类Base的私有成员,派生类Derive不能访问,所以在函数setvalue中对data的赋值是错误的。

第3题:

下列程序的输出结果是非曲直【 】。includeclass base{ int x, y;public: base(int i,

下列程序的输出结果是非曲直【 】。

include<iostream, h>

class base

{

int x, y;

public:

base(int i, int j){x=i; y=j;}

virtual int add(){return x+ y;}

};

class three: public base

{

int z;

public:

three(int i, int j, int k) :base(i, j){z=k; }

int add() { return (base:: add()+z); }

};

void main()

{

three * q=new three(lO,20,30);

cout<<q->add()<<end1;

}


正确答案:60
60 解析:本题考察继承中子类对父类的继承方式,注意子类的add成员函数,它直接使用了父类的成员函数进行运算。

第4题:

有如下程序;include using namespace std;class Base{public;Base(inti){x=i;}void d

有如下程序; #include <iostream> using namespace std; class Base { public; Base(inti){x=i;} void dispa0{cout<<x<<',';} private; int x; }; class Derived;public Base { public; Derived(int i);Base(i+10) {x=i;) void dispb(){dispa();cout<<x<<end1;} private; int x; }; int main() { Derived b(2) ; b.dispb(); return 0; } 运行的结果是( )。

A.2,2

B.12,2

C.12,10

D.10,2


正确答案:B
解析:"Derived b(2) ;¨以整数2实例化变量b,在执行Derived的构造函数时,以i+10即12去调用类Derived的父类的构造函数,将Base::x初始化为12,然后将Derived::x赋值为2。在执行b.dispb()时,先调用父类中的dispa,输出Base::x,即12,再输出Derived::x,即2,所以运行结果为“12,2”。

第5题:

为完成下面的程序,应在划线处填入的语句是includeusingnamespace std;class Base{pri

为完成下面的程序,应在划线处填入的语句是 #include<iostream> using namespace std; class Base { private: int x; public: Base (int i) { x=i; } ~Base(){} }; class Derived:public Base { public: ______________//完成类Derive构造函数的定义 }; int main() { Derived Obj; return 0; }

A.Derived(int i):Base(i){}

B.Derived(){}

C.void Derived (int i):Base(i){}

D.Derived(int i){Base(i);}


正确答案:A
解析:本题考核派生类中的构造函数。程序中,类Derived是基类Base的公有派生。在类Derived的构造函数应该包括调用基类构造函数使基类的数据成员得以初始化。

第6题:

下列程序的功能是为变量赋值,程序运行后,输出i=51。请改动main方法中的错误,使程序能够正确编译、运行并输出正确的结果。

注意:不改动程序结构。

class A

{

private int a;

public void setA (int x)

{

a=x;

}

public int getA()

{

return a;

}

}

public class MethodTest

{

public static void main(String args[])

{

A a=A();

a.getA(51);

int i=a.getA();

System.out.println ("i="+i);

}

}


正确答案:改正后的main方法如下: public static void main(String args[]) { A a=new A(); a.getA(51); int i=a.getA(); System.out.println("i="+i); }
改正后的main方法如下: public static void main(String args[]) { A a=new A(); a.getA(51); int i=a.getA(); System.out.println("i="+i); } 解析:本题综合考查类及类成员的定义与使用方面的知识。该程序中定义了两个类:A和MethodTest,类A中封装了一个私有的成员变量a和两个公有的方法setA和getA。在类MethodTest中包含了main方法。创建对象应使用new操作符来实例化对象,程序在创建对象a时未使用new,故存在错误。由于a是对象a的私有变量,在main方法中不能直接访问,只能通过对象a的公有方法setA和getA来访问。公有方法setA的功能是将传递回来的参数值赋给a,所以应当调用setA方法来为变量a赋值。

第7题:

在如下源代码文件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

第8题:

( 31 )有如下程序:

#include

using namespace std;

class A

{

public;

A ( int i ) {x= i ;}

Void sispa () {cout<<X<< ’ , ’ ;}

Private:

int x ;

};

class B;publicA

{

public;

B ( int i ) :A ( i +10 ) {x =i ;}

voud dispb () {dispa () ;cout<<X<,ENDL;}

Private:

Int x;

};

int main ()

{

B b ( 2 ) ;

b.dispb ()

retum 0:

}

运行时输出的结果是

A ) 10 , 2

B ) 12 , 10

C ) 12 , 2

D ) 2 , 2


正确答案:C

第9题:

有以下程序 include using namespace std; class Base { int a; public: Base(int x)

有以下程序

include <iostream>

using namespace std;

class Base

{

int a;

public:

Base(int x){ a=x; }

void show(){ cout<<a; }

class Derived : public Base

{

int b;

public:

Derived(int i) :Base(i+1),b(i){}

void show() { cout<<b;

};

int main ()

{

Base b(5),*pb;

Derived d(1);

pb=&d;

pb->show ();

return 0;

}

运行后的打印结果是______。


正确答案:2
2 解析:本题考核基类指针与派生类指针的使用。本例程序中类Derived是从基类Base公有继承来的。main()中定义了基类对象b和一个基类指针pb,又定义了派生类Derived的对象d。由于Derived是Base的子类型,因此可以将派生类Derived的对象d的地址赋值给指向基类Base的指针pb,但这时指针pb只能使用从基类Base继承的成员。所以通过对象指针Pb调用的show函数是基类的成员函数show(),从而输出基类私有数据成员a的值2。

第10题:

下面程序的打印结果是【】。 include using namespace std; class Base { public:Base(i

下面程序的打印结果是【 】。

include <iostream>

using namespace std;

class Base

{

public:

Base(int x)

{

a=x;

}

void show()

{

cout<<a;

}

private:

int a;

};

class Derived : public Base

{

public:

Derived(int i) :Base(i+1) ,b(i) { }

void show()

{

cout<<b;

}

private:

int b;

};

int main ( )

{

Base b(5) , *pb;

Derived d(1);

pb=&d;

pb->show();

return 0;

}


正确答案:2
2 解析:基类Base派生出派生类Derived,在主函数中,定义了基类对象b,基类指针pb,以及派生类对象d,并让基类指针pb指向派生类对象乙在C++中,当派生类的对象赋值给基类对象时,只能使用派生类对象中从基类继承的成员。所以最后执行语句“pb->show();”是调用基类的成员函数show(),输出a的值2。

更多相关问题