当前位置:首页 > 高级语言程序设计(Java) - 习题集(含答案)
18): public void showX() {
19): System.out.println(\20): } 21): };
22): public void showY() { 23): a.showX(); } 24): }
程序第5行的方法调用输出的结果如何?
153. 阅读程序,回答问题
1): public class A {
2): public static void main(String[] args) { 3): long first=1,second=1,third;
4): System.out.print(first + \ \ \5): for(int i = 3; i <= 6; i++) { 6): third = first + second;
7): System.out.print(third + \ \8): first = second; 9): second = third; 10): } 11): } 12): }
程序输出的结果是什么?
154. 写出下列程序完成的功能s
public class Sum{
public static void main( String args[ ]){
double sum = 0.0 ; for ( int i = 1 ; i <= 100 ; i + + ) sum += 1.0/(double) i ; System.out.println( \} }
155. 写出下列程序的功能。
import java.io.*; public class Class1{ public static void main( String args[] ){ int i1=1234,i2=456,i3=-987;
int MaxValue;
MaxValue=max(i1,i2,i3);
System.out.println(\三个数的最大值:\alue); }
public static int max(int x,int y,int z){
第 13 页 共 33 页
int temp1,max_value; temp1=x>y?x:y;
max_value=temp1>z?temp1:z; return max_value;
} }
156. 写出下列程序的功能
import java.io.*;
public class Class1 { public static void main( String args[ ] ){
SubClass a = new SubClass( 10,3 );
System.out.println( a.exp( )); } }
class SuperClass { float x; int n;
SuperClass(float xx,int nn ) {
x = xx ; n = nn; } }
class SubClass extends SuperClass{
SubClass(float xx , int nn) {
super( xx , nn ); }
float exp( ) { float s = 1;
for ( int i = 1; i <= n; i++ )
s = s*x; return s; } }
157. 阅读程序,回答问题
1): public class A {
2): public static void main(String args[]) { 3): MyInterface obj2=new B(\4): obj2.show(); 5): }
第 14 页 共 33 页
6): } 7):
8): interface MyInterface { 9): double G=88; 10): void show(); 11): } 12):
13): class B implements MyInterface { 14):
15): String str;
16): public B(String s) { 17): str=s; 18): }
19): public void show() {
20): System.out.println(str+\21): } 22): }
程序第8~11行定义的MyInterface是类还是接口?程序第4行输出的结果是什么?
158. 阅读下面的程序(或程序片段),回答问题 。
现有类说明如下:
class A{
int x=10; int GetA(){
return x; } }
class B extends A{
int x=100; int GetB(){
return x; } }
若b是类B的对象,则b.GetA()的返回值是什么?
159. 阅读以下程序段,回答问题。
class Parent{ void printMe(){ System.out.println(“parent”);
} }
class Child extends Parent{ void printMe(){ System.out.println(“child”);
第 15 页 共 33 页
}
public class Test_this{ public static void main(String args[ ]){ Child myC=new Child(); myC.printAll();
} }
输出结果是什么?
}
void printAll(){ super.printMe(); this.printMe(); printMe(); }
160. 阅读程序,回答问题。
1): public class A {
2): public static void main(String args[]) { 3): try{
4): int x[]={1,2,3};
5): int k=Integer.parseInt(args[0]); 6): System.out.println(x[k]);
7): System.out.println(\结束!\8): }
9): catch(ArrayIndexOutOfBoundsException e){ 10): System.out.println(\执行catch\11): }
12): finally{
13): System.out.println(\执行finally\14): }
15): System.out.println(\程序结束!\16): } 17): }
若在命令行编译该程序后,键入下面的内容运行该程序: java A 3
则程序输出的结果如何?
161. 阅读下面的程序,回答问题。
class student{ String name;
int age;
第 16 页 共 33 页
共分享92篇相关文档