单选题编译如下的Java程序片段:  Class test{     Int count=9;     Public void a(){   Int count=10;   System.out,println(“count 1=” + count); }  Public void count(){   System.out.println(“count 2 =”+ count); }  Public static void main(String args[] ){   Test t=new Test(

题目
单选题
编译如下的Java程序片段:  Class test{     Int count=9;     Public void a(){   Int count=10;   System.out,println(“count 1=” + count); }  Public void count(){   System.out.println(“count 2 =”+ count); }  Public static void main(String args[] ){   Test t=new Test();   t.a();   t.count(); } }  结果是()
A

不能通过编译

B

输出:count 1 =10  count 2=9

C

输出:count 1=9 count 2=9


相似考题
更多“单选题编译如下的Java程序片段:  Class test{     Int count=9;     Public void a(){   Int count=10;   System.out,println(“count 1=” + count); }  Public void count(){   System.out.println(“count 2 =”+ count); }  Public static void main(String args[] ){   Test t=new Test(”相关问题
  • 第1题:

    如下程序的输出结果是( )。 public class Test { void printValue(int m) { do { System.out.println("The value is"+m); } while( --m>10) } public static void main(String args[]) { int i=10; Test t=new Test(); t.printValue(i); } }

    A.The value is 8

    B.The value is 9

    C.The value is 10

    D.The value is 11


    正确答案:C
    解析:此题考查的是do-while循环和“--”操作符的知识。do-while最少执行一次,在执行完do中的内容后,判断while中的条件是否为true。如果为true,就再执行do中的内容,然后再进行判断。以此类推,直到while的判断为false时退出循环,执行循环后面的内容。而“--”操作符的规则是,变量右边的“--”将先进行运算,然后才使变量的值减一。而在变量左边的“--”,则先将变量的值减一再运算。本程序中I的值为10,当程序运行到do-while循环时,程序先执行一次循环后然后再做判断,因此选C。

  • 第2题:

    ( 35 )有如下程序:

    #include <iostream>

    using namespace std;

    Class B{

    public:

    B(int xx):x(xx) {++cout; x+=10;}

    virtual void show() const

    {cout<<count<< ' _ ' <<x<<endl;}

    protected:

    static int count;

    private:

    int x;

    };

    class D:public B{

    public:

    D(int xx,int yy):B(xx),y(yy) {++count; y+=100;}

    virtual void show() const

    {cout<<count<< ' _ ' <<y<<endl;}

    private:

    int y;

    };

    int B::count=0;

    int main(){

    B *ptr=new D(10,20);

    ptr->show();

    delete ptr;

    return 0;

    }

    运行时的输出结果是

    A ) 1_120

    B ) 2_120

    C ) 1_20

    D ) 2_20


    正确答案:B

  • 第3题:

    有如下程序: #include using name space std; classB{ public: B(int xx):x(xx){++count;x+=10;} virtual void show()const {cout<<count<<'_'<<x<<endl;} protected: static int count; private: intx; }; class D:publicB{ public: D(int xx,int yy):B(xx),y(yy){++count;y+=100;} virtual void show()const {cout<<count<<'_'<<y<endl}; pnvate: inty; }; int B::count=0; intmain(){ B*ptr=new D(10,20); ptr->show(); delete ptr; return 0; } 运行时的输出结果是( )。

    A.1_120

    B.2_120

    C.1_20

    D.2_20


    正确答案:B
    解析:本题考查了类的继承。继承有三种方式,public公有、pnvate私有和protected保护,本题都涉及到了。本题中类D公有继承类B。在类B中又定义了虚函数,并且有保护类静态类型count及私有变量x。主函数中调用类D,类D又继承了类B,经过系统及调用,本题最终结果为2120。

  • 第4题:

    下列程序的执行结果是 ( ) public class Test { public int aMethod() { satic 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

  • 第5题:

    下列代码的执行结果是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不能修饰局部变量。

  • 第6题:

    有如下程序:includeusing namespace std;elass MyClass{public:MyClass( ){++count;

    有如下程序: #include<lostreanl> using namespace std; elass MyClass{ public: MyClass( ){++count;} ~MyClass( ){--count;} static int getCount( ){return count;} private: static int count; }; int MyClass::count=0; int main( ){ MyClass obj; cout<<obj.getCount( ); MyClass*ptr=new MyClass; cout<<MyClass::getCount( ); delete ptr; cout<<MyCiass::getCount( ); return 0; } 程序的输出结果是

    A.121

    B.232

    C.221

    D.122


    正确答案:A

  • 第7题:

    有如下程序:includeusing namespace std;class MyClass{public:MyClass(){++count;}~

    有如下程序: #include<iostream> using namespace std; class MyClass{ public: MyClass(){++count;} ~MyClass(){--count;} static int getCount(){return count;} private: static int count; }; int MyClass::count=0; int main(){ MyCl

    A.121

    B.232

    C.221

    D.122


    正确答案:A
    解析: 本题考查的知识点是静态成员。类中的静态成员是解决同—:个类的不同对象之间的数据和函数共享问题的。静态成员被所有属于这个类的对象共享。这种共享与全局变量或全局函数相比,既没有破坏数据隐藏的厚则,又保证了安全性。题目中,首先定义了一个obj对象,其构造函数使MyClass的静态数据成员count增1,所以第一条输出语句输出的结果是1。然后通过指针动态创建了一个MyClass类的对象,构造函数再次被调用,count变为2,所以第2次输出结果是2。接下来是用dilete删除了刚才动态创建的MyClass对象。析

  • 第8题:

    有如下程序: #inClude<iostream> using namespaCe耐: Class MyClass{ publiC: MyClass{++Count;} ~MyClass{--Count;} statiC int getCount{retum Count;} private: statiC int Count; }; ‘ int MyClass::Count=0; int main { MyClass obj; Cout<<obj.getCount; MyClass+ptr=new MyClass: Cout<<MyClass::9etCount; ’delete ptr; Cout<<MyClass::9etCount; return 0; } 执行这个程序的输出结果是( )。

    A.121

    B.232

    C.221

    D.122


    正确答案:A
    本题考查静态数据成员。静态数据成员在内存中只占一份空间,静态数据成员属于类,即使不定义对象,也会给静态数据成员分配空间,可以被引用。本题中先是定义了一个对象obj,执行构造函数使得静态数据成员count变为了l,然后又定义了指针对象,执行构造函数后,count变为了2,释放指针后,执行析构函数,使得count变为了1。所以本题答案为A。

  • 第9题:

    下列程序的输出结果是______。 include using namespace std; class Test( public: Te

    下列程序的输出结果是______。 #include<iostream> using namespace std; class Test( public: Test() {cnt++;} ~Test() {cnt--;} static int Count(){return cnt;} private: static int cnt; }; int Test::cnt=0; int main() { cout<<Test::Count()<<""; Test t1,t2; Test*pT3=new Test; Test*pT4=new Test; cout<<Test::Count()<<""; delete pT4; delete pT3; cout<<Test::Count()<<end1; return 0; }

    A.024

    B.042

    C.420

    D.240


    正确答案:B
    解析:此题考查的是类的构造函数与析构函数的调用。语句 coutTcst::Count()"";;输出“0”,因为static型变量cnt的默认值是0;“T Test t1,t2;Test*pT3=new Test;Test*pT4=new Test;”调用4次类的构造函数,使得cnt的值增加到4,并输出4;然后delete pT4;delete pT3;调用两次析构函数,cnt的值变为2,并输出2。

  • 第10题:

    编译如下的Java程序片段:  Class test{     Int count=9;     Public void a(){   Int count=10;   System.out,println(“count 1=” + count); }  Public void count(){   System.out.println(“count 2 =”+ count); }  Public static void main(String args[] ){   Test t=new Test();   t.a();   t.count(); } }  结果是()

    • A、不能通过编译
    • B、输出:count 1 =10  count 2=9
    • C、输出:count 1=9 count 2=9

    正确答案:B

  • 第11题:

    public class Test {  public int aMethod() {  static int i = 0;  i++;  return i;  }  public static void main (String args[]) {  Test test = new Test();  test.aMethod();  int j = test.aMethod();  System.out.println(j);  }  }  What is the result?()  

    • A、 0
    • B、 1
    • C、 2
    • D、 Compilation fails.

    正确答案:D

  • 第12题:

    单选题
    编译如下的Java程序片段:  Class test{     Int count=9;     Public void a(){   Int count=10;   System.out,println(“count 1=” + count); }  Public void count(){   System.out.println(“count 2 =”+ count); }  Public static void main(String args[] ){   Test t=new Test();   t.a();   t.count(); } }  结果是()
    A

    不能通过编译

    B

    输出:count 1 =10  count 2=9

    C

    输出:count 1=9 count 2=9


    正确答案: C
    解析: 暂无解析

  • 第13题:

    下面程序的正确输出是( )。 public class Hello { public static void main(String args[]) { int count, xPos=25; for ( count=1; count<=10; count++ ) { if ( count==5 ) break; System.out.println(count ); xPos += 10; } } }

    A.1 2 3 4

    B.1 3 4

    C.编译错误

    D.以上都不是


    正确答案:A
    解析:该程序经过编译、运行后,在屏幕上显示;1 2 3 4。public class Hello中Hello为类名,public static void main(String args[])有关参数的解释如下。public——表示该方法可以被任意代码所调用,包括Java解释器。 static——它告诉编译器,main()方法是一个类方法,可以通过类名直接调用,调用时不需要该类的实例对象。void——表示main()方法没有返回值。这是非常重要的。因为Java类型检查非常严格,包括检查这些方法返回值的类型是否与声明的一致。String args[]——声明一个字符串类型的数组。它是命令行传递给main()方法的参数,参数在命令行中出现在类名称后面。包含main()方法的类(Hello为包含main()方法的类名)名称必须与其文件名相同,也就是说,其文件名必须为Hello.java。经过编译,在当前文件夹下将出现一个Hello.class文件,这就是编译后的字节码文件。在程序段最后有两个大括号,分别说明结束main()方法和Hello类。在本程序中由于有count==5这个条件判断,因此当count==5时,程序结束,输出为1 2 3 4。

  • 第14题:

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

    有如下程序: #include <iostream> using namespace std; class MyClass { public: MyClass() { ++count; } ~MyClass() { --count; } static int getCount() { return count; } private: static int count; }; int MyClass::count=0; int main() { MyClass obj; cout<<obj.getCount(); MyClass *ptr=new MyClass; cout<<MyClass::getCount(); delete ptr; cout<<MyClass::getCount(); return 0; }程序的输出结果是

    A.121

    B.232

    C.221

    D.122


    正确答案:A
    解析:本题考查的知识点是:静态成员。对于类中的非静态数据成员,每一个类对象都拥有一个拷贝(副本),即每个对象的同名数据成员可以分别存储不同的数值,这是保证每个对象拥有区别于其他对象的特征的需要。而类中的静态成员则是解决同一个类的不同对象之间的数据和函数共享问题的。静态成员的特性是不管这个类创建了多少个对象,它的静态成员都只有一个拷贝(副本),这个副本被所有属于这个类的对象共享。这种共享与全局变量或全局函数相比,既没有破坏数据隐藏的原则,又保证了安全性。题目中,首先定义了一个obj对象,其构造函数使MyClass的静态数据成员count增1,所以第一条输出语句输出的结果是1。然后通过指针动态创建了一个MyClass类的对象,构造函数再次被调用,count变为2,所以第2次输出结果是2。接下来是用delete删除了刚才动态创建的MyClass对象,析构函数被调用,count减1变为1,第3次输出1。故本题的输出为121,选项A正确。

  • 第15题:

    下面程序的运行结果是【】。 include using namespace std; class count{ static int n;

    下面程序的运行结果是【 】。

    include <iostream>

    using namespace std;

    class count

    {

    static int n;

    public:

    count()

    {

    n++;

    }

    static int test()

    {

    for(int i=0;i<4;i++)

    n++;

    return n;

    }

    };

    int count::n = O;

    int main()

    {

    cout<<count:: test()<<" ";

    count c1, c2;

    cout<<count:: test()<<endl;

    return 0;

    }


    正确答案:410
    410 解析:本题主要考查C++类中静态数据成员的使用。题目程序首先定义了类count,其内部含有private 类型数据成员static int n;同时含有public 类型构造函数 count()和静态成员函数static int test(),这两个函数的功能分别是为对象申请系统资源并将静态数据成员n加1和将静态数据成员n加4。主函数前,程序将静态数据成员n初始化为0,该数据成员为所有类count 的对象所共有的数据成员;主函数中程序首先执行静态成员函数test() (由于test 声明为 static,因此其调用时无需通过具体对象),其执行过程中,静态数据成员n应该加4变成n:4,因此此处输出为4;此后程序创建对象c1和c2,由于在每次创建过程中都要调用构造函数count(),而每次调用count()函数后,静态数据成员n值都会加1。因此,创建两个对象之后,n值变为n=6:再次执行test()函数后,n的值再次加4,因此变为n=6+4=10。故程序全部执行后,变量n值变为10,而中间程序输出为“410”。

  • 第16题:

    如下两个源程序文件,分别编译后,运行Example.class文件,运行结果为______。

    AB.java文件代码如下;

    package test;

    public class AB

    {

    int a=60;

    public void show()

    {

    System.out.println(”a=”+a);

    }

    Example.java文件代码如下:

    import test.AB;

    class Example

    {

    public static void main(String args[])

    {

    AB bj=new AB();

    obj.show();

    }

    }


    正确答案:a=60
    a=60

  • 第17题:

    有如下程序: include using namespace std; class pumpkin{ public:pumpkin(){++count

    有如下程序:

    include<iostream>

    using namespace std;

    class pumpkin{

    public:

    pumpkin(){++count;}

    ~pumpkin(){--count;}

    static void total_count(){

    cout<<count<<"pumpkin(s)"<<end1;

    }

    private:

    static int count;

    };

    int pumpkin::count=0;

    int main(){

    pumpkin p1[10];

    pumpkin::total_count();

    return 0;

    }

    这个程序的输出结果是______。


    正确答案:10pumpkin(s)
    10pumpkin(s) 解析:此题考查的是静态数据成员。题目中count数据成员要定义成静态的,静态数据成员是类中所有对象共享的成员,而不是某个对象的成员。本题中,pumpkin类定义了一个静态数据成员count,并初始化为0。在主函数中,首先定义了该类对象p1[10],所以构造函数被调用10次,count累加1十次;然后调用函数total count(),通过函数该输出count的值10。total count()函数结束,对象被释放,故析构函数被调用了10次,count变成0。

  • 第18题:

    有以下程序:include using namespace std;class count{ static int n;public: count

    有以下程序: #include <iostream> using namespace std; class count { static int n; public: count ( ) { n++; } static int test() { for (int i = 0; i < 4; i++ ) n++; return n; } }; int count :: n = 0; int main() { cout<<count :: test()<<" "; count c1, c2; cout<<count :: test()<<end1; return 0; } 执行后的输出结果是( )。

    A.4 10

    B.1 2

    C.22

    D.24


    正确答案:A
    解析:程序首先定义了类count,其内部含有private类型数据成员“staticintn;”,同时含有public类型构造函数count()和静态成员函数staticinttest(),这两个函数的功能分别是为对象申请系统资源并将静态数据成员n加1和将静态数据成员n加4。主函数前,程序将静态数据成员n初始化为0,该数据成员为所有类count的对象所共有的数据成员。主函数中,程序首先执行静态成员函数test()(由于test声明为static,因此其调用时无需通过具体对象)。而其执行过程中,静态数据成员n应该加4变成n=4,因此此处输出为4。此后程序创建对象c1和c2,由于在每次创建过程中都要调用构造函数count(),而每次调用count()函数后,静态数据成员n值都会加1,因此,创建两个对象之后,n值变为n=6;再次执行test()函数后,n的值再次加4,此时变为n=6+4=10。故程序全部执行后,变量n值变为10,而中间程序输出为“410”。

  • 第19题:

    有如下程序:includeusing namespace std;class Toy{public:Toy(char*_n){strcpy(name

    有如下程序: #include<iostream> using namespace std; class Toy{ public: Toy(char*_n){strcpy(name,_n);count++;} ~Toy(){count--;} char*GetName( ){return name;} static int getCount( ){return count;} private: char name[10]; static int count; }; int Toy::count=0: int main( ){ Toy tl("Snoopy"),t2("Mickey"),t3("Barbie"); cout<<t1.getCount( )<<endl; return 0; } 程序的输出结果是

    A.1

    B.2

    C.3

    D.运行时出错


    正确答案:C
    解析:静态数据成员是同一个类的不同对象之间的数据共享,无论创建多少个类,均只有一个静态数据成员,通过对静态数据成员的调用,实现了数据共享。本题创建了3个Toy对象,所以调用3次构造函数,count被增加3次,故sount=3。

  • 第20题:

    下列程序的输出结果是( )。 public class Test { void printValue(int m) { do{ System.out.println("The value is"+m) } while(--m>10) } public static void main(String args[]) { int i=10; Test t=new Test: t.printValue(i); } }

    A. The value is 8

    B.The value is 9

    C.The value is 10

    D.The value is 11


    正确答案:C
    C。【解析】此题考查的是do-while循环和“--”操作符的知识。do-while最少执行一次,在执行完d0中的内容后,判断while中的条件是否为true。如果为true,就再执行do中的内容,然后进行判断。以此类推,直到while的判断为false时退出循环,执行循环后面的内容。而“--”操作符的规则是,变量右边的“-”将先进行运算,然后才使变量的值减一。而在变量左边的“--”,则先将变量的值减1再运算。本程序中i的值为10,当程序运行到do-while循环时,程序先执行一次循环,然后判断,因此选C。

  • 第21题:

    以下程序调试结果为:

    public class Test {

    int m=5;

    public void some(int x) {

    m=x;

    }

    public static void main(String args []) {

    new Demo().some(7);

    }

    }

    class Demo extends Test {

    int m=8;

    public void some(int x) {

    super.some(x);

    System.out.println(m);

    }

    }

    A.5

    B.8

    C.7

    D.无任何输出

    E.编译错误


    正确答案:B

  • 第22题:

    编译如下Java程序片断:  class test{  int count = 9;  public void a(){    int count=10;  System.out.println("count 1 =" + count);  }  public void count(){  System.out.println("count 2 =" + count);  }  public static void main(String args[]){    test t=new test();    t.a();   t.count();   } }  结果将()。    

    • A、不能通过编译
    • B、输出:       count 1 = 10        count 2 = 9
    • C、输出:          count 1 = 9          count 2 = 9

    正确答案:B

  • 第23题:

    public class NamedCounter {  private final String name;  private int count;  public NamedCounter(String name) { this.name = name; }  public String getName() { return name; }  public void increment() { coount++; }  public int getCount() { return count; } public void reset() { count = 0; } }  Which three changes should be made to adapt this class to be used safely by multiple threads? ()

    • A、 declare reset() using the synchronized keyword
    • B、 declare getName() using the synchronized keyword
    • C、 declare getCount() using the synchronized keyword
    • D、 declare the constructor using the synchronized keyword
    • E、 declare increment() using the synchronized keyword

    正确答案:A,C,E

  • 第24题:

    单选题
    编译如下Java程序片断:  class test{  int count = 9;  public void a(){    int count=10;  System.out.println("count 1 =" + count);  }  public void count(){  System.out.println("count 2 =" + count);  }  public static void main(String args[]){    test t=new test();    t.a();   t.count();   } }  结果将()。
    A

    不能通过编译

    B

    输出:       count 1 = 10        count 2 = 9

    C

    输出:          count 1 = 9          count 2 = 9


    正确答案: C
    解析: 暂无解析