public class Test {  private static int[] x;  public static 

题目
单选题
public class Test {  private static int[] x;  public static void main(String[] args) {  System.out.println(x[0]);  }  }   What is the result?()
A

 0

B

 null

C

 Compilation fails.

D

 A NullPointerException is thrown at runtime.

E

 An ArrayIndexOutOfBoundsException is thrown at runtime.

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

第1题:

有如下程序: #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。

第2题:

已知如下类说明: public class Test { private float f = 1.0f; int m = 12; static int n=1; public static void main(String arg[]) { Test t = new Test(); // 程序代码… } } 如下哪个使用是正确的?()

A.t.f

B.this.n

C.Test.m

D.Test.n


正确答案:AD

第3题:

有如下类定义:

class Test

{

public:

Test(){ a = 0; c = 0;} // ①

int f(int a)const{this->a = a;} // ②

static int g(){return a;} // ③

void h(int

B . {Test::b = b;}; // ④

private:

int a;

static int b;

const int c;

};

int Test::b = 0;

在标注号码的行中,能被正确编译的是

A . ①

B . ②

C . ③

D . ④


正确答案:C

第4题:

已知有下列类的说明,则下列哪个语句是正确的?public class Test { private float f=1.0f; int m=12; static int n=1; public static void main(String arg[]) { Test t= new Test(); }}

A.t.f;

B.this. n

C.Test.m;

D.Test.f;


正确答案:A
解析:此题主要考查对象的正确使用,其格式为对象名.调用的方法名或变量名。在static方法中,不能使用 this。变量m和f都不是静态成员,所以不能用类名.成员方式访问。

第5题:

给出下列代码,如何使成员变量m被方法fun( )直接访问? class Test{ private int m; public static void fun( ){ … } }

A.将private int m改为protected int m

B.将private int m改为public int m

C.将private int m改为static i

D.将private int m改为int m


正确答案:C
解析:在静态方法中不能直接访问非静态的成员,如果要在fun()中直接访问变量m,应将变量m用static修饰。

第6题:

执行下面程序段,屏幕上将输出( )。 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。

第7题:

给出下列代码,如何使成员变量m被方法fun( )直接访问?Class Test{private int m;public static void fun( ){} }

A.将private int m改为protected int m

B.将private int m改为public int m

C.将private int m改为static int m

D.将private int m改为int m


正确答案:C

第8题:

有如下程序: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的值,并输出。

第9题:

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

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

A.n=0

B.n=1

C.n=2

D.n=3


正确答案:A
解析:本题考查构造函数和析构函数的调用。类的静态成员和成员函数是类属,不依赖于对象实例存在。

第10题:

下列代码的执行结果是public class Test{ public int aMethod(){ static int i=0; i++; System.out.println(i); } public static void main(String args[]){ Test test= new Test(); test. aMethod(); }}

A.编译错误

B.0

C.1

D.运行成功,但不输出


正确答案:A
解析:static不能修饰局部变量。

更多相关问题