JAVA练习题整理②

为保证文章篇幅,java练习题后续将整理至本文。

目录

【一】线程相关

【二】使用数据流

【三】使用JDBC连接Java与MySQL

【四】内部类和类型转换

【五】使用正则表达式和本地化

【六】使用泛型和集合

【七】函数式编程


【一】线程相关

1、Which of the following is not an advantage of multithreading?

以下哪项不是多线程的优势?

a、Improve performance-提升性能

b、Minimize system resource usage-减少资源占用

c、Simplified program structure-简化程序结构

【d】、Competition variable usage-竞争变量使用

2、What will the state of thread after calling the sleep method?调用sleep方法后,线程的状态会是什么?

a\newly created-创建新线程

b\runnable-仍可运行

c\not operational-不可运行(暂时休眠)

d\Terminate death-线程完全终止(死亡)

答案:c

3、Jerry wants to implement a thread object, which of the following code will be incorrect?Jerry想要实现一个线程对象,下面哪一个代码是不正确的?

//A
class InfoManage extends Thread{ 
    public static void main(String[] args) { 
        InfoManage im = new InfoManage(); im.start();
     }
 }

//B
class InfoManage implements Runnable{ 
    @Override 
    public void run() { 
        System.out.println("running"); 
    } 
    public static void main(String[] args) { 
        InfoManage im = new InfoManage(); 
        im.start(); 
    } 
}
//C
class InfoManage{ 
    public static void main(String[] args) { 
        Thread t = new Thread(new Runnable() { 
            @Override 
            public void run() { 
                System.out.println("running"); 
                } 
             }); 
            t. start();
     }
 }
//D
class InfoManage implements Runnable{ 
    @Override 
    public void run() {
           System.out.println("running");
            }
    public static void main(String[] args)
            { 
             InfoManage im = new InfoManage();
             Thread t = new Thread( im);
             t.start(); 
            } 
}

答案:B

4、Which of the following methods can make the main thread wait until the child thread call completes?以下哪种方法可以使主线程等待子线程调用完成?

A\isAlive

B\sleep

C\join

D\interrupt

答案:c

5、Which of the following statements about thread priority is false?以下关于线程优先级的语句中哪一个是错误的?

a、When the processor encounters a thread with a higher priority, it will suspend the current thread and execute the higher priority thread first.当处理器遇到优先级较高的线程时,它将挂起当前线程,并首先执行优先级较高的线程。

b、Thread priority is an integer value between 1 and 10;线程优先级是介于1和10之间的整数值。

c、The higher the thread priority number, the higher the priority;线程优先级越高,优先级就越高

d、Each thread that is ready will be queued according to priority, and the thread with higher priority will be called first每个准备好的线程将根据优先级排队,优先级较高的线程将首先被调用

答案:a

6、Which of the following will cause the thread to leave the control and wait until another thread wakes it up?以下哪一项会导致线程离开控制并等待另一个线程唤醒它?

waitsleepnotifysynchronized

答案:wait

7、Jerry wrote the following program, predict the impossible output?Jerry写了下面的程序,预测不可能的输出?

class Passenger extends Thread{
 String name;
 Passenger(String name){
  this.name = name;
 }
 public void takeBus() {
  System.out.println(name + "" arrives at the bus stop"");
  System.out.println(name + "" leaves the bus stop"");
 }
 public void run() {
  this.takeBus();
 }
 public static void main(String[] args) {
  Passenger tom = new Passenger(""tom"");
  Passenger jerry = new Passenger(""jerry"");
  tom.start();
  jerry.start();
 }
}
jerry arrives at the bus stop tom arrives at the bus stop jerry leaves the bus stop tom leaves the bus stoptom arrives at the bus stop tom leaves the bus stop jerry arrives at the bus stop jerry leaves the bus stoptom arrives at the bus stop jerry arrives at the bus stop jerry leaves the bus stop tom leaves the bus stopjerry leaves the bus stop tom leaves the bus stop tom arrives at the bus stop jerry arrives at the bus stop

答案:

jerry leaves the bus stop

tom leaves the bus stop

tom arrives at the bus stop

jerry arrives at the bus stop

【二】使用数据流

8、Jerry writes a program that needs to read the data from file stored on the hard disk. Which one of the following class can be used?Jerry编写了一个程序,需要从存储在硬盘上的文件中读取数据。以下哪一个类可以使用?

FileWriterObjectOutputStreamBufferedReadernone of the above

答案:BufferedReader

9、Which method in BufferedInputStream can skip n bytes of data in the stream?BufferedInputStream中的哪种方法可以跳过流中的n个字节的数据?

markskipreset

close

答案:skip

10、How to declare the size of the buffer in bufferedInputStream?如何在bufferedInputStream中声明缓冲区的大小?

declare the length of the byte array in the read methoddeclare by skipdeclare by the size parameter in the constructordeclare by available

答案: declare by the size parameter in the constructor(通过构造函数中的size参数声明

11、Which statement is incorrect about input and output streams?关于输入流和输出流,哪个语句不正确?

The FileWriter class writes character data to a fileFileInputStream reads data and byte streams from files, while FileReader reads characters from files, not bytesBufferedReader uses in-memory buffers to improve the efficiency of read operationsFileOutputStream sets whether the content is written to the end of the file or the beginning through setAppend

答案:FileOutputStream sets whether the content is written to the end of the file or the beginning through setAppend(FileOutputStream通过setAppend设置将内容写入文件的末尾或者开头)

12、To implement serialization, which interface needs to implement by serialized class?要实现序列化,需要通过序列化类实现哪个接口?

SerializableiinterfaceOutputStreamOobjectOutputStream

答案:Serializable

13、Which of the following input streams can be used for deserialization?以下哪些输入流可以用于反序列化?

FileWriterFileReaderObjectInputStreamObjectOutputStream

答案:ObjectInputStream

【三】使用JDBC连接Java与MySQL

14、Which of the following statement is /statements are correct: A: JDBC architecture is divided into JDBC application layer and JDBC driver layer B: JDBC application layer is responsible for managing database connections and providing standard database access interfaces

以下哪一条是正确的:

A:JDBC体系结构分为JDBC应用层和JDBC驱动层

B:JDBC应用层负责管理数据库连接并提供标准的数据库访问接口

答案:A B都对

15、Which of the following is not a type of JDBC driver?
以下哪项不是JDBC驱动程序类型?

JDBCODBC Bridge DriverNative API drivernative protocol driverDistributed driver

答案:Distributed driver

16、Which one of the following JDBC class loads the driver for the database?以下哪个是JDBC类“加载数据库”的驱动程序?

DriverManagerConnectionStatementResultSet

答案:DriverManager

17、Jerry wants to use JDBC to access the database, order the following steps correctly:
1: Create and execute SQL statements 2: Handle SQL exceptions 3: Connect to database 4: Load drivers

Jerry想要使用JDBC来访问数据库,请正确地顺序执行以下步骤:
1:创建并执行SQL语句

2:处理SQL异常

3:连接到数据库

4:加载驱动程序

答案:4312

18、Which of the following methods in Statement can be used to execute a delete statement?语句中的以下哪种方法可以用于执行删除语句?

executeQueryexecuteUpdateexecuteDeleteexecute

答案:executeUpdate

19、 which of the following statements is incorrect about SQL exceptions?以下关于SQL异常的语句中哪一条是不正确的?

SQLException returns its SQL state via getSQLState( )SQLException is a checked exceptionIn SQLException, each database vendor uses the same error codeSQLException returns the error code through getErrorCode( )

答案:In SQLException, each database vendor uses the same error code(在SQLException中,每个数据库供应商使用相同的错误代码)

20、Below are the description about classes we use in JDBC operation. Which one of these is wrong?下面是关于我们在JDBC操作中使用的类的描述。其中哪一个是错的?

By setting, the corresponding table data can be updated when the ResultSet objectPreparedStatement is more secure than StatementIn JDBC applications, when a group of SQL statements are executed, the transaction is automatically committed by defaultBy setting the set of ConnectionAutoCommit(true) to change the commit mode to manual

答案:By setting the set of ConnectionAutoCommit(true) to change the commit mode to manual(通过设置ConnectionAutoCommit(true)集将提交模式更改为手动)

【四】内部类和类型转换

1、Tom wrote the following code, please identify the relationship between the School class and the Student;Tom 编写了以下代码,请确定 School 类和 Student 类之间的关系。

 class. class School{ static class Student{ } }

Student has no relationship with School

学生与学校没有关系

The Student class is a member inner class of the School class

学生类是学校班级的成员内部类

The Student class is a static inner class of the School class

学生类是学校类的静态内部类

The Student class is a partial inner class of the School class

学生是学校的部分内部类

答案:The Student class is a static inner class of the School class(学生类是学校类的静态内部类)

2、Tom wrote the following code to call the object instance of Student, which line of code is wrong? Tom 编写了以下代码来调用 Student 的对象实例,哪一行代码出错了?

class School{ 
    class Student{ 
        public void showName(){ 
            System.out.println("msg from student"); 
            } 
    } 
    public void showSchool() { 
        Student stu1 = new Student(); // 1 
    } 
} 
class Meeting{ 
    public void meetMsg() { 
        Student stu2 = new Student(); // 2 
        Student stu3 = new School.Student(); //3 
        School sch = new School(); 
        Student stu4 = sch.new Student(); //4 
        } 
}

答案: 2&3

3、Which of following description about type conversion is wrong?以下关于类型转换的描述中哪一个是错误的?

The int type can be automatically converted to the long type, which is an implicit conversionint

int可以自动转换为 long 类型,这是一种隐式转换

Double type cannot be automatically converted to float type, it is an explicit conversion

double类型不能自动转换为float类型,是显式转换

There is no risk of partial data loss when converting from int to char

从 int 转换为char时没有部分数据丢失的风险

There is no risk of partial data loss when converting a short type to an int type

将short 转换为 int 类型时没有部分数据丢失的风险

答案:There is no risk of partial data loss when converting from int to char(从 int 转换为char时没有部分数据丢失的风险)

4、Tom declares the following classes, which is the following object reference conversion error?Tom 声明了以下类,下面是哪些对象引用转换错误?

class Student{ }

class ComputerStudent extends Student{ }

class ArtStudent extends Student{ }

//a
ComputerStudent cs = new ComputerStudent(); 
Student stu = cs;	
//b
ArtStudent as = new ArtStudent(); 
Student stu = (Student)as;
//c
Sstudent stu = new Student();
ArtStudent as = stu;	
//d
Sstudent stu = new ArtStudent(); 
ArtStudent as = (ArtStudent)stu;

答案:c

5、Tom wrote the following code, what will be the output of the code? Tom 写了下面的代码,代码的输出会是什么?

class Student{ 
    public void showType() { 
        System.out.println("Student"); 
    } 
} 
class ComputerStudent extends Student{ 
    public void showType() { 
        System.out.println("Computer Student");
    } 
} 
class ArtStudent extends Student{ 
    public void showType() { 
    System.out.println("Art Student"); 
    } 
} 
class Invite{ 
    public static void main(String[] args) { 
        Student stu = new Student(); 
        Student cs = new ComputerStudent( ); 
        ArtStudent as = (ArtStudent)stu; 
        cs.showType(); as.showType(); 
        } 
}
Computer Student Art StudentComputer Student StudentSstudent Art Studentruntime error

答案:runtime error(编译错误)

6、Tom wrote the following regular expression program, what will be the output? Tom 编写了下面的正则表达式程序,输出会是什么?

Pattern myPattern = Pattern.compile(":");
String[] split = myPattern.split("one:two:three:four:", 2);
for(String element:split) { 
    System.out.println(" element = " + element); 
}
runtime errorelement = one element = two:three:four:element = one element = two element = three element = fourelement = one element = two element = three element = four element =

答案:element = one element = two:three:four:

【五】使用正则表达式和本地化

7、Identify the output of the following code 标识以下代码的输出

boolean matches = Pattern.matches("[[^dz]&&[^0-9]]{5,}", "abcdef");
System.out.println(matches ? "match": "Mismatch");
runtime errormatchMismatchno output

答案:Mismatch

8、Which one of the following class can implement the localization operation of text date and other information以下哪一个类可以实现文本日期等信息的本地化操作

LocaleDateTimeFormatterPatternException

答案:Locale

9、Tom wants to define a generic class, which of the following method satisfy the requirements?Tom 想要定义一个泛型类,以下哪种方法满足要求?

class Person{ <T> }class Person<T>{ }class <T>Person{ }class Person{ }

答案:b、class Person<T>{ }

【六】使用泛型和集合

10、Tom wants to define a generic method in a class. Which one of the following is correct code?Tom 想要在类中定义一个泛型方法。以下哪一项是正确的代码?

public <T> T showValue(T val) { return val; }public T showValue(T val) { return val; }public T showValue(T val) <T>{ return val; }public T showValue(<T> T val) { return val; }

答案:a、public <T> T showValue(T val) { return val; }

11、Tom wants to implement an ArrayList of Strings. Which one of the following line of code cannot satisfy his requirements?Tom 想要实现一个字符串数组列表。以下哪一行代码不能满足他的要求?

List<String> strList = new ArrayList<String>();ArrayList<String> strList = new ArrayList<String>();List<> strList = new ArrayList<String>();ArrayList<String> strList4 = new ArrayList<>();

答案:3、List<> strList = new ArrayList<String>();

12、Tom implements a method show, which of the following is wrong about the invocation of the show method? Tom 实现了 show 方法,以下哪项关于 show 方法的调用是错误的?

public void show(List<? extends Number> list) { }
show(new ArrayList<String>())show(new ArrayList<Integer>())show(new ArrayList<Double>())show(new ArrayList<Byte>())

答案:1、show(new ArrayList<String>())

13、Tom wrote a collection utility, what will be the output? Tom 写了一个集合实用程序,输出会是什么?

Set<Integer> dataSet = new HashSet<Integer>(); 
    dataSet.add(111); 
    dataSet.add(222); 
    dataSet.add(111); 
    dataSet.add(222); 
    dataSet.add(333); 
for (int data : dataSet) { 
    System.out.println(data);
}

答案:333 222 111

14、Which of the following collection classe can guarantee that elements are unique and sorted以下哪个集合类可以保证元素是唯一且已排序的?

HashSetLinkedListTreeSetLinkedHashSet

答案:TreeSet

15、Which of the following statements is false about collection classes?以下关于集合类的陈述中哪项是错误的?

Vector's methods are synchronized and thread-safe

Vector的方法是同步的,线程安全

TreeSet can sort the elements in the collection

TreeSet可以对集合中的元素进行排序

HashSet insertion is faster than TreeSet

HashSet的插入速度比TreeSet快

The methods of ArrayList are synchronized and are thread-safe like Vector.

ArrayList的方法是同步的,并且像Vector一样是线程安全的

答案:4、

The methods of ArrayList are synchronized and are thread-safe like Vector.

ArrayList的方法是同步的,并且像Vector一样是线程安全的

16、Jerry wrote the following code, what will be the output after running the code?
“Jerry 写了下面的代码,运行代码后输出会是什么?

class Person{ 
    private int id; 
    Person(int id){ 
        this.id = id; 
    } 

    public void show() { 
        System.out.println(this.id); 
    } 
    public static void main(String[] args){ 
        Set <Person> personSet = new TreeSet<Person>(); 
        personSet.add(new Person(10)); 
        personSet.add(new Person(1)); 
        personSet.add(new Person(100)); 
        for(Person person : personSet) { 
            person.show(); 
            }
    }
 }
runtime error10 1 1001
10 100
100 10 1

答案:runtime error(运行错误)

17、Jerry wrote the following code, find out the correct output?Jerry 写了下面的代码,运行代码后输出会是什么?

Map<String, String> dataMap = new TreeMap<String, String>();
    dataMap.put("tom", "from batch 01"); 
    dataMap.put("jerry", "from batch 01"); 
    dataMap. put("linda", "from batch 01");
    dataMap.put("tom", "from batch 02"); 
System.out.println(dataMap.get("tom"));
from batch01from batch02tomruntime error

答案:b、from batch02(二班牛逼

【七】函数式编程

18、Jerry needs a functional interface, which one of the following is correct lines of code ?Jerry需要一个功能接口,以下哪一行是正确的代码行?

//a
interface Travel{ 
    int id = 123456;
    void getToSchool( ); 
}	
//b
class Travel{ 
    void getToSchool( ) { } void leaveSchool( ) { } 
}	
//c
@FunctionalInterface 
    interface Travel{ 
        void getToSchool( ); 
        void leaveSchool( ); 
    }	
//d
class Travel{ 
    int id = 123456;
    void getToSchool() { } 
}

答案:a

19、Jerry needs to implement the following interface through lambda, which one will be the wrong option for it? Jerry 需要通过 lambda 实现以下接口,哪一个会是错误的选择?

interface Math{ 
    int add(int valA, int valB); 
}
Math math = (int valA, int valB) -> valA + valB;Math math = (int valA, int valB) -> { return valA + valB; };Math math = (valA, valB) -> { return valA + valB; };Math math = (valA, valB) -> return valA + valB;

答案:4、Math math = (valA, valB) -> return valA + valB;

20、Which of the following functional interface can be used for consumption scenarios, data processing, and method invocation that satisfies the input of an object and no return value?以下哪一个功能接口可以用于满足对象输入且无返回值的消费场景、数据处理和方法调用?

PredicateConsumerSupplierFunction

答案:2、Consumer

21、Which one in the stream API can be used to implement pagination?流 API 中的哪一个可用于实现分页?

ffiltersortedmapnone of the above

答案:4、none of the above

22、Jerry wrote the following code, what will be the output of the code? Jerry 写了下面的代码,代码的输出会是什么?

Consumer<List<String>> cons = (list) -> { 
    for(String s : list) { 
        System.out.println(s); 
    } 
}; 

ArrayList<String> list = new ArrayList<String>(); 
    list.add("tom");
    list.add("jerry"); 
    list.add("linda"); 
    list.add("jerry"); 
    list.add("tom"); 
cons.accept(list) ;
runtime errortom jerry lindatom jerry linda jerry tomtom tom jerry jerry linda

答案:3、tom jerry linda jerry tom

23、Which one the following statement about StreamAPI's is incorrect?以下关于 StreamAPI 的陈述哪一个不正确?

StreamAPI strictly distinguish execution order

Stream API严格区分执行顺序

StreamThe result of the previous operation of the API is the raw material for the latter operation

StreamAPI 上一个操作的结果是后一个操作的原材料

Pipeline operations are divided into intermediate operations and terminal operations

管道操作分为中间操作和终端操作

Intermediate operations can output results but cannot be connected to other operations

中间操作可以输出结果,但不能连接到其他操作

答案:4、Intermediate operations can output results but cannot be connected to other operations(中间操作可以输出结果,但不能连接到其他操作

24、Which one of the following can be used to convert the list into a parallel stream?以下哪一项可用于将列表转换为并行流?

list.stream( ).parallel( )list.stream( )list.parallel( )list.streamParallel( )

答案:1、list.stream( ).parallel( )

以上为全部练习题及答案。祝君武运昌隆,旗开得胜!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值