资源描述
程序分析题(共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]);
}
}
运营成果:Length=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);
}
}
public 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 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
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 {
public 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"; 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));
}
}
运营成果: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");
}
}
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();
for(int i=0;i<figures.length;i++){
figures[i].draw();
}
}
}
运营成果:drawing Figure
drawing Rectangle
drawing Circle
7. 写出下面程序旳运营成果。
import java.util.*;
public class ArrayTester{
public static void main(String[] args){
int[] arrays1 = {1,3,5};
int[] arrays2 = {2,4,6};
int[] arrays3 = new int[3];
arrays2=arrays3;
for(int i=0;i<arrays3.length;i++){
arrays3[i]=arrays1[i];
}
for(int i=0;i<arrays2.length;i++){
System.out.println(arrays2[i]);
}
}
}
运营成果:1
3
5
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.println();
}
}
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBounds!");
}
catch(Exception e){
System.out.println("Exception!");
}
finally{
System.out.println("Program End!");
}
}
}
运营成果:
ArrayIndexOutOfBounds!
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);
}
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.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");
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 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()
{
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");
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] = '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 = 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 ; 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 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;
System.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 static 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 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 = 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 main(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(ArrayIndexOutOfBoundsException 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 class 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
展开阅读全文