《网络程序设计》一.选择题[ 1 ] 假设有如下代码:1. public class Colors { 2. public static void main(String args[]) { 3. int n = 1; 4. System.out.println("The Color is " + args[n]); 5. } 6. } 设程序已经通过编译并生成文件Colors.class,运行以下哪一条命令会产生输出"The Color is blue"? A. Colors red green blue yellow B. java Colors blue green red yellow C. java Colors green blue red yellow D. java Colors.class blue green red yellow E. java Colors.class green blue red yellow 答: C [ 2 ] 当编译和运行下列代码时会产生什么情况?1. public class StrEq { 2. private StrEq() { 3. String s = "Bob"; 4. String s2 = new String("Bob"); 5. if (s == s2){ 6. System.out.println("Equal"); 7. } 8. else{ 9. System.out.println("Not equal"); 10. } 11. } 12. public static void main(String args[]){ 13. StrEq s = new StrEq(); 14. } 15. } A. 程序能通过编译并输出"Equal". B. 程序能通过编译并输出"Not Equal". C. 程序在编译时出错。D. 程序在运行时出错。答: B [ 3 ] 对下列不完整的代码来说,哪些选项的声明语句能使程序完整并通过编译?1. public class Declare { 2. 3. public static void main(String [] args) { 4. 5. System.out.println("The variable is " + x); 6. } 7. } A. "int x;" 放在第 2 行B. "int x;" 放在第 4 行C. "int x = 5;" 放在第 2 行D. "int x = 5;" 放在第 4 行E. "static int x;" 放在第 2 行F. "int x = new int();" 放在第 4 行答: D,E 二.编程题1. 写一程序,它能接收命令行输入的2 个整数,相加后将结果在屏幕上输出。(注: 输入的两个命令行参数分别对应args[0]和 args[1],但为 String 类型,可用Integer 类的 parseInt 方法来将它转换为整数后实现相加。例:String str1; int i; str1 = new String("123"); i = Integer.parseInt(str1); ) 答:主要程序代码如下:public class Add2Integer { public static void main(String[] args) { if (args.length != 2) System.out.println("参数个数不对 !"); ...