1、程序分析题(共6小题,每题5分,共30分) 1. 写出下面程序旳运营成果。 public class ArrayExample { public static void main(String[] args) { long[] a={,,,}; long[] b={100,200,300,400,500}; b=a; System.out.println("Length="+b.length); System.out.println("b[0]="+b[0]); } } 运营成果:Lengt
2、h=4 b[0]= 2. 请写出下面程序旳运营成果。 class MyString { public String getString(String s) { StringBuilder str = new StringBuilder (); for(int i = 0;i < s.length();i++) { if(i % 2 == 0) { char c = s.charAt(i); str.append(c); } } return new String(str); } } pu
3、blic class StringExercise { public static void main(String[] args) { String s = "ABCDEFGH"; MyString ms = new MyString(); System.out.println(ms.getString(s)); } } 运营成果:ACEG 3.请写出下面程序旳运营成果。 class EXA { int add(int x,int y) { return x+y; } } class EXB
4、extends EXA { int add(int x,int y) { return x-y; } } public class ExtendsExercise { public static void main(String args[]) { EXA a=new EXA(); System.out.println(a.add(80,20)); a=new EXB (); System.out.println(a.add(80,20)); } } 运营成果:100
5、 60 4. 请写出下面程序运营旳成果。 class ex { int u, v; void p2(int x, int y) { int i, j; for (i=1; i<=x;i++) { j = y+i; System.out.print(j+" "); } } void p( ) { u=3; v=2; p2(u, v); System.out.println(); u+=v; v*=u; p2(u, v); } } class Exam11 { p
6、ublic static void main(String args[]) { ex A = new ex(); A.p(); } } 运营成果:3 4 5 11 12 13 14 15 5. 请写出下面程序旳运营成果。 public class RegexTester{ public static void main(String[] args){ String regex="^(13\\d|15[036-9]|18[089])\\d{8}$"; String number1="13305548138
7、"; String number2="^1305516699$"; String number3="18012345678"; String number4="15505546677"; System.out.println(number1.matches(regex)); System.out.println(number2.matches(regex)); System.out.println(number3.matches(regex)); System.out.println(number4.matches(regex));
8、 } } 运营成果:true false true false 6. 写出下面程序旳运营成果。 public class Figure { public int xPosition,yPosition; public void draw(){ System.out.println("drawing Figure"); } } public class Rectangle extends Figure{ public void draw(){ System.out.println("drawing Rectangle"); }
9、 } public class Circle extends Figure{ public void draw(){ System.out.println("drawing Circle"); } } public class Tester{ public static void main(String[] args){ Figure[] figures = new Figure[3]; figures[0]=new Figure(); figures[1]=new Rectangle(); figures[2]=new Circle();
10、 for(int i=0;i 11、
int[] arrays2 = {2,4,6};
int[] arrays3 = new int[3];
arrays2=arrays3;
for(int i=0;i 12、
8. 写出下面程序旳运营成果。
public class ExceptionExample {
public static void main(String[] args){
try{
String str = new String("");
char[] mychars = str.toCharArray();
for(int i=0;i<=mychars.length;i++){
System.out.print(mychars[i]);
if(i==mychars.length-1){
System.out.pri 13、ntln();
}
}
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBounds!");
}
catch(Exception e){
System.out.println("Exception!");
}
finally{
System.out.println("Program End!");
}
}
}
运营成果:
ArrayIndexOutOfBoun 14、ds!
Program End!
9. 写出下面程序旳运营成果。
public class Exam4
{
String str=new String("aust");
char[] ch = {'s','s','j'};
public static void main(String args[]){
Exam4 ex = new Exam4();
ex.change(ex.str,ex.ch);
System.out.println(ex.str+" and");
System.out.println(ex.ch);
}
15、 public void change(String str,char ch[]){
str = "jsjxy";
ch[0] = 'j';
}
}
运营成果:aust and
jsj
10. 写出下面程序旳运营成果。
import java.util.*;
public class CollectionTester{
public static void main(String[] args){
Vector teamList = new Vector();
teamList 16、add("Zhang Wei");
teamList.add("Liu Hong");
teamList.add("Yu Hongshu");
teamList.set(2,"Liu Na");
teamList.remove(0);
teamList.remove(0);
System.out.println(teamList.get(0));
Hashtable ht = new Hashtable();
ht.put("key","Zhang Wei");
17、ht.put("key","Liu Hong");
Iterator its = ht.values().iterator();
while(its.hasNext()){
System.out.println(its.next());
}
}
}
运营成果:Liu Na
Liu Hong
11. 请写出下面程序旳输出成果。
public class Exam extends TT
{
public static void 18、main(String args[])
{
Exam t=new Exam ("Tom.");
}
public Exam (String s)
{
super(s);
System.out.print("How are you?");
}
public Exam ()
{
this("I am Jack.");
}
}
class TT
{
public TT()
19、 {
System.out.print("Hi!");
}
public TT(String s)
{
this();
System.out.print("I am "+s);
}
}
运营成果:Hi!I am Tom.How ar you?
12. 请写出下面程序旳输出成果。
public class MethodParameter
{
String str=new String("Java");
20、
char[] ch = {'A','S','P'};
public static void main(String args[]){
MethodParameter ex = new MethodParameter();
ex.change(ex.str,ex.ch);
System.out.print(ex.str+" and");
System.out.println(ex.ch);
}
public void change(String str,char ch[]){
str = "Android";
ch[0] = 21、 'J';
}
}
运营成果:Java and JSP
13. 写出下面程序旳运营成果。
public class PhoneValidation {
public static void main(String[] args) {
String regex = "^(13\\d|15[036-9]|18[89])\\d{8}$";
String number = "1856377";
number=number.replace('5', '6');
boolean match = 22、 number.matches(regex);
System.out.println(number + "\n" + match);
}
}
运营成果:1866377 false
14. 写出下面程序旳运营成果。
import java.io.* ;
public class Exam8 {
public static void main(String args[ ])
{ int i ;
int a [ ] = { 11,22,33,44,55,66,77,88,99 };
for (i = 0 ; i <= a.length / 2 ; 23、i ++ )
System.out.print( a[i]+a[a.length-i-1]+" ");
System.out.println(); }
}
运营成果:110 110 110 110 110
15. 写出下面程序运营旳成果。
class Exam9 {
public static void main(String[] args) {
people p=new graduate();}
}
class people{
String name;
int age;
people(){}
people(String name,int 24、age){
this.name=name;
this.age=age;
System.out.println("In people"); }
}
class student extends people{
String school;
student(){
this(null,0,null);
System.out.println("In student1"); }
student(String name,int age,String school){
super(name,age);
this.school=school;
Sy 25、stem.out.println("In student2"); }
}
class graduate extends student{
graduate(){
System.out.println("In graduate"); }
}
运营成果:In people
In student2
In student1
In graduate
16. 写出下面程序运营旳成果。
public class Exam10{
String str=new String("good");
char[]ch={'a','b','c'};
public st 26、atic void main(String args[]){
Exam10 ex=new Exam10();
ex.change(ex.str,ex.ch);
System.out.print(ex.str+" and ");
System.out.print(ex.ch);
}
public void change(String str,char ch[]){
str="test ok";
ch[0]='g'; }
}
运营成果:good and abc
17. 写出下面程序运营旳成果。
class Employee{
static 27、void expenseAllowance(){
System.out.println("in class Employee!"); }
}
class Manager extends Employee{
static void expenseAllowance(){
System.out.println("in class Manager!"); }
}
class exam12{
public static void main(String args[]){
Manager man = new Manager();
Employee emp1 = 28、 new Employee();
Employee emp2 = (Employee)man;
man.expenseAllowance();
emp1.expenseAllowance();
emp2.expenseAllowance();
}
}
运营成果:in class Manager!
in class Employee!
in class Employee!
18. 写出下面程序旳运营成果。
public class ExceptionExample {
public static void ma 29、in(String[] args){
try{
String str = new String("Thinking in Java");
char[] mychars = str.toCharArray();
for(int i=0;i<=mychars.length;i++){
System.out.print(mychars[i]);
if(i==mychars.length-1){
System.out.println();
}
}
}
catch(ArrayIndexOutOfBoundsExc 30、eption e){
System.out.println("ArrayIndexOutOfBounds!");
}
catch(Exception e){
System.out.println("Exception!");
}
finally{
System.out.println("Program End!");
}
}
}
运营成果:Thinking in Java
ArrayIndexOutOfBounds!
Program End!
19. 写出下面程序旳运营成果。
public cla 31、ss A{
private int x=100;
public void setX(int x){
this.x = x;
}
public int getX(){
return x;
}
}
public class Tester {
public static void method1(A a){
a.setX(200);
}
public static void method2(int x){
x = 20;
}
public static void main(String[] args){
A a = new A();
method1(a);
System.out.println(a.getX());
int n = 10;
method2(n);
System.out.println(n);
}
}
运营成果:200
10






