请写出下面的输出:
class B
{
public:
virtual void Print(void)
{
printf(“B::Print\n”);
}
virtual void Reprint(void)
{
printf(“B:Reprint\n”);
}
void Algo(void)
{
Print();
Reprint();
}
};
class D : public B
{
public:
virtual void Print(void)
{
printf(“D::Print\n”);
}
};
void main()
{
B *p = new D();
p->Print();
p->Algo();
}
第1题:
下面程序的运行结果为
#include<iostream.h>
Class A
static int n;
public:
A( ) {n=1;}
A(int num) {n=num;}
void print( ) {cout < < n;}
};
int A: :n=2;
void main( )
{
A a,b(3) ;
a. print( ) ;
b. print( ) ;
cout < < endl;
}
A.11
B.13
C.23
D.33
第2题:
下面程序运行时输出结果为【 】。
include<iostream.h>
include<malloc.h>
class Rect
{
public:
Rect(int1,int w)(length=1;width=w;)
void Print(){cout<<"Area:"<<length *width<<endl;)
void *operator new(size-t size){return malloc(size);}
void operator delete(void *p){free(p)
private:
int length,width;
};
void main()
{
Rect*p;
p=new Rect(5,4);
p->Print();
delete p;
}
第3题:
有如下程序: #include<iostream> using namespace std; class Base{ public: void output( ){cout<<l;} virtual void Print( ){cout<<'B';} }; class Derived:public Base{ public: void output( ){cout<<1;} void Print( ){cout<<'D';} }; int main( ){ Base*prt=new Derived; prt->output( ); prt->Print( ); delete prt; return 0; } 程序的输出结果是
A.1B
B.1D
C.2B
D.2D
第4题:
下面程序段的输出结果是 class Base { int i; Base() { add(1); } void add(int v) { i+=v; } void print() { System.out.println(i); } } class Extension extends Base { Extension() { add(2); } void add(int v) { i+=v*2; } } public class Test { public static void main(String args[]) { bogo(new Extension()); } static void bogo(Baseb){ b.add(8); b.print(); } }
A.9
B.18
C.20
D.22
第5题:
有如下程序: #inClude<iostream> using namespaCe std; Class Base{ publiC: void output{Cout<<1;} virtual void Print{Cout<<B’;} }; Class Derived:publiC Base{ publiC: void output{Cout<<2;} void Print{Cout<<’D ;} }; int main { Base * ptr=new Derived; ptr一>output; ptr一>Print; delete ptr; retum0; } 执行这个程序的输出结果是( )。
A.1B
B.lD
C.2B
D.2D
第6题:
C 程序写运行结果。
class A
{
public:
void f1()
{
printf("A::f1\r\n");
}
virtual void f2()
{
printf("A::f2\r\n");
}
void callfunc()
{
printf("A::callfunc\r\n");
f1();
f2();
}
};
class B :public A
{
public:
void f1()
{
printf("B::f1\r\n");
}
void f2()
{
printf("B::f2\r\n");
}
void callfunc()
{
printf("B::callfunc\r\n");
f1();
f2();
}
};
int main()
{
B *pB=new B;
pB->callfunc();
A *pA=pB;
pA->callfunc();
return 0;
}
第7题:
下面程序的运行结果为 #include < iostream.h > class A { static int n; public: A( ){n=1;} A(int num){ n = num;} void print( ){cout<< n;} }; int A::n=2; void main( ) { A a,b (3); a.print( ); b.print( ); cout<< end1; }
A.11
B.13
C.23
D.33
第8题:
写出程序的输出结果
public abstract class A
{
public A()
{
Console.WriteLine('A');
}
public virtual void Fun()
{
Console.WriteLine("A.Fun()");
}
}
public class B: A
{
public B()
{
Console.WriteLine('B');
}
public new void Fun()
{
Console.WriteLine("B.Fun()");
}
public static void Main()
{
A a = new B();
a.Fun();
}
}
第9题:
下面程序的结果是 ______。 #include<iostream.h> class A{ public: virtual void fun()=0{}; }; class B:public A{ public: void fun () {cout<< "new file" ;} }; class C: public A{ public: void fun (){cout<<"open file"<< " " } }; class D: public A{ public: void fun () {cout<< "save file\n" ;} }; void main() { A a,*p; B b; C c; D d; p=&c; p->fun (); p=&b; p->fun (); p=&d; p->fun(); }
A.new file open file save file
B.new file new file new file
C.编译出错
D.open file new file save file
第10题:
有以下程序: #include 〈iostream〉 using namespace std; class A { public: virtual void setx(int i,int j=0) { x=i; y=j; } virtual void print()=0; protected: int x,y; }; class B : public A { public: void print() { cout〈〈x*x〈〈", "; } }; class C : public A { public: void print() { cout〈〈x*x*x〈〈end1; } }; int main() { A *pa; B b; C c; pa=&b; pa->setx(5); pa->print (); pa=&c; pa->setx(2); pa->print(); return 0; } 程序运行后的输出结果是( )。
A.25,8
B.2,5
C.5,2
D.8,25
第11题:
public class X { public static void main(String [] args) { try { badMethod(); System.out.print(“A”); } catch (Exception ex) { System.out.print(“B”); } finally { System.out.print(“C”); } System.out.print(“D”); } public static void badMethod() {} } What is the result?()
第12题:
public class X { public static void main(String [] args) { try { badMethod(); System.out.print(“A”); } catch (Exception ex) { System.out.print(“B”); } finally { System.out.print(“C”); } System.out.print(“D”); } public static void badMethod() { throw new RuntimeException(); } } What is the result? ()
第13题:
下列程序打包到example包,main方法调用线程类输出0~9这10个数,请填写横线处的内容。
注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。
______
interface MyInterface
{
public abstract void print(int n);
}
class Mythread extends Thread ______ MyInterface
{
public void run()
{
for(int i = 0; i < 10; i++)
this.print(i);
}
public void print(int n)
{
System.out.print(n +" ");
}
}
public class Example1_6
{
public static void main(String argv[])
{
Mythread th = new Mythread();
______
}
}
第14题:
下列程序的运行结果是______。
include<iostream.h>
class A
{
public:
virtual void use( ) {cout<<"in A\n";}
};
class B:public A
{
public:
virtual void use( ) {cout<<"in B\n";}
};
class C:public B
{
public:
virtual void use( ){cout<<"in C\n";}
};
void main( )
{
A*obj;
bj=new C;
obj->use( );
delete obj;
}
第15题:
下列程序的输出结果是【 】。
include <iostream>
using namespace std;
class A {
int a;
public:
A():a(9){}
virtual void print() const { cout<<a;};
};
class B: public A {
char b;
public:
B( ){b='S';}
void print() const {cout<<b;}
};
void show(A &x){ x.print0;}
int main()
{ Ad1,*p;
B d2;
p=&d2;
d1.print();
d2.print0;
p->print();
show(d1);
show(d2);
return 0;}
第16题:
下面程序的运行结果为( )。 class A{ static int n; public: A(){n=1;} A(int num){n=num;} void print(){cout<<n;} } A::n=0; void main(){ A a,b(2); a.print(); b.pint(); }
A.12
B.11
C.22
D.21
第17题:
请阅读下述代码,写出程序执行的结果
#include <iostream>
using namespace std;
class CBase
{
public:
virtual void print()
{
cout<< "base" << endl;
}
void DoPrint()
{
print();
}
};
class CChild1: public CBase
{
public:
virtual void print()
{
cout<< "child1" << endl;
}
};
class CChild2: pu
{
public:
virtual void pri
{
cout<< "chil
}
};
void DoPrint(CB
{
base->DoPrint
}
void main()
{
CBase* base =
CChild1* child
CChild2* child
DoPrint(child1
DoPrint(child2
DoPrint(base);
delete base;
base = child1;
base->print();
delete child1;
delete child2;
}
第18题:
下面程序段的输出结果是( )。 class Base { int i; Base { add(1); } void add(int v) { i+=v: } void print { System.out.println(i); } } class Extension extends Base { Extension { add(2); } void add(int v) { i+=v*2: } } public class Test { public static void main(String args[]) { bogo(new Extension); } static void bogo(Base b) { add(8); print; } }
A.9
B.18
C.20
D.22
第19题:
请阅读以下一段程序,并给出答案。
class A
{
public:
A(){ doSth() }
virtual void doSth(){ printf("I am A");}
}
class B:public A
{
public:
virtual void doSth(){ printf("I am B");}
}
B b;
执行结果是什么?为什么?
第20题:
下面程序段的输出结果是 class Base { int i; Base() { add(1); } void add(int v) { i+=v; } void print() { System.out.println(i); } } class Extension extends Base { Extension() { add(2); } void add(int v) { i+=*2; } } public class Test { public static void main(String args[]) { bogo(new Extension()); } static void bogo(Base b) { b.add(8); b.print(); } }
A.9
B.18
C.20
D.22
第21题:
下面程序的运行结果为( )。 #include<iostream.h> class A static int n; public: A{n=1;} A(int num){n=hum;} void print{cout<<n;} }: int A::n=2; void main { A a,b(3); a.print; b.print; cout<<endl:
A.11
B.13
C.23
D.33
第22题:
class Parent { String one, two; public Parent(String a, String b){ one = a; two = b; } public void print(){ System.out.println(one); } } public class Child extends Parent { public Child(String a, String b){ super(a,b); } public void print(){ System.out.println(one + " to " + two); } public static void main(String arg[]){ Parent p = new Parent("south", "north"); Parent t = new Child("east", "west"); p.print(); t.print(); } } Which of the following is correct?()
第23题:
public class Pet{ public void speak(){ System.out.print(“ Pet ”); } } public class Cat extends Pet{ public void speak(){ System.out.print(“ Cat ”); } } public class Dog extends Pet{ public void speak(){ System.out.print(“ Dog ”); } } 执行代码 Pet[] p = {new Cat(),new Dog(),new Pet()}; for(int i=0;i〈p.length;i++) p[i].speak(); 后输出的内容是哪项?()