单选题现有:   1. class Book {   2. private final void read() { System.out.print("book "); }   3. }   4. class Page extends Book {   5. public static void main(String [] args) {   6. // insert code here   7. }   8. private final void read() { System.out.print("

题目
单选题
现有:   1. class Book {   2. private final void read() { System.out.print("book "); }   3. }   4. class Page extends Book {   5. public static void main(String [] args) {   6. // insert code here   7. }   8. private final void read() { System.out.print("page "); }   9. }   和如下三个代码片段( x, y, z ):   x. // just a comment   y. new Page().read();  z. new Book().read();   分别插入到第6行,有几个允许代码通过编译并可以运行且无异常?()
A

 0

B

 1

C

 2

D

 3


相似考题
参考答案和解析
正确答案: D
解析: 暂无解析
更多“现有:   1. class Book {   2. private final void read() { Syste”相关问题
  • 第1题:

    以下语句能顺利通过编译: class class1 { private final void method1() {} }。()

    此题为判断题(对,错)。


    答案:对

  • 第2题:

    阅读下列说明和C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。
    【说明】
    以下C++代码实现一个超市简单销售系统中的部分功能,顾客选择图书等物品(Item)加入购物车(ShoppingCart),到收银台(Cashier)对每个购物车中的物品统计其价格进行结账,设计如图6-1所示类图。



    【C++代码】
    using namespace std;class Book;class Visitor {public: virtual void visit(Book* book)=0; //其它物品visit方法}; class Item {public:virtual void accept(Visitor* visitor)=0; virtual doublegetPrice()=0;};class Book (1){private: double price;public: Book (double price){ //访问本元素 (2); } void accept(Visitor* visitor) { (3); }double getPrice(){ returnprice; }};class Cashier(4){private; double totalForCart;public: //访问Book类型对象的价格并累加 (5){ //假设Book类型的物品价格超过10元打8折 if(book->getPrice()getPrice(); } else totalForCart+=book->getPrice()*0.8; } //其它visit方法和折扣策略类似,此处略 double getTotal() { return totalForCart; }};class ShoppingCart {private: vectoritems;public: double calculatePrice() { Cashier* visitor=new Cashier(); for(int i=0;i getTotal(); return total; } void add(Item*e) { items.push_back(e); }};


    答案:
    解析:
    (1):public Item
    (2)this->price=price
    (3)visitor->visit(this)
    (4)public visitor
    (5)void visit(Book*book)
    (6)item->accept(visitor)
    【解析】
    这里考察的是访问者模式。其定义如下:封装某些作用于某种数据结构中各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作

  • 第3题:

    Which declaration prevents creating a subclass of an outer class?()

    • A、 Static class FooBar{}
    • B、 Private class FooBar{}
    • C、 Abstract public class FooBar{}
    • D、 Final public class FooBar{}
    • E、 Final abstract class FooBar{}

    正确答案:D

  • 第4题:

    下列哪个类的声明是正确的()

    • A、abstract final class HI{}
    • B、abstract private move(){}
    • C、protected private number;
    • D、public abstract class Car{}

    正确答案:D

  • 第5题:

    class BaseClass{  private float x= 1.0f;  protected void setVar (float f) {x = f;}  }  class SubClass extends BaseClass   {  private float x = 2.0f;  //insert code here  }   Which two are valid examples of method overriding?()        

    • A、 Void setVar(float f) {x = f;}
    • B、 Public void setVar(int f) {x = f;}
    • C、 Public void setVar(float f) {x = f;}
    • D、 Public double setVar(float f) {x = f;}
    • E、 Public final void setVar(float f) {x = f;}
    • F、 Protected float setVar() {x=3.0f; return 3.0f; }

    正确答案:C,E

  • 第6题:

    单选题
    Which declaration prevents creating a subclass of an outer class?()
    A

     Static class FooBar{}

    B

     Private class FooBar{}

    C

     Abstract public class FooBar{}

    D

     Final public class FooBar{}

    E

     Final abstract class FooBar{}


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

  • 第7题:

    多选题
    public abstract class Shape {  private int x;  private int y;  public abstract void draw();  public void setAnchor(int x, int y) {  this.x = x;  this.y = y;  }  }  Which two classes use the Shape class correctly?()
    A

    public class Circle implements Shape { private int radius; }

    B

    public abstract class Circle extends Shape { private int radius; }

    C

    public class Circle extends Shape { private int radius; public void draw(); }

    D

    public abstract class Circle implements Shape { private int radius; public void draw(); }

    E

    public class Circle extends Shape { private int radius;public void draw() {/* code here */} }

    F

    public abstract class Circle implements Shape { private int radius;public void draw() { / code here */ } }


    正确答案: B,E
    解析: 暂无解析

  • 第8题:

    单选题
    现有:   1. class Book {    2.   private final void read() { System.out.print("book "); }    3. }   4. class Page extends Book {    5.   public static void main(String [] args) {    6.     // insert code here   7.   }    8.   private final void read() { System.out.print("page "); }    9. }    和如下三个代码片段 ( x, y, z ):   x. // just a comment    y. new Page().read();   z. new Book().read();    分别插入到第6行,有几个允许代码通过编译并可以运行且无异常?()
    A

     0

    B

     1

    C

     2

    D

     3


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

  • 第9题:

    单选题
    Given  1.  public class Foo {  2.  public static void main (String [] args) }  3.  try { return;}  4.  finally { Syste.out.printIn (“Finally”);}  5. }  6. }   What is the result( )?
    A

     The program runs and prints nothing.

    B

     The program runs and prints “Finally”.

    C

     The code comiles. But an exception is thrown at runtime.

    D

     The code will not compile because the catch block is missing.


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

  • 第10题:

    单选题
    现有:  1. interface Animal {  2. void eat();  3. }  4.  5. // insert code here  6.  7. public class HouseCat extends Feline {  8. public void eat() { }  9. }   和五个声明:  abstract class Feline implements Animal { }  abstract class Feline implements Animal { void eat(); }  abstract class Feline implements Animal { public void eat(); }  abstract class Feline implements Animal { public void eat() { } }  abstract class Feline implements Animal { abstract public void eat(); }  分别插入到第5行,有几个可以通过编译?()
    A

    0

    B

    1

    C

    2

    D

    3


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

  • 第11题:

    多选题
    class BaseClass{  private float x= 1.0f;  protected void setVar (float f) {x = f;}  }  class SubClass extends BaseClass   {  private float x = 2.0f;  //insert code here  }   Which two are valid examples of method overriding?()
    A

    Void setVar(float f) {x = f;}

    B

    Public void setVar(int f) {x = f;}

    C

    Public void setVar(float f) {x = f;}

    D

    Public double setVar(float f) {x = f;}

    E

    Public final void setVar(float f) {x = f;}

    F

    Protected float setVar() {x=3.0f; return 3.0f; }


    正确答案: F,E
    解析: 暂无解析

  • 第12题:

    多选题
    现有:  public  class  TestDemo{     private int X-2;      static int y=3;  public  void method(){      final int i=100;      int j  =10;     class Cinner {  public void mymethod(){      //Here     }     }     }     } 在Here处可以访问的变量是哪些?()
    A

    X

    B

    y

    C

    j

    D

    i


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

  • 第13题:

    第六题 阅读下列说明和C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。
    【说明 】
    以下C++代码实现一个超市简单销售系统中的部分功能,顾客选择图书等物品(Item)加入购物车(ShoppingCart),到收银台(Cashier)对每个购物车中的物品统计其价格进行结账,设计如图6-1所示类图。

    using namespace std;
    class Book;
    class Visitor {
    public:
    virtual void visit(Book* book)=0;
    //其它物品的visit方法
    };

    class Item {
    public:virtual void accept(Visitor* visitor)=0;
    virtual double getPrice()=0;
    };

    class Book (1){
    private: double price;
    public:
    Book (double price){ //访问本元素
    (2);
    }
    void accept(Visitor* visitor) {
    (3);
    }
    double getPrice() { return price; }
    };
    class Cashier(4){
    private;
    double totalForCart;
    public:
    //访问Book类型对象的价格并累加
    (5){
    //假设Book类型的物品价格超过10元打8折
    if(book->getPrice()<10.0) {
    totalForCart+=book->getPrice();
    } else
    totalForCart+=book->getPrice()*0.8;
    }
    //其它visit方法和折扣策略类似,此处略
    double getTotal() {
    return totalForCart;
    }
    };

    class ShoppingCart {
    private:
    vectoritems;
    public:
    double calculatePrice() {
    Cashier* visitor=new Cashier();

    for(int i=0;i (6);
    }
    double total=visitor->getTotal();
    return total;
    }

    void add(Item*e) {
    items.push_back(e);
    }
    };


    答案:
    解析:
    (1):public Item
    (2)this->price=price
    (3)visitor->visit(this)
    (4)public visitor
    (5)void visit(Book*book)
    (6)item->accept(visitor)

    【解析】

    这里考察的是访问者模式。其定义如下:封装某些作用于某种数据结构中各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作。

  • 第14题:

    1. public class Target {  2. private int i = 0;  3. public int addOne() {  4. return ++i;  5. }  6. }  And:  1. public class Client {  2. public static void main(String[] args) {  3. System.out.println(new Target().addOne());  4. }  5. }  Which change can you make to Target without affecting Client?() 

    • A、 Line 4 of class Target can be changed to return i++;
    • B、 Line 2 of class Target can be changed to private int i = 1;
    • C、 Line 3 of class Target can be changed to private int addOne() {
    • D、 Line 2 of class Target can be changed to private Integer i = 0;

    正确答案:D

  • 第15题:

    Given classes defined in two different files:  1. package util;  2. public class BitUtils {  3. private static void process(byte[] b) { }  4. }  1. package app;  2. public class SomeApp {  3. public static void main(String[] args) {  4. byte[] bytes = new byte[256];  5. // insert code here  6. }  7. }  What is required at line 5 in class SomeApp to use the process method of BitUtils?() 

    • A、 process(bytes);
    • B、 BitUtils.process(bytes);
    • C、 app.BitUtils.process(bytes);
    • D、 util.BitUtils.process(bytes);
    • E、 import util.BitUtils. *; process(bytes);
    • F、 SomeApp cannot use the process method in BitUtils.

    正确答案:F

  • 第16题:

    public class X {  public X aMethod() { return this;}  }  public class Y extends X {  }  Which two methods can be added to the definition of class Y?()

    • A、 public void aMethod() {}
    • B、 private void aMethod() {}
    • C、 public void aMethod(String s) {}
    • D、 private Y aMethod() { return null; }
    • E、 public X aMethod() { return new Y(); }

    正确答案:C,E

  • 第17题:

    Given  1.  public class Foo {  2.  public static void main (String [] args) }  3.  try { return;}  4.  finally { Syste.out.printIn (“Finally”);}  5. }  6. }   What is the result( )?

    • A、 The program runs and prints nothing.
    • B、 The program runs and prints “Finally”.
    • C、 The code comiles. But an exception is thrown at runtime.
    • D、 The code will not compile because the catch block is missing.

    正确答案:B

  • 第18题:

    单选题
    下列哪个类的声明是正确的()
    A

    abstract final class HI{}

    B

    abstract private move(){}

    C

    protected private number;

    D

    public abstract class Car{}


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

  • 第19题:

    单选题
    Given classes defined in two different files:  1. package util;  2. public class BitUtils {  3. private static void process(byte[] b) { }  4. }  1. package app;  2. public class SomeApp {  3. public static void main(String[] args) {  4. byte[] bytes = new byte[256];  5. // insert code here  6. }  7. }  What is required at line 5 in class SomeApp to use the process method of BitUtils?()
    A

     process(bytes);

    B

     BitUtils.process(bytes);

    C

     app.BitUtils.process(bytes);

    D

     util.BitUtils.process(bytes);

    E

     import util.BitUtils. *; process(bytes);

    F

     SomeApp cannot use the process method in BitUtils.


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

  • 第20题:

    单选题
    下列哪个类声明是正确的()。
    A

    abstract final class H1{…}

    B

    abstract private move(){…}

    C

    protected private number;

    D

    public abstract class Car{…}


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

  • 第21题:

    多选题
    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


    正确答案: B,D
    解析: 暂无解析

  • 第22题:

    单选题
    final class Tree {  private static String tree = "tree ";  String getTree() { return tree; }  }  class Elm extends Tree {  private static String tree = "elm "; public static void main(String [] args) {  new Elm().go(new Tree());  }  void go(Tree t) {  String s = t.getTree()+Elm.tree+tree+(new Elm().getTree());  System.out.println(s);  } }  结果为:()
    A

    elm elm elm elm

    B

    tree elm elm elm

    C

    tree elm tree elm

    D

    编译失败


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

  • 第23题:

    多选题
    class BaseClass{   private float x= 1.0f;   protected void setVar (float f) {x = f;}   }   class SubClass exyends BaseClass {   private float x = 2.0f;   //insert code here  8. }   Which two are valid examples of method overriding?()
    A

    Void setVar(float f) {x = f;}

    B

    Public void setVar(int f) {x = f;}

    C

    Public void setVar(float f) {x = f;}

    D

    Public double setVar(float f) {x = f;}

    E

    Public final void setVar(float f) {x = f;}

    F

    Protected float setVar() {x=3.0f; return 3.0f; }


    正确答案: F,E
    解析: 暂无解析

  • 第24题:

    多选题
    public class TestDemo{   private int x = 2;   static int y = 3;   public void method(){   final int i=100;   int j = 10;   class Cinner{   public void mymethod(){  //Here  }  }  }  }   在Here处可以访问的变量是哪些?()
    A

    x

    B

    y

    C

    i

    D

    j


    正确答案: D,A
    解析: 暂无解析