一、选择题
5.若a和b均是整型变量并已正确赋值,正确的switch语句是( )。D
A) switch(a+b); { … } B) switch( a+b*3.0 ) { … }
C) switch a { … } D) switch ( a%b ) { … }
switch里面只能是char, byte, short, int, Character, Byte, Short, Integer, String, or an enum
14、已知如下类说明:
public class Test {
private float f = 1.0f;
int m = 12;
int n=1;
public static void main(String arg[]) {
Test t = new Test();
// some code…
}
}
some code中如下哪个使用是正确的? AD
A、 t.f B、this.n C、Test.m D、t.n
静态方法里面不能有this
二、简答题
1.解释面向过程和面向对象的关系及各自的优缺点?
面向过程是具体化的,解决问题需要一步一步的实现
面向对象是模型化的,你只需抽象出一个类,需要什么功能直接使用就可以了,不必一步一步的实现,至于这个功能是如何实现的,我们不用管,我们会用就可以了
面向对象的底层其实还是面向过程,它只是把面向过程抽象成类,然后封装,方便我们使用
面向过程的优点是性能好,因为不用类的实例化,缺点是不易维护、不易复用、不易扩展
面向对象的优点是易维护、易复用、易扩展,可以设计出低耦合的系统,缺点是性能差
2.char型变量能存储一个字母,那能不能存储一个汉字呢
在java中,char类型占2个字节,一个Unicode码是16位,也就是说一个Unicode码占2个字节,而汉字和英文字母都是用Unicode编码表示,所以char类型变量可以存储一个中文汉字
三、编程题
1.请给下面Person类中三个属性,并且提供setter和getter方法
public class Person{
private int age;
private String name;
private boolean sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
2.请静态初始化int类型数组,数组元素分别为7,2,1,9,并按从小到大排序
package test;
/*
冒泡排序
*/
public class Sort {
public static void main(String[] args) {
int[] a = { 7, 2, 1, 9 };
//排序
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if(a[j] > a[j + 1]){
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
//打印数组元素
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}