类和对象练习题 下载本文

String s1=new String(\ String s2=\ String s3=\

System.out.println(s1==s2);

System.out.println(s1.equals(s2)); System.out.println(s2==s3);

System.out.println(s2.equals(s3)); String s4=s1;

System.out.println(s1==s4);

System.out.println(s1.equals(s4)); } }

程序运行结果为:

4、public class Test {

private static void printArray(int[] arr) { for(int i=0;i< arr.length;i++) System.out.print(arr[i]+\ System.out.println(\ }

private static void changeValue(int value) { value*=2; }

private static void changeValue(int[] arr) { for(int i=0;i

public static void main (String[] args) { int[] arr={1,2,3,4,5}; changeValue(arr[0]); printArray(arr); changeValue(arr); printArray(arr); } }

程序运行结果为:

5、class MyException extends Exception { public MyException(String message) { super(message); } }

public class Test {

private static void fun1() throws MyException {

throw new MyException(\in fun1().\ }

private static void fun2() throws MyException { try { fun1(); }

catch(NullPointerException ex) { System.out.println(\、\ }

finally {

System.out.println(\ } }

public static void main (String[] args) { try {

fun2(); }

catch(MyException ex) {

System.out.println(\); }

catch(Exception ex) {

System.out.println(\ } } }

程序运行结果为:

6、下面是一个类的定义,请完成程序填空。 public class ___ _______ {

int x, y;

Myclass ( int , int ) // 构造方法 {

x=i; y=j; } }

程序运行结果为:

7、 下面是一个类的定义,请将其补充完整。 class ___ __ { String name; int age;

Student( ___ __ s, int i) { name=s; age=i; } }

程序运行结果为:

8、下面程序的功能是通过调用方法max()求给定的三个数的最大值,请将其补充完整。 public class Class1{

public static void main( String args[] ){ int i1=1234,i2=456,i3=-987;

System.out.println(\三个数的最大值、\ ); }

public static int max(int x,int y,int z) { int temp1,max_value; temp1=x>y?x:y;

max_value=temp1>z?temp1:z; return max_value; } }

程序运行结果为:

9、写出下列程序的输出结果 public class Test{

public static void main(String[] args){ Count myCount = new Count(); int times = 0;

for(int i=0;i<100;i++)

increment(myCount , times);

System.out.println(“count is” + myCount.count); System.out.println(“time is”+ times); }

public static void increment(Count c , int times){ c.count++; times++; } }

class Count{

public int count; Count(int c){ count =c; }

Count(){ count =1; } }

程序运行结果为:

10、阅读下列程序,写出程序运行的结果: class Circle { double radius;

public Circle(double r){ this.radius=r; } }

public class Test{

public static void main(String[] args){ Circle circle1 = new Circle(1); Circle circle2 = new Circle(2);