1. 判断正整数 x(x>1)是否为质数的 boolean functionPrime(int n)方法如下, 请将该方法补
充完整。
static boolean functionPrime(int x) { int i; if(x==2) return true; if(x%2==0) return false; for(i=3;___Math.sqrt(x)__;i+=2) if(____x%i==0_____) break; if(____i*i>x_____) return true; return ____false____; }
2. 要遍历元素对象,请在下面横线处填写相应代码。
ArrayList<String> list = new ArrayList< >( ); list.add("a"); list.add("b"); list.add("b"); for( __String x : list__ ) { System.out.println(x); }
3. 请举例说明类之间的 is-a 关系和类之间的 has-a 关系的含义。
答:is-a:类之间存在继承关系,如类A继承扩展类B,类A类B之间是is-a关系。
has-a:类之间存在包含关系,如类A的某成员变量类型是类B,类A类B之间是has-a关系。
4. 写出以下程序的运行结果。
public class ChangeStrDemo { public static void changestr(String str){ str="welcome"; } public static void main(String[] args) { String str="1234"; changestr(str); System.out.println(str); } }
运行结果:1234
5.阅读下列程序,请写出该程序的输出结果。
class Test33 { static int merger(int[] a, int[] b, int[] c) { int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { if (a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; } while (i < a.length) c[k++] = a[i++]; while (j < b.length) c[k++] = b[j++]; return k; } public static void main(String[] args) { int a[] = { 3, 6, 9 }; int b[] = { 1, 2, 5 }; int[] c = new int[100]; int p = merger(a, b, c); for (int k = 0; k < p; k++) System.out.print(c[k] + (k < p - 1 ? " " : "\n")); } }
运行结果:1 2 3 5 6 9