一些关于java读程序的练习题`
宝子们有疑问 评论区见哦~
`
1.关于Java的叙述中,正确的是( ac )
A.Java语言的标识符是区分大小写的
B.源文件名与public类名可以不相同
C.源文件扩展名为.java
D.源文件中public类的数目不限
2.以下( b )是应用程序的main方法头
public static int main(char args[ ])
public static void main(String a[ ])
public static void MAIN(String args[ ])
public static void main(String args)
3.下列哪些是合法的Java标识符名字(ad)
A. counterl B. $index, C. name-7
D. _byte E. 1array F. 2i
G. try H. char
4、
public class test { public static void main(String args[]) {int x=1,y=1,z=1;if (--x==0 && y++==1||z++==1)System.out.println("x="+x+",y="+y+",z="+z); } }
运行结果:x=0,y=2,z=1
5.
int m=8,n=5; while (m>2) { if (m>n)m = m-n; else n = n-m; } System.out.println(m+", "+n);
**运行结果:1,2**
6.
初始化部分和迭代部分可以使用逗号语句,来进行多个操作。所谓逗号语句是用逗号分隔的语句序列。例如: for( int i=0, j=10; i<j; i++, j-- ) {System.out.println(i+", "+j); }
**结果 0,10 1,9 2,8 3,7 4,6**
7、
.public class Test3{ public static void main(String args[]) {int f=1;for (int k=2;k<4;k++)f = f * k; System.out.println("f="+f); } }
**f=6**
8.
public class Test5{public static void main(String args[]) {for(int i=1;i<3;i++) {for (int j=0;j<2;j++)System.out.print("\t"+(i+j));System.out.println();}}}
1223
9.
输出10~20之间不能被3或5整除的数 public class ContinueTest {public static void main(String args[]) {int j=9;do { j++;if(j%3==0||j%5==0) continue;System.out.print(j + " "); } while(j<20); }}11 13 14 16 17 1910.outer: for (int i=1;i<3;i++)for (int j=1;j<4;j++){if (i==1 && j==2)continue outer;System.out.println("i="+i+" j="+j); }
i=1 j=1i=2 j=1i=2 j=2i=2 j=3
11.以下程序调试结果为? (d)
public class Test {
public static void main(String argv[]) {
int x[] = new int[2];
System.out.println(x[2]);
}
}
a.编译错误;
b.null
c.0
d.运行时出现异常 √
12.
以下代码的输出结果?class test3{public static void main(String args[ ]){ int[ ] a={1,2,3,4}; for (int i=0;i<4;i++) a[i]=a[i]+i; for(int b:a) System.out.println(b);}}
1357
13.填空题 等级考试上机题—输出结果为: 0 1 2 3 4 5 6 7 8 9
public class Java_3 { public static void main(String[] args) {int[] anArray; // 声明一个整型数组 anArray = new int[10]; // 创建数组,包含10个元素。// 给数组每个元素赋值并打印输出 for (int i = 0; i < anArray.length; i++) {anArray[i]=i;System.out.print(anArray[i] + " "); }System.out.println(); }}
14.将程序补充完整
public class Java_2 { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 59, 12, 106, 20, 8, 62, 127 };int searchfor = 12;int i = 0;boolean foundIt = false;for ( ; i < arrayOfInts.length ; i++) {if (arrayOfInts[i] == searchfor) {foundIt = true; break; }}if (foundIt) { System.out.println("Found " + searchfor + " at index " + i); }else { System.out.println(searchfor + "not in the array"); }}}
15.例4-3 二维数组动态创建示例
public class ArrayOfArraysDemo2 { public static void main(String[] args) { int[][] aMatrix = new int[4][]; for (int i = 0; i < aMatrix.length; i++) { aMatrix[i] = new int[i+1]; for (int j = 0; j < aMatrix[i].length; j++) aMatrix[i][j] = i + j; } for (int i = 0; i < aMatrix.length; i++) { for (int j = 0; j < aMatrix[i].length; j++) System.out.print(aMatrix[i][j] + " "); System.out.println(); } } }
【运行结果】01 22 3 43 4 5 6
16. 例: 求矩阵的最外一圈元素之和
public class Demo { public static void main(String[ ] args) { int [ ][ ] a = new int[4][5]; int sum=0;for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) {a [i][j] = (int)(Math.random()*10);System.out.print(a[i][j] + "\t"); if (i==0||j==0||i==a.length-1||j==a[0].length-1)sum += a[i][j];}System.out.println(); } System.out.println("sum= " +sum); } }
7 9 3 7 88 1 5 2 84 2 1 5 41 1 1 2 6sum= 69
17。例4-4 编写求阶乘的方法,并利用求阶乘的方法实现一个求组合的方法,利用求组合方法计算输出杨辉三角形。
public class Ex4_4{/* 求n!的方法fac(n) */public static long fac(int n) {long res=1;for (int k=2;k<=n;k++)res = res * k;return res;}/* 求n个中取m个的组合方法 */public static long c(int n , int m) {return fac(n) / (fac(n-m) * fac(m) );}public static void main(String args[ ]) {for (int n=0;n<=3;n++) {for (int m=0;m<=n;m++)System.out.print( c (n, m) +" ");System.out.println( );}}}
11 11 2 11 3 3 1c(0,0)c(1,0) c(1,1) c(2,0) c(2,1)c(2,2) c(3,0) c(3,1)c(3,2)c(3,3)
18。例4-5 基本类型参数传递演示
public class Test1{static void paraPass(int x) {x = x+1;System.out.println("x= "+x);}public static void main(String args[]) {int m=5;paraPass(m);System.out.println("m= "+m);}}
x=6m=5
19. 例4-5 引用类型的参数传递
public class Test2 {static void paraRef(int x[ ]) {x[1]=x[1]+1;}public static void main(String args[]) {int a[ ] = { 1 , 4 , 6 , 3 }; paraRef(a);for (int k=0;k<a.length;k++)System.out.print (a[k]+ "\t");}}
15 63
20.以下代码的输出结果?****(b)
public class Test{ static int x=5; public static void main(String argv[ ]){ change(x); x++; System.out.println(x); } static void change(int m){ m+=2; } }
A. 7 B. 6 C. 5 D. 8
21. 思考程序运行结果?
class Test3{public static void main(String args[ ]){int[ ] a ={1,2,3,4};int m=f(a);System.out.println("m="+m);for (int k=0;k<a.length;k++)System.out.print(a[k]+"\t");}static int f(int b[ ]) {int s=0;for (int k=0;k<b.length;k++){s+=b[k];b[k]++;}return s;}}
m=102 34 5
22. Point类中增加一个main方法
public static void main(String args[]) {Point p1 = new Point(); Point p2 = new Point(); Point p3 = p1;p1.move(5,8);p2.x = 12;System.out.println("p1"+p1);System.out.println("p2"+p2);System.out.println("p3"+p3);}
p1点: 5,8p2点: 12,0p3点: 5,8
23. 写出程序执行结果
public class A {int v=0; { v=v+1; } //初始化块public A( ) { }public A(int x ) { v=x; }public static void main(String args[ ]) {A m1 = new A(25); System.out.println(m1.v); A m2 = new A(); System.out.println(m2.v); }}
251
24. 写程序运行结果
public class RangeTest {int count=8;public void m( ){ for(int count=1;count<4;count++)System.out.println(count);System.out.println("count="+ count);}public static void main(String args[ ]) {new RangeTest().m( ); }}
123count=8
25. 给类变量赋初值—用静态初始化代码块 以下程序的执行结果?
class test {static int x=5;static { x+=10; }public static void main(String args[ ]) {System.out.println("x="+x);}static { x=x-5; }}
x=10
26. 思考题
public class Test{ long a[ ] = new long[10]; public static void main ( String arg[ ] ) { System.out.println ( a[6] ); } } 叙述正确的是( c )。
A. 输出 null
B. 输出 0
C. 出现编译错误
D. 运行出错
27. Point类的再设计
public class Point {private int x, y; public Point(int x, int y) { this.x = x; this.y = y;}public Point() { this(0,0); }public double distance(Point p) { return Math.sqrt((this.x-p.x)* (x-p.x) + (y-p.y)*(y-p.y)); }/ * 以下两个方法在利用上面方法求距离,纯粹为了演示概念 */public double distance2(Point p) { return p.distance(this); //p到当前点的距离 } public double distance3(Point p) { return this.distance(p); //调用当前对象另一方法}……}
28. 写程序运行结果
public class Ex1{static int m=2;public static void main(String args[ ]) {Ex1 obj1=new Ex1( );Ex1 obj2=new Ex1( );obj1.m=m+1;System.out.println("m="+obj2.m);}}
m=3
29. 方法的重载
public class A {void test(int x) { System.out.println("test(int):" + x );}void test(long x) { System.out.println("test(long):" + x ); } void test(double x) { System.out.println("test(double):" + x); }public static void main (String[ ] args) {A a1 = new A();a1.test(5.0);a1.test(5);}}
test(double):5.0test(int):5
30. 写运行结果
class Test5 {int k = 8;public void doSome() {System.out.println("k1="+k);}}class Sub extends Test5 {int k = 66;public void doSome() { k=k-2;System.out.println("k2="+k);}public static void main(String args []) {Test5 obj = new Sub();obj.doSome();System.out.println("k3="+obj.k);}}
k2=64k3=8
31.
class exSuper { public void func(String p, String s) { System.out.println(p); }}public class example extends exSuper { public void func(String p, String s) { System.out.println(p + " : " + s); } static public void main(String arg[ ]) { exSuper e1 = new exSuper( ); e1.func("hello1", "hi1"); exSuper e2 = new example(); e2.func("hello2", "hi2"); }}
hello1 hello2 : hi2
32. 写运行结果
public class IsEqual {int x;public IsEqual(int x1) {x=x1;}public static void main(String a[ ]) {IsEqual m1=new IsEqual(4);IsEqual m2=new IsEqual(4);IsEqual m3=m2;m3.x=6; System.out.println("m1=m2 is "+(m1==m2)); System.out.println("m2=m3 is "+(m2==m3)); System.out.println("m1.x==m2.x is "+(m1.x==m2.x)); System.out.println("m1.equals(m2)="+m1.equals(m2));}}
m1=m2 is false m2=m3 is truem1.x==m2.x is falsem1.equals(m2)=false
33. 对象引用赋值转换
public class A {void test(Object obj) {System.out.println("test(Object):" + obj ); } void test(String str ) {System.out.println("test(String):" + str); }public static void main (String[ ] args) {A a1 = new A();a1.test("hello");}}
test(Object):hello
34. 写运行结果
public class Test6{static void m(double x) { System.out.println("m(double):" + x); }static void m(Object obj) {System.out.println("m(Object):" + obj );} public static void main (String args[]) { m("hello"); m(5); } }
m(Object):hellom(double):5.0
35. 写程序运行结果
public class Test{public static void main(String argv[]){String x="hello";myMethod(x); System.out.println("x="+x);}static void myMethod(String k){k=k+",你好";System.out.println("k="+k);}}
k=hello,你好x=hello
37. 写程序运行结果
public class Test{ public static void main(String args[ ]) { String s1="abc";String s2="Abc"; System.out.println(s1=s2);System.out.println(s1==s2);}}
AbcTrue
38. 写程序的运行结果
public class Test3 {
public static void main(String a[]) {String s="ABCDEFGHIJK";System.out.println(s.charAt(3));System.out.println(s.substring(1,4));System.out.println(s.indexOf("EF"));}}
DBCD4
39. 思考程序执行结果?
class E { public static void main(String[ ] args) { StringBuffer sb1 = new StringBuffer("hello"); StringBuffer sb2 = new StringBuffer("hello"); if (sb1.equals(sb2)) System.out.println("equal");else System.out.println("not equal");}}
not equal
40. 写程序的运行结果
public class Foo { public static void main (String [ ] args) { StringBuffer a = new StringBuffer("A"); StringBuffer b = new StringBuffer("B"); operate(a,b); System.out.println(a + "," +b); }static void operate (StringBuffer x, StringBuffer y) { x.append(y); y = x; }}
AB,B
41. 写出程序运行结果
abstract class P{public P() { System.out.println("parent"); }abstract void m();}class S extends P {public S() { System.out.println("child"); }public static void main(String a[ ]) {P x= new S();x.m();}void m() {System.out.println("m()"); }}
parentchildm()
42. abstract class MineBase { abstract void amethod( ); static int i; } public class Mine extends MineBase{ public static void main(String argv[ ]){ int[ ] ar = new int[5]; for (i = 0;i < ar.length;i++) System.out.println(ar[i]); } }****(c)
A.输出5个0;
B.编译错误:ar 未初始化就使用;
C.编译错误:Mine 必须定义为抽象的;
D.运行错误,i超出数组下标范围。
43. public class Outer{ public String name = “Outer”; public static void main(String argv[ ]){ Inner i = new Inner( ); i.showName( ); } private class Inner{ String name =new String(“Inner”); void showName( ){ System.out.println(name); } } }
A.输出结果 Outer
B.输出结果 Inner
C.编译错误,因Inner类定义为私有访问
D.在创建Inner类实例的行出现编译错误
44. 思考程序运行结果
public final class Test4 {class Inner {void test() {if (Test4.this.flag) sample();else System.out.println("inner test");} } private boolean flag = false;public void sample() {System.out.println("Sample");}public Test4() {flag=true;(new Inner()).test();}public static void main(String args [ ]) {new Test4();} }
Sample
45. class InOut{ String s= new String(“Between”); public void amethod(final int iArgs){ int iam; class Bicycle{ public void sayHello( ){ //Here } } } public void another( ){ int iOther; } } 以下可以安排在//Here处的语句是( ad )。
A. System.out.println(s);
B.System.out.println(iOther);
C. System.out.println(iam);
D. System.out.println(iArgs);