常用类和数组习题
一、单项选择题
1.SimpleDateFormat类在下面哪个包中?( )
A.java.lang B.java.util
C.java.io D.java.text
2.下列答案正确的是( )。
int[] arr = new int[10];
A.arr[0] 的值为null B.arr[10]的值为0
C.arr[9] 的值为0 D.arr[1]的值为1
3.下面代码得到的结果是( )。
int i = ((int) Math.random() + 3) %2;
A.0 B.1
C.编译错误 D.一个随机值
4.下列哪个语句能引起编译器错误?( )
A.int[] a = {1, 2, 3, 4 };
B.int a[][] ={ 1, 2 }, { 3, 4 };
C.int a[] =new int[4];
D.String a[] ={ "1", "2", "3" };
5.下列程序运行的结果是( )。
int[] x = { 122, 33,55, 678, -987 };
int y = x[0];
for (int i = 1; i <x.length; i++) {
if (x[i] > y)
y = x[i];
}
System.out.println(y);
A.122 B.678
C.没有输出结果 D.编译错误
6.阅读下列代码后,下列选项说法正确的是( )。
public class Test {
int arr[] = new int[10];
public static void main(String args[]) {
System.out.println(arr[1]);
}
}
A.编译时将产生错误
B.编译时正确,运行时将产生错误
C.输出0
D.输出null
7.下列选项中正确的是( )。
public static voidmain(String[] args) {
int index;
char[] array = { "x", "y", "z" };
System.out.println(array[index]);
}
A.编译时将产生错误
B.编译时正确,运行时将产生错误
C.输出"x"
D.输出x
8.下面程序运行的结果是( )。
public class Test {
public static voidmain(String [] args) {
int [] a = new int[5];
modify(a);
System.out.println(a[1]);
}
public static voidmodify(int [] a) {
a[1]++;
}
}
A.0 B.1
C.发生运行时异常 D.编译错误
9.下面正确声明一维数组的是( )。
A.String [10] a ; B.String a[10] ;
C.char a[][5] ; D.String a[];
10.下列数组初始化形式正确的是( )。
A.int t1[][] ={ { 1, 2 },{ 3, 4 },{ 5, 6 } };
B.int t2[][] ={ 1, 2, 3, 4, 5, 6 };
C.int t3[3][2] = { 1, 2, 3, 4, 5, 6 };
D.int t4[][]; t4 = { 1, 2, 3, 4, 5, 6 };
二、简答题
一维数组创建的两种方式是什么?请举例说明。
三、编程题
1.从键盘上输入两个整数,把其中的最大值输出在控制台上。
2.从键盘接收三个double型变量a、b、c,判断方程ax2+ bx + c = 0是否有实数解。如果有实数解则输出其解,否则输出“方程无实数解”。
3.设计一个Test类,要求从键盘上录入5个字符串数据,保存在一个一维数组中,并输出所有包含"a"的元素。
4.有这样一个包含有十个元素的整型数组:
arrayOfInts= { 32, 87, 3, 589, 12, 4076, 2000, 8, 622, 127 };
要求将它们按照从大到小的顺序重新排列并输出。
5.写一个Java程序,要求有如下输出(使用数组)
第0行-> 0 1 23 4
第1行-> 5 6 78 9
第2行-> 10 1112 13 14
第3行-> 15 1617 18 19
第4行-> 20 2122 23 24
6.定义一个类,类中只有一个主方法,在主方法中定义一个大小为50的一维整型数组,数组中存放{ 1, 3, 5, …, 99 },输出这个数组中的所有元素,每输出十个换一行,相邻元素之间用空格隔开。
7.定义一个字符串类型的数组,将字符串类型的元素按照字典次序从前向后排列后,输出数组的元素。
8.在主方法中定义一个大小为6* 6的二维字符型数组,正反对角线上存的是‘*’,其余位置存的是‘#’,输出这个数组中的所有元素,元素之间用空格隔开。
一、单项选择题
1.D 2.C 3.B 4.B 5.B 6.D
7.B 8.A 9.A 10.B
二、简答题
一维数组的创建分为两种:动态创建和静态创建。
①动态创建
数组元素的类型[] 数组名字= new 数组元素的类型[数组元素的个数];
数组元素的类型 数组名字[]= new 数组元素的类型[数组元素的个数];
例如:int boy[] = new int[3];boy[0] = 12;boy[1] = 13; boy[2] = 100;
②静态创建
例如:int[] boy = { 12, 13, 100 };
三、编程题
1.
importjava.util.Scanner;
class ScannerTest {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
long a = reader.nextLong();
long b = reader.nextLong();
long max = Math.max(a, b);
System.out.println("输入的两个整数为"+ a + ", " + b);
System.out.println("其中最大值为"+ max);
}
}
程序的输出结果:
10
50
输入的两个整数为10, 50
其中最大值为50
2.
importjava.util.Scanner;
class Equation {
static void resolve(double a, double b,double c) {
double p = b * b - 4 * a * c;
if (p < 0) {
System.out.println("方程无实数解");
} else {
if (p == 0) {
double x1 = -b / (2 * a);
System.out.println("方程有一个实数解:"+ x1);
} else {
double x1 = (-b + Math.sqrt(p))/ (2 * a);
double x2 = (-b - Math.sqrt(p))/ (2 * a);
System.out.println("方程有两个实数解,分别是:"+
x1 + ", " + x2);
}
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请从键盘输入三个double型的数值:");
double a = s.nextDouble();
double b = s.nextDouble();
double c = s.nextDouble();
resolve(a, b, c);
}
}
程序的输出结果:
(1)请从键盘输入三个double型的数值:
1 2 1
方程有一个实数解:-1.0
(2)请从键盘输入三个double型的数值:
1 -3 2
方程有两个实数解,分别是:2.0, 1.0
(3)请从键盘输入三个double型的数值:
1 2 3
方程无实数解
3.
importjava.util.Scanner;
public class Test {
public static void main(String[] args) {
String str[] = new String[5];
Scanner s = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.println("请输入:");
str[i] = s.next();
}
System.out.println("包含a元素的有:");
for (int i = 0; i < str.length; i++){
if (str[i].indexOf("a") !=-1) {
System.out.print(str[i] + "");
}
}
}
}
程序的输出结果:
请输入:zhang
请输入:wang
请输入:zhao
请输入:li
请输入:wu
包含a元素的有:
zhangwang zhao
4.
public classArraySequent {
public static void main(String args[]) {
int arrayOfInts[] = { 32, 87, 3, 589,12, 4076, 2000, 8, 622, 127 };
for (int i = 0; i <arrayOfInts.length; i++) {
for (int j = i + 1; j <arrayOfInts.length; j++) {
if (arrayOfInts[j] >arrayOfInts[i]) {
int temp = arrayOfInts[i];
arrayOfInts[i] = arrayOfInts[j];
arrayOfInts[j] = temp;
}
}
}
for (int i = 0; i <arrayOfInts.length; i++) {
System.out.print(arrayOfInts[i] +" ");
}
System.out.println();
}
}
程序的输出结果:
4076 2000 622 589 127 87 32 12 8 3
5.
class ArrayTest {
public static void main(String args[]) {
int a[][] = new int[5][5];
int temp = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length;j++) {
a[i][j] = temp;
temp++;
}
for (int i = 0; i < a.length; i++) {
System.out.print("第" + i +"行-> ");
for (int j = 0; j < a[0].length;j++) {
System.out.print(a[i][j]+ " ");
}
System.out.println();
}
}
}
程序的输出结果:
第0行-> 0 1 2 3 4
第1行-> 5 6 7 8 9
第2行-> 10 11 12 1314
第3行-> 15 16 17 1819
第4行-> 20 21 22 23 24
6.
class TestOne {
public static void main(String[] args) {
// 元素太多,适合动态创建
int[] x = new int[50];
// 用i表示数组下标
for (int i = 0; i < 50; i++) {
// 为元素赋值{1,3,5,...,99}
x[i] = 2 * i + 1;
}
// 用cnt来统计所输出元素的个数
int cnt = 0;
for (int i = 0; i < 50; i++) {
// 元素之间用空格隔开
System.out.print(x[i] + "");
// 每输出一个元素个数+1
cnt++;
// 每输出10个数换行
if (cnt % 10 == 0) {
System.out.println();
}
}
}
}
程序的输出结果:
1 3 5 7 9 11 13 1517 19
21 23 25 27 29 3133 35 37 39
41 43 45 47 49 5153 55 57 59
61 63 65 67 69 7173 75 77 79
81 83 85 87 89 91 93 95 97 99
7.
importjava.util.Arrays;
class ArrayTest {
public static void main(String[] args) {
String[] a = { "one","two", "three", "four" };
Arrays.sort(a);
// 输出结果:字典次序
for (int i = 0; i < a.length; i++) {
System.out.println(a[i] + "");
}
}
}
程序的输出结果:
four
one
three
two
8.
class TestStar {
public static void main(String[] args) {
// 创建大小为6*6的二维字符型数组
int n= 6;
char c[][] = newchar[n][n];
for (int i = 0; i <c.length; i++) {
for (int j = 0; j <c[i].length; j++) {
// 为元素赋值正反对角线上存的是"*",其余位置存的是"#"
if (i == j || i + j== n - 1) {
c[i][j] = "*";
} else {
c[i][j] = "#";
}
// 输出元素,元素之间用空格隔开
System.out.print(c[i][j]+ " ");
}
// 换行
System.out.println();
}
}
}
程序的输出结果:
* # # # # *
# * # # * #
# # * * # #
# # * * # #
# * # # * #
* # # # # *
- 上一篇: 数组习题及详解
- 下一篇: Java 数组练习题2018.3.22