---
title: "Java面试——数组"
author: "fairy_xzc"
date: "2013-09-18T22:22:52Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2013-09-18T22:24:26Z"
id: 1944114
url: "https://agilestyle.iteye.com/blog/1944114"
---

1.下面哪几个选项是合法的数组申明？ABD

A.int\[] array\[];

B.char\[] Chars\_one;

C.int\[10] Int\_one;

D.Student myStudents\[];

E.Student myStudents\[12];

 

2.下面哪些选项声明了一个数组并用5个数对数组进行初始化？BD

A.Array a = new Array(5);

B.int\[] a = {23, 22, 21, 20, 19};

C.int\[] array;

D.int array\[] = new int\[5];

E.int a\[] = new int(5);

F.int\[5] array;

 

3.关于数组元素默认初始化，下面哪些选项是正确的？CD

A.int -&gt; -1

B.String -&gt; ""

C.Person -&gt; null

D.char -&gt; '\\u0000'

E.float -&gt; 0.0  
F.boolean -&gt; true

![](http://dl2.iteye.com/upload/attachment/0089/4637/e2c269ff-249a-3454-a03d-12af95094163.png)  
 

4.针对下面的代码，下面哪个叙述是正确的？C

```
public class TestClass
{
	static int len[] = new int[10];
	
	public static void main(String[] args)
	{
		System.out.println(len[1]);
	}
}
```

A.编译时将发生错误

B.编译时正确但是运行时候出错

C.输出为0

D.输出为null

 

5.给定下面的程序，哪些说法是正确的？E

```
public class MyClass
{
	public static void main(String[] args)
	{
		String[] mychars = {"a", "b", "c", "d"};
		
		if (args.length == 0)
		{
			System.out.println("no result");
		}
		else
		{
			System.out.println(mychars[args.length] + " result");
		}
	}
}
```

A.该代码无法编译

B.不带任何程序参数运行，将会抛出NullPointerException

C.分别带0个参数、3个参数运行该程序，会打印no result和b result

D.分别带0个参数、3个参数运行该程序，会打印no result和c result

E.分别带0个参数、3个参数运行该程序，会打印no result和d result

F.分别带0个参数、3个参数运行该程序，会打印a arguments和b arguments

 

6.编程题：求一个长度为10的整型数组中的最大元素，采用随机赋值的方式并输出各元素的值。

```
public class MaxArray
{
	public static void main(String[] args)
	{
		int[] a = new int[10];
		setValue(a);
		showValue(a);
		System.out.println("\nmax: " + getMaxValue(a));
	}

	public static int getMaxValue(int[] a)
	{
		int max = a[0];

		for (int i = 0; i < a.length; i++)
		{
			if (a[i] > max)
			{
				max = a[i];
			}
		}

		return max;
	}

	public static void setValue(int[] a)
	{
		for (int i = 0; i < a.length; i++)
		{
			a[i] = (int) (Math.random() * 100);
		}
	}

	public static void showValue(int[] a)
	{
		for (int i = 0; i < a.length; i++)
		{
			System.out.print(a[i] + " ");
		}
	}
}
```

 

7.仔细分析下面给出的程序，输出的运行结果是多少？A

![](http://dl2.iteye.com/upload/attachment/0089/4646/61a67929-cfe7-316a-9e85-4e18aa8e9254.png)  
A.yes

B.no

C.编译错误

D.运行时发生异常 

 

8.下面的哪些选项插入到程序的第7行，程序可以编译通过？ABEF

![](http://dl2.iteye.com/upload/attachment/0089/4648/1d4a6b02-e99c-31c2-a751-7c534452f79e.png)  
 

9.下面哪个选项对数组的声明、创建和初始化是合法的？D

![](http://dl2.iteye.com/upload/attachment/0089/4650/e161384c-f70f-3fcd-9320-4b2607e37d96.png)

 

10.根据下面的声明，假设已经初始化了该数组，则哪个表达式会返回数组的大小？C

int\[]\[] array = new int\[5]\[];

A.array\[].length()

B.array.length()

C.array\[2].length

D.array\[0]\[0].length

E.array\[3].size()

F.array.size()  
![](http://dl2.iteye.com/upload/attachment/0089/4652/2e26a6cf-a6c6-361b-a75e-817d9bfc6a09.png)