1. public class OuterClass {  2. private double d1 = 1.0;  3

题目
多选题
1. public class OuterClass {  2. private double d1 = 1.0;  3. // insert code here  4. }  Which two are valid if inserted at line 3?()
A

static class InnerOne { public double methoda() { return d1; } }

B

static class InnerOne { static double methoda() { return d1; } }

C

private class InnerOne { public double methoda() { return d1; } }

D

protected class InnerOne { static double methoda() { return d1; } }

E

public abstract class InnerOne { public abstract double methoda(); }

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

第1题:

有如下两个类定义: class XX{ private: double X1; protected: double x2; public: doublex3; ); clase YY:protected XX{ private: doubley1; protected: doubley2; public: double y3; }; 在类YY中保护成员变量的个数是( )。

A.1

B.2

C.3

D.4


正确答案:C
解析:派生类从基类保护继承(classYY:protectedXX)时,基类的公有成员在派生类中变为保护成员,基类的保护成员在派生类中仍然是保护成员;本题中YY本身有一个保护成员y2,再加上基类XX的x2和x3,故选C。

第2题:

下列程序的执行结果为【 】。include class Point{public:Point(double i, double j)

下列程序的执行结果为【 】。

include <iostream. h>

class Point

{

public:

Point(double i, double j) { x=i; y=j;}

double Area() const { return 0.0;}

private:

double x, y;

};

class Rectangle: public Point

{

public:

Rectangle(double i, double j, double k, double 1)

double Area() const {return w * h;}

private:

double w, h;

};

Rectangle: :Rectangle(double i, double j, double k. double 1): Point(i,j).

{

w=k, h=1

}

void fun(Point &s)

{

cout<<s. Area()<<end1;

}

void main( )

{

Rectangle rec(3.0, 5.2, 15.0. 25.0);

fun(rec)

}


正确答案:×
0 解析:注意本题不同于基类的指针指向派生类对象。Fun函数的形参是Point基类的引用。在可以用基类对象的地方,均可以用派生类替代,完成基类的行为。反之,在使用派生类对象的地方却不能用基类对象代替,这是因为派生类中的某些行为在基类对象中是不存在的。本题调用的是Point类对象的面积函数,其值永远为0。

第3题:

阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。

【说明】下面的程序先构造Point类,再顺序构造Ball类。由于在类Ball中不能直接存取类Point中的xCoordinate及yCoordinate属性值,Ball中的toString方法调用Point类中的toString方法输出中心点的值。在MovingBall类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值。

public class Point

{

private double xCoordinate;

private double yCoordinate;

public Point 0 }

public Point(ouble x, double y)

{

xCoordinate = x;

yCoordinate = y;

}

public String toString()

{

return "( + Double.toString(Coordinate)+ ","

+ Double.toString(Coordinate) + ");

}

//other methods

}

public class Ball

{

(1); //中心点

private double radius; //半径

private String colour; ///颜色

public Ball() { }

public Ball(double xValue, double yValue, double r)// 具有中心点及半径的构造方法

{

center=(2);//调用类Point 中的构造方法

radius = r;

}

public Ball(double xValue, double yValue, double r, String c)

// 具有中心点、半径及颜色的构造方法

{

(3);//调用3个参数的构造方法

colour = c;

}

public String toString()

{

return "A ball with center" + center, toString() + ", radius"

+ Double.toString(radius) + ", colour" + colour;

}

//other methods

}

public class MovingBall. (4)

{

private double speed;

public MovingBall() { }

public MovingBall(double xValue, double yValue, double r, String e, double s)

{

(5);// 调用父类Ball中具有4个参数的构造方法

speed = s;

}

public String toString( )

{ return super, toString( ) + ", speed "+ Double.toString(speed); }

//other methods

}

public class Tester{

public static void main(String args[]){

MovingBall mb = new MovingBall(10,20,40,"green",25);

System.out.println(mb);

}

}


正确答案:(1)private Point center (2)new Point (xValueyValue) (3)this(xValueyValuer) (4)extends Ball (5)super(xValueyValuerc)
(1)private Point center (2)new Point (xValue,yValue) (3)this(xValue,yValue,r) (4)extends Ball (5)super(xValue,yValue,r,c) 解析:(1)private Point center
Ball类以Point类的center对象作为私有成员。
(2)new Point (xValue,yValue)
用类Point中的构造方法Point构造Point类的center对象。
(3)this(xValue,yValue,r)
利用this指针调用本类的3个参数的重载构造方法。
(4)extends Ball
MovingBall类由Ball类扩展而来。
(5)super(xValue,yValue,r,c)
调用父类Ball中具有4个参数的构造方法:
Ball(double xValue, double yValue, double r,String c)

第4题:

1. public class ReturnIt {  2. return Type methodA(byte x, double y) {  3. return (long)x / y * 2;  4. }  5. }  What is the narrowest valid returnType for methodA in line2?()  

  • A、 int
  • B、 byte
  • C、 long
  • D、 short
  • E、 float
  • F、 double

正确答案:F

第5题:

有以下程序:includeincludeusing namespace std; class point{private:double

有以下程序: #include<iostream> #include<math> using namespace std; class point { private: double x; double y; public: point(double a,double B) { x=a; y=b; } friend double distance (point a,point B) ;

A.1

B.5

C.4

D.6


正确答案:C
解析:本题考核友元函数的应用。分析程序:类point中定义了两个私有成员x和y,以及一个友元函数distance。从而,函数distance可以访问类point中的任何成员。在函数distance中,返回值为sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))。由此可知,函数distance的功能是计算a、b两点之间的距离。在主函数main中,先定义两点:p1(1,2)和p2(5,2)。然后调用函数distance计算两点之间的距离为4,所以程序最后输出为4。

第6题:

阅读以下函数说明和Java代码,将应填入(n)处的字句写在对应栏内。

【说明】

下面的程序先构造Point类,再顺序构造Ball类。由于在类Ball中不能直接存取类Point中的xCoordinate及yCoordinate属性值,Ball中的toString方法调用Point类中的toStrinS方法输出中心点的值。在MovingBsll类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值。

【Java代码】

//Point.java文件

public class Point{

private double xCoordinate;

private double yCoordinate;

public Point(){}

public Point(double x,double y){

xCoordinate=x;

yCoordinate=y;

}

public String toStrthg(){

return"("+Double.toString(xCoordinate)+","

+Double.toString(yCoordinate)+")";

}

//other methods

}

//Ball.java文件

public class Ball{

private (1);//中心点

private double radius;//半径

private String color;//颜色

public Ball(){}

public Ball(double xValue, double yValue, double r){

//具有中心点及其半径的构造方法

center=(2);//调用类Point中的构造方法

radius=r;

}

public Ball(double xValue, double yValue, double r, String c){

//具有中心点、半径和颜色的构造方法

(3);//调用3个参数的构造方法

color=c;

}

public String toString(){

return "A ball with center"+center.toString()

+",radius "+Double.toString(radius)+",color"+color;

}

//other methods

}

class MovingBall (4) {

private double speed;

public MovingBall(){}

public MoyingBall(double xValue, double yValue, double r, String c, double s){

(5);//调用父类Ball中具有4个参数的构造方法

speed=s;

}

public String toString(){

return super.toString()+",speed"+Double.toString(speed);

}

//other methods

}

public class test{

public static void main(String args[]){

MovingBall mb=new MovingBall(10,20,40,"green",25);

System.out.println(mb);

}

}


正确答案:(1) Point center (2) new Point(xValueyValue) (3) this(xValueyValuer) (4) extends Ball (5) super(xValueyValuerc)
(1) Point center (2) new Point(xValue,yValue) (3) this(xValue,yValue,r) (4) extends Ball (5) super(xValue,yValue,r,c) 解析:在类Ball的有参数构造函数中,对成员变量center通过调用Point类的构造方法初始化,而center在类Ball中尚未声明。结合注释可得空(1)是将center变量声明为Point对象引用,故空(1)应填Point。空(2)是调用Point类的构造函数,根据题意,此处应将xValue和yValue作为参数调用类Point的有参数构造函数,故空(2)应填new Point(xValue,yValue)。
根据注释,空(3)是调用类Ball的有3个参数的构造方法,而其所在方法本身就是类Ball的一个构造方法,因此可用this来调用自身的构造方法,故空(3)应填this(xValue,yValue,r)。
根据题述“在MovingBall类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值”,可知类MovingBall是类Ball的子类,因此空(4)应填extends Ball。
根据注释,空(5)是调用父类Ball中具有4个参数的构造方法,通过super关键字实现,故空(5)应填super(xValue,yValue,r,c)。

第7题:

有以下程序:includeusing namespace std;class sample{private:int x;public:sample(

有以下程序: #include<iostream> using namespace std; class sample { private: int x; public: sample(int A) { x=a; friend double square(sample s); }; double square(sample s) { return S.X*S.K; } int main() { sa

A.20

B.30

C.900

D.400


正确答案:C
解析: 本题考查友元函数的应用。程序中函数square是类sample的一个友元函数,它可以直接访问类sam- pie的所有成员。它的功能是返回类sample的私有数据成员x的平方。所以程序的执行结果是900。

第8题:

( 23 )有如下两个类定义

class XX{

private:

double x1;

protected:

double x2;

public:

double x3;

};

class YY:protected XX{

private:

double y1;

protected:

double y2;

public:

double y3;

};

在类 YY 中保护成员变量的个数是

A ) 1

B ) 2

C ) 3

D ) 4


正确答案:C

第9题:

有如下两个类定义: class XX{ private: double xl; protected: double x2; public: double x3; }; class YY:protected XX{ private: double yl; protected: double y2; public: double y3; 在类YY中保护成员变量的个数是( )。

A.1

B.2

C.3

D.4


正确答案:C
本题考查保护继承中派生类对基类的访问属性,在受保护继承中,基类的公用成员和保护成员在派生类中成了保护成员,所以基类的成员x2、x3变成了保护成员,派生类中的y2也是保护成员,所以共有3个保护成员。本题答案为C、

第10题:

1. class BaseClass {  2. private float x = 1.of;  3. protected float getVar() { return x; }  4. }  5. class SubClass extends BaseClass {  6. private float x = 2.Of;  7. // insert code here 8. }   Which two are valid examples of method overriding when inserted at line 7?() 

  • A、 float getVar() { return x; }
  • B、 public float getVar() { return x; }
  • C、 public double getVar() { return x; }
  • D、 protected float getVar() { return x; }
  • E、 public float getVar(float f) { return f; }

正确答案:B,D

更多相关问题