类和对象练习题 下载本文

System.out.println(\swap:circle1 =\ swap(circle1,circle2); System.out.println(\swap:circle1 = \ }

public static void swap(Circle x , Circle y){

System.out.println(\swap: x = \ y = \

Circle temp = x; x = y; y = temp;

System.out.println(\ x = \= \ } }

程序运行结果为:

11、下列程序的输出结果是什么? public class Foo{ static int i=0; static int j=0;

public static void main(String[] args){ int i=2; int k=3; int j=3;

System.out.println(“i + j is ”+ i + j); k = i +j;

System.out.println(“k is ”+k); System.out.println(“j is ”+ j); } }

程序运行结果为:

12、运行类C的输出结果是什么? class A{

public A(){

System.out.println(\default constructor of A y is

invoked\} }

class B extends A{ public B(){

System.out.println(\default constructor of B is invoked\ } }

public class C{

public static void main(String[] args){ B b = new B(); } }

程序运行结果为:

13、阅读下列程序写出输出结果: class A{

String s=\ void show(){

System.out.println(s); } }

class B extends A{

String s=\

void show() {

System.out.println(s); } }

public class TypeConvert{

public static void main(String args[]){ B b1;

B b2=new B();

A a1,a2; a1=(A)b2; a2=b2;

System.out.println(a1.s); a1.show();

System.out.println(a2.s); a2.show(); b1=(B)a1;

System.out.println(b1.s); b1.show();

System.out.println(b2.s); b2.show(); } }

程序运行结果为:

14、阅读下列程序,写出程序运行的结果: class A { int i, j;

public A(){ this.i=0; this.j=0; }

public void print(){ System.out.println (\}

class B extends A{ int m;

public B (int i, int j, int m){ super (); this.m=m; }

public void print (){System.out.println (\ m=\}

public class C{

public static void main(String args[]){ A a=new A();

B b=new B(1, 2, 3); a.print (); b.print (); } }}

程序运行结果为:

15、阅读下列程序,写出程序运行的结果: class Demo { public void test ()

{System.out.println (\public void test (int i) {System.out.println (i);} public void test (int a, int b) {System.out.println (a+b);} }

public class Test {

public static void main(String args[]){ Demo de=new Demo(); de.test(); de.test(5); de.test(6,8); } }

程序运行结果为:

16、阅读下列程序,写出程序运行的结果: class Cube{ int width; int height; int depth;

Cube(int x,int y,int z){ this.width=x; this.height=y; this.depth=z; }

public int vol(){

return width*height*depth;}} public class UseCube {

public static void main(String[] args) { Cube a=new Cube(3,4,5);

System.out.println(\长度=\ System.out.println(\体积=\ } }