java基础代码练习题 java编程代码题库及答案( 二 )


A : 1 B: 2 C : 3 D : 4
14. 已知如下代码:
switch(m){
case 0: System.out.println("Condition 0");
case 1: System.out.println("Condition 1");
case 2: System.out.println("Condition 2");
case 3: System.out.println("Condition 3");break;
default:System.out.println("Other Condition");
}
当m的值为( D )时,输出“Condition 3”
(A)2 (B)0、1 (C)0、1、2 (D)0、1、2、3
15. 下列语句输出的结果是:( C )
public class X3 {
public static void main(String[] args) {
for(int i=0; i10; i++){
if(i==5) break;
System.out.print(i);
}
}
}
A:编译错误; B:1234;C:01234;D:12345;
16. 下列语句输出的结果是:( D )
public class Lx1 {
public static void main(String[] args) {
for(int i=0;i5;i++){
switch(i){
case 0:System.out.print("B");
case 1:System.out.print("e");break;
case 2:System.out.print("g");
case 3:System.out.print("!");break;
case 4:System.out.print("!");break;
default:System.out.print("!");
}
}
}
}
A:Beg!!! B:Beeg! C:Beg! D:Beeg!!!
17. 下面foreach循环的程序输出结果是( D ) 。
public class Lx1{
public static void main(String[] args) {
String s1[]={"欢迎您","3","G","同","学",};
Arrays.sort(s1);
for(String s0:s1)
System.out.print (s0);
}
}
A:欢迎您3G同学 B:3G欢迎您同学 C:同学欢迎您3G D:3G同学欢迎您
18. 阅读以下程序,选择正确的运行结果:( B )
public class Lx1 {
public static void main(String[] args) {
byte d[]="YOUIHE你我他".getBytes();
String s=new String(d,6,2);
System.out.println(s);
}
}
A:HE; B:你; C:我; D:他
19. 设有下列数组定义语句:
int a[][]= {{1, 2}, {3}};
则对此语句的叙述正确的是( D ) 。
A: 定义了一个名为a的一维数组 B: a数组 a[1][1]为0
C: a数组元素的下标为1~3 D: 数组中每个元素的类型都是整数
20. 下列程序输出的结果是:( B )
public class Lx1 {
public static void main(String[] args) {
String a[][] ={{"","","",""},{""},{"",""}};
System.out.println(a[2].length);
}
}
A:1 B:2 C:3 D:4
21. 关于以下程序的说明,正确的是( C )
1. class StaticStuff
2. {
3. static int x=10;
4. static { x+=5;}
5. public static void main(String args[ ])
6. {
7. System.out.println(“x=” + x);
8. }
9. static { x/=3;}
10. }
A、4行与9行不能通过编译,因为缺少方法名和返回类型
B、9行不能通过编译,因为只能有一个静态初始化器
C、编译通过,执行结果为:x=5
D、编译通过,执行结果为:x=3
22. 给出下面代码 , 关于该程序以下哪个说法是正确的?( C )
public class Person{
static int arr[] = new int[5];
public static void main(String a[]) {
for(int i=0;i
System.out.print(arr[0]);
}
}
A、编译时将产生错误 B、编译时正确,运行时将产生错误 C、输出零 D、输出空
23. 下面程序中类ClassDemo中定义了一个静态变量sum,分析程序段的输出结果 。( C )
class ClassDemo {
public static int sum=1;
public ClassDemo() {
sum = sum + 5;}
}
public class ClassDemoTest{
public static void main(String args[]) {
ClassDemo demo1=new ClassDemo();
ClassDemo demo2=new ClassDemo();
System.out.println(demo1.sum);}
}
A: 0 B: 6 C: 11 D: 2
24. 下面关于方法的说法,不正确的是( C ) 。
A: Java中的构造方法名必须和类名相同
B: 方法体是对方法的实现 , 包括变量声明和合法语句

推荐阅读