求Java基础,练习题。

Python088

求Java基础,练习题。,第1张

选择题 (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(8>>2)

}}

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

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

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

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类型的默认值是’\0’ 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=0i<10i++){

if(i==5) break

System.out.print(i)

}

}

}

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

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

public class Lx1 {

public static void main(String[] args) {

for(int i=0i<5i++){

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:HEB:你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=0i

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) 上述说法都不对

2、类的方法与异常处理:

在一个类的main方法中定义一个长度为8的int型数组,用for循环对数组中的元素1到8的值。随机产生两个整数,将这两个整数作为数组的下标来引用数组中的元素,求出这两个元素的积,并在屏幕上输出这两个元素的值和积。要求在出现数组下标越界时,采用try-catch的方法捕获异常。

import java.util.Random

public class SecondTest {

public static void main(String[] args) {

//定义一个长度为8的int型数组

int[] ary = {35, 42, 13, 46, 59, 37, 125, 99}

//用for循环对数组中的元素1到8的值

for(int i= 1i <ary.lengthi++){

System.out.print(ary[i] + "\t")

}

System.out.println()

try{

Random rand = new Random()

//随机产生两个整数

int first = rand.nextInt()

int second = rand.nextInt()

//这两个整数作为数组的下标来引用数组中的元素,求出这两个元素的积

long mul = ary[first] * ary[second]

//屏幕上输出这两个元素的值和积

System.out.println("积为" + mul)

}catch(ArrayIndexOutOfBoundsException exp){

//出现数组下标越界时,采用try-catch的方法捕获异常

System.out.println("数组下标越界! Error!")

}

}

}

、类与对象的基础题:

1)编程实现:设计一个类Simple,有三个成员变量,分别为int型、double型和String型,这三个成员变量分别含有各自的get方法和set方法,可以用toString方法显示这三个成员变量。,

2)声明测试类:在测试类的main 方法中创建Simple类的对象aSimple,此对象调用set方法分别对对象的各个属性设置具体的值,然后调用get方法将aSimple的具体的值输出在屏幕上。

public class TestSimple {//测试类的

public static void main(String[] args) {

//创建Simple类的对象aSimple

Simple aSimple = new Simple()

//set方法分别对对象的各个属性设置具体的值

aSimple.setIntValue(5)

aSimple.setDoubleValue(123.45D)

aSimple.setStrValue("string value for simple")

//get方法将aSimple的具体的值输出在屏幕上

System.out.println("int value for simple is: " + aSimple.getIntValue())

System.out.println("double value for simple is: " + aSimple.getDoubleValue())

System.out.println("String value for simple is: " + aSimple.getStrValue())

}

}

class Simple{//类Simple

private int intValue//int型成员变量

private double doubleValue//double型成员变量

private String strValue//String型成员变量

//用toString方法显示这三个成员变量

public String toString(){

return intValue + ", " + doubleValue + ", " + strValue

}

public double getDoubleValue() {

return doubleValue

}

public void setDoubleValue(double doubleValue) {

this.doubleValue = doubleValue

}

public int getIntValue() {

return intValue

}

public void setIntValue(int intValue) {

this.intValue = intValue

}

public String getStrValue() {

return strValue

}

public void setStrValue(String strValue) {

this.strValue = strValue

}

}

第二题:

public class suijishu

{

int x=(int)(Math.random()*(100-40)+40)

int y=(int)(Math.random()*(100-40)+40)

int z=(int)(Math.random()*(100-40)+40)

}

class use

{

public static void main(String args[])

{

suijishu s=new suijishu()

int score=(s.x+s.y+s.z)/3

System.out.println("三个随机数是"+s.x+","+s.y+","+s.z)

System.out.println("平均成绩是"+score)

}

}