public class ClassA {  public int getValue() {  int value=0;  boolean setting = true;  String title=”Hello”;  (value || (setting  title == “Hello”)) { return 1; }  (value == 1  title.equals(”Hello”)) { return 2; }  }  } And:  ClassA a = new ClassA();  a.g

题目

public class ClassA {  public int getValue() {  int value=0;  boolean setting = true;  String title=”Hello”;  (value || (setting && title == “Hello”)) { return 1; }  (value == 1 & title.equals(”Hello”)) { return 2; }  }  } And:  ClassA a = new ClassA();  a.getValue();  What is the result?() 

  • A、 1
  • B、 2
  • C、 Compilation fails.
  • D、 The code runs with no output.
  • E、 An exception is thrown at runtime.
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

有如下类定义: ClaSS MyClass{ int value; public: MyClass(int n):value(n){ } int getValue( )const{return value;} l; 则类MyClass的构造函数的个数是

A.1个

B.2个

C.3个

D.4个


正确答案:A
解析:对于类的构造函数,如果有显式定义,那么在构造该类对象时就自动调用显式定义的构造函数,否则系统将给类一个默认的空构造函数。

第2题:

请阅读下面程序 public class ThreadTest{ public static void main(String args[]) ( Thread t1=new Thread(new Hello()); Thread t2=new Thread(new Hello()); t1.start(); t2.start(); } } class Hello implements Runnable { int i; public void run() { while(true) { System.out.prinfin("Hello"+i++); if(i=5) break; } } } 该程序创建线程使用的方法是

A.继承Thread类

B.实现Runnable接口

C.t1.start()

D.t2.start()


正确答案:B
解析:本题考查线程的创建。Java中,线程的创建有两种方法:
  (1)通过实现Runnable接口创建线程。Runnable接口中只定义了一个run()方法作为线程体。
  (2)通过继承Thread类创建线程。Thread类本身实现了Runnable接口。
  无论使用哪种方法来创建线程,新建的线程不会自动运行,必须调用线程的start()方法,才能运行该线程。
  本题程序中的Hello类实现了Runnable接口,即采用的是第一种方法创建线程。因此,本题的正确答案是选项B。

第3题:

( 28 ) 有如下程序

#include

using namespace std;

int i=1;

class Fun{

public:

static int i;

int value( ){ return i-1;}

int value( )const{ return i+1;}

};

int Fun::i=2;

int main( ){

int i=3;

Fun fun1;

const Fun fun2;

____________

return 0;

}

若程序的输出结果是 :

123

则程序中下划线处遗漏的语句是

A ) cout<<<Fun::i<

B ) cout<<Fun::i<<fun1.value()<

C ) cout<<<<Fun::i;

D ) cout<


正确答案:A

第4题:

若有以下程序:includeusingnamespacestd;classA{private:int x; public:int x;void s

若有以下程序: #include<iostream> usingnamespacestd; classA { private: int x; public: int x; void setx(int i) { x=i; } int getx() { return x; } }; class B:public A { private: int m; public: int p; void setvalue (int a,int b,int C) { setx(A) ; z=b; m=c; } void display() { cout<<getx()<<","<<z<<","<<m<<end1; } }; int main() { B obj; obj.setvalue(2,3,4); obj.display(); return 0; } 程序运行以后的输出结果是

A.产生语法错误

B.2,3,4

C.2,2,2

D.4,3,2


正确答案:B
解析:本题考核继承与派生。当类的继承方式为公有继承时,基类的公有成员和保护成员分别作为派生类的公有成员和保护成员,派生类的其他成员可以直接访问它们。其他外部使用者只能通过派生类的对象访问继承宋的公有成员。在本题中,数据成员z和函数setx都是基类A的公有成员,它们经过公有继承以后,在派生类B中还是公有成员,而派生类B中的函数setvalue和display都是公有成员,可以通过对象对它们进行访问。所以程序中对各成员的访问是正确的。本程序的功能是输出已设置的各成员的值。

第5题:

public class ClassA{public int getValue(){int value=0;boolean setting=true;String title=&q

public class ClassA{public int getValue(){int value=0;boolean setting=true;String title="Hello";if(value||(setting && title=="Hello")){return 1;}if(value==1&title.equals("Hello")){return 2;}}}And:ClassA a=new ClassA();a.getValue();What is the result?()

A.1

B.2

C.Compilation fails.

D.The code runs with no output.

E.An exception is thrown at runtime.


参考答案:C

第6题:

通过实现Rmmable接口创建线程,请在下面横线处填写代码完成此程序。

public class ThreadTest

{

public static void main(String args [])

{

Thread testObj1 = new Thread (new Hello ());

Thread testObj2 = new Thread (new Hello ());

testObj 2.start ( );

}

}

class Hello implements Runnable

{

int j;

public void run()

{

System.out.println("Hello" + j ++);

}

}


正确答案:testObj 1.start();
testObj 1.start();

第7题:

有如下程序 include using namespace std;int i=1;class Fun {public:static int i;i

有如下程序 #include <iostream>using namespace std;int i=1;class Fun {public:static int i;int value(){ return i-1;}int value()const{ return i+1;}};int Fun:: i=2;int main() {int i=3;Fun fun1;const Fun fun2;______________________return ();}若程序的输出结果是:123

A.cout<<fun1.value()<<Fun:: i<<fun2.value();

B.cout<<Fun::i<<fun1.value()<<fun2.value();

C.cout<<fun1. value()<<fun2.value()<<Fun:: i;

D.cout<<fun2.value()<<Fun:: i<<fun 1.value();


正确答案:A

第8题:

有如下程序 include using namespace std; int i=1; class Fun {

有如下程序 #include <iostream> using namespace std; int i=1; class Fun { public: static int i; int value( ){ return i-l;} int value( )const { return i+1;} }; int Fun::i=2; int main( ){ int i=3; Fun funl; const Fun fun2; ___________ return 0; } 若程序的输出结果是: 123 则程序中下划线处遗漏的语句是

A.cout<<funl.value( )<<Fun::i<<fun2.value( );

B.cout<<Fun::i<<funl.value()<<fun2.value( );

C.cout<<funl.value( )<<fun2.value( )<<Fun::i;

D.cout<<fun2.value( )<<Fun::i<<fun1.value( );


正确答案:A
解析:本题主要考查的是常成员函数。const关键字可以用于参与对重载函数的区分。重载的原则是:常对象调用常成员函数,一般对象调用一般成员函数。当类中只有一个常成员函数时,一般对象也可以调用该常成员函数。由此可见,通过fun1调用value()函数时,是调用前一个,而通过fun2调用value()函数,是调用后一个。另外,value()函数中访问的i是类中定义的数据成员i,全局变量i在类中被“屏蔽”。观察4个选项不难发现,要输出的表达式来来去去只有3个:fun1.value()、fun2.value()和Fun::i。fun1.value()调用的是前一个value()函数,输出i-1,这里的i是Fun类的成员i,即等于2-1=1。fun2.value()调用的后一个value()函数,输出i+1=2+1=3。 Fun::i是通过类名直接访问类的静态数据成员i的值,即2。所以正确的输出顺序应该是:fun1.value(),Fun::i,fun2.value()。故应该选择A。

第9题:

publicclassClassA{publicintgetValue(){intvalue=0;booleansetting=true;Stringtitle=”Hello”;(value||(setting&&title==Hello”)){return1;}(value==1&title.equals(”Hello”)){return2;}}}And:ClassAa=newClassA();a.getValue();Whatistheresult?()

A.1

B.2

C.Compilationfails.

D.Thecoderunswithnooutput.

E.Anexceptionisthrownatruntime.


参考答案:C

第10题:

有如下程序:includeusing namespace std;classA{public:A(){cout<<"A";}};class B{pu

有如下程序: #include<iostream> using namespace std; classA { public: A(){cout<<"A";} }; class B{public:B(){cout<<"B";}}; class C:public A { B b; public: C(){cout<<"C";} }; int main(){C obj;return 0;} 执行后的输出结果是( )

A.ABC

B.BAC

C.ACB

D.CBA


正确答案:A

更多相关问题