1、【答案】 +k 的类型为 int 型。(3)有错。 +k 为 int 型, ch1 为 char 型,将高精度赋给低精度类型时必须实行强制转换。6.请指出下面程序在编译时是否会出现错误。public class doubleTointExample public static void main(String args ) int a;double b=1,c=2;a=(int)(b+c);System.out.println(a=+a);【答案】无错误。输出结果: a=37.请指出执行完下面程序后 x、y 和 z 的输出值是多少?请上机验证。int x,y,z;x=1;y=2;z=(x+y3
2、?x+:+y);x=+x);y=+y);z=+z);【答案】x=1y=3z=38.请指出下面程序片段输出的结果是什么。int i=1,j=10;doif (i+-j) break;while(i”+i+“”j= ” +j);【答案】 i=5j=69. 请分别用 if-else 语句和 switch 语句编写实现下列功能的程序。某同学某门课的成绩可能的结果为 1,2,3,4,5。当成绩为 1 时请输出不及格;成绩为 2 时请输出及格;成绩为 3 时请输出中等;成绩为 4 时请输出良好;成绩为 5 时请输出优秀。 残骛楼諍锩瀨濟溆塹籟婭骒東。/*if-else 描述程序 */ public cla
3、ss xt020901 int score=4;if (score=1) System.out.println( 不及格 ); else if (score=2)System.out.println( 及格 else if (score=3) 中等 else if (score=4) 良好 else System.out.println( 优秀 /*switch 描述程序 */ public class xt020902 switch (score)case 1 : break;case 2 :case 3 :case 4 :case 5 :10. 请编写输出乘法口诀表的程序。 乘法口诀表的部
4、分内容如下:1*1=11*2=2 2*2=31*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=16 public class xt0210public static void main(String args ) final double PI = 3.141592654; double area,r; area =PI*r*r;System.out.println(面积=”+ area);public class xt0210int i,j;for(i=1;i=9;i+)for(j=1;j=i;j+)System.out.pri nt(j+*+i+=+i*j+
5、System.out.pri ntl n();11.请编写程序实现如下效果图。AB 0DEFG H I JK L M NO P QR STpublic class xt0211public static void main(String args ) int i,j,k,num;char ch;num=-1;=4;for(k=1;k=4-i;k+) System.out.pri nt(num=nu m+1; ch=(char) num;System.out.print(ch+System.out.pri ntl n(); for(i=1;=i-1;=5-i; 12.分别利用for语句、whil
6、e语句以及do while语句编写一个求和程序 (即sum=1+2+3+n)。酽锕极額閉镇桧猪訣锥顧荭钯。/*for 语句实现 */public class xt021201int i,n=100;long sum=0;=n;i+) sum=sum+i;sum1:n=+sum);/*while 语句实现 */public class xt021202i=1;while(i=n)sum=sum+i;i=i+1;/*do while 语句实现 */public class xt021203while (i50) v=9; p=v+u;Java语言规定,任何变量在使用之前, 必须对变量赋值。由于u的
7、值是由随机方法产生的,当 u=50 时, v 在使用前没有赋值,此时执行 p=v+u; 会出现错误。 茕桢广鳓鯡选块 网羈泪镀齐鈞。改正措施:将intv,p;变为int v=0, p;即可(3) B.java 内容如下:class Aint x , y;static float f(int a)return a;float g(int x1 , int x2)return x1*x2;public class Bpublic static void main(String args) A a=new A();A.f(3);a.f(4);a.g(2, 5);A.g(3 , 2); A.g(3,2
8、); 由于类方法不仅可以由对象调用而且还可以直接由类名调用,而实例方法不能由类名调用。 鹅娅尽損鹌惨歷茏鴛賴縈诘聾。 删除 A.g(3,2); 或将 A .g(3,2); 改为 a.g(3,2);3.简答题(1)简述面向对象程序和面向过程程序设计的异同。(2)简述类中成员变量的分类及差异。(3)简述类中方法的分类及差异。(4)简述类中变量的初始化方式。(5)简述类中成员的几种访问控制修饰符的差异,并举例说明。(6)简述构造方法的作用。 【答案】答案见教材 。第四章1. 简述 Java 中继承的含义及特点。【答案】答案见教材 略 。2.指出下列程序中的错误,请说明错误原因。 class A pu
9、blic int a = 1;private int b = 2;protected int c = 3;int d=4;public int dispA() return a;private int dispB() return b; protected int dispC() return c; int dispD() return d; public class B extends A public static void main (String args ) B bb=new B(); bb.testVisitControl ();public void testVisitContr
10、ol () System.out.println(a+dispA(); System.out.println(b+dispB(); System.out.println(c+dispC(); System.out.println(d+dispD(); 【答案】 System.out.println(b+dispB(); 行编辑时出现错误。原因如下: (1) 子类 B 和父类 A 在同一包中;(2) 子类 B 不能继承父类的 private 型属性和方法。3.根据下面程序片段,画出类和对象的内存映像图。 class Astatic int sv1=10;int sv2=20;int sv3=30
11、;static void sf1() void f1() class B extends Astatic int sv2=30;int v2=3;A ref1=new A();B ref2=new B(); ref1=ref2;BA/svlB/sv2Jifl1030* E * * * BA/svl Ro4.简述子类对象的成员初始化的方法。【答案】答案见教材 5.简述成员变量的隐藏的含义,并举例说明。6.简述方法的重载和方法的覆盖的区别,并举例说明。7.列举this和super的用途。8.指出下列程序运行的输出结果。class Point int x, y;Point( ) this(-1,-1
12、);Poin t( int a, int b) x=a; y=b;void showxy()System.out.pri ntln( “ x= ” +x+ ” y= ” +y);public class reloadi ngExample public static void main (String args ) Point a=new Poin t ();Point b=new Point (1,1);a.showxy();b.showxy();【答案】输出结果为:x=-1 y=-1 x=1 y=19.指出下列程序运行的输出结果。int x=1, y=2;double add() retu
13、rn x+y;int x=10,y=20;double add() return super.x+super.y ;class ex2 public static void main(String args )B b=new B();a.add=+a.add();b.add=+b.add();a.add=3.0b.add=3.010.简述接口和抽象类的含义,以及它们两者之间的不同。第五章1.为什么说 Java 多维数组是数组元素为数组的一维数组,请用事实说明。2.判断下面数组的定义是否正确?如果不正确,请改正。(1)int a5;char ch54;(2)int a =new int 4;(3
14、)int N=10;int a=new intN;【答案】 (1)数组定义错。因为数组声明时方括号中不能用数字,即不允许静态说明数组。(2)数组定义错。因为数组维数声明顺序应该从高到低,先声明高维,再声明低维。(3)正确。因为数组元素个数可以是常量,也可以是变量。3.若 int a=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 ,请问 a.length , a2.length, a3.length 分别等于多少? 籟丛妈羥为贍偾蛏练淨槠挞曉。【答案】 a.length, a2.length, a3.length 分别等于 5、 3、2。4.写出下列程序的运行结果。vo
15、id operate(int c)int i;for(i=0;ic.length;i+) ci=3*ci; public class ArrayExample4public static void main(String args) int b =1,2,3,4;a. operate (b);預頌圣鉉儐歲龈讶骅籴買闥for(int i=0; b.length;i+) System.out.println(bi);龅。 【答案】运行结果为: 3 6 9 125.写出下列程序的运行结果。 public class StringExample4String s1=abcString s2=s1; s
16、2+=def s1.concat(s1=+s1+s2=+s2); 【答案】运行结果为: s1=abc s2=abcdef6.写出下列程序运行的结果。 public class StringExample5 public static void main(String args)String s=ab,cd; reverse(s0,s1);s0=+s0+ s1=+s1); static void reverse(String s0, String s1)String s;s=s0; s0=s1;s1=s; s0=ab s1=c7.写出下列程序的运行结果。public class StringEx
17、ample6 reverse(s);static void reverse(String s)String s0;s0=s1;s1=s0;s0=s0;【答案】运行结果为:s0=c s1=ab8.写出下列程序的运行结果。public class StringBufferExample3 public static void main (String args) StringBuffer s1= new StringBuffer (ABStringBuffer s2 = new StringBuffer (CD operate (s1,s2); s2=static void operate(Str
18、ingBuffer x, StringBuffer y) 渗釤呛俨匀谔鱉调硯錦鋇絨钞。 x.append(y);x= y;s1=ABCD s2=CD1.简述异常的含义及作用。2. 简述 Java 异常处理的机制。3. 简述 finally 块的用途,举例说明。4. 简述 throw 和 throws 的用途,以及两者之间的差异。5. 编写一个程序,自定义一个异常,并对其进行处理。 【答案】/ myException.java import java.util.*;class myException extends Exception class UserTrialint score;publi
19、c UserTrial(int a) score=a; void show() throws myException if (score100) throw new myException();score=+ score);class xt060501public static void main(String args) UserTrial trial =new UserTrial(4); Scanner reader=new Scanner(System.in); System.out.println( 请输入分数,并按回车键确认 int x = reader.nextInt();trial.score=x;try trial.show();catch (myExceptio
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1