Ocjp (1z0-851) simulation question analysis (7)

Source: Internet
Author: User

Exam: 1z0-851 Java Standard Edition 6 programmer certified professional exam

All of the following analyses are based on my own analysis or reference on the Internet. You may also question my analysis.

Question 201

Given:
1. Public class teststring3 {
2. Public static void main (string [] ARGs ){
3. // insert code here
5. system. Out. println (s );
6 .}
7 .}
Which two code fragments, inserted independently at line 3, generate the output 4247? (Choose two .)
A. String's = "123456789 ";
S = (S-"123"). Replace (1, 3, "24")-"89 ";
B. stringbuffer S = new stringbuffer ("123456789 ");
S. Delete (). Replace (, "24"). Delete );
C. stringbuffer S = new stringbuffer ("123456789 ");
S. substring (3, 6). Delete (1, 3). insert (1, "24 ");
D. stringbuilder S = new stringbuilder ("123456789 ");
S. substring (3, 6). Delete (1, 2). insert (1, "24 ");
E. stringbuilder S = new stringbuilder ("123456789 ");
S. Delete (). insert (1, "24 ");
Answer: Be

The substring (INT start, int end) function is a method in the stringbuffer class and is a substring that returns a specific index (index from start to end-1, another method is public stringbuffer Delete (INT start, int end), which is a character used to delete a specific index (index from start to end-1. Both B and E can output 4247.

Question 202
Given:
11. String test = "Test A. Test B. Test C .";
12. // insert code here
13. String [] result = test. Split (RegEx );
Which regular expression, inserted at line 12, correctly splits test into "test a", "Test B", and "Test C "?
A. String RegEx = "";
B. String RegEx = "";
C. String RegEx = ".*";
D. String RegEx = "\ s ";
E. String RegEx = "\. \ s *";
F. String RegEx = "\ W [\.] + ";
Answer: E

Point and space. Select E.OneVertex andOne or noSpace

Question 203
Which statement is true?
A. a class's finalize () method cannot be invoked explicitly.
B. Super. Finalize () is called implicitly by any overriding finalize () method.
C. The finalize () method for a given object is called no more than once by the garbage collector.
D. The order in which finalize () is called on two objects is based on the order in which the two objects
Became finalizable.
Answer: c

The finalize () method will only be called before garbage collection. If there is no garbage collection, it will not be called.

The finalize () method can be explicitly called, such as super. Finalize (). The finalize () method that is rewritten in the subclass does not implicitly call the finalize () of the parent class, and requires the programmer's own super. Finalize ().

Question 204
Given:
11. Public class itemtest {
12. Private Final int ID;
13. Public itemtest (int id) {This. ID = ID ;}
14. Public void updateid (INT newid) {id = newid ;}
15.
16. Public static void main (string [] ARGs ){
17. itemtest fa = new itemtest (42 );
18. Fa. updateid (69 );
19. system. Out. println (Fa. ID );
20 .}
21 .}
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The attribute ID in the itemtest object remains unchanged.
D. The attribute ID in the itemtest object is modified to the new value.
E. A new itemtest object is created with the preferred value in the ID attribute.
Answer:

Once the final variable is assigned a value, it cannot be modified ~~

Question 205
Given:
11. Interface declarestuff {
12. Public static final int easy = 3;
13. Void dostuff (int t );}
14. Public class testdeclare implements declarestuff {
15. Public static void main (string [] ARGs ){
16. Int x = 5;
17. New testdeclare (). dostuff (++ X );
18 .}
19. Void dostuff (INT s ){
20. S + = easy ++ S;
21. system. Out. println ("S" + S );
22 .}
23 .}
What is the result?
A.s 14
B. S 16
C. s 10
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: d

The access modifier of the dostuff () function is reduced ~~ The interface is pubic void dostuff (), which becomes the default value in testdeclare ~~

Question 207
Given:
11. Public class commander {
12. Public static void main (string [] ARGs ){
13. String myprop =/* insert code here */
14. system. Out. println (myprop );
15 .}
16 .}
And the command line:
Java-dprop. Custom = gobstopper Commander which two, placed on line 13, will produce the output
Gobstopper? (Choose two .)
A. System. Load ("prop. Custom ");
B. System. getenv ("prop. Custom ");
C. System. Property ("prop. Custom ");
D. system. getproperty ("prop. Custom ");
E. System. getproperties (). getproperty ("prop. Custom ");
Answer: de

Question 208
Given:
1. Interface dostuff2 {
2. Float getrange (INT low, int high );}
3.
4. Interface domore {
5. Float getavg (int A, int B, int C );}
6.
7. abstract class doabstract implements dostuff2, domore {}
8.
9. Class dostuff implements dostuff2 {
10. Public float getrange (int x, int y) {return 3.14f ;}}
11.
12. Interface doall extends domore {
13. Float getavg (int A, int B, int C, int D );}
What is the result?
A. The file will compile without error.
B. Compilation fails. Only Line 7 contains an error.
C. Compilation fails. Only line 12 contains an error.
D. Compilation fails. Only Line 13 contains an error.
E. Compilation fails. Only lines 7 and 12 contain errors.
F. Compilation fails. Only lines 7 and 13 contain errors.
G. Compilation fails. Lines 7, 12, and 13 contain errors.

!!! Be sure to pay attention to it! In this special case, abstract classes can be implemented without implementing the implements interface. Instead, abstract classes are left to their subclasses for implementation !!!!

Compiled successfully !!

Question 211
Given:
1. Package com. Company. Application;
2.
3. Public class mainclass {
4. Public static void main (string [] ARGs ){}
5 .}
And mainclass exists in the/apps/COM/Company/application directory. Assume the classpath
Environment variable is set to "." (current directory). Which two Java commands entered at the command
Line will run mainclass? (Choose two .)
A. Java mainclass if run from the/apps directory
B. Java com. Company. application. mainclass if run from the/apps directory
C. Java-classpath/apps com. Company. application. mainclass if run from any directory
D. Java-classpath. mainclass if run from the/apps/COM/Company/application directory
E. Java-classpath/apps/COM/Company/Application:. mainclass if run from the/apps directory
F. Java com. Company. application. mainclass if run from the/apps/COM/Company/application directory
Answer: BC

Question 212
Given:
12. Import java. util .*;
13. Public class assumer2 {
14. Public static void main (string [] ARGs ){
15. treeset <integer> S = new treeset <integer> ();
16. treeset <integer> subs = new treeset <integer> ();
17. For (INT I = 606; I <613; I ++)
18. If (I % 2 = 0) S. Add (I );
19. Subs = (treeset) S. subset (608, true, 611, true );
20. S. Add (629 );
21. system. Out. println (S + "" + subs );
22 .}
23 .}
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. [608,610,612,629] [608,610]
D. [608,610,612,629] [608,610,629]
E. [606,608,610,612,629] [608,610]
F. [606,608,610,612,629] [608,610,629]
Answer: E

Question 213
Given that the elements of a priorityqueue are ordered according to natural ordering, and:
2. Import java. util .*;
3. Public class getinline {
4. Public static void main (string [] ARGs ){
5. priorityqueue <string> PQ = new priorityqueue <string> ();
6. PQ. Add ("banana ");
7. PQ. Add ("Pear ");
8. PQ. Add ("apple ");
9. system. Out. println (PQ. Poll () + "" + PQ. Peek ());
10 .}
11 .}
What is the result?
A. apple pear
B. Banana Pear
C. Apple
D. Apple Banana
E. Banana banana
Answer: d

PQ. Poll () and PQ. Peek (), the former is to retrieve the first and return, the latter is to return the first copy

Question 214
Given a pre-generics implementation of a method:
11. Public static int sum (list ){
12. Int sum = 0;
13. For (iterator iter = List. iterator (); ITER. hasnext ();){
14. Int I = (integer) ITER. Next (). intvalue ();
15. Sum + = I;
16 .}
17. Return sum;
18 .}
What three changes allow the class to be used with generics and avoid an unchecked warning? (Choose
Three .)
A. Remove line 14.
B. Replace line 14 with "int I = ITER. Next ();".
C. Replace line 13 with "For (int I: intlist ){".
D. Replace line 13 with "For (iterator ITER: intlist ){".
E. Replace the method declaration with "sum (list <int> intlist )".
F. Replace the method declaration with "sum (list <integer> intlist )".
Answer: ACF

Question 215
Given:
34. hashmap props = new hashmap ();
35. Props. Put ("key45", "some value ");
36. Props. Put ("key12", "Some other value ");
37. Props. Put ("key39", "yet another value ");
38. Set S = props. keyset ();
39. // insert code here what, inserted at line 39, will sort the keys in the props hashmap?
A. arrays. Sort (s );
B. S = new treeset (s );
C. Collections. Sort (s );
D. S = new sortedset (s );
Answer: B

The static method sort () in the arrays class must implement the object array of the comparable interface, that is, the public static void sort (object [] A). Here, a set is sorted and does not match.
Collections. Sort (s) the parameter must be a list <t> object that implements the comparable interface. Here, an object that implements the set interface is sorted and does not match.

Sortedset is an interface.

Question 216
Given:
11. Public class person {
12. Private string name;
13. Public Person (string name ){
14. This. Name = Name;
15 .}
16. Public Boolean equals (Object O ){
17. If (! (O instanceof person) return false;
18. Person P = (person) O;
19. Return P. Name. Equals (this. Name );
20 .}
21 .}
Which statement is true?
A. Compilation fails because the hashcode method is not overridden.
B. A hashset cocould contain multiple person objects with the same name.
C. All person objects will have the same hash code because the hashcode method is not overridden.
D. If a hashset contains more than one person object with name = "Fred", then removing another person,
Also with name = "Fred", will remove
Them all.
Answer: B

The person class does not override the hashcode () method. Therefore, the storage address is returned when the hashcode () method of the object is called. The storage addresses of these different person objects are certainly different.

When a hashset stores an object, it first compares the hashcode. If the return value of hashcode is different, the two elements must be different and new elements can be added.

The add method of hashset is as follows:

public boolean add(E e) {        return map.put(e, PRESENT)==null;    }

Here, persent is an empty object of the object type. That is to say, the hashset class uses the PUT Method of hashmap ~~ The two work in the same way. Here e is equivalent to the key ~~ in hashmap ~~

Question 217
Given:
3. Import java. util .*;
4. Public class Hancock {
5. // insert code here
6. List. Add ("foo ");
7 .}
8 .}
Which two code fragments, inserted independently at line 5, will compile without warnings? (Choose two .)
A. Public void addstrings (list ){
B. Public void addstrings (list <string> List ){
C. Public void addstrings (list <? Super String> List ){
D. Public void addstrings (list <? Extends string> List ){
Answer: BC

Pecs rule. The list here is the consumer, and super ~~ is used ~~

Question 218
Given:
1. Public class threads4 {
2. Public static void main (string [] ARGs ){
3. New threads4 (). Go ();
4 .}
5. Public void go (){
6. runnable r = new runnable (){
7. Public void run (){
8. system. Out. Print ("foo ");
9 .}
10 .};
11. thread t = new thread (R );
12. T. Start ();
13. T. Start ();
14 .}
15 .}
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The Code executes normally and prints "foo ".
D. The Code executes normally, but nothing is printed.
Answer: B

Started twice ~~ Illegalstateexception

Question 219
Given:
1. Public class testone {
2. Public static void main (string [] ARGs) throws exception {
3. thread. Sleep (3000 );
4. system. Out. println ("Sleep ");
5 .}
6 .}
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The Code executes normally and prints "Sleep ".
D. The Code executes normally, but nothing is printed.
Answer: c

:

Question 220
Given:
1. Public class testseven extends thread {
2. Private Static int X;
3. Public synchronized void dothings (){
4. Int current = X;
5. Current ++;
6. x = current;
7 .}
8. Public void run (){
9. dothings ();
10 .}
11 .}
Which statement is true?
A. Compilation fails.
B. An exception is thrown at runtime.
C. synchronizing the run () method wocould make the class thread-safe.
D. The data in variable "X" are protected from concurrent access problems.
E. Declaring the dothings () method as static wocould make the class thread-safe.
F. wrapping the statements within dothings () in a synchronized (new object () {} block wocould make
Class thread-safe.

Answer: E

Note that the X variable is static, and the dothings () function is non-static.

Thread security issues. If nothing is done, here the dothings () function can be called by different testseven objects to change X, so that the value of X cannot be increased as expected, this is NOT thread-safe.

Public static synchronized void dothings () can be used to convert the dothings () function into a class-level function and use a class lock;

You can also change X to private int X and convert X to object-level. The X and dothings () functions use Object locks.

By referencing other people, the common synchronization method locks the monitor associated with this, while the static synchronization method is the monitor associated with the class object corresponding to the class object containing this method;

Question 221
Which two code fragments will execute the method dostuff () in a separate thread? (Choose two .)
A. New thread (){
Public void run () {dostuff ();}
};
B. New thread (){
Public void start () {dostuff ();}
};
C. New thread (){
Public void start () {dostuff ();}
}. Run ();
D. New thread (){
Public void run () {dostuff ();}
}. Start ();
E. New thread (New runnable (){
Public void run () {dostuff ();}
}). Run ();
F. New thread (New runnable (){
Public void run () {dostuff ();}
}). Start ();
Answer: DF

The run () method can only be executed on the main thread, while the START () method creates a subthread for execution ~~

Question 222
Given:
11. Public static void main (string [] ARGs ){
12. Object OBJ = new int [] {1, 2, 3 };
13. Int [] somearray = (INT []) OBJ;
14. For (int I: somearray) system. Out. Print (I + "");
15 .}
What is the result?
A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.
E. A classcastexception is thrown at runtime.
Answer:

Question 224
Given:
11. Public static void parse (string Str ){
12. Try {
13. Float F = float. parsefloat (STR );
14.} catch (numberformatexception NFE ){
15. f = 0;
16.} finally {
17. system. Out. println (f );
18 .}
19 .}
20. Public static void main (string [] ARGs ){
21. parse ("invalid ");
22 .}
What is the result?
A.m. 0.0
B. Compilation fails.
C. A parseexception is thrown by the parse method at runtime.
D. A numberformatexception is thrown by the parse method at runtime.
Answer: B

F of Row 3, undeclared variable ~~ The F scope of row 13 is only within {} of try ~~

Question 226
Given:
1. Interface Testa {string tostring ();}
2. Public class test {
3. Public static void main (string [] ARGs ){
4. system. Out. println (New TESTA (){
5. Public String tostring () {return "test ";}
6 .});
7 .}
8 .}
What is the result?
A. Test
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 1.
E. Compilation fails because of an error in line 4.
F. Compilation fails because of an error in line 5.
Answer:

Very simple local internal class ~~

Question 227
Given:
11. Public interface a {public void M1 ();}
12.
13. Class B implements {}
14. Class C implements a {public void M1 (){}}
15. Class D implements a {public void M1 (int x ){}}
16. abstract class E implements {}
17. abstract class F implements a {public void M1 (){}}
18. abstract class G implements a {public void M1 (int x ){}}
What is the result?
A. compilation succeeds.
B. Exactly one class does not compile.
C. Exactly two classes do not compile.
D. Exactly four classes do not compile.
E. Exactly three classes do not compile.
Answer: c

Class B and class D are incorrect;

Abstract classes do not need to implement implementations ~~ The subclass implementation that is handed over to it is OK ~~

Question 228
Given:
21. abstract class C1 {
22. Public C1 () {system. Out. Print (1 );}
23 .}
24. Class C2 extends C1 {
25. Public C2 () {system. Out. Print (2 );}
26 .}
27. Class C3 extends C2 {
28. Public C3 () {system. Out. println (3 );}
29 .}
30. Public class ctest {
31. Public static void main (string [] A) {New C3 ();}
32 .}
What is the result?
A. 3
B. 23
C. 32
D. 123.
E. 321
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: d

The constructor of the subclass calls the non-parameter constructor of the superclass by default ~~

Question 229

Click the exhibit button. What is the result?

A.m. 4321
B. 0000
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 18.
Answer: d

Constructor problems ~~ No default constructor for the person class

Question 230
Given:
10. Class One {
11. Public one Foo () {return this ;}
12 .}
13. Class Two extends one {
14. Public one Foo () {return this ;}
15 .}
16. Class Three extends two {
17. // insert method here
18 .}
Which two methods, inserted individually, correctly complete the three class? (Choose two .)
A. Public void Foo (){}
B. Public int Foo () {return 3 ;}
C. Public two Foo () {return this ;}
D. Public one Foo () {return this ;}
E. Public object Foo () {return this ;}
Answer: CD

When rewriting a method, the return type can be the original one class or the subclass of one class ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.