JAVA基础强化练习题2(附答案)
void process() { System.out.println("B ");
} } A. 输出:B B. 无输出 C. B 类的 process 方法处,出现编译错误 D. 代码 a.process();行,出现编译错误 正确答案:D
8. 下列代码编译和运行的结果是:
B. 事务的提交(Commit)指将事务中所有对数据库的更新写到磁盘上的物理数据库中去, 事务正常结束。
C. 事务的回滚(Rollback)指在事务运行的过程中发生了某种故障,事务不能继续进行, 将事务中对数据库的所有以完成的操作全部撤消,回滚到事务开始的状态。
D. JDBC 通过 Connection 对象控制事务,默认方式下,在执行完更改语句后需要必须要调用 Connection 的 commit 方法,对数据的更改才能生效。 正确答案:D
10. 运行下列程序:
public static void main(String[] args) { String str = "**oracle***oracle*****oracle***"; String str1 = "oracle"; int index = 0; while ((index = str.indexOf(str1, index)) != -1) { System.out.print(index + ""); index += str1.length(); }
13. 下列属于 DML 语句的是 :() 。
A. COMMIT B. INSERT C. DROP D. GRANT 正确答案:B
14. 下面关于事务(Transaction)的说法错误的是:()。
A. 事务具备 ACID 四个基本特性,即 A(Atomicity)—原子性、C(Consistency)—一致性、 I(Isolation)—隔离性、D(Durability)—持久性。
System.out.println("Have a nice day."); } } A. 编译失败 B. 输出:pi is bigger than 3. C. 输出:pi is bigger than 3. Have a nice day D. 输出:pi is not bigger than 3. Have a nice day. 正确答案:A
7. 下列代码编译和运行的结果是:
public class A { void process() throws Exception { throw new Exception(); } public static void main(String[] args) { A a = new B(); a.process(); }
3. synchronized 关键字做同步,可以使用在:()。
A. 方法上 B. 接口上 C. 变量上 D. 类上
正确答案:A
4. 类 A 的定义如下:
class A { protected void f() throws FileNotFoundException { ……… }
} 下列代码段没有编译错误的是:()。
9. 需要读取一个比较大的文本文件,这个文件里有很多字节的 数据,那么下列最合适读这个文件的选项是:()。
A. new FileInputStream(“fileName”); B. new InputStreamReader(new FileInputStream(“fileName”)); C. new BufferedReader(new InputStreamReader(new FileInputStream(“fileName”))); D. new RandomAccessFile(“fileName”,”+rw”); 正确答案:C
} 控制台输出的结果是:()。
A. 1 10 21 B. 2 11 22 C. 3 13 23 D. 5 13 22 正确答案:B
11. 运行下列代码,输出为 false 的是:()。
A. String st1 = "abc"; System.out.println("abc" == st1);
B. String st2 = "abc"; System.out.println(st2.equals(new String("abc")));
5. 下列选项中能够得到字符串的一部分的单行函数是()。
A. INSERT B. SUBSTR C. LPAD D. LEAST 正确答案:B
6. ResultSet 提供了获取数据库中某个字段值的方法,如果某个 字段为 NUMBER 类型,可以获取该字段值的方法是:( )。
A. getNumber() B. getDouble() C. setNumber() D. setDouble() 正确答案:B
17. 下列代码的运行结果是()。
public class Forest implements Serializable { private Tree tree = new Tree(); public static void main(String[] args) { Forest f = new Forest(); try { FileOutputStream fs = new FileOutputStream("Forest.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(f); os.close(); } catch (Exception ex) { ex.printStackTrace();
1. 下列代码的输出结果是:
public class Yikes { public static void go(Long n) { System.out.println("Long "); } public static void go(Short n) { System.out.println("Short "); } public static void go(int n) { System.out.println("int "); } public static void main(String[] args) { short y = 6; long z = 7; go(y); go(z); }
16. 查询客户姓名以及他的推荐人,没有推荐人的客户信息不 显示,下列 sql 语句正确的是:
A. select a1.real_name customer, a2.real_name recommender from account a1 join account a2 on a1.id = a2.id; B. select a1.real_name customer, a2.real_name recommender from account a1 join account a2 on a1.recommender_id = a2.recommender_id; C. select a1.real_name customer, a2.real_name recommender from account a1 join account a2 on a1.recommender_id = a2.id; D. select a1.real_name customer, a2.real_name recommender from account a1, a2 on a1.recommender_id = a2.id; 正确答案:C
15. 运行下面程序:
public class Foo { public static void main(String[] args) { try { test(); System.out.println("condition 1"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("condition 2"); } catch (Exception e) { System.out.println("condition 3");
A. 查询结束后,所有的结果数据将一次被存储在 ResultSet 对象中 B. Statement 对象 close 后,由其创建的 ResultSet 对象将自动的 close C. 查询结束后,ResultSet 中的游标指向第一条记录之上,因此要先调用一次 next()才有可 能取得记录 D. ResultSet 的的方法 getString(...)意为取得该列的数据以字符串的形式返回,数据库中 的该列类型并不一定必须是字符类型 正确答案:A
A. class B extends A { public void f() throws Exception { ……… } }
B. class B extends A { public void g() throws IOException { f(); } }
C. class B extends A { public void g() { try{ f(); ……… } catchபைடு நூலகம்IOException e) { ……… } catch(FileNotFoundException e1) { ……… } } }
C. Integer i = 100; System.out.println(100 == i);
D. ArrayList list = new ArrayList(); System.out.println(list.contains(null));
java基础练习题带答案.doc
java基础练习题带答案第1章Java开发入门一、填空题1、Java EE、Java SE、Java ME2、JRE3、javac4、bin5、path> classpath二、选择题1、A BCD、C、D、B、 B三、简答题1、面向对象、跨平台性、健壮性、安全性、可移植性、多线程性、动态性等。
2、JRE,它相当于操作系统部分,提供了Java程序运行时所需要的基本条件和许多Java基础类,例如,10类、GUI控件类、网络类等。
JRE是提供给普通用户使用的,如果你只想运行别人开发好的Java程序,那么,你的计算机上必须且只需安装JRE。
JDK,它包含编译工具、解释工具、文档制作工具、打包工具多种与开发相关的工具,是提供给Java开发人员使用的。
初学者学习和使用Java语言时,首先必须下载和安装JDKo JDK中已经包含了JRE部分,初学者安装JDK后不必再去下载和安装JRETo四、编程题public class HelloWorld {} public static void main { } System, out. printin;第2章Java编程基础一、填空题1、class2、true 和false3、单行注释、多行注释、文档注释4、基本数据类型、引用数据类型5、1、2、4、86、& && |7、08、9、10、6二、判断题1、错、对、错、对、错三、选择题1、AD、AD、C、ABCD、 C 、A、AC、A、 B 10、A四、程序分析题1、编译不通过。
int值4和b相加时,由于变量b 的类型为byte,取值范围没有int类型大,存不下int 类型的值,因此编译不通过。
2、编译不通过。
这是因为y是在最里层的代码块中定义的一个变量,只有在那个代码块中才可使用,在使用y = x;语句时已经超过了y变量的作用域,所以编译无法通过。
3、打印结果为:3o4、打印结果为:987五、问答题1、J ava语言的八种基本数据类型有:byte字节型,占一个字节。
JAVA练习题2
12、已有语句 int a = 123 ;表达式 a + “0” 的值为: A)123 B)a C)”a0” D)”1230” 13、设有定义 int a = 12 ;则执行语句 a *= 12; 后,a 的值为()。 A) 144 B) 12 C) 24 D) 0
14、算术表达式1+2/3-4*5的值为()。 A) 1 B) -19 C) -5 D) 0 15、整型变量a、b定义为int a=3; int b=4; 则表达式 ++a == b 的值是 ()。 A) 4 B) 3 C) 1 D) true 16.若所用变量都已正确定义,以下选项中,非法的表达式是()。 A) a != 4||b==1 B) 'a' % 3 C) 'a' = 1/2 D) 'a' + 32 17.若已有变量定义:int a=1; boolean b=false ; char c=’A’ ;float f =1.2F;double d =3.4;String s=”123”,以下表达式正确的是()。 A) d/=(a +c*f) B) a = (int)(f*d)+d C) a=(int)(b)+(int)(s) D) Double.parasDouble(s)>a||b 18、下列语句序列执行后,x 的值是()。 public class ex16 { public static void main(String[] args) { int a=1, b=2, x=1; x+=a++ + b-- - --a*++b); System.out.println(x); } } A) 1 B)2 C) 3 D)4 19、执行下列程序段之后,输出结果为()。 public class ex18 { public static void main(String[] args) { int a=1; int b=2; int c=3; c+=(a++)+(++a)/( --b)*(b--); System.out.println(c); } } A) 4 B) 6 C) 7 D)8 20、执行下列程序段之后,输出结果为()。 public class ex19 {
java基础考试试题及答案
java基础考试试题及答案### Java基础考试试题及答案#### 一、选择题(每题5分,共20分)1. 下列哪个选项是Java的基本数据类型?A. StringB. IntegerC. intD. ArrayList答案:C2. Java中,哪个关键字用于声明一个类?A. classB. interfaceC. structD. function答案:A3. 在Java中,下列哪个是正确的数组初始化方式?A. int[] myArray = new int[5];B. int myArray[] = new int[5];C. int myArray = new int[5];D. int[] myArray = 5;答案:A4. Java中,哪个方法用于获取字符串的长度?A. length()B. size()C. count()D. length答案:A#### 二、填空题(每题5分,共20分)1. Java中的`boolean`类型只有两个可能的值,它们是________和________。
答案:true、false2. Java中的`switch`语句中,每个`case`后必须跟随一个_______。
答案:break3. 在Java中,`try-catch`语句用于异常处理,其中`try`块中的代码可能会抛出一个_______。
答案:Exception4. Java中,`System.out.println()`函数用于在控制台输出信息,其中`System`是一个_______。
答案:类#### 三、简答题(每题10分,共30分)1. 简述Java中的继承机制。
答案:在Java中,继承是一种面向对象编程的概念,它允许一个类(称为子类或派生类)继承另一个类(称为父类或基类)的属性和方法。
子类可以重写父类的方法,也可以添加自己的属性和方法。
继承提供了代码的重用性,并且支持多态性。
2. 描述Java中的接口是什么。
java语言程序设计(基础篇)第二章程序练习题标准答案
2.1(将摄氏温度转化为华氏温度)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter a degree in celsius: ");double Celsius = input.nextDouble();double Fahrenheit;Fahrenheit = (9.0/5) * Celsius + 32;System.out.println(Celsius + " Celsius is" + Fahrenheit + " Fahrenheit");}}2.2(计算圆柱体的体积)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter the radius and length of a cylinder: ");double radius = input.nextDouble();double length =input.nextDouble();double area = radius * radius * Math.PI;double volume = area * length;System.out.println("The area is " + area);System.out.println("The volume is " + volume);}}2.3(将英尺转换为米)import java.util.Scanner;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter a value for feet: ");double feet = input.nextDouble();double meters = feet * 0.305;System.out.println(feet+ " feet is " + meters + " meters");}}2.4(将磅转换为千克)import java.util.Scanner;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter a number in pounds: ");double pounds = input.nextDouble();double kilograms = pounds * 0.454;System.out.println(pounds + " pounds is " + kilograms + " kilograms");}}2.5(财务应用程序:计算消费)import java.util.Scanner;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter the subtotal and gratuity rate: ");double subtotal = input.nextDouble();double Gratuity = input.nextDouble();double gratuity = subtotal * Gratuity * 0.01;double total = gratuity + subtotal;System.out.println("The gratuity is $" + gratuity + " and total is " + total);}}2.6(求一个整数个位数的和)import java.util.Scanner;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter a number between 0 and 1000: ");int number = input.nextInt();int sum = number % 10 + (number / 10) % 10 + (number / 100) % 10;System.out.println("The sum of the digits is " + sum);}}2.7(求出年数)import java.util.Scanner;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter the number of minutes: ");double minutes = input.nextDouble();int years = (int)minutes / (60*24*365);int days= (int)minutes/ (60*24) - ((int)minutes/ (60*24*365)) * 365;System.out.println(minutes + "minutes is approximately " + years + " years and " + days + " days");}}2.8(当前时间)import java.util.*;public class ShowXureentTime {public static void main(String[] args) {// TODO Auto-generated method stublong totalMilliseconds = System.currentTimeMillis();//得到1970年1月1日到现在的毫秒数long totalSeconds = totalMilliseconds / 1000;//将总毫秒转化为总秒long currentSecond = totalSeconds % 60;//当前的秒数long totalMinutes = totalSeconds / 60;//得到总的分钟long currentMinute = totalMinutes % 60;//当前的分钟数long totalHours = totalMinutes / 60;//得到总小时Scanner input = new Scanner(System.in);System.out.print("Enter the time zone offset to GMT: ");long zone = input.nextInt();long currentHour = totalHours % 24 + zone;//当前的小时数if(currentHour <0) {currentHour = currentHour + 24;}System.out.println("Current time is " + currentHour + ":" +currentMinute + ":" + currentSecond + " GMT");}//GMT世界时}2.9(物理:加速度)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter v0, v1 and t: ");double v0 = input.nextDouble();double v1 = input.nextDouble();double t = input.nextDouble();double acceleration = (v1 - v0) / t;System.out.println("The average acceleration is " +String.format("%.4f", acceleration));}}2.10(科学:计算能量)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter the amount of water in kilograms: ");double M = input.nextDouble();System.out.print("Enter the initial temperature: ");double temperature1 = input.nextDouble();System.out.print("Enter the final temperature: ");double temperature2 = input.nextDouble();double energy = M * (temperature2 - temperature1) * 4184;System.out.println("The energy needed is " + energy);}}2.1(人口统计)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter the number of years: ");int year = input.nextInt();int population = 312032486;int temp= (365*12*60*60/7)-(365*12*60*60/13)+(365*12*60*60/45);//j = 诞生-死亡+移民迁入;for(int i = 1 ;i <= year ;i ++ ) {population = temp + population ;}System.out.println("The population in " + year + " years is " + population);}}2.12(物理:求出跑道的长度)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter speed and acceleration: ");double speed = input.nextDouble();double acceleration = input.nextDouble();double length = speed * speed / (2 * acceleration);System.out.println("The minimum runway length for this airplane is " + String.format("%.3f", length));}}2.13(财务应用程序:复利值)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter the monthly saving aomunt: ");double monthly = input.nextDouble();double money = 0;for(int i = 1; i <= 6; i++) {money = (monthly + money) * (1 + 0.00417);}System.out.println("After the sixth month, the account value is " + String.format("%.2f", money));}}2.14(医疗应用程序:计算BMI)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter weight in pounds: ");double pounds = input.nextDouble();double kilogram = pounds * 0.45359237;System.out.print("Enter height in inches: ");double inches = input.nextDouble();double meter = inches * 0.0254;double BMI = (kilogram / (meter * meter)) ;System.out.println("BMI is " + String.format("%.4f", BMI));}}2.15(几何:两点间距离)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter x1 and y1: ");double x1 = input.nextDouble();double y1 = input.nextDouble();System.out.print("Enter x2 and y2: ");double x2 = input.nextDouble();double y2 = input.nextDouble();double distance = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);System.out.println("The distance between the two points is " + Math.pow(distance, 0.5));}}2.16(几何:六边形面积)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter the side: ");double side = input.nextDouble();double area = 3 * Math.sqrt(3) / 2 * side * side;System.out.println("The area of the hexagon is " + area);}}2.17(科学:风寒温度)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter the temperature in Fahrenheit between -58°F and 41°F: ");double Fahrenheit = input.nextDouble();System.out.print("Enter the wind speed (>=2) in miles per hour: ");double speed = input.nextDouble();double t = 35.74 + 0.6215 * Fahrenheit - 35.75 * Math.pow(speed, 0.16) + 0.4275 * Fahrenheit * Math.pow(speed, 0.16);System.out.println("The win chill index is " + String.format("%.5f", t));}}2.18(打印表格)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("a\t" + "b\t" + "pow(a, b)");System.out.println("1\t" + "2\t" + (int)Math.pow(1, 2));System.out.println("2\t" + "3\t" + (int)Math.pow(2, 3));System.out.println("3\t" + "4\t" + (int)Math.pow(3, 4));System.out.println("4\t" + "5\t" + (int)Math.pow(4, 5));System.out.println("5\t" + "6\t" + (int)Math.pow(5, 6));}}2.19(几何:三角形的面积)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter three points fir a trinangle: ");double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double x3 = input.nextDouble();double y3 = input.nextDouble();double edge1= Math.pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);double edge2= Math.pow((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2), 0.5);double edge3= Math.pow((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1), 0.5);double s =(edge1 + edge2 + edge3) / 2;double area = Math.pow(s* (s- edge1) * (s- edge2) * (s- edge3), 0.5);System.out.println("The area of the tringle is "+ String.format("%.1f", area));}}2.20(财务应用程序:计算利息)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter balance and interst rate (e.g, 3 for 3%): ");double balance = input.nextDouble();double annual = input.nextDouble();double interst = balance * ( annual / 1200);System.out.println("The interst is "+ String.format("%.5f", interst));}}2.21(财务应用:计算未来投资值)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter investment amount: ");double investment = input.nextDouble();System.out.print("Enter annual interest rate in percentage: ");double interest = input.nextDouble();interest = interest / 100 / 12;System.out.print("Enternumber of yuears: ");double years = input.nextDouble();double value = investment * Math.pow((1 + interest), (years * 12));System.out.println("Accumulated value is $" + String.format("%.2f", value));}}2.22(财务应用:货币单位)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter an amount in int, for example 1156: ");int amount = input.nextInt();int remainingAmount = amount;int numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;int numbersOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;int numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;int numberOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;int numberOfPennies = remainingAmount;System.out.println("Your amount " + amount + " consosts of");System.out.println(" " + numberOfOneDollars + " dollars");System.out.println(" " + numbersOfQuarters + " quarters");System.out.println(" " + numberOfDimes + " dimes");System.out.println(" " + numberOfNickels + " nickels");System.out.println(" " + numberOfPennies + " pennies");}}2.23(驾驶费用)import java.util.*;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.print("Enter the driving distance: ");double drivingDistance = input.nextDouble();System.out.print("Enter miles per gallon: ");double milesPerGallon = input.nextDouble();System.out.print("Enter price per gallon: ");double pricePerGallon = input.nextDouble();double costOfDriving = drivingDistance / milesPerGallon * pricePerGallon;System.out.print("The cost of driving is $" + String.format("%.2f", costOfDriving));}}。
计算机二级JAVA考试强化训练题及解析
计算机二级JAVA考试强化训练题及解析1). 结构化分析方法是面向( )的自顶向下逐步求精进行需求分析的方法。
A.对象B.数据结构C.数据流D.目标正确答案:C答案解析:结构化分析方法是面向数据流进行需求分析的方法,采用自顶向下、逐层分解、建立系统的处理流程。
2). 一个Java Application 运行后,在系统中是作为一个( )。
A.线程B.进程C.进程或线程D.不确定正确答案:B答案解析:线程为一个程序中的单个执行流;进程是程序的一次动态执行过程,它对应了从代码加载、执行到执行完毕的一个完整过程,这个过程也是进程本身从产生、发展到消亡的过程。
一个JavaApplication 运行后,在系统中应该就是一个进程了(动态)。
3). 下列方法中,不属于Throwable 类的方法是( )。
A.pri nt MessageB.get MessageC.toStri ngD.fillStackTrace正确答案:C答案解析:toStri ng 是Object 类的方法,所有类都从Object 类继承。
4). 使新创建的线程参与运行调度的方法是( )。
A.run()B.start()C.i nit()D.resume()正确答案:B答案解析:start()方法使线程参与运行调度。
5). 下列关于Applet的叙述中,错误的是A.Applet是Java类,所以可以由JDK中的解释器java.exe直接解释运行B.Applet应该定义为java.applet.Applet类或javax.swing.Japplet类的子类C.Applet与Applicationg的主要区别在执行方式上D.通过在HTML文件中采用标记可以向Applet传递参数正确答案:A6). 下列关于实型变量书写正确的是( )。
A.E3B.2e0.3C.1.2e3D.1.2EO.3正确答案:C答案解析:本题考查实型变量科学计算格式的.书写。
java基础练习题及答案
java基础练习题及答案Java基础练习题及答案Java是一门广泛应用于软件开发领域的编程语言,具有跨平台、面向对象、安全性高等特点。
在学习Java的过程中,练习题是不可或缺的一部分。
通过练习题的实践,可以巩固对Java基础知识的理解,并提高编程能力。
本文将介绍几个常见的Java基础练习题,并给出相应的答案。
1. 编写一个Java程序,计算并输出1到100之间所有偶数的和。
```javapublic class SumOfEvenNumbers {public static void main(String[] args) {int sum = 0;for (int i = 1; i <= 100; i++) {if (i % 2 == 0) {sum += i;}}System.out.println("1到100之间所有偶数的和为:" + sum);}}```2. 编写一个Java程序,判断一个整数是否是素数。
```javapublic class PrimeNumber {public static void main(String[] args) {int num = 29;boolean isPrime = true;for (int i = 2; i <= Math.sqrt(num); i++) {if (num % i == 0) {isPrime = false;break;}}if (isPrime) {System.out.println(num + "是素数。
");} else {System.out.println(num + "不是素数。
");}}}```3. 编写一个Java程序,找出一个字符串中出现次数最多的字符及其出现次数。
```javapublic class MostFrequentCharacter {public static void main(String[] args) {int[] count = new int[256];for (int i = 0; i < str.length(); i++) {count[str.charAt(i)]++;}int maxCount = 0;char mostFrequentChar = ' ';for (int i = 0; i < count.length; i++) {if (count[i] > maxCount) {maxCount = count[i];mostFrequentChar = (char) i;}}System.out.println("出现次数最多的字符是:" + mostFrequentChar); System.out.println("出现次数:" + maxCount);}}```4. 编写一个Java程序,将一个字符串反转输出。
2020年计算机等级考试Java强化测试题及参考答案
2020年计算机等级考试Java强化测试题及参考答案1.Java中的抽象类Reader和Writer所处理的流是( )。
A.图像流B.对象流C.字节流D.字符流答案:D2.下列叙述中,错误的是( )。
A.内部类的名称与定义它的类的名称可以相同B.内部类可用abstract修饰C.内部类可作为其他类的成员D.内部类可访问它所在类的成员答案:A3.用于在子类中调用被重写父类方法的关键字是( )。
A.thisB.superC.ThisD.Super答案:B4.下列为窗口事件的是( )。
A.MouseEventB.WindowEventC.ActionEventD.KeyEvent答案:B5.用鼠标单击菜单项(Menultem)产生的事件是( )。
A.MenuEventB.ActionEventC.KeyEventD.MouseEvent答案:B6.Java中定义常量的保留字是( )。
A.constB.finalC.finallyD.Native答案:C7.下列关于Java布尔类型的描述中,正确的是( )。
A.一种基本的数据类型,它的类型名称为booleanB.用int表示类型C.其值可以赋给int类型的变量D.有两个值,l代表真,0代表假答案:A8.Java中所有类的父类是( )。
A.FatherB.DangC.ExceptionTMD.Object答案:D9.下列数中为八进制的是( )。
A.27B.0x25C.026D.028答案:C10.下列选项中,与成员变量共同构成一个类的是( )。
A.关键字B.方法C.运算符D.表达式答案:B。
java经典编程题二及详细答案
java经典编程题⼆及详细答案2.1上机练习基本题1)编程观察如下输出语句:System.out.println("hello test escape char");在“escape”处换上各种转义字符测试输出效果。
如何实现如下输出:Hello "test"Hello ‘\test’2)设有如下代码段,考虑赋值转换问题:int a=234;byte b=a;System.out.println("a=" a ",b=" b);将a,b的类型以及数据进⾏各种替换,观察什么时候能赋值,什么时候需要使⽤强制转换,什么时候不能强制转换,什么时候强制转换会丢失数据。
3) 编写⼀个程序求半径R为10的圆的⾯积,结果保留2为有效⼩数。
公式为:S=πR2注:π可⽤Math类的常量PI表⽰(即Math.PI)。
保留⼩数请查阅DecimalFormat类?提⾼题1)设有⼀个边长为30的等边三⾓形,三⾓形内有⼀个内切圆,计算除内切圆外三⾓形内其它部分的⾯积。
【提⽰】先算三⾓形⾯积,再计算圆的⾯积,其差即为结果。
h是等边三⾓形的⾼,内切圆的半径r=h/3,⾯积s=πh?/9。
2)调试分析以下程序的运⾏结果:public class test {public static void main(String a[]) {int m=10,n=517;System.out.println(n % m);System.out.println(n / m);System.out.println(m >> 2);System.out.println(m & n);System.out.println(Integer.toBinaryString(m));System.out.println(Integer.toBinaryString(m >> 2));}}注意:Integer.toBinaryString(int)⽤于将⼀个整数转化为⼆进制形式的数字串。
计算机二级Java强化习题及答案
计算机二级Java强化习题及答案在复习阶段多做练习可以帮助我们稳固知识和提高做题速度以及掌握做题技巧等。
下面是为大家的Java强化习题及答案,希望可以帮助到大家的学习!1、构造化程序设计主要强调的是。
A、程序的规模B、程序的易读性C、程序的执行效率D、程序的可移植性构造化程序设计主要强调的是构造化程序清晰易读,可理解性好,程序员能够进展逐步求精、程序证明和测试,以保证程序的正确性。
此题答案为B。
2、数据库设计包括两个方面的设计内容,它们是。
A、概念设计和逻辑设计B、模式设计和内模式设计C、内模式设计和物理设计D、构造特性设计和行为特性设计数据库设计可分为概念设计与逻辑设计。
数据库概念设计的目的是分析数据间内在语义关联,在此根底上建立一个数据的抽象模型。
数据库逻辑设计的主要工作是将ER图转换为指定的RDBMS中的关系模型。
此题答案是A。
3、为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为。
A、static void method( )B、public void method( )C、final void method( )D、abstract void method( )此题考查的是修饰符的含义。
1、public修饰的方法可被其它类访问或引用。
2、abstract修饰的方法是抽象方法,抽象方法没有方法体,要使用抽象方法,必须先实现此抽象方法。
3、final修饰的方法不能被继承。
4、static修饰的方法为静态方法,静态方法不需要类的实例化就可以被类直接调用。
故要使得类名AB可以直接调用method()方法,那么必须在method()前用static来修饰。
故此题答案为A。
4、偷听接口的方法返回值的类型是。
A、intB、StringC、voidD、Object此题考查的是偷听。
每个事件都有自己的偷听,偷听本身是一种接口,该接口中的方法,返回值是void类型。
Java基础练习题 附答案
C.输出True
D.输出False
8)以下程序的编译和运行结果为?
classtest{
staticbooleancheck;
publicstaticvoidmain(Stringargs[]){
inti;
if(check==true)
i=1;
else
i=2;
if(i=2)i=i+2;
表达式grade70在什么地方被计算中计算在2中未计算中计算在1中未计算非法因为false应写false10设有一个整数x其二进制值为10011100的二进制值为
Java基础练习题附答案
一、简单Java程序调试
1)以下哪个是Java应用程序main方法的有效定义?
A.publicstaticvoidmain();
inttotal=0;
for(inti=0,j=10;total>30;++i,--j){
i="+i+":j="+j);
total+=(i+j);
}
"+total);
}
}
A.产生运行错误
B.产生编译错误
C.输出"Total0"
D.产生如下输出:
i=0:j=10
i=1:j=9
i=2:j=8
Total30
C.x=0,y=1,z=1
D.x=0,y=2,z=2
5)编译和运行以下代码结果为:
1.publicclassEqualsTest{
2.publicstaticvoidmain(Stringargs[]){
3.byteA=(byte)4096;
