public class Threads 1 {  intx=0;  public class Runner imple

题目
多选题
public class Threads 1 {  intx=0;  public class Runner implements Runnable {  public void run() {  int current = 0;  for(int=i=0;i<4;i++){  current = x;  System.out.print(current + “, “);  x = current + 2;  }  }  }  public static void main(String[] args) {  new Threads1().go();  }  public void go() {  Runnable r1 = new Runner();  new Thread(r1).start();  new Thread(r1 ).start();  }  }  Which two are possible results?()
A

0, 2, 4, 4, 6, 8, 10, 6,

B

0, 2, 4, 6, 8, 10, 2, 4,

C

0, 2, 4, 6, 8, 10, 12, 14,

D

0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,

E

0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,

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

第1题:

下面程序的运行结果是 include class base{ protected: int a; public: base( ) {c

下面程序的运行结果是

#include<iostream.h>

class base{

protected:

int a;

public:

base( ) {cout < < "0";}

};

class base l: virtual base{

public:

base l ( ) {cout < <"1";}

};

class base 2: virtual base{

public:

base2 ( ) {cout < <"2";}

};

class derived: public base 1, public base2{

public:

derived( ) {cout < < "3";}

};

void main( )

{

derive obj;

cout < < endl;

}

A.0123

B.3120

C.0312

D.3012


正确答案:A
解析:本题考查的是含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最后调用派生类自己的构造函数。

第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题:

有以下程序: included using namespace std; class Base { public: Base( ) { x=0; }

有以下程序:

included<iostream>

using namespace std;

class Base

{

public:

Base( )

{

x=0;

}

int x;

};

class Derived1:virtual public Base

{

public:

Derived1( )

{

x=10;

}

};

class Derived2:virtual public Base

{

public:

Derived2( )

{

x=20;

}

};

class Derived: public Derived1,protected Derived2

{ };

int main( )

{

Derived obj;

cout<<obj. x<<endl;

return 0;

}

该程序运行后的输出结果是______。


正确答案:20
20 解析:本题考核虚基类。本题中,虽然Derived 1和Derived 2都是由共同的基类x派生而来的,但由于引入虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derired 1中修改,还是在类Derived 2中修改,都是直接对这唯一拷贝进行操作。本题程序执行语句“Derived obj;”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derived 1的构造函数使得x=10,再执行类Derived 2的构造函数,使得x=20。最后输出x的值为20。

第4题:

下面程序的输出结果是【】。include using namespace std; class base { protected: int

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

include <iostream>

using namespace std;

class base

{

protected:

int a;

public:

base(){cout<<"0":}

};

class basel: virtual public base

{

public:

base1(){ cout<<"1";}

};

class base2 : virtual public base

{

public:

base2(){cout<<"2";}

};

class derived : public base1,public base2

{

public:

derived () {cout<<"3"; }

}

int main ()

{

derived obj;

cout<<end1;

return 0;

}


正确答案:0123
0123 解析:本题考核含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最有调用派生类自己的构造函数.题中先调用base的构造函数,然后调用base1、base2的构造函数,最后调用derived的构造函数。

第5题:

有以下程序:include using namespace std; class Base { public: Base() { K=0; } int

有以下程序:

include<iostream>

using namespace std;

class Base

{

public:

Base()

{

K=0;

}

int x;

};

class Derivedl:virtual public Base

{

public:

Derivedl()

{

x=10;

}

};

class Derived2:virtua1 public Base


正确答案:20。
20。 解析: 本题中,虽然Derived1和Derived2由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derived1中修改,还是在类Derived2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derived obi;”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derived1的构造函数使得x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。

第6题:

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

有如下程序: #include <iostream> using namespace std; class Base { public: Base(int x=0) { cout<<x; } } class Derived: public Base{ public: Derived(int x=0) { cout<<x; } private: Base val; }; int main() { Derived d(1); return 0; }程序的输出结果是

A.0

B.1

C.1

D.1


正确答案:D
解析:本题考查的知识点是:类的构造。建立一个类的对象时,构造函数的执行顺序如下:
①执行基类的构造函数,调用顺序按照各个基类被继承时声明的顺序(自左向右);
②执行成员对象的构造函数,调用顺序按照各个成员对象在类中声明的顺序(自上而下):(如果一行声明了多个对象,则还要遵循自左向右)
③执行自身的构造函数。
本题Derived类继承于Base类,所以首先会构造基类Base,但Derived类的构造函数没有初始化列表,所以将调用Base类的默认构造函数,输出一个0。接下来由于它的成员中还定义了一个Base类的对象,而构造函数也没有显示初始化这个对象,所以再次调用Base类的默认构造函数输出一个0。最后构造自身,因为主函数中传入了构造参数1,所以构造自身时输出了一个1。故最终输出结果为001,应该选择 D。

第7题:

有如下程序:includeusing namespace std;class Base{public:Base(int x=0){cout<

有如下程序: #include<iostream> using namespace std; class Base{ public: Base(int x=0){cout<<x;} }; class Derived:public Base{ public: Derived(int x=0){cout<<x;} private: Base val; }; int main( ){ Derived d(1); return 0; } 程序的输出结果是

A.0

B.1

C.01

D.001


正确答案:D
解析:派生对象在创建时先调用基类的构造函数,然后调用派生类的构造函数;撤销对象时,先调用派生类的构造函数,然后调用基类的构造函数。当类中出现其他类对象时,在初始化时先调用该对象的类的构造函数创建该对象。

第8题:

下面程序的运行结果是

#include

class base

{

protected:

int a;

public:

base(){ cout <<”0”;}

class basel:virtual base

}

public:

base 1(){cout <<”1”;}

};

class base2:virtual base

{

public:

base2(){cout <<”2”;(

};

class derived:public base1,public base2

{

public:

derived(){cout <<”3”;}

};

void main()

{

derived obj;

cout <

}

A.0123

B.3120

C.0312

D.3012


正确答案:A

第9题:

若有以下程序:include using namespace std;class Base{public: Base ( ) {x=0; } in

若有以下程序: #include <iostream> using namespace std; class Base { public: Base ( ) { x=0; } int x; }; class Derivedl : virtual public Base { public: Derivedl () { x=10; } }; class Derived2 : virtual public Base { public: Derived2 () { x=20; } }; class Derived : public Derivedl,protected Derived2{ }; int main ( ) { Derived obj; cout<<obj .x<<end1; return 0; } 该程序运行后的输出结果是 ( )。

A.20

B.30

C.10

D.0


正确答案:A
解析:本题中,虽然Derivedl和Derived2都是由共同的基类x派生而来的,但由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derived1修改,还是在类Derived2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derivedobj”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derived1的构造函数,使得x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。

第10题:

在下列源代码文件Test.java中,正确定义类的代码是( )。

A.pblic 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.protected class Test extends T2{ public int x = 0; public Test(int x) { this. x = x; } }


正确答案:B
解析:本题主要考查类声明格式为[修饰符]class类名[extends父类名][implements类实现的接口列表],选项A中源文件名与程序名不相同,Java不支持多重继承所以选项C错误,选项D中类的访问权限不对,应为public。

更多相关问题