当前位置:文档之家› java 简单练习题(含答案)

java 简单练习题(含答案)

java 简单练习题(含答案)
java 简单练习题(含答案)

下列习题仅为初级入门练习

来自csdn

【程序1】

题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

//这是一个菲波拉契数列问题

public class lianxi01 {

public static void main(String[] args) {

System.out.println("第1个月的兔子对数: 1");

System.out.println("第2个月的兔子对数: 1");

int f1 = 1, f2 = 1, f, M=24;

for(int i=3; i<=M; i++) {

f = f2;

f2 = f1 + f2;

f1 = f;

System.out.println("第" + i +"个月的兔子对数: "+f2);

}

}

}

【程序2】

题目:判断101-200之间有多少个素数,并输出所有素数。

程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

public class lianxi02 {

public static void main(String[] args) {

int count = 0;

for(int i=101; i<200; i+=2) {

boolean b = false;

for(int j=2; j<=Math.sqrt(i); j++)

{

if(i % j == 0) { b = false; break; }

else { b = true; }

}

if(b == true) {count ++;System.out.println(i );}

}

System.out.println( "素数个数是: " + count);

}

}

【程序3】

题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

public class lianxi03 {

public static void main(String[] args) {

int b1, b2, b3;

for(int m=101; m<1000; m++) {

b3 = m / 100;

b2 = m % 100 / 10;

b1 = m % 10;

if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m) {

System.out.println(m+"是一个水仙花数"); }

}

}

}

【程序4】

题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:

(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。

(2)如果n <> k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。

(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

import java.util.*;

public class lianxi04{

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print( "请键入一个正整数: ");

int n = s.nextInt();

int k=2;

System.out.print(n + "=" );

while(k <= n) {

if(k == n) {System.out.println(n);break;}

else if( n % k == 0) {System.out.print(k + "*");n = n / k; }

else k++;

}

}

}

【程序5】

题目:利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

import java.util.*;

public class lianxi05 {

public static void main(String[] args) {

int x;

char grade;

Scanner s = new Scanner(System.in);

System.out.print( "请输入一个成绩: ");

x = s.nextInt();

grade = x >= 90 ? 'A'

: x >= 60 ? 'B'

:'C';

System.out.println("等级为:"+grade);

}

}

【程序6】

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

/**在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。* /

import java.util.*;

public class lianxi06 {

public static void main(String[] args) {

int a ,b,m;

Scanner s = new Scanner(System.in);

System.out.print( "键入一个整数:");

a = s.nextInt();

System.out.print( "再键入一个整数:");

b = s.nextInt();

deff cd = new deff();

m = cd.deff(a,b);

int n = a * b / m;

System.out.println("最大公约数: " + m);

System.out.println("最小公倍数: " + n);

}

}

class deff{

public int deff(int x, int y) {

int t;

if(x < y) {

t = x;

x = y;

y = t;

}

while(y != 0) {

if(x == y) return x;

else {

int k = x % y;

x = y;

y = k;

}

}

return x;

}

}

【程序7】

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

import java.util.*;

public class lianxi07 {

public static void main(String[] args) {

int digital = 0;

int character = 0;

int other = 0;

int blank = 0;

char[] ch = null;

Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

ch = s.toCharArray();

for(int i=0; i

if(ch >= '0' && ch <= '9') {

digital ++;

} else if((ch >= 'a' && ch <= 'z') || ch > 'A' && ch <= 'Z') {

character ++;

} else if(ch == ' ') {

blank ++;

} else {

other ++;

}

}

System.out.println("数字个数: " + digital);

System.out.println("英文字母个数: " + character);

System.out.println("空格个数: " + blank);

System.out.println("其他字符个数:" + other );

}

}

【程序8】

题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

import java.util.*;

public class lianxi08 {

public static void main(String[] args) {

long a , b = 0, sum = 0;

Scanner s = new Scanner(System.in);

System.out.print("输入数字a的值:");

a = s.nextInt();

System.out.print("输入相加的项数:");

int n = s.nextInt();

int i = 0;

while(i < n) {

b = b + a;

sum = sum + b;

a = a * 10;

++ i;

}

System.out.println(sum);

}

}

【程序9】

题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。

public class lianxi09 {

public static void main(String[] args) {

System.out.println("1到1000的完数有:");

for(int i=1; i<1000; i++) {

int t = 0;

for(int j=1; j<= i/2; j++) {

if(i % j == 0) {

t = t + j;

}

}

if(t == i) {

System.out.print(i + " ");

}

}

}

【程序10】

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

public class lianxi10 {

public static void main(String[] args) {

double h = 100,s = 100;

for(int i=1; i<10; i++) {

s = s + h;

h = h / 2;

}

System.out.println("经过路程:" + s);

System.out.println("反弹高度:" + h / 2);

}

}

【程序11】

题目:有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?public class lianxi11 {

public static void main(String[] args) {

int count = 0;

for(int x=1; x<5; x++) {

for(int y=1; y<5; y++) {

for(int z=1; z<5; z++) {

if(x != y && y != z && x != z) {

count ++;

System.out.println(x*100 + y*10 + z );

}

}

}

}

System.out.println("共有" + count + "个三位数");

}

}

【程序12】

题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润,求应发放奖金总数?

import java.util.*;

public class lianxi12 {

public static void main(String[] args) {

double x = 0,y = 0;

System.out.print("输入当月利润(万):");

Scanner s = new Scanner(System.in);

x = s.nextInt();

if(x > 0 && x <= 10) {

y = x * 0.1;

} else if(x > 10 && x <= 20) {

y = 10 * 0.1 + (x - 10) * 0.075;

} else if(x > 20 && x <= 40) {

y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05;

} else if(x > 40 && x <= 60) {

y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03;

} else if(x > 60 && x <= 100) {

y = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (x - 60) * 0.015;

} else if(x > 100) {

y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x - 100) * 0.01;

}

System.out.println("应该提取的奖金是" + y + "万");

}

}

【程序13】

题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

public class lianxi13 {

public static void main(String[] args) {

for(int x =1; x<100000; x++) {

if(Math.sqrt(x+100) % 1 == 0) {

if(Math.sqrt(x+268) % 1 == 0) {

System.out.println(x + "加100是一个完全平方数,再加168又是一个完全平方数");

}

}

}

}

}

/*按题意循环应该从-100开始(整数包括正整数、负整数、零),这样会多一个满足条件的数-99。

但是我看到大部分人解这道题目时都把题中的“整数”理解成正整数,我也就随大流了。*/ 【程序14】

题目:输入某年某月某日,判断这一天是这一年的第几天?

import java.util.*;

public class lianxi14 {

public static void main(String[] args) {

int year, month, day;

int days = 0;

int d = 0;

int e;

input fymd = new input();

do {

e = 0;

System.out.print("输入年:");

year =fymd.input();

System.out.print("输入月:");

month = fymd.input();

System.out.print("输入天:");

day = fymd.input();

if (year < 0 || month < 0 || month > 12 || day < 0 || day > 31) {

System.out.println("输入错误,请重新输入!");

e=1 ;

}

}while( e==1);

for (int i=1; i

switch (i) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

days = 31;

break;

case 4:

case 6:

case 9:

case 11:

days = 30;

break;

case 2:

if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {

days = 29;

} else {

days = 28;

}

break;

}

d += days;

}

System.out.println(year + "-" + month + "-" + day + "是这年的第" + (d+day) + "天。"); }

}

class input{

public int input() {

int value = 0;

Scanner s = new Scanner(System.in);

value = s.nextInt();

return value;

}

}

【程序15】

题目:输入三个整数x,y,z,请把这三个数由小到大输出。

import java.util.*;

public class lianxi15 {

public static void main(String[] args) {

input fnc = new input();

int x=0, y=0, z=0;

System.out.print("输入第一个数字:");

x = fnc.input();

System.out.print("输入第二个数字:");

y = fnc.input();

System.out.print("输入第三个数字:");

z = fnc.input();

if(x > y) {

int t = x;

x = y;

y = t;

}

if(x > z) {

int t = x;

x = z;

z = t;

}

if(y > z) {

int t = y;

y = z;

z = t;

}

System.out.println( "三个数字由小到大排列为:"+x + " " + y + " " + z); }

}

class input{

public int input() {

int value = 0;

Scanner s = new Scanner(System.in);

value = s.nextInt();

return value;

}

}

【程序16】

题目:输出9*9口诀。

public class lianxi16 {

public static void main(String[] args) {

for(int i=1; i<10; i++) {

for(int j=1; j<=i; j++) {

System.out.print(j + "*" + i + "=" + j*i + " " );

if(j*i<10){System.out.print(" ");}

}

System.out.println();

}

}

}

【程序17】

题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。public class lianxi17 {

public static void main(String[] args) {

int x = 1;

for(int i=2; i<=10; i++) {

x = (x+1)*2;

}

System.out.println("猴子第一天摘了" + x + " 个桃子");

}

}

【程序18】

题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

public class lianxi18 {

static char[] m = { 'a', 'b', 'c' };

static char[] n = { 'x', 'y', 'z' };

public static void main(String[] args) {

for (int i = 0; i < m.length; i++) {

for (int j = 0; j < n.length; j++) {

if (m[i] == 'a' && n[j] == 'x') {

continue;

} else if (m[i] == 'a' && n[j] == 'y') {

continue;

} else if ((m[i] == 'c' && n[j] == 'x')

|| (m[i] == 'c' && n[j] == 'z')) {

continue;

} else if ((m[i] == 'b' && n[j] == 'z')

|| (m[i] == 'b' && n[j] == 'y')) {

continue;

} else

System.out.println(m[i] + " vs " + n[j]);

}

}

}

}

【程序19】

题目:打印出如下图案(菱形)

*

***

*****

*******

*****

***

*

public class lianxi19 {

public static void main(String[] args) {

int H = 7, W = 7;//高和宽必须是相等的奇数

for(int i=0; i<(H+1) / 2; i++) {

for(int j=0; j

System.out.print(" ");

}

for(int k=1; k<(i+1)*2; k++) {

System.out.print('*');

}

System.out.println();

}

for(int i=1; i<=H/2; i++) {

for(int j=1; j<=i; j++) {

System.out.print(" ");

}

for(int k=1; k<=W-2*i; k++) {

System.out.print('*');

}

System.out.println();

}

}

}

【程序20】

题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。public class lianxi20 {

public static void main(String[] args) {

int x = 2, y = 1, t;

double sum = 0;

for(int i=1; i<=20; i++) {

sum = sum + (double)x / y;

t = y;

y = x;

x = y + t;

}

System.out.println("前20项相加之和是:" + sum);

}

}

【程序21】

题目:求1+2!+3!+...+20!的和

public class lianxi21 {

public static void main(String[] args) {

long sum = 0;

long fac = 1;

for(int i=1; i<=20; i++) {

fac = fac * i;

sum += fac;

}

System.out.println(sum);

}

}

【程序22】

题目:利用递归方法求5!。

public class lianxi22 {

public static void main(String[] args) {

int n = 5;

rec fr = new rec();

System.out.println(n+"! = "+fr.rec(n));

}

}

class rec{

public long rec(int n) {

long value = 0 ;

if(n ==1 ) {

value = 1;

} else {

value = n * rec(n-1);

}

return value;

}

}

【程序23】

题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?

public class lianxi23 {

public static void main(String[] args) {

int age = 10;

for(int i=2; i<=5; i++) {

age =age+2;

}

System.out.println(age);

}

}

【程序24】

题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

//使用了长整型最多输入18位

import java.util.*;

public class lianxi24 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("请输入一个正整数:");

long a = s.nextLong();

String ss = Long.toString(a);

char[] ch = ss.toCharArray();

int j=ch.length;

System.out.println(a + "是一个"+ j +"位数。");

System.out.print("按逆序输出是:");

for(int i=j-1; i>=0; i--) {

System.out.print(ch[i]);

}

}

}

【程序25】

题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

import java.util.*;

public class lianxi25 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

int a;

do{

System.out.print("请输入一个5位正整数:");

a = s.nextInt();

}while(a<10000||a>99999);

String ss =String.valueOf(a);

char[] ch = ss.toCharArray();

if(ch[0]==ch[4]&&ch[1]==ch[3]){

System.out.println("这是一个回文数");}

else {System.out.println("这不是一个回文数");}

}

}

//这个更好,不限位数

import java.util.*;

public class lianxi25a {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

boolean is =true;

System.out.print("请输入一个正整数:");

long a = s.nextLong();

String ss = Long.toString(a);

char[] ch = ss.toCharArray();

int j=ch.length;

for(int i=0; i

if(ch[i]!=ch[j-i-1]){is=false;}

}

if(is==true){System.out.println("这是一个回文数");}

else {System.out.println("这不是一个回文数");}

}

}

【程序26】

题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

import java.util.*;

public class lianxi26 {

public static void main(String[] args) {

getChar tw = new getChar();

System.out.println("请输入星期的第一个大写字母:");

char ch = tw.getChar();

switch(ch) {

case 'M':

System.out.println("Monday");

break;

case 'W':

System.out.println("Wednesday");

break;

case 'F':

System.out.println("Friday");

break;

case 'T': {

System.out.println("请输入星期的第二个字母:");

char ch2 = tw.getChar();

if(ch2 == 'U') {System.out.println("Tuesday"); }

else if(ch2 == 'H') {System.out.println("Thursday"); } else {System.out.println("无此写法!");

}

};

break;

case 'S': {

System.out.println("请输入星期的第二个字母:");

char ch2 = tw.getChar();

if(ch2 == 'U') {System.out.println("Sunday"); }

else if(ch2 == 'A') {System.out.println("Saturday"); } else {System.out.println("无此写法!");

}

};

break;

default:System.out.println("无此写法!");

}

}

}

class getChar{

public char getChar() {

Scanner s = new Scanner(System.in);

String str = s.nextLine();

char ch = str.charAt(0);

if(ch<'A' || ch>'Z') {

System.out.println("输入错误,请重新输入");

ch=getChar();

}

return ch;

}

}

【程序27】

题目:求100之内的素数

//使用除sqrt(n)的方法求出的素数不包括2和3

public class lianxi27 {

public static void main(String[] args) {

boolean b =false;

System.out.print(2 + " ");

System.out.print(3 + " ");

for(int i=3; i<100; i+=2) {

for(int j=2; j<=Math.sqrt(i); j++) {

if(i % j == 0) {b = false;

break;

} else{b = true;}

}

if(b == true) {System.out.print(i + " ");}

}

}

}

//该程序使用除1位素数得2位方法,运行效率高通用性差。public class lianxi27a {

public static void main(String[] args) {

int[] a = new int[]{2, 3, 5, 7};

for(int j=0; j<4; j++)System.out.print(a[j] + " "); boolean b =false;

for(int i=11; i<100; i+=2) {

for(int j=0; j<4; j++) {

if(i % a[j] == 0) {b = false;

break;

} else{b = true;}

}

if(b == true) {System.out.print(i + " ");}

}

}

}

【程序28】

题目:对10个数进行排序

import java.util.*;

public class lianxi28 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

int[] a = new int[10];

System.out.println("请输入10个整数:");

for(int i=0; i<10; i++) {

a[i] = s.nextInt();

}

for(int i=0; i<10; i++) {

for(int j=i+1; j<10; j++) {

if(a[i] > a[j]) {

int t = a[i];

a[i] = a[j];

a[j] = t;

}

}

}

for(int i=0; i<10; i++) {

System.out.print(a[i] + " ");

}

}

}

【程序29】

题目:求一个3*3矩阵对角线元素之和

import java.util.*;

public class lianxi29 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

int[][] a = new int[3][3];

System.out.println("请输入9个整数:");

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++) {

a[i][j] = s.nextInt();

}

}

System.out.println("输入的3 * 3 矩阵是:");

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++) {

System.out.print(a[i][j] + " ");

}

System.out.println();

}

int sum = 0;

for(int i=0; i<3; i++) {

for(int j=0; j<3; j++) {

if(i == j) {

sum += a[i][j];

}

}

}

System.out.println("对角线之和是:" + sum);

}

}

【程序30】

题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。//此程序不好,没有使用折半查找插入

import java.util.*;

public class lianxi30 {

public static void main(String[] args) {

int[] a = new int[]{1, 2, 6, 14, 25, 36, 37,55};

int[] b = new int[a.length+1];

int t1 =0, t2 = 0;

int i =0;

Scanner s= new Scanner(System.in);

System.out.print("请输入一个整数:");

int num = s.nextInt();

if(num >= a[a.length-1]) {

b[b.length-1] = num;

for(i=0; i

b[i] = a[i];

}

} else {

for(i=0; i

if(num >= a[i]) {

b[i] = a[i];

} else {

b[i] = num;

break;

}

}

for(int j=i+1; j

b[j] = a[j-1];

}

}

for (i = 0; i < b.length; i++) {

System.out.print(b[i] + " ");

}

}

}

【程序31】

题目:将一个数组逆序输出。

import java.util.*;

public class lianxi31 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

int a[] = new int[20];

System.out.println("请输入多个正整数(输入-1表示结束):"); int i=0,j;

do{

a[i]=s.nextInt();

i++;

}while (a[i-1]!=-1);

System.out.println("你输入的数组为:");

for( j=0; j

System.out.print(a[j]+" ");

}

System.out.println("\n数组逆序输出为:");

for( j=i-2; j>=0; j=j-1) {

System.out.print(a[j]+" ");

}

}

}

【程序32】

题目:取一个整数a从右端开始的4~7位。

import java.util.*;

public class lianxi32 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("请输入一个7位以上的正整数:");

long a = s.nextLong();

String ss = Long.toString(a);

char[] ch = ss.toCharArray();

int j=ch.length;

if (j<7){System.out.println("输入错误!");}

else {

System.out.println("截取从右端开始的4~7位是:"+ch[j-7]+ch[j-6]+ch[j-5]+ch[j-4]);

}

}

}

【程序33】

题目:打印出杨辉三角形(要求打印出10行如下图)

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

…………

public class lianxi33 {

public static void main(String[] args) {

int[][] a = new int[10][10];

for(int i=0; i<10; i++) {

a[i][i] = 1;

a[i][0] = 1;

}

for(int i=2; i<10; i++) {

for(int j=1; j

a[i][j] = a[i-1][j-1] + a[i-1][j];

}

}

for(int i=0; i<10; i++) {

for(int k=0; k<2*(10-i)-1; k++) {

System.out.print(" ");

}

for(int j=0; j<=i; j++) {

System.out.print(a[i][j] + " ");

}

System.out.println();

}

}

}

【程序34】

题目:输入3个数a,b,c,按大小顺序输出。

import java.util.Scanner;

public class lianxi34 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.println("请输入3个整数:");

int a = s.nextInt();

int b = s.nextInt();

int c = s.nextInt();

if(a < b) {

int t = a;

a = b;

b = t;

}

if(a < c) {

int t = a;

a = c;

c = t;

}

if(b < c) {

int t = b;

b = c;

c = t;

}

System.out.println("从大到小的顺序输出:");

System.out.println(a + " " + b + " " + c);

}

}

【程序35】

题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。import java.util.*;

public class lianxi35 {

java基础笔试测试题与答案

Java 一章至五章考试 一. 填空题(8 分) 1. 面向对象的三大原则是( 封装),( 继承) 和( 多态).2 分 2. 如果想在对象实例化的同时就初始化成员属性,则使用( 构造函数).2 分 3. ( 实体) 方法和( 构造) 方法不能修饰为abstract ?2分 二.选择题(60 分) 1) 在Java 语言中,下列(a,d )是不满足命名规范的变量名。(选择二项) a) 姓名 b) $Name c) _instanceof d) instanceof 2) 下列Java 代码片段的输出结果是( a ) 。 char c='a'; int i=c; float f=i; byte b=(byte)c; System.out.println(c+","+i+","+f+","+b); a) 编译错误 b) a,97,97,97 c) a,97,97.0,97 d) a,97,97.0f,97 3) 下列Java 代码中,空白处的代码是(b,c )。( 选择两项) public interface Fee{ public float calLabFee(float unitPrice, float time); } public class FeeImpl implements Fee { public float calLabFee(float unitPrice, float time){ return unitPrice * time; } } public class FeeInterfaceTest { public static void main(String[] args){ ________________ Float labFee = fee.calLabFee(400.00,5); } }

java期末考试试题及答案

1.谈谈final, finally, finalize的区别。 final关键字: a) 如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承。因此一个类不能既被声明为abstract的,又被声明为final的。 b) 将变量或方法声明为final,可以保证它们在使用中不被改变。 c) 被声明为final的变量必须在声明时给定初值,而在以后的引用中只能读取,不可修改。 d) 被声明为final的方法也同样只能使用,不能重载。 finally关键字:在异常处理时提供finally 块来执行任何清除操作。如果抛出一个异常,那么相匹配的catch 子句就会执行,然后控制就会进入finally 块。 finalize:方法名,不是关键字。Java技术允许使用finalize() 方法在垃圾收集器将对象从内存中清除出去之前做必要的清理工作。这个方法是由垃圾收集器在确定这个对象没有被引用时对这个对象调用的。它是在Object 类中定义的,因此所有的类都继承了它。子类覆盖finalize() 方法以整理系统资源或者执行其他清理工作。finalize()方法是在垃圾收集器删除对象之前对这个对象调用的。 2.GC是什么? 为什么要有GC? GC是垃圾收集器。Java 程序员不用担心内存管理,因为垃圾收集器会自动进行管理。要请求垃圾收集,可以调用下面的方法之一: System.gc() Runtime.getRuntime().gc() 3.Math.round(11.5)等於多少? Math.round(-11.5)等於多少? 写程序Math.round(11.5) = 12 Math.round(-11.5) = -11 4.给我一个你最常见到的runtime exception ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DOMException, EmptyStackException, IllegalArgumentException, IllegalMonitorStateException, IllegalPathStateException, IllegalStateException, ImagingOpException, IndexOutOfBoundsException, MissingResourceException, NegativeArraySizeException, NoSuchElementException, NullPointerException, ProfileDataException, ProviderException, RasterFormatException, SecurityException, SystemException, UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException

Java笔试题及答案

Java笔试题及答案 一、单项选择题 1.Java是从()语言改进重新设计。 A.Ada B.C++ C.Pasacal D.BASIC 答案:B 2.下列语句哪一个正确() A. Java程序经编译后会产生machine code B. Java程序经编译后会产生byte code C. Java程序经编译后会产生DLL D.以上都不正确 答案:B 3.下列说法正确的有() A. class中的constructor不可省略 B. constructor必须与class同名,但方法不能与class同名 C. constructor在一个对象被new时执行 D.一个class只能定义一个constructor 答案:C 详解:见下面代码,很明显方法是可以和类名同名的,和构造方法唯一的区别就是,构造方法没有返回值。 package net.study; public class TestConStructor { public TestConStructor() {

} public void TestConStructor() { } public static void main(String[] args) { TestConStructor testConStructor = new TestConStructor(); testConStructor.TestConStructor(); } } 4.提供Java存取数据库能力的包是() 答案:A 5.下列运算符合法的是() A.&& B.<> C.if D.:= 答案:A 详解: java 中没有<> := 这种运算符,if else不算运算符 6.执行如下程序代码 a=0;c=0; do{ --c; a=a-1; }while(a>0); 后,C的值是()

JAVA复习题库及答案

第一题单项选择题 1、在下列说法中,选出最正确的一项是(A )。 A、Java 语言是以类为程序的基本单位的 B、Java 语言是不区分大小写的 C、多行注释语句必须以//开始 D、在Java 语言中,类的源文件名和该类名可以不相同 2、下列选项中不属于Java 虚拟机的执行特点的一项是(D )。 A、异常处理 B、多线程 C、动态链接 D、简单易学 3、下列选项中,属丁JVM 执行过程中的特点的一项是( C )。 A、编译执行 B、多进程 C、异常处理 D、静态链接 4、在Java 语言中,那一个是最基本的元素?( C ) A、方法 B、包 C、对象 D、接口 5、如果有2 个类A 和B,A 类基于B 类,则下列描述中正确的一个是( B )。 A、这2 个类都是子类或者超类 B、A 是B 超类的子类 C、B 是A 超类的子类 D、这2 个类郡是对方的子类 6、使用如下哪个保留字可以使只有在定义该类的包中的其他类才能访问该类?(D ) A、abstract B、private (本类) C、protected(本包及其他包的子类) D、不使用保留字 7、编译一个定义了3 个类的Java 源文件后,会产生多少个字符码文件,扩展名是什么?(D ) A、13 个字节码文件,扩展名是.class B、1 个字节码文件,扩展名是.class C、3 个字节码文件,扩展名是.java D、3 个字节码文件,扩展名是.class 8、下列关于Java 程序结构的描述中,不正确的一项是( C )。 A、一个Java 源文件中可以包括一个package 语句 B、一个Java 源文件中可以包括多个类定义,但是只能有一个public 类 C、一个Java 源文件中可以有多个public 类 D、源文件名与程序类名必须一致 9、下列说法正确的一项是( C )。 A、java.1ang.Integer 是接口 B、java.1ang.Runnable 是类 C、Doulble 对象在iava.1ang 包中 D、Double 对象在java.1ang.Object 包中 10、以下关于面向对象概念的描述中,不正确的一项是( B )。 A、在现实生活中,对象是指客观世界的实体

JAVA期末试题及答案

Java 程序设计》课程试卷 1.使用 Java 语言编写的源程序保存时的文件扩展名是( )。 (A ) .class ( B ) .java C ) .cpp ( D ) .txt 2.设 int a=-2 ,则表达式 a>>>3 的值为( )。 (A ) 0 (B )3 (C ) 8 (D )-1 3.设有数组的定义 int[] a = new int[3] ,则下面对数组元素的引用错误的是( ) ( A )a[0]; ( B ) a[a.length-1]; (C )a[3]; (D )int i=1 ; a[i]; 4.在类的定义中可以有两个同名函数,这种现象称为函数( )。 (A )封装 (B )继承 (C )覆盖 (D )重载 5.在类的定义中构造函数的作用是( )。 (A )保护成员变量 (B )读取类的成员变量 (C )描述类的特征 (D )初始化成员变量 6.下面关键字中,哪一个不是用于异常处理语句( )。 ( A ) try ( B ) break ( C ) catch ( D ) finally 7.类与对象的关系是( )。 (A )类是对象的抽象 (B )对象是类的抽象 15. Java 语言使用的字符码集是 (A) ASCII (B) BCD (C) DCB 16. 如果一个类的成员变量 (A) public (B) (C 对象是类的子类 (D )类是对象的具体实例 )。 8.下面哪一个是 Java 中不合法的标识符( ( A )$persons ( B ) twoNum ( C )_myVar ( D )*point 9.为 AB 类的一个无形式参数无返回值的方法 ( ) 。 ( A ) static void method( ) ( B ) public void method( ) ( C ) final void method( ) ( D ) abstract void method( ) 10.欲构造 ArrayList 类的一个实例,此类继承了 ( A ) ArrayList myList=new Object( ) ( B ) List myList=new ArrayList( ) ( C ) ArrayList myList=new List( ) ( D ) List myList=new List( ) 11. Java 源文件和编译后的文件扩展名分别为( (A) .class 和 .java (C).class 和 .class 12. 在 Java Applet 程序用户自定义的 (A) start( ) (B) stop( ) (C) init( ) 13. 对于一个 Java 源文件, (A) package,import,class (C) import,package,class 14. 下面哪个是非法的: (A) int I = 32; (C) double d = 45.0; method 书写方法头,使得使用类名 List 接口,下列哪个方法是正确的( ) ( B).java 和 .class (D) .java 和 .java Applet 子类中,一般需要重载父类的 (D) paint( ) import, class (B) class,import,package (D) package,class,import ( ) 定义以及 package 正确的顺序是: (B) float f = 45.0; (D) char c = // 符号错 AB 作为前缀就可以调用它,该方法头的形式为 方法来完成一些画图操作。 (D) Unicode 只能 在所在类中使用 则该成员变量必须使用的修饰是

java基础测试题及答案

一、选择题(每题2分,共40分) 1、下面哪个是Java语言中正确的标识符( C ) A、3com B、import C、that D、this 2、下面哪个语句(初始化数组)是不正确的:( B) A.int x[] = {1,2,3}; B.int x[3] = {1,2,3}; C.int[] x = {1,2,3}; D.int x[] = new int[]{1,2,3}; 3、下述概念中不属于面向对象方法的是( D )。 A.对象、消息 B.继承、多态 C.类、封装 D.过程调用 4、下面的代码段中,执行之后i 和j 的值是什么? ( B ) int i = 1; int j; j = i++*2+3*--i; A.1, 2 B.1, 5 C. 2, 1 D. 2, 2 5、下面哪条语句把方法声明为抽象的公共方法?( B ) A.public abstract method(); B.public abstract void method(); C.public abstract void method(){} D.public void method() extends abstract; 6、下面关于java中类的说法哪个是不正确的?( C ) A.类体中只能有变量定义和成员方法的定义,不能有其他语句。 B.构造函数是类中的特殊方法。 C.类一定要声明为public的,才可以执行。 D.一个java文件中可以有多个class定义。 7、假设A类有如下定义,设a是A类的一个实例,下列语句调用哪个是错误的?( C ) class A { int i; static String s; void method1() { } static void method2() { } } A、; B、(); C、(); D、() 8、容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小 的变化而改变? ( B ) A、 CardLayout B、 FlowLayout C、 BorderLayout D、 GridLayout 9、下列哪个用户图形界面组件在软件安装程序中是常见的? ( C ) A.滑块 B.进度条 C.按钮 D.标签

java期末考试复习题及答案(1)

《Java程序设计》课程试卷 1.使用Java语言编写的源程序保存时的文件扩展名是( B )。 (A).class (B).java (C).cpp (D).txt 2.设int a=-2,则表达式a>>>3的值为( C )。 (A)0 (B)3 (C)8 (D)-1 3.设有数组的定义int[] a = new int[3],则下面对数组元素的引用错误的是( C )。 (A)a[0]; (B)a[]; (C)a[3]; (D)int i=1; a[i]; 4.在类的定义中可以有两个同名函数,这种现象称为函数( D )。 (A)封装(B)继承(C)覆盖(D)重载 5.在类的定义中构造函数的作用是( D )。 (A)保护成员变量(B)读取类的成员变量(C)描述类的特征(D)初始化成员变量 6.下面关键字中,哪一个不是用于异常处理语句( B )。 (A)try (B)break (C)catch (D)finally 7.类与对象的关系是( A )。 (A)类是对象的抽象(B)对象是类的抽象(C)对象是类的子类(D)类是对象的具体实例 8.下面哪一个是Java中不合法的标识符( D )。 (A)$persons (B)twoNum (C)_myVar (D)*point 9.为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为( A )。 (A)static void method( ) (B)public void method( ) (C)final void method( ) (D)abstract void method( ) 10.欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的( C )。 (A)ArrayList myList=new Object( ) (B)List myList=new ArrayList( ) (C)ArrayList myList=new List( ) (D)List myList=new List( ) 源文件和编译后的文件扩展名分别为( B ) (A) .class和 .java (B).java和 .class (C).class和 .class (D) .java和 .java 12.在Java Applet程序用户自定义的Applet子类中,一般需要重载父类的( D )方法来完成一些画图操作。 (A) start( ) (B) stop( ) (C) init( ) (D) paint( ) 13.对于一个Java源文件,import, class定义以及package正确的顺序是: ( A ) (A) package,import,class (B) class,import,package (C) import,package,class (D) package,class,import 14.下面哪个是非法的:( D ) (A) int I = 32; (B) float f = ; (C) double d = ; (D) char c = ‘u’;如果一个类的成员变量只能在所在类中使用,则该成员变量必须使用的修饰是( C ) (A) public (B) protected (C) private (D) static 17.下面关于main方法说明正确的是( B ) (A) public main(String args[ ]) (B) public static void main(String args[ ]) (C) private static void main(String args[ ]) (D) void main() 18.哪个关键字可以对对象加互斥锁( B ) (A) transient (B) synchronized (C) serialize (D) static 19.关于抽象方法的说法正确的是( D ) (A)可以有方法体 (B) 可以出现在非抽象类中 (C) 是没有方法体的方法(D) 抽象类中的方法都是抽象方法 包的File类是( B ) (A)字符流类(B) 字节流类 (C) 对象流类 (D) 非流类 21.Java application中的主类需包含main方法,以下哪项是main方法的正确形参( B ) A、 String args B、String args[] C、Char arg D、StringBuffer args[] 22.以下代码段执行后的输出结果为( A ) i nt x=-3; int y=-10; 、-1B、2 C、1 D、3 23.以下关于继承的叙述正确的是()。

java基础考试题及答案

新员工考试 一、选择题(共30题,每题 2 分) 1. 下面哪些是合法的标识符?(多选题) A. $persons B. TwoUsers C. *point D. this E. _endline 答案A,B,E 分析Java 的标识符可以以一个Unicode 字符,下滑线(_),美元符($)开始,后续字符可以是前面的符号和数字,没有长度限制,大小写敏感,不能是保留字(this 保留字)。 2. 哪些是将一个十六进制值赋值给一个long 型变量?(单选题) A. long number = 345L; B. long number = 0345; C. long number = 0345L; D. long number = 0x345L 答案D 分析十六进制数以Ox开头,Io ng型数以L (大小写均可,一般使用大写,因为小写的 l 和数字1 不易区分)。 3. 下面的哪些程序片断可能导致错误? (多选题) A. String s = "Gone with the wind"; String t = " good "; String k = s + t; B. String s = "Gone with the wind"; String t; t = s[3] + "one"; C. String s = "Gone with the wind"; String standard = s.toUpperCase(); D. String s = "home directory"; String t = s - "directory"; 答案B,D 分析 A:String 类型可以直接使用+进行连接运算。 B:String 是一种Object ,而不是简单的字符数组,不能使用下标运算符取其值的某个元 素,错误。 C:toUpperCase()方法是String 对象的一个方法,作用是将字符串的内容全部转换为大写并返回转换后的结果(String 类型)。 D:String 类型不能进行减(- )运算,错误。 4. point x 处的哪些声明是句法上合法的? (多选题) cIass Person { private int a; pubIic int change(int m){ return m; } } pubIic cIass Teacher extends Person { public int b;

Java基础练习题-附答案

Java基础练习题附答案 一、简单Java程序调试 1)以下哪个是Java应用程序main方法的有效定义 A. public static void main(); B. public static void main( String args ); C. public static void main( String args[] ); D. public static void main( Graphics g ); 【 E. public static boolean main( String a[] ); 2) 编译和运行以下代码的结果为: public class MyMain{ public static void main(String argv){ "Hello cruel world"); } } A.编译错误; ~ B.运行输出 "Hello cruel world"; C.编译无错,但运行时指示没有定义构造方法。 D.编译无错,但运行时指示没有正确定义main方法。3)下列选项中不属于Java虚拟机的执行特点的一项是:A.异常处理 B.多线程 C.动态链接 D.简单易学4)不属于Java语言特点的一项是: A.分布式 B. 安全性 C. 编译执行 D.面向对象5)以下程序的运行结果为: ; public class Test{ public static void main(String argv[ ]){ "x="+5); } } A. 5 B. x=5 C. "x="+5 D. "x="5

6) 以下程序的运行结果为: public class Test{ ` public static void main(String argv[ ]){ "good"+"morning"); } } A. goodmorning B. "good"+"morning" C. good morning D. good+morning 二、Java符号与表达式 1) 现有一个int类型的整数和一个double类型的数进行加法运算,则得到的结果类型为: , A.int类型 B. double类型 C. float类型 D. long类型 2)下面程序段的输出结果是: int a = 2; a++); a); A.333 B.334 C.234 D.233 3) 以下代码的输出结果 public class Test{ ] int x=3; public static void main(String argv[]){ int x= 012; } } A.12 B.012 C.10 D.3 4) 下列定义语句正确的是: A.char c="/n"; B.int i=12; C.float f=; D.boolean b=null; … 5)检查如下代码: public class Quiz2_l{ public static void main(String[] args) { int a = 8;

java笔试题及答案.doc

java笔试题及答案 有了下面java笔试题及答案,进行java笔试时就容易多了,请您对下文进行参考: 1、作用域public,private,protected,以及不写时的区别 答:区别如下: 作用域当前类同一package子孙类其他package public 7 7 7 7 protected 7 7 7 X friendly 7 7 X X private 7 X X X 不写时默认为friendly 2、Anonymouslnner Class (匿名内部类)是否可以exte nd s (继承)其它类,是否可以imple ment s (实现)i nterf ace (接口) 答:匿名的内部类是没有名字的内部类。不能exte n ds (继承)其它类,但一个内部类可以作为一个接口,由另一个内部类实现 3、Sta ti cNestedC las s 和Inner Clas s 的不同答: Nes tedC lass (一般是C+ +的说法),In ne rClass (—般是JAVA的说法)。J ava内部类与C++嵌套类最大的不同就在于是否有指向外部的引用上。注:静态内部类(I

nn erClass)意味着1创建一个st atic内部类的对象,不需要一个外部类对象,2不能从一个st atic内部类的一个对象访问一个外部类对象 4、和的区别 答:是位运算符,表示按位与运算,是逻辑运算符,表示遷辑与(and ) 5、Coll ect ion 和Col lect ions 的区别 答:Coll ect ion是集合类的上级接口,继承与他的接口主要有Set和List. Col lections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作 6、什么时候用assert 答:asserti on (断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制。在实现中,a ssertion 就是在程序中的一条语句,它对一个boolea n表 达式进行检查,一个正确程序必须保证这个bool ean表达 式的值为tr ue;如果该值为fal se,说明程序己经处于不正确的状态下,系统将给出警告或退出。一般来说,

java笔试题含答案

班级:_______________ 学号:______________ 姓名:___________ Java 笔试题 (可多选) 1. 下面哪些是Thread类的方法( ABD) A start() B run() C exit() D getPriority() 2. 下面关于类的说法正确的是(A) A 继承自Throwable B Serialable C 该类实现了Throwable 接口 D 该类是一个公共类 3. 下面程序的运行结果是( false ) String str1 = "hello"; String str2 = "he" + new String("llo"); == str2); 4. 下列说法正确的有( C) A. class中的constructor不可省略

B. constructor必须与class同名,但方法不能与class同名C. constructor在一个对象被new时执行 D.一个class只能定义一个constructor 5. 指针在任何情况下都可进行>, <, >=, <=, ==运算( true ) 6. 下面程序的运行结果:(B) public static void main(String args[]) { Thread t = new Thread() { public void run() { pong(); } }; (); "ping"); } static void pong() { "pong"); } A pingpong

B pongping C pingpong和pongping都有可能 D 都不输出 7. 下列属于关系型数据库的是(AB) A. Oracle B MySql C IMS D MongoDB 8. GC(垃圾回收器)线程是否为守护线程( true ) 9. volatile关键字是否能保证线程安全( false ) 10. 下列说法正确的是(AC) A LinkedList继承自List B AbstractSet继承自Set C HashSet继承自AbstractSet D WeakMap继承自HashMap 11. 存在使i + 1 < i的数吗(存在) 12. 的数据类型是(B) A float B double C Float D Double

JAVA练习题含答案-answer to pratice 3

Chapter 3 Flow of Control Multiple Choice 1)An if selection statement executes if and only if: (a)the Boolean condition evaluates to false. (b)the Boolean condition evaluates to true. (c)the Boolean condition is short-circuited. (d)none of the above. Answer:B (see page 97) 2) A compound statement is enclosed between: (a)[ ] (b){ } (c)( ) (d)< > Answer:B (see page 98) 3) A multi-way if-else statement (a)allows you to choose one course of action. (b)always executes the else statement. (c)allows you to choose among alternative courses of action. (d)executes all Boolean conditions that evaluate to true. Answer:C (see page 100) 4)The controlling expression for a switch statement includes all of the following types except: (a)char (b)int (c)byte (d)double Answer:D (see page 104) Copyright ? 2006 Pearson Education Addison-Wesley. All rights reserved. 1

Java经典面试题大全_带答案

Java经典面试题带答案一、单项选择题 1.Java是从()语言改进重新设计。 A.Ada B.C++ C.Pasacal D.BASIC 答案:B 2.下列语句哪一个正确() A.Java程序经编译后会产生machine code B.Java程序经编译后会产生byte code(字节码) C.Java程序经编译后会产生DLL D.以上都不正确 答案:B 3.下列说法正确的有() A.class中的constructor不可省略 B.constructor必须与class同名,但方法不能与class同名C.constructor在一个对象被new时执行(构造器) D.一个class只能定义一个constructor 答案:C 4.提供Java存取数据库能力的包是() A.Java.sql /sql/数据库还有Oracle 也是一种数据库 B.java.awt C.https://www.doczj.com/doc/ef5114510.html,ng D.java.swing 答案:A 5.下列运算符合法的是() A.&& B.<> C.if D.:= 答案:A 6.执行如下程序代码 a=0;c=0; do{ --c; a=a-1; }while(a>0); 后,C的值是() A.0 B.1 C.-1 D.死循环

答案:C 7.下列哪一种叙述是正确的() A.abstract修饰符可修饰字段、方法和类 B.抽象方法的body部分必须用一对大括号{}包住 C.声明抽象方法,大括号可有可无 D.声明抽象方法不可写出大括号 答案:D 8.下列语句正确的是() A.形式参数可被视为localvariable B.形式参数可被字段修饰符修饰 C.形式参数为方法被调用时,真正被传递的参数 D.形式参数不可以是对象 答案:A 9.下列哪种说法是正确的() A.实例方法可直接调用超类的实例方法 B.实例方法可直接调用超类的类方法 C.实例方法可直接调用其他类的实例方法 D.实例方法可直接调用本类的类方法 答案:D 二、多项选择题 1.Java程序的种类有() A.类(Class) B.Applet C.Application D.Servlet 2.下列说法正确的有() A.环境变量可在编译sourcecode时指定 B.在编译程序时,所能指定的环境变量不包括class path C.javac一次可同时编译数个Java源文件 D.javac.exe能指定编译结果要置于哪个目录(directory)答案:BCD 3.下列标识符不合法的有() A.new B.$Usdollars C.1234 D.car.taxi 答案:ACD 4.下列说法错误的有() A.数组是一种对象 B.数组属于一种原生类 C.intnumber=[]={31,23,33,43,35,63} D.数组的大小可以任意改变 答案:BCD 5.不能用来修饰interface的有()

java考试试卷及答案

JA V A考试试卷及答案 选择题 3、在Java Applet程序用户自定义的Applet子类中,一般需要重载父类的( D )方法来完成一些画 图操作。 A. start() B. stop() C. init() D. paint() 3、Java语言具有许多优点和特点,下列选项中,哪个反映了Java程序并行机制的特点?B A)安全性B)多线程C)跨平台D)可移植 4、下列哪个类声明是正确的?D A)abstract final class HI{···}B)abstract private move(){···} C)protected private number; D)public abstract class Car{···} 6、在Java语言中,下列哪些语句关于内存回收的说明是正确的? B A.程序员必须创建一个线程来释放内存; B.内存回收程序负责释放无用内存 C.内存回收程序允许程序员直接释放内存 D.内存回收程序可以在指定的时间释放内存对象 10、下列Object类中的方法,哪一项不是完全跟线程有关:A A.String toString() B.void notify() C.void notifyAll() D.void wait() 11、给出下面代码:C

public class Person{ static int arr[] = new int[10]; public static void main(String a[]) { System.out.println(arr[1]); } } 下列说法中正确的是? A.编译时将产生错误; B.编译时正确,运行时将产生错误; C.输出零; D.输出空。 12、字符串是Java已定义的类型,关于它的构造函数,下面说法不正确的是:B A.String(char[] value, int offset, int count) B.String(int[] codePoints,int offset, int count) C.String(String original) D.String(StringBuffer buffer) 13、下列说法中正确的是:C A.导入包会影响程序的性能 B.包存储在类库中 C.包是类的容器D.上述说法都不对 14、下列不是String类的常用方法是:C

JAVA练习题含答案-answer to practice 6

Chapter 6 Arrays Multiple Choice 1)The individual variables that together make up the array are referred to as: (a)indexed variables (b)subscripted variables (c)elements of the array (d)all of the above Answer: D 2)What is the correct expression for accessing the 5th element in an array named colors? (a)colors[3] (b)colors[4] (c)colors[5] (d)colors[6] Answer: B 3)Consider the following array: What is the value of myArray[myArray[1] – myArray[0]] (a)7 (b)9 (c)-3 (d)6 Answer: C Copyright ? 2006 Pearson Education Addison-Wesley. All rights reserved. 1

2 Walter Savitch ?Absolute Java2/e: Chapter 6 Test Bank 4)The subscript of the first indexed variable in an array is: (a)0 (b)1 (c)2 (d)3 Answer: A 5)The correct syntax for accessing the length of an array named Numbers is: (a)Numbers.length() (b)Numbers.length (c)both A and B (d)none of the above Answer: B 6)An ArrayIndexOutOfBounds error is a: (a)compiler error (b)syntax error (c)logic error (d)all of the above Answer: C 7)Which of the following initializer lists correctly initializes the indexed variables of an array named myDoubles? (a)double myDoubles[double] = {0.0, 1.0, 1.5, 2.0, 2.5}; (b)double myDoubles[5] = new double(0.0, 1.0, 1.5, 2.0, 2.5); (c)double[] myDoubles = {0.0, 1.0, 1.5, 2.0, 2.5}; (d)array myDoubles[double] = {0.0, 1.0, 1.5, 2.0, 2.5}; Answer: C 8)The base type of an array may be all of the following but: (a)string (b)boolean (c)long (d)all of these may be a base type of an array. Answer: D 9)The correct syntax for passing an array as an argument in a method is: (a)a[] (b)a() (c)a (d)a[0]..a[a.length] Answer: C Copyright ? 2006 Pearson Education Addison-Wesley. All rights reserved.

Java开发工程师笔试题(带答案)

Java开发工程师笔试试题 (请不要在试题上留任痕迹,所有答案均写在答题纸上) 一.编程题(共26分) 1.任意写出一种排序算法。(6分) public void sort(int [] array){ //代码区 } 2.求1+2+3+..n(不能使用乘除法、for 、while 、if 、else 、switch 、case 等关键字 以及条件判断语句)(8分) public int sum(int n){ //代码区 return 0; } 3.完成下面法,输入一个整数,输出如下指定样式图案。(12分) 输入:3, 输出: 1*2*3 7*8*9 4*5*6

输入:4 输出: 1*2*3*4 9*10*11*12 13*14*15*16 5*6*7*8 public void drawNumPic(int n){ //代码区 } 二.选择题(定项选择每题3分,不定项选择每题4分,共63分) 1.在基本JAVA类型中,如果不明确指定,整数型的默认是__类型,带小数的默认是__类型?( B ) A.int float B.int double C.long float D.long double 2.只有实现了__接口的类,其对象才能序列化( A ) A.Serializable B.Cloneable https://www.doczj.com/doc/ef5114510.html,parable

D.Writeable 3.代码System. out. println(10 % 3 * 2);将打印出?( B ) A. 1 B.2 C.4 D.6 4.以下程序运行的结果为( A ) public class Example extends Thread{ @Override public void run(){ try{ Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); } System.out.print("run"); } public static void main(String[] args){ Example example=new Example(); example.run(); System.out.print("main"); } } A.run main B.main run C.main D.run E.不能确定 5.下面有关java实例变量,局部变量,类变量和final变量的说法,错误的是?( B ) A.实例变量指的是类中定义的变量,即类成员变量,如果没有初始化,会有默认值

相关主题
文本预览
相关文档 最新文档