public static void main(String args[ ]){ Cruncher crun=new Cruncher ( ); char ch=’p’; crun.crunch(ch); } }
9. 阅读以下程序,输出结果为 。
class Q1{ public static void main(String args[ ]){ double d=1.23; Dec dec=new Dec( ); dec.decrement(d); System.out.println(d); } classs Dec{ public void decrement(double decMe){ decMe = decMe - 0.1;
}
}
10. 以下程序的输出结果为 。 public class Short{ public static void main(String args[ ]) { StringBuffer s = new StringBuffer(“Hello”); if((s.length( )>5)&& (s.append(“there”) . equals(“False”))) ; System.out.println(“value is”+s); } }
11. 以下程序段的输出结果为 。 int x=0,y=4, z=5; if ( x>2){ if (y<5){ System.out.println(“Message one”); } else { System.out.println(“Message two”); } } else if(z>5){ System.out.println(“Message three”); } else { System.out.println(“Message four”); }
41
12. 以下程序段的输出结果为 。 int j=2; switch ( j ) { case 2: System.out.print(“Value is two.”); case 2+1 : System.out.println(“Value is three.”); break; default: System.out.println(“value is “+j); break; }
13.
阅读以下程序段: class Parent { void printMe() { System.out.println(“parent”); } }
class Child extends Parent { void printMe() { System.out.println(“child”); } void printAll() { super.printMe(); this.printMe(); printMe(); } }
public class Test_this { public static void main(String args[ ]) {
Child myC=new Child(); myC.printAll();} }
输出结果为:
14. 以下程序的功能是________将文件a.txt中的大写字母复制到文件b.txt _____________ 。import java.io.*; public class Test1{
public static void main(String[] args) throws IOException {
42
File inputFile = new File(\
File outputFile = new File(\
FileReader in = new FileReader(inputFile); FileWriter out = new FileWriter(outputFile); int c;
while ((c = in.read() ) != -1)
if(c>='A'&&c<='Z') //判断是否为大写字母 out.write(c); in.close();
out.close(); } }
15. 以下程序的运行结果是___________1__________________。
public class Test2
{ public static void main(String args[ ]){ int i , j=0 ;
int a[ ] = { 3,4,9,1,7,6};
for ( i = 1 ; i < a.length-1; i ++ ) if ( a[j]>a[i] ) j = i;
System.out.println(a[j]+\ \ } }
16. 以下程序的运行结果是_______in Test3 in Test3 _______________________。class First{ public First(){ aMethod(); }
public void aMethod(){
System.out.println(\ First\}
public class Test3 extends First{ public Test3 (){ aMethod(); }
public void aMethod(){ System.out.println(\ Test3\
public static void main(String[ ] args){ new Test3( ); } }
17. 以下程序的运行结果是______________welcome _______________________。
import java.io.*; public class Test4
{ public static void main(String args[ ])
{ StringC s = new StringC (\ System.out.println(s); } }
class StringC {
43
String s1; String s2;
StringC( String str1 , String str2 ) { s1 = str1; s2 = str2; } public String toString( ) { return s1+s2;} }
18. 以下程序的运行结果是_____________int_________________________。
class Test5{
void f( int i ){
System.out.print(\ } void f(String s){
System.out.print(\
public static void main(String args[ ]){ Test5 t=new Test5 ( ); char ch='c'; t.f(ch); } }
19. 描述下面程序完成的功能。 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( \ } }
计算 1/1+1/2+1/3+...+1/99 的值。
20. 描述下面程序完成的功能。 import java.io.* ;
public class Reverse
{ public static void main(String args[ ]) { int i , n =10 ;
int a[ ] = new int[10]; for ( i = 0 ; i < n ; i ++ ) try {
BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); // 输入一个整数
a[i] = Integer.parseInt(br.readLine( )); } catch ( IOException e ) { } ; for ( i = n - 1 ; i >= 0 ; i-- ) System.out.print(a[i]+\ \
44
System.out.println( ); } }
从标准输入(即键盘)读入10个整数存入整型数组a中,然后逆序输出这10个整数。
21. 写出下面程序的运行结果。 public class Test extends TT{
public static void main(String args[]){ Test t = new Test(\ }
public Test(String s){ super(s);
System.out.println(\ }
public Test(){
this(\ } }
class TT{
public TT(){
System.out.println(\ }
public TT(String s){ this();
System.out.println(\ } }
What a pleasure! I am Tom
How do you do?
22. 阅读下面程序,该程序有若干错误导致无法编译,找出不能编译的原因。 Class A{
Private int x;
Public static void main(String args[]) {
new B(); }
class B{ B(){
System.out.println(x);
} }
}
23. public class Sum
45