以下程序运行后输出的结果是______。public class exl7{public static void main(String args []){int a = 0;for(int j = 1; j <= 20; j++)if(j%5 != 0)a = a + j;System.out.println (a);

题目

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

public class exl7

{

public static void main(String args [])

{

int a = 0;

for(int j = 1; j <= 20; j++)

if(j%5 != 0)

a = a + j;

System.out.println (a);


相似考题
更多“以下程序运行后输出的结果是______。public class exl7{public static void main(String args []) ”相关问题
  • 第1题:

    下列程序的运行结果是______。include class Base { public: void f(int x){cout<<“B

    下列程序的运行结果是______。

    include<iostream.h>

    class Base

    {

    public:

    void f(int x){cout<<“Base:”<<x<<endl;}

    );

    class Derived:public Base

    {

    public:

    void f(char*str){cout<<“Derived:”<<str<<endl;}

    };

    void main(void)

    {

    Base*pd=ne


    正确答案:Base:97。
    Base:97。 解析: 本题主要考查两个知识点,一是基类指针可以指向派生类对象,并可以访问派生类的所有成员。二是在函数重载中进行隐式类型转换。如pd->f(‘a’);系统到底调用哪个重载函数呢?实参既不是派生类中的形参,也不是基类中f函数的形参类型。此时系统根据就近原则和从高优先级到低优先级的规则尝试隐式转换。单字符更接近整数,故调用的是基类的f函数。

  • 第2题:

    有以下程序:includeusing namespace std;Class A{public:A(){tout{("A"}};classB{pub

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

    A.CBA

    B.BAC

    C.ACB

    D.ABC


    正确答案:D
    解析: 本题考查的是类的继承和派生。系统首先要通过派生类的构造函数调用基类的构造函数,对基类成员初始化,然后对派生类中的新增成员初始化。

  • 第3题:

    若有以下程序:includeusingnamespaceStd;classsample{ inti;publiC: sample(){} void

    若有以下程序: #include <iostream> using namespace Std; class sample { int i; publiC: sample(){} void setvalue(int m) { i=m; } void fun(int m) { i+=m; } void disp() { cout<<i<<end1; } }; int main() { sample *ps; ps=new sample; ps->setvalue(20); ps->fun(5); ps->disp(); return 0; } 程序运行后,输出的结果是( )。

    A.15

    B.20

    C.25

    D.30


    正确答案:C
    解析:本题考核对象指针的应用。程序首先定义了一个类sample,其中包含一个私有成员i和3个公有成员函数。函数setvalue()的功能是给私有成员i赋值,函数fun()的功能是将私有成员i的值增加5,函数disp()的功能是输出变量i的值。在主函数中,先定义了类sample的一个对象指针ps,并申请了一块内存空间。执行语句ps->setvalue(20);后,类中i的值为20,执行语句ps->fun(5);后,类中i的值为25。所以程序最后输出25。

  • 第4题:

    以下程序运行后的输出结果是______。includeinclude usingnamespacestd;classY

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

    include <iostream>

    include <string>

    using namespace std;

    class Y;

    class X

    {

    int x;

    char *strx;

    public:

    X(int a, char *str)

    {

    x=a;

    strx=new char[strlen(str)+1]

    strcpy (strx,str);

    }

    void show(Y &ob);

    };

    class Y

    {

    prlvate:

    int y;

    char *stry;

    public:

    Y(int b,char *str)

    {

    y=b;

    stry=new char[strlen(str)+1];

    strcpy(stry,str);

    }

    friend void X::show(Y &ob);

    };

    void X::show{Y &ob)

    {

    cout<<strx<<",",

    cout<<ob.stry<<endl;

    }

    int main{

    {

    X a (10, "stringX");

    Y b (20, "stringY");

    a. show (b);

    renurn 0;

    }


    正确答案:stringX stringY
    stringX stringY 解析:本题考核友元函数的应用。该程序中,类X的成员函数show()在类Y中说明为类Y的友元函数,因此,在该友元成员show()中可以访问类Y的私有成员stry.成员函数show()的功能就是输出类X的私有成员strx和 Y对象ob的私有成员stry。主函数main()中定义了 X类的一个对象a和Y类的一个对象b,并且都进行了初始化.然后调用对象a的成员函数show,输出对象a中私有成员strx中的内容和对象b中私有成员stry中的内容,即字符串stringX和stringY。

  • 第5题:

    若有以下程序:includeusing namespace std;class TestClass{public:void who(){cout<

    若有以下程序: #include<iostream> using namespace std; class TestClass { public: void who(){cout<<"TestClass"<<endl;} }; class TestClass1:public TestClass { public: void who(){cout<<"TestClass1"<<endl;} }; int main() { TestClass *p; TestClass1 obj1; P=&obj1; P->who(); return 0; 则该程序运行后的输出结果是( )。

    A.TestClass1

    B.TestClass

    C.0

    D.无输出


    正确答案:B

  • 第6题:

    阅读以下说明和Java程序,填写程序中的空(1)~(6),将解答写入答题纸的对应栏内。
    【说明】
    以下Java代码实现一个简单绘图工具,绘制不同形状以及不同颜色的图形。部分接口、类及其关系如图5-1所示。




    【Java代码】
    interface?DrawCircle?{? //绘制圆形 public(1) ;}class?RedCircle?implements?DrawCircle?{? ?//绘制红色圆形???????public?void?drawCircle(int?radius,intx,?int?y)??{????????????System.out.println("Drawing?Circle[red,radius:"?+?radius?+",x:"?+?x?+?",y:"?+y+?"]");???????}}class?GreenCircle?implements?DrawCircle?{????//绘制绿色圆形??????public?void?drawCircle(int?radius,?int?x,int?y)?{???????????System.out.println("Drawing?Circle[green,radius:"?+radius+",x:?"?+x+?",y:?"?+y+?"]");??????}}abstract?class?Shape?{????//形状? protected? ? (2)???;? ? public?Shape(DrawCircle?drawCircle)?{? ?this.drawCircle=?drawCircle;? ? ? public?abstract?void?draw();}class?Circle?extends?Shape?{? //圆形? ?private?int?x,y,radius;? public?Circle(int?x,int?y,intradius,DrawCircle?drawCircle)?{? ?(3)???;? this.x?=?x;? ? ? this.y?=?y;? ?this.radius?=radius;? }? ? ?public?void?draw()?{? ? drawCircle.? ?(4)? ?;? ? ? }}public?class?DrawCircleMain?{? public?static?void?main(String[]?args)?{? Shape?redCircle=new?Circle(?100,100,10,? (5) );//绘制红色圆形? Shape?greenCircle=new?Circle(200,200,10,(6) );//绘制绿色圆形? ?redCircle.draw(); greenCircle.draw();? ?}}


    答案:
    解析:
    (1)void drawCircle (int radius,int x,int y)
    (2)DrawCircle drawCircle
    (3)super.drawcircle=drawcircle
    (4)drawCircle(radius,x,y)
    (5)new RedCircle()
    (6)new GreenCircle()【解析】
    第一空是填接口里面的方法,在接口的实现里面找,可以发现应该填void drawCircle (int radius,int x,int y)。
    第二空可以根据后面this drawCircle=drawCircle判断,这里应该有一个drawCircle属性,因此应该填)DrawCircle drawCircle。
    第三空这里用super,用super. drawcircle来引用父类的成员。
    第四空调用drawCircle(radius,x,y)方法。
    第五、六空分别创建一个红色圆形对象和一个绿色圆形对象作为Circle里面的实参。