#Java学习#习题四

其他JAVA学习的内容见:目录

单选题

1.下面关于数组声明和初始化的语句那个有语法错误?

A.int a1[]={3,4,5};
B.String a2[]={"string1","string1","string1"};
C.String a3[]=new String(3);
D.int[][] a4=new int[3][3];

C
A是静态赋值D是动态赋值
[]在int后面和在a1后面都是对的,在里面不能有数值
C应改为:String a3[]=new String[3];
二维数组赋值,右面两个中括号里面,第一个必须不为空,第二个可空可不空

2.list是一个ArrayList的对象,哪个选项的代码填写到//todo delete处,可以在Iterator遍历的过程中正确并安全的删除一个list中保存的对象?( )

        Iterator it = list.iterator();
        int index = 0;
        while (it.hasNext()){ 
              Object obj = it.next(); 
              if (needDelete(obj)) { //needDelete返回boolean,决定是否要删除
                   //todo delete
               } 
              index ++;
        }

A.list.remove(obj);
B.list.remove(index);
C.list.remove(it.next());
D.it.remove();

D

3.下面哪个Set是按照插入顺序排序的?

A.LinkedHashSet
B.HashSet
C.AbstractSet
D.TreeSet

A

4.定义了一维int型数组a[10]后,下面错误的引用是( ) 。

A.a[0]=1
B.a[10]=2
C.a[0]=5*2
D.a[1]=a[2]*a[0]

B,下标为0-9

5.定义了int型二维数组a[6][7]后,数组元素a[3][4]前的数组元素个数为( )

A.24
B.25
C.18
D.17

B

6.使用Iterator时,判断是否存在下一个元素可以使用以下哪个方法?()

A.next()
B.hash()
C.hasPrevious()
D.hasNext()

D

7.在Java中,关于HashMap类的描述,以下选项错误的是

A.HashMap使用键/值得形式保存数据
B.HashMap能够保证其中元素的顺序
C.HashMap允许将null用作键
D.HashMap允许将null用作值

B

8.对于“boolean boo[] = new boolean[3];”,下列哪个叙述是正确的?

A.boo[0]、boo[1]、boo[2]的值都是0
B.boo[0]、boo[1]、boo[2]的值不确定
C.boo[0]、boo[1]、boo[2]的值都是false
D.boo[0]、boo[1]、boo[2]的值都是true

C
int默认0,double默认0.0,boolean类型默认false

9.执行完以下代码int[ ] x = new int[25];后,以下哪项说明是正确的( )

A.x[24]为0
B.x[24]未定义
C.x[25]为0
D.x[0]为空

A

10.以下语句不能通过编译的是( )
A.int[] a={1,2,3,4};
B.int b[];
C.int c[]=new int[3];
D.int d=new int[];

D

填空题

1

请写出以下程序运行结果:

 //环境 JDK 1.5及以上
 public static void main(String args[])
    {
    	Set<Integer> set=new TreeSet<Integer>();
    	List<Integer> list=new ArrayList<Integer>();//向下转型
    	for (int i=-3;i<3;i++)
    	{
    		set.add(i);//自动解包,自动打包机制
    		list.add(i);
    	}
    	for (int i=0;i<3;i++)
    	{
    		set.remove(i);
    		list.remove(i);
    	}
    	System.out.println(set+" "+list);
    }

程序运行的输出结果为

[-3, -2, -1] [-2, 0, 2]

2

给出以下代码:

public enum Main {//枚举类型
  PLUS   { int eval(int x, int y) { return x + y; } },
  MINUS  { int eval(int x, int y) { return x - y; } },
  TIMES  { int eval(int x, int y) { return x * y; } },
  DIVIDE { int eval(int x, int y) { return x / y; } };
  abstract int eval(int x, int y);
  public static void main(String args[]) {
	  	int x = 4;
        int y = 2;
        for (Main op : Main.values())
            System.out.printf("%d %s %d = %d%n", x, op, y, op.eval(x, y));
    }
}

程序运行结果为(一行一空):

4 PLUS 2 = 6

4 MINUS 2 = 2

4 TIMES 2 = 8

4 DIVIDE 2 = 2

3

使用Iterator遍历集合时,首先需要调用( )方法判断是否存在下一个元素,若存在下一个元素,则调用( )方法取出该元素

hasNext()

next()

4

Map集合中存储元素需要调用( )方法,要想根据该集合的键获取对应的值需要调用( )方法.

put()

get()

程序填空题

1

下列给定程序中,函数fun的功能是:计算N×N矩阵的主对角线元素和反向对角线元 素之和,并作为函数值返回。要求先累加主对角线元素中的值,再累加反向对角线 元素中的值。

例如:若N=3,有下列矩阵:
1 2 3
4 5 6
7 8 9

首先累加1、5、9,然后累加3、5、7,函数返回值为30。

public class Main {
	public static void main(String[] args) {
		int  t[][]={{21,2,13,24},{25,16,47,38},{29,11,32,54},{42,21,3,10}};
		System.out.println("The original data:");
		for(int i=0;i<t.length; i++){
			for(int j=0; j<t[i].length; j++)//第一个空
				System.out.printf("%4d",t[i][j]);
			System.out.println();
		}
		System.out.println("The result is:"+fun(t));
	}
	public static int fun(int  t[][]){
		int  sum;
		sum=0;//第二个空
		for(int i=0; i<t.length; i++)
			sum+=t[i][i];//第三个空
		for(int i=0; i<t.length; i++)
			sum+= t[i][t.length-i-1];//第四个空
		return sum;
	}
}

2

以下程序的功能是求一个二维数组中每行的最大值和每行的和。

输入样例
3
1 2 3
6 5 4
7 9 8

输出样例
1 2 3 3 6
6 5 4 6 15
7 9 8 9 24

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);//第一个空
		int n=sc.nextInt();
		int a[][]=new int[n][n];
		int b[]=new int[n];
		int c[]=new int[n];
		for(int i=0;i<a.length;i++){
			for(int j=0;j<a[i].length;j++){//第二个空
				a[i][j]=sc.nextInt();
			}
		}		
		int max,s;
		for(int i=0;i<a.length;i++){
			max=a[i][0];	
			s=0;//第三个空
			for(int j=0;j<a[i].length;j++){
				if(a[i][j]>max){
					max=a[i][j];//第四个空
				}
				s+=a[i][j];
			}
			b[i]=max;
			c[i]=s;
		}
		for(int i=0;i<a.length;i++){
			for(int j=0;j<a[i].length;j++){
				System.out.printf("%3d",a[i][j]);//第五个空
			}
			System.out.printf("%3d%3d",b[i],c[i]);
			System.out.println();
		}
	}
}

函数题

jmu-Java-05集合-List中指定元素的删除

编写以下两个函数

/*以空格(单个或多个)为分隔符,将line中的元素抽取出来,放入一个List*/
public static List<String> convertStringToList(String line) 
/*在list中移除掉与str内容相同的元素*/
public static void remove(List<String> list, String str)

裁判测试程序:

public class Main {

    /*covnertStringToList函数代码*/   
		
    /*remove函数代码*/
		
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            List<String> list = convertStringToList(sc.nextLine());
            System.out.println(list);
            String word = sc.nextLine();
            remove(list,word);
            System.out.println(list);
        }
        sc.close();
    }

}

样例说明:底下展示了4组测试数据。

输入样例

1 2 1 2 1 1 1 2
1
11 1 11 1 11
11
2 2 2
1
1 2 3 4 1 3 1
1

输出样例

[1, 2, 1, 2, 1, 1, 1, 2]
[2, 2, 2]
[11, 1, 11, 1, 11]
[1, 1]
[2, 2, 2]
[2, 2, 2]
[1, 2, 3, 4, 1, 3, 1]
[2, 3, 4, 3]

AC代码

	/*covnertStringToList函数代码*/   
	public static List<String> convertStringToList(String line){
		String[] s = line.split("\\s+");
		List<String> list = new ArrayList<String>();
		int i;
		for(i = 0; i < s.length; i++)
			list.add(s[i]);
		return list;
	}
	
    /*remove函数代码*/
	public static void remove(List<String> list, String str){
		if(!list.contains(str))
			return;
		else
		{
			int i;
			for(i = 0; i < list.size(); i++)
			{
				if(list.get(i).equals(str)){
					list.remove(i);
					i--;
				}
			}
		}
	}

老师代码

	/*covnertStringToList函数代码*/   
	public static List<String> convertStringToList(String line){
		String[] lst = line.split(" ");//可以使用转义字符:\\
		List<String> tmp = new ArrayList<String>();
		for(String a:lst){
			if(!a.equals(""))
				tmp.add(a);
		}
		return tmp;
	}
	
    /*remove函数代码*/
	public static void remove(List<String> list, String str){
		Iterator it = list.iterator();
		while(it.hasNext()){
			String tmp = (String)it.next();
			if(tmp.equals(str)){
				it.remove();
			}
		}
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章