首页 > 编程知识 正文

一个java练习题,java基础训练题

时间:2023-12-29 13:16:25 阅读:329494 作者:IFCJ

本文目录一览:

java练习题

public class Vehicle {

private String name;

private String gender;

private int age;

private String driverLicense;

private String plateNumber;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getDriverLicense() {

return driverLicense;

}

public void setDriverLicense(String driverLicense) {

this.driverLicense = driverLicense;

}

public String getPlateNumber() {

return plateNumber;

}

public void setPlateNumber(String plateNumber) {

this.plateNumber = plateNumber;

}

@Override

public String toString() {

return "Vehicle [name=" + name + ", gender=" + gender + ", age=" + age + ", driverLicense=" + driverLicense

+ ", plateNumber=" + plateNumber + "]";

}

public void drive() {

System.out.println(name + "司机正在开" + plateNumber + "牌号的车。");

}

public void stop() {

System.out.println(name + "司机把" + plateNumber + "牌号的车停了下来。");

}

public boolean checkName(String check) {

if (this.name != null  this.name != "") {

return this.name.equals(check);

}

return false;

}

public boolean checkPlateNumber(String check) {

if (this.plateNumber != null  this.plateNumber != "") {

return this.plateNumber.equals(check);

}

return false;

}

}

懒得写注释了

求Java基础,练习题。

选择题 (25道)

1. 下列选项中,不属于Java语言特点的一项是( C )。

A:分布式 B:安全性 C:编译执行 D:面向对象

2. Java语言的特点与 C/C+ +语言的比较中说法错误的是:( D )

A:简单性Java继承了 C/C+ +的语法 ,丢弃了其中不常用又容易引起混淆的功能。

B:Java是一种纯面向对象的语言 ,具有封装、继承 ( Inheritance)和多态( Polymorphism)的特点。

C:Java应用程序可凭借URL打开并访问网络上的对象。

D:解释型Java写成的源代码需要被编译成高阶的字节码 ,它们与机器架构有关。

3. 阅读下列代码,选出该代码段正确的文件名( C )。

class A{

void method1(){

System.out.println("Method1 in class A");

}

}

public class B{

void method2(){

System.out.println("Method2 in class B");

}

public static void main(String[] args){

System.out.println("main() in class B");

}

}

A: A.java B:A.class C: B.java D: B.class

4. 如果一个类的文件名为Student.java,但是类的代码为:

public class Student {

public static void main(String[] args) {

System.out.println(82);

}}

那么下列说法正确的是:( B )

A:程序运行结果为8; B:程序运行结果为2;

C:程序运行结果为0; D:程序编译错误,不能运行;

5. 符合对象和类的关系的是( D )。

A:教师和学生 B:书和房子

C:狗和猫 D:飞机和交通工具

6. 关于垃圾回收机制描述不正确的是( B )

A:垃圾回收机制不须通过程序员调用相应方法,也能自动启动。

B:java程序员用System.gc()方法一定能进行垃圾回收;

C:垃圾回收机制属于jvm自动操作,java程序员可以不进行垃圾回收操作。

D:垃圾回收机制并不是由操作系统自动执行。

7. 编译下面源程序会得到哪些文件( D )?

class A1{

}

class A2 exdends A1{

}

public class B{

public static void main(String[] args){

}

}

A: 只有B.class文件 B:只有A1.class和A2.class文件

C: 编译不成功 D:A1.class、A2.class和B.class文件

8. 下列关于基本数据类型的说法中,不正确的一项是( D )。

(A)boolean类型变量的值只能取真或假

(B)float是带符号的32位浮点数

(C)double是带符号的64位浮点数

(D)char是8位Unicode字符

9. 下列(D )是合法的标识符?

A:12class B:void C:-5 D:_blank

10. 在编写Java程序时,如果不为类的成员变量定义初始值,Java会给出它们的默认值,下列说法中不正确的一个是( D )。

A:byte的默认值是0 B:boolean的默认值是false

C: char类型的默认值是’’ D: long类型的默认值是0.0L

11. 下列程序执行的结果是:( B )

public class News {

public static void main(String[] args) {

System.out.println(1+2+ "aa"+3);

}}

A: "12aa3" B: "3aa3 " C: "12aa" D: "aa3"

12. 表达式(12==0) (1/0 1)的值为( B )。

A: true B: false C: 0 D: 运行时抛出异常

13. 下列循环体执行的次数是( C )。

int y=2, x=4;

while(--x != x/y){ }

A : 1 B: 2 C : 3 D : 4

14. 已知如下代码:

switch(m){

case 0: System.out.println("Condition 0");

case 1: System.out.println("Condition 1");

case 2: System.out.println("Condition 2");

case 3: System.out.println("Condition 3");break;

default:System.out.println("Other Condition");

}

当m的值为( D )时,输出“Condition 3”

(A)2 (B)0、1 (C)0、1、2 (D)0、1、2、3

15. 下列语句输出的结果是:( C )

public class X3 {

public static void main(String[] args) {

for(int i=0; i10; i++){

if(i==5) break;

System.out.print(i);

}

}

}

A:编译错误; B:1234;C:01234;D:12345;

16. 下列语句输出的结果是:( D )

public class Lx1 {

public static void main(String[] args) {

for(int i=0;i5;i++){

switch(i){

case 0:System.out.print("B");

case 1:System.out.print("e");break;

case 2:System.out.print("g");

case 3:System.out.print("!");break;

case 4:System.out.print("!");break;

default:System.out.print("!");

}

}

}

}

A:Beg!!! B:Beeg! C:Beg! D:Beeg!!!

17. 下面foreach循环的程序输出结果是( D )。

public class Lx1{

public static void main(String[] args) {

String s1[]={"欢迎您","3","G","同","学",};

Arrays.sort(s1);

for(String s0:s1)

System.out.print (s0);

}

}

A:欢迎您3G同学 B:3G欢迎您同学 C:同学欢迎您3G D:3G同学欢迎您

18. 阅读以下程序,选择正确的运行结果:( B )

public class Lx1 {

public static void main(String[] args) {

byte d[]="YOUIHE你我他".getBytes();

String s=new String(d,6,2);

System.out.println(s);

}

}

A:HE; B:你; C:我; D:他

19. 设有下列数组定义语句:

int a[][]= {{1, 2}, {3}};

则对此语句的叙述正确的是( D )。

A: 定义了一个名为a的一维数组 B: a数组 a[1][1]为0

C: a数组元素的下标为1~3 D: 数组中每个元素的类型都是整数

20. 下列程序输出的结果是:( B )

public class Lx1 {

public static void main(String[] args) {

String a[][] ={{"","","",""},{""},{"",""}};

System.out.println(a[2].length);

}

}

A:1 B:2 C:3 D:4

21. 关于以下程序的说明,正确的是( C )

1. class StaticStuff

2. {

3. static int x=10;

4. static { x+=5;}

5. public static void main(String args[ ])

6. {

7. System.out.println(“x=” + x);

8. }

9. static { x/=3;}

10. }

A、4行与9行不能通过编译,因为缺少方法名和返回类型

B、9行不能通过编译,因为只能有一个静态初始化器

C、编译通过,执行结果为:x=5

D、编译通过,执行结果为:x=3

22. 给出下面代码,关于该程序以下哪个说法是正确的?( C )

public class Person{

static int arr[] = new int[5];

public static void main(String a[]) {

for(int i=0;i

System.out.print(arr[0]);

}

}

A、编译时将产生错误 B、编译时正确,运行时将产生错误 C、输出零 D、输出空

23. 下面程序中类ClassDemo中定义了一个静态变量sum,分析程序段的输出结果。( C )

class ClassDemo {

public static int sum=1;

public ClassDemo() {

sum = sum + 5;}

}

public class ClassDemoTest{

public static void main(String args[]) {

ClassDemo demo1=new ClassDemo();

ClassDemo demo2=new ClassDemo();

System.out.println(demo1.sum);}

}

A: 0 B: 6 C: 11 D: 2

24. 下面关于方法的说法,不正确的是( C )。

A: Java中的构造方法名必须和类名相同

B: 方法体是对方法的实现,包括变量声明和合法语句

C: 如果一个类定义了构造方法,也可以用该类的默认构造方法

D: 类的私有方法不能被其他类直接访问

25. 在Java中下列说法正确的是( C )

A) 一个子类可以有多个父类,一个父类也可以有多个子类

B) 一个子类可以有多个父类,但一个父类只可以有一个子类

C) 一个子类可以有一个父类,但一个父类可以有多个子类

D) 上述说法都不对

JAVA 练习题

public class JavaExos {

public static void charInt(String chaine){ //1044

String[] charInt = new String[2];

int count = -1;

char maxChar = 'A';

int[] letterCount = new int[26];

String word = chaine.toLowerCase();

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

int indexOfChar = (byte)word.charAt(i)-97;

letterCount[indexOfChar]++;

if (letterCount[indexOfChar]count || (letterCount[indexOfChar]==count word.charAt(i)maxChar)){

count = letterCount[indexOfChar];

maxChar = word.charAt(i);

}

}

charInt[0] = String.valueOf(maxChar);

charInt[1] = ""+count;

System.out.println(charInt[0]+" "+charInt[1]);

}

public static void getDate(int n){ //1047 这题如果给1,其实是指2000年1月2号.

n++;

int[] getYear = getYear(n);

int year = getYear[0];

int[] getMonth = getMonth(year,getYear[1]);

int month = getMonth[0];

String monthString ;

if(month10) monthString = "0"+String.valueOf(month);

else monthString = String.valueOf(month);

int day = getMonth[1];

System.out.println(year+"-"+monthString+"-"+day+" "+getDayOfWeek(n));

}

private static boolean isBissextile(int n){

if (n%4==0 !(n%100==0n%400!=0))

return true;

else

return false;

}

private static int[] getYear(int n){

int[] getYear = new int[2];

int year = 2000;

while(n0){

if(isBissextile(year)) n -= 366;

else n -= 365;

if (n0) year++;

}

if(isBissextile(year)) n+=366;

else n += 365;

getYear[0] = year;

getYear[1] = n;

return getYear;

}

private static int[] getMonth(int year, int n){

int[] getMonth = new int[2];

int month = 1;

while(n0){

if(month=7 month%2 != 0) n -= 31;

else if (month==2 isBissextile(year) ) n -= 29;

else if (month==2 !isBissextile(year)) n -= 28;

else if(month=7 month%2==0) n -= 30;

else if(month%2==0) n-=31;

else n -= 30;

if (n0) month++;

}

if(month=7 month%2 != 0) n += 31;

else if (isBissextile(year) month==2) n += 29;

else if (!isBissextile(year) month==2) n += 28;

else if(month=7 month%2==0) n += 30;

else if(month%2==0) n+=31;

else n += 30;

getMonth[0] = month;

getMonth[1] = n;

return getMonth;

}

private static String getDayOfWeek(int n){

int quotient = n/7;

int remainder = n -= quotient*7;

switch(remainder){

case 0 : return "Sunday";

case 1 : return "Monday";

case 2 : return "Tuesday";

case 3 : return "Wednesday";

case 4 : return "Thursday";

case 5 : return "Friday";

case 6 : return "Saturday";

default : return "Never arrive";

}

}

public static void getCode(String chaine){ //1048

chaine = chaine.toUpperCase();

System.out.println("START");

System.out.println(chaine);

System.out.println("END");

System.out.println();

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

System.out.print((changChar(chaine.charAt(i))));

}

System.out.println();

}

private static char changChar(char c){

if(c=65 c=90 c-565) return (char)(c+26-5);

else if(c=65 c=90) return (char)(c-5);

else return c;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

JavaExos.charInt("adfadffasdfda");

JavaExos.getDate(1751);

JavaExos.getCode("NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX");

java练习题求大神

按照你的要求编写的Java程序如下(每次的运行结果会因有随机数而有所不同)

public class M{

public static void main(String[] args){

int a[][]=new int[8][8];

int b[][]=new int[8][2];

int i,j,row,col;

boolean flag;

for(i=0;i8;i++){

flag=false;

row=(int)(Math.random()*8);

col=(int)(Math.random()*8);

//去掉重复的row,col

for(j=0;j8;j++){

  if(row==b[j][0]-1 col==b[j][1]-1){

   flag=true;

   i--;

   break;

  }

}

if(flag==false){

  b[i][0]=row+1;

  b[i][1]=col+1;

  a[row][col]=1;

}

}

for(i=0;i8;i++){

for(j=0;j8;j++){

  if(j==7){

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

  }else{

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

  }

}

System.out.println();

}

}

}

java编程练习题

1

import java.util.Scanner;

public class test1 {

private static Scanner input= new Scanner(System.in); 

public static void main(String[] args){

long num;

do{

System.out.print("nInput your num: ");

num=input.nextLong();

}while(num10000||num99999);

num=num/100;

num*=100;

System.out.println("nThis is num: "+num);

}

}

2.

import java.util.Scanner;

public class test2 {

private static Scanner input= new Scanner(System.in); 

public static void main(String[] args){

long num;

do{

System.out.print("nInput your num: ");

num=input.nextLong();

}while(num0||num1000);

int sum=0;

while(num0){

sum+=num%10;

num/=10;

}

System.out.println("nThis is sum: "+sum);

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。