1
2
3
4
第1题:
( 12 )有如下程序:
#include <iostream>
using namespace std
class Animal{
public:
virtual char* getType () const { return "Animal" ; }
virtual char* getVoice () const { return "Voice" ; }
};
Class Dog : public Animal {
public:
char* getType ( ) const {return "Dog" ; }
char* getVoice ( ) const {return "Woof"}
};
void type ( Animal& a ) {cout<<a.getType ( ) ; }
void speak ( Animal a ) {cout<<a.getVoice ( ) ; }
int main ( ) {
Dog d; type ( d ) ; cout<<" speak" ; speak ( d ) ; cout<<endi;
return 0;
}
运行时的输出结果是【 12 】 。
第2题:
使用VC6打开考生文件夹下的工程test19_1,此工程包含一个源程序文件test19_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
1:
weight:5
age:0
2:
weight:7
age:9
源程序文件test19_1.cpp 清单如下:
include <iostream.h>
class animal
{
public:
/**************** found *******************/
friend void setvalue(animal&,int);
/**************** found *******************/
void print()
protected:
int itsweight;
int itsage;
};
void animal::print()
{
cout<<"weight:"<<itsweight<<end1;
cout<<"age:"<<itsage<<end1;
}
void setvalue(animal &ta,int tw)
{
ta.itsweight=tw;
ta.ihsage=0;
}
void setvalue(animal &ta,int tw, int tn)
{
ta.itsweight=tw;
ta.itsage=tn;
}
void main()
{
/**************** found *******************/
animal peppy
setvalue(peppy,5);
cout<<"1:"<<end1;
peppy.print();
setvalue(peppy,7,9);
cout<<"2:"<<end1;
peppy.print();
}
第3题:
有如下程序:
include<iostream>
using namespaee std;c lass Animal{
public:
virtual char*getType( )const{return"Animal";}
virtual char*getVoice( )const{return"Voice";}
};
class Dog:public Animal{
public:
char*getType( )const{return"Dog";}
char*getVoice( )eonst{return"Woof";}
};
void type(Animal&A) {cout<<a.getType( );}
void speak(Animal A) {eout<<a.getVoice( );}
int main( ){
Dog d;type(D) ;cout<<"speak";speak(D) ;cout< return 0;
}
程序的输出结果是______。
第4题:
public interface A { String DEFAULT_GREETING = “Hello World”; public void method1(); } A programmer wants to create an interface called B that has A as its parent. Which interface declaration is correct?()
第5题:
interface Playable {
void play();
}
interface Bounceable {
void play();
}
interface Rollable extends Playable, Bounceable {
Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
private String name;
public String getName() {
return name;
}
public Ball(String name) {
this.name = name;
}
public void play() {
ball = new Ball("Football");
System.out.println(ball.getName());
}
}
这个错误不容易发现。
错。"interface Rollable extends Playable, Bounceable"没有问题。interface 可继承多个
interfaces,所以这里没错。问题出在interface Rollable 里的"Ball ball = new Ball("PingPang");"。
任何在interface 里声明的interface variable (接口变量,也可称成员变量),默认为public static
final。也就是说"Ball ball = new Ball("PingPang");"实际上是"public static final Ball ball = new
Ball("PingPang");"。在Ball 类的Play()方法中,"ball = new Ball("Football");"改变了ball 的
reference,而这里的ball 来自Rollable interface,Rollable interface 里的ball 是public static final
的,final 的object 是不能被改变reference 的。因此编译器将在"ball = new Ball("Football");"
这里显示有错。
第6题:
A.
B.
C.
D.
E.
F.
第7题:
有如下程序:
nclude<iostream>
using namespace std;
class Animal{
public:
virtual char*getType()const{return“Animal”;}
virtual char*getVoice()const{return“Voice”;}
};
class Dog:public Animal{
public:
char*getType()const{rgturn“Dog”;}
char*getVoice()const{retum“Woof”;}
};
void type(Animal&A){cout<<a.getType();}
void speak(AnimalA){cout<<a.getVoice();}
int main(){
Dog d.type(D);tout<<“speak”;speak(D);cout<<endl;
return 0;
}
运行时的输出结果是【 】
第8题:
下列程序片段中,能通过编译的是
A.public abstract class Animal{ public void speak();}
B.public abstract class Animal{ public void speak(){}}
C.public class Animal{ public abstract void speak();}
D.public abstract class Animal{ public abstract void speak(){}}
第9题:
下列程序片段中,能通过编译的是( )。
A.public abstract class Animal{ public void speak;}
B.public abstract class Animal{ public void speak{);}
C.public class Animal{ pubilc abstract void speak;}
D.public abstract class Animal{ pubile abstract void speak{};}
第10题:
现有: 1. interface Animal f 2. void eat(); 3. } 4. 5. // insert code here 6. 7. public class HouseCat implements Feline { 8. public void eat() { } 9. } 和以下三个接口声明: interface Feline extends Animal ( ) interface Feline extends Animal {void eat(); } interface Feline extends Animal {void eat() { } } 分别插入到第5行,有多少行可以编译?