Java 编 程 练 习 题 库 (一)


1. 用“ 筛 选 法” 求 小 于N 的 素 数 的 程 序
2. 选 择 排 序 的 程 序
3. 将 一 个 数 插 入 一 个 有 序 的 数 组 中 的 程 序
4. 打 印“ 杨 辉 三 角 形” 的 程 序
5. 打 印 奇 数 阶 的“ 魔 幻 方 阵” 的 程 序
6. 测 试String 类 的 各 种 构 造 函 数 的 程 序

1. 读 懂 下 面 这 个 用“ 筛 选 法” 求 小 于N 的 素 数 的 程 序, 在 横 线 上 填 上 合 适 的 逻 辑 表 达 式。
// array1.java
// Display primes (

 备 选 答 案:
A. a[i] & a[j]   B. a[i] | a[j]   C. a[i] ^ a[j]   D. 以 上 都 不 对   

答 案:(A)

回 到 页 首

2. 下 面 给 出 了 一 个 选 择 排 序 的 程 序, 试 在 横 线 上 填 上 适 当 的 语 句。
// array2.java
// Sort with selection

class array2 {

	public static void main(String args[]) {
 		final int N = 10;		
		int i,j,min,temp;
		int a[] = { 20,10,50,40,30,70,60,80,90,100 };
		
		System.out.println("The source data:");
		for(i = 0;i  a[j]) ___________;
			temp = a[i];
			a[i] = a[min];
			a[min] = temp;
		}
		// Output
		System.out.println("The sorting result:");
		for(i = 0;i 
 备 选 答 案:
A. min = j -1   B. min = j   C. min = j + 1    D. 以 上 都 不 对    

答 案:(B)

回 到 页 首

3. 以 下 程 序 将 一 个 数 插 入 一 个 有 序 的 数 组 中, 试 在 横 线 上 填 出 缺 少 的 语 句。
// array3.java
// Insert a number into a sorted array

class array3 {
	public static void main(String args[]) {
		int a[] = { 1,4,6,9,13,16,19,28,40,100,0 };
		
		int number = 5;
		int temp1,temp2,end,i,j;
		System.out.println("The initial array is as following:");
		for(i = 0;i <10;i++) System.out.print(a[i] + " "); System.out.println(); System.out.println("The number to insert:" + number); end="a[9];" if(number> end)	a[10] = number;
		else {
			for(i = 0;i <10;i++) { if(a[i]> number) {
					temp1 = a[i];
					_____________;
					for(j = i+1;j <11;j++) { temp2="a[j];" a[j]="temp1;" temp1="temp2;" } break; } } } for(i="0;i" < 11;i++) System.out.print(a[i] + " "); } } 
备 选 答 案:
A. temp2 = number   B. a[i] = number    C. temp2 = temp1   D. 以 上 都 不 对   

答 案:(B)

回 到 页 首

4. 补 足 以 下 打 印&ldquo; 杨 辉 三 角 形&rdquo; 的 程 序
// array4.java
// Print out Yanghui Triangle (first 10 lines)

class array4 {
	public static void main(String args[]) {
		final int N = 11;
		int i,j,a[][] = new int[N][];
		for(i = 0;i 
 备 选 答 案:
A. a[i] = new int[i]  B. a[i][] = new int[i + 1]  C. a[i] = new int[i + 1]  D. 以 上 都 不 对   

答 案:(C)

回 到 页 首

5. 以 下 例 程 可 打 印 出 奇 数 阶 的&ldquo; 魔 幻 方 阵&rdquo;, 试 补 上 缺 失 的 语 句。
// array5.java
// Print out magic phalanx (N * N , N is an odd number)

class array5 {
	public static void main(String args[]) {
		final int N = 5;
		___________________;
		
		int i,j,k;
		
		// Initialize
		for(i = 1;i <= N;i++) for(j="1;j" <="N;" j++) a[i][j]="0;" // Creat magic phalanx j="(N" + 1) / 2 ; a[1][j]="1;" for(k="2;k" <="N" * N;k++) { i--; j++; if((i < 1) && (j> N)) {
				i += 2;  j--;
			}
			else {
				if(i <1) i="N;" if(j> N) j = 1;
			}
			if(a[i][j] == 0)  a[i][j] = k;
			else { 
				i += 2;  j--;
				a[i][j] = k;
			}
		}
		
		// Output
		for(i = 1;i <= N;i++) { for(j="1;j" <="N;j++)" System.out.print(a[i][j] + " "); System.out.println(); } } } 
备 选 答 案:
A. int a[][] = new int[N + 1][N + 1]  B. int a[][] = new int[N ][N ] 
C. int a[N+1] = new int[N + 1]  D. 以 上 都 不 对   

答 案:(A)

回 到 页 首

6. 以 下 程 序 测 试String 类 的 各 种 构 造 函 数, 试 选 出 其 运 行 结 果。
 // STR1.java

class STR1 {
	public static void main(String args[]) {
		String s1 = new String();
		String s2 = new String("String 2");
		char chars[] = { 'a',' ','s','t','r','i','n','g' };
		String s3 = new String(chars);
		String s4 = new String(chars,2,6);
       byte bytes[] = { 0,1,2,3,4,5,6,7,8,9 };
       String s5 = new String(bytes,0,1,5);
     	String s6 = new String(bytes,10);
		StringBuffer sb = new StringBuffer(s3);
		String s7 = new String(sb);
		
		System.out.println("The String No.1 is " + s1);
		System.out.println("The String No.2 is " + s2);
		System.out.println("The String No.3 is " + s3);
		System.out.println("The String No.4 is " + s4);
		System.out.println("The String No.5 is " + s5);
		System.out.println("The String No.6 is " + s6);
		System.out.println("The String No.7 is " + s7);
	}
}
备 选 答 案:
A. The String No.1 is    
   The String No.2 is String 2   
   The String No.3 is a string   
   The String No.4 is string   
   The String No.5 is   
   The String No.6 is ??????????   
   The String No.7 is a string
B. The String No.1 is
   The String No.2 is String 2   
   The String No.3 is a string
   The String No.4 is tring
   The String No.5 is
   The String No.6 is ??????????
   The String No.7 is a string
C. The String No.1 is
   The String No.2 is String 2
   The String No.3 is a string
   The String No.4 is strin
   The String No.5 is 
   The String No.6 is ??????????
   The String No.7 is a string
D. 以 上 都 不 对。   

答 案:(A)

回 到 页 首

返回