java-common mistakes

[Single choice]

1. Which of the following codes is correct? ()

List list=new ArrayList<>();
list.add(“lily”);
list.add(“lucy”);
list.add(“tom”);
Iterator iterator=(1);
while(____(2)_____){
System.out.println(iterator.next());
}
A(1)list.iterator() (2) iterator.next()
B(1)list.listIterator() (2)iterator.next()
C(1)list.getIterator() (2)iterator.hasNext()

D(1) list.listIterator() (2) Iterator.hasNext()
analysis: D
list.listIterator() and list.iterator() can get iterators, because ListIterator inherits the Iterator interface; iterator.hasNext() can Determine whether there are any more elements.
2. The difference between Array Array and List ArrayList is wrong

AArrayList provides more methods. Array has no methods.
BArray can contain basic types and object types. ArrayList can only contain reference types
. The length of Array is fixed, and the size of ArrayList changes dynamically.

D collection can store basic data types.
Analysis: D
collection uses automatic boxing to reduce coding workload when processing basic data types. However, when dealing with fixed-size basic data types, this method is relatively slow
. 3.
The following statement will modify the address (address) and add the city name (city) ()

A、Update Person Address=‘Zhongshan 23’,City=‘Nanjing’ WHERE LastName=‘Wilson’

B、Update Person SET Address=‘Zhongshan 23’,City=‘Nanjing’

C、Update Person SET Address=‘Zhongshan 23’,City=‘Nanjing’ WHERE LastName=‘Wilson’ and Address=‘Zhongshan 23’

D、Update Person SET Address=‘Zhongshan 23’,City=‘Nanjing’ WHERE LastName=‘Wilson’
D
4.3.
[单选]

The following statement that cannot correctly perform string assignment is

Achar str [] = "good!";

Bchar str[5]=('g','o','o','d');
Cchar str[5]="good!";
Dchar *str="good!";
Analysis: B
char str[ 5] is to define a character array containing five elements, the D option is str[0]='g',str[1]='o', str[2]='o',str[3]= 'd', str[4] is not assigned, but the vc compiler system automatically assigns it to'\0'.
5.
[Multiple choice]

Comparator provides those methods

equal
Btostring

Ccompare
DCompare To
analysis: AC
as an interface Comparator provides two abstract methods;

Compare() compares the two parameters used to sort;

Equals() indicates whether some other object is "equal to" this Comparato
6.
[Single choice]

The output of the following program segment is

String s=“ABCD”;

s.concat(“E”);

s.replace(C,F);

System.out.println(s);

AABCD
BABFDE
CABCDE
DABCDEF
analysis: A
String s = "ABCD"; is a constant and will not change.
7. What
is correct about the Map interface?

AHashMap allows any type of key and value objects, and does not allow null to be used as a key or value.
BHashMap, HashSet, and HashTable are all Map implementation classes

The hashCode() and equals() of the two keys stored in CHashMap will have the same return value, which will be overwritten.
DHashMap is thread-safe, Hashtable is non-thread-safe, and faster than HashMap.
Analysis: C
HashMap is non-thread-safe and speed Will be fast

HashMap allows Null value as key, HashTable does not allow

HashSet is an implementation class of Set
8.
What methods are provided in the comparable interface

Atostring
Bequals

Answer to CcompareTo
Dcompare
: C
9.
[single choice]

The keywords used to throw exceptions in java are

Athrows
Bcatch

Cthrow
Dtry
answer: C
10.
The difference between StringBuilder and StringBuffer in java, what is wrong in the following statement?

A: StringBuffer is thread safe

B: StringBuilder is not thread-safe

C: When StringBuffer changes the String type, it is actually equivalent to generating a new String object and then pointing the pointer to the new String object.

D: Efficiency comparison String<StringBuffer<StringBuilder, but String S1 = "This is only a" + "simple"

  • When "test", String is the most efficient.

AB
BD
CA

DC
analysis: D
String in Java is a class, not a basic data type. String is passed by reference, not passed by value.

StringBuffer and StringBuilder, the two methods are not very different. But in terms of thread safety, StringBuffer allows multiple threads to perform character operations.
11. The
following description of the advantages and disadvantages of using Comparatable and Comparator for overall sorting is correct

The disadvantage of AComparatable is that for multi-element sorting, its sorting basis element is fixed

The advantage of BComparatable is that direct natural sorting can be achieved for single-element collections

The advantage of CComparator is that the sorting of elements is variable according to the elements

The disadvantage of DComparator is that whether it is multi-element or single-element, you must create an external class yourself to achieve sorting.
Analysis:
all the above are correct
12

[Single choice]

Run the following code, when the input num value is a, the system will output

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
int num = input.nextInt();
System.out.println(“one”);
} catch(Exception e) {
System.out.println(“two”);
} finally {
System.out.println(“three”);
}
System.out.println(“end”);
}
Atwo end
Bone two three end

Ctwo three end
Done three end
analysis: C an exception will terminate the entire program
13

[Single choice]

The syntax error in the following statement is

Achar *ss[6];ss[1]=“right?”;
Bchar *ss[]=(“right?”);

Cchar ss[6][20];ss[1]="right?";
Dchar ss[][20]=("right?");
Analysis:
Cannot use assignment to directly assign a string constant to a string Array.
14
[single choice]

The following description of the File class is correct

The AFile class cannot read and write files. The
BFile class cannot manipulate file attributes. The
CFile class describes the attributes of the file object in a system-related way
. The DFile class is a non-streaming class in the java. io. file package.
Answer: A
15

[Single choice]

Which of the following stream classes belong to byte-oriented input streams ()

AInputStreamReader

BFileInputStream
CBufferedWriter
DObjectOutputStream
答案:B

Guess you like

Origin blog.csdn.net/huiguo_/article/details/109023483