public void test(int x) {  int odd = x%2;  if (odd) {   Syst

题目
单选题
public void test(int x) {  int odd = x%2;  if (odd) {   System.out.println(“odd);  } else {  System.out.println(“even”);  }  }  Which statement is true?()
A

Compilation fails.

B

 “odd” will always be output.

C

 “even” will always be output.

D

 “odd” will be output for odd values of x, and “even” for even values.

E

 “even” will be output for add values of x, and “odd” for even values.

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

第1题:

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

第2题:

若有以下程序: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都是公有成员,可以通过对象对它们进行访问。所以程序中对各成员的访问是正确的。本程序的功能是输出已设置的各成员的值。

第3题:

运行下面的程序时,会产生( )。 public class Test{ public static void main(String args[ ] ) { int x =0; int y = 2/x; int z[ ] = {1,2,4,6}; int p=z[4]; } }

A.ArrayIndexOutOfBoundsExcePtion异常

B.NumberFormatException异常

C.ArithmeticException异常

D.ArithmeticException异常和ArrayIndexOutOfBoundsExcePtion异常


正确答案:C
解析:只能出现ArithmeticException异常,由于在程序第4行出现了这个异常后,程序就跳出,不能继续执行下去,因此不会出现ArrayIndexOutOfBoundsException异常。

第4题:

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

第5题:

以下程序的执行结果是______。 include class A{ int a: public: void seta(int x) {

以下程序的执行结果是______。

include<iostream.h>

class A

{

int a:

public:

void seta(int x) {a=x;}

void showa() {cout<<a<<end1;}

};

class B

{

int b;

public:

void setb(int x) {b=x;}

void showb(){cout<<b<<end1;}

};


正确答案:1 3
1 3

第6题:

下列程序的运行结果是【 】。 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 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

第7题:

完成下列类的构造函数,初始化语句为______。 include class Test { private: int x,y

完成下列类的构造函数,初始化语句为______。

include<iostream.h>

class Test

{

private:

int x,y;

public:

void Test(int initx,int inity){

______

}

void printx( ){cout<<x<<"-"<<y<<"="<<x-y;}

};

void main( )

{

Test x(300,200);

x.printx( );

}


正确答案:x=initx; y=inity;
x=initx; y=inity; 解析:本题考查的是类的私有数据如何从接口成员函数那里得到数据的。本题虽然简单,却体现了类的数据封装思想,并指明了如何完成这种封装。

第8题:

执行下面程序段,屏幕上将输出( )。 public class Test { private int x=10,y=20; public Test (int x,int y) { System.out.println (x+this.x); System.out.println (y+y); } public static void main (String[] args) { Testt= new Test(30,50); } }

A.无输出

B.20 40

C.40 100

D.40 70


正确答案:C
解析:通过new生成一个类的实例时,自动调用该类的构造方法,本题中的构造方法中有两个输出语句,因此A不正确。其余三个答案主要是考查局部变量和成员变量的区别。在方法中如要用成员变量,为区别于局部变量,必须用this关键字。第一个输出语句中的x为参数,是局部变量,其值为30,this.x引用的是成员变量,其值为10,因此第一个语句输出的值为40;第二个输出语句中的y都是局部变量,每一个y的值是接收到的参数值50,因此输出为100。

第9题:

执行下列代码段之后,变量z的值为______。 Public class Test8 { public static void main(String[] args) { int x=2; int y=3; int z=4; z-....= y-x--; System.out.println(z); }

A.1

B.2

C.3

D.4


正确答案:D
解析:表达式中的运算次序应该是先对y做减量运算,得到y=2,然后再取x的值x=2,做减法运算得到0,最后用z减去0,得到答案为4。

第10题:

有如下类定义: class Test { private int x; public int y; public void setX (int m) {x=m;} public int getX( ) {return x;} }现用Test t=new Text();生成一个对象t,则如下语句中,错误的是( )。

A.t.x=10;

B.t.y=10;

C.t. setX(10);

D.int m=t.getX( );


正确答案:A
解析:本题考查public和private修饰符的访问权限。public指明变量和方法为公有,可以通过其对象直接访问,因此,选项B、C、D都是正确的访问方法。private指明变量为私有访问,只能被该类自己访问或调用,因此,A选项中不能通过其对象来访问私有变量。

更多相关问题