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

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 167
Given:
1. Import java. util .*;
2. Public class wrappedstring {
3. Private string S;
4. Public wrappedstring (string s) {This. S = s ;}
5. Public static void main (string [] ARGs ){
6. hashset <Object> HS = new hashset <Object> ();
7. wrappedstring WS1 = new wrappedstring ("aardvark ");
8. wrappedstring WS2 = new wrappedstring ("aardvark ");
9. String S1 = new string ("aardvark ");
10. String S2 = new string ("aardvark ");
11. Hs. Add (WS1); HS. Add (WS2); HS. Add (S1); HS. Add (S2 );
12. system. Out. println (HS. Size ());}}
What is the result?
A. 0
B. 1
C. 2
D. 3
E. 4
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: d
The hashcode () of wrappedstring is inherited from the object class, while the hashcode () of string is overwritten only related to the content of string. The storage address of WS1 and WS2 is different, so the return value of hashcode () is different, while the content of S1 and S2 strings is the same and the hash value is the same.
Question 168
Given a class whose instances, when found in a collection of objects, are sorted by using the compareto ()
Method, which two statements are true? (Choose two .)
A. The class implements java. Lang. comparable.
B. The class implements java. util. comparator.
C. The interface used to implement sorting allows this class to define only one sort sequence.
D. The interface used to implement sorting allows this class to define except different sort sequences.
Answer: The accomparable interface has only one compareto () function ~~ Remember


Question 169
Given:
1. Import java. util .*;
2. Public class example {
3. Public static void main (string [] ARGs ){
4. // insert code here
5. Set. Add (New INTEGER (2 ));
6. Set. Add (New INTEGER (1 ));
7. system. Out. println (SET );
8 .}
9 .}
Which code, inserted at line 4, guarantees that this program will output [1, 2]?
A. Set set = new treeset ();
B. Set set = new hashset ();
C. Set set = new sortedset ();
D. List set = new sortedlist ();
E. Set set = new linkedhashset ();
Answer:
It must be a sorted set, so treeset is suitable. Hashset should also be acceptable. The hashcode () function of the integer type returns the corresponding int type value. If there is only one answer, treeset is the best option. Sortedset is an interface, and there is no sortedlist class in Java.

Question 171
Drag drop
Click the task button.

Answer:
Polymorphism ~~ The getname () of A and B are both Class A methods. For this method, the name variable in B cannot be seen.
Question 177
Given:
1. Class testexception extends exception {}
2. Class {
3. Public String sayhello (string name) throws testexception {
4. If (name = NULL) throw new testexception ();
5. Return "hello" + name;
6 .}
7 .}
8. Public class Testa {
9. Public static void main (string [] ARGs ){
10. New A (). sayhello ("Aiko ");
11 .}
12 .}
Which statement is true?
A. compilation succeeds.
B. Class A does not compile.
C. The method declared on line 9 cannot be modified to throw testexception.
D. Testa compiles if Line 10 is enclosed in a try/Catch Block that catches testexception.
Answer: d
Line 10 new A (). sayhello ("Aiko"); will throw a testexception (), so either throws testexception ., Either try {} catch (testexception e ){}

Question 178
Given:
11. Public static void main (string [] ARGs ){
12. For (INT I = 0; I <= 10; I ++ ){
13. If (I> 6) break;
14 .}
15. system. Out. println (I );
16 .}
What is the result?
A.6
B. 7
C. 10
D. 11
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: E
I Scope
Question 179
Given:
3. Public class breaker {
4. Static string o = "";
5. Public static void main (string [] ARGs ){
6. Z:
7. O = O + 2;
8. For (INT x = 3; x <8; X ++ ){
9. If (x = 4) break;
10. If (x = 6) Break Z;
11. O = O + X;
12 .}
13. system. Out. println (O );
14 .}
15 .}
What is the result?
A. 23
B. 234
C. 235
D. 2345.
E. 2357
F. 23457
G. Compilation fails.
Answer: G
The checkpoint label can only appear in the previous sentence of the for loop or in the previous sentence of the while loop ~~

Question 180
Given:
5. Class {
6. Void Foo () throws exception {Throw new exception ();}
7 .}
8. Class subb2 extends {
9. Void Foo () {system. Out. println ("B ");}
10 .}
11. Class tester {
12. Public static void main (string [] ARGs ){
13. A = new subb2 ();
14. A. Foo ();
15 .}
16 .}
What is the result?
A. B
B. B, followed by an exception.
C. Compilation fails due to an error on line 9.
D. Compilation fails due to an error on line 14.
E. An exception is thrown with no other output.
Answer: d
The compiler reports an error: unhandled exception type exception must be in the following format:

Try {
A. Foo ();
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

Or throws an exception. This polymorphism is fully implemented according to the parent class method during compilation, that is, to check whether the Foo () method is in Class A during compiler, if an exception is thrown or caught according to the requirements of the parent class, the Foo () method of subb2 is called at runtime. If the subclass method can throw an exception that is not present in the parent class, no exception processor can handle the exception. Therefore, the subclass method is not allowed to throw an exception that is not present in the parent class.


Question 182
Given:
1. Public class mule {
2. Public static void main (string [] ARGs ){
3. boolean assert = true;
4. If (assert ){
5. system. Out. println ("assert is true ");
6 .}
7 .}
8 .}
Which command-line invocations will compile?
A. javac mule. Java
B. javac-source 1.3 mule. Java
C. javac-source 1.4 mule. Java
D. javac-source 1.5 mule. Java
Answer: B
Assertions are introduced in JDK se 1.4. Therefore, assert is not considered a reserved word before.

Question 183
Given:
11. Static void test (){
12. Try {
13. String x = NULL;
14. system. Out. Print (X. tostring () + "");
15 .}
16. Finally {system. Out. Print ("finally ");}
17 .}
18. Public static void main (string [] ARGs ){
19. Try {test ();}
20. Catch (exception ex) {system. Out. Print ("exception ");}
21 .}
What is the result?
A. null
B. Finally
C. null finally
D. Compilation fails.
E. Finally exception
Answer: E
X. tostring () will throw an nulpointerexception
Question 184
Given:
1. Public class boxer1 {
2. Integer I;
3. Int X;
4. Public boxer1 (INT y ){
5. x = I + Y;
6. system. Out. println (X );
7 .}
8. Public static void main (string [] ARGs ){
9. New boxer1 (New INTEGER (4 ));
10 .}
11 .}
What is the result?
A. the value "4" is printed at the command line.
B. Compilation fails because of an error in line 5.
C. Compilation fails because of an error in line 9.
D. A nullpointerexception occurs at runtime.
E. A numberformatexception occurs at runtime.
F. An illegalstateexception occurs at runtime.
Answer: d
The problem lies in the execution of the fifth line.

Question 185
Which two code fragments are most likely to cause a stackoverflowerror? (Choose two .)
A. Int [] x = {1, 2, 3, 4, 5 };
For (INT y = 0; y <6; y ++)
System. Out. println (X [y]);
B. Static int [] x = {7, 6, 5, 4 };
Static {x [1] = 8;
X [4] = 3 ;}
C. For (INT y = 10; y <10; y ++)
Dostuff (y );
D. Void doone (int x) {dotwo (x );}
Void dotwo (INT y) {dothree (y );}
Void dothree (int z) {dotwo (z );}
E. For (INT x = 0; x <1000000000; X ++)
Dostuff (X );
F. Void counter (int I) {counter (++ I );}
Answer: DF
Stackoverflowerror can be implemented through continuous iteration.


Question 186
Given:
11. Static void test () throws runtimeexception {
12. Try {
13. system. Out. Print ("test ");
14. Throw new runtimeexception ();
15 .}
16. Catch (exception ex) {system. Out. Print ("exception ");}
17 .}
18. Public static void main (string [] ARGs ){
19. Try {test ();}
20. Catch (runtimeexception ex) {system. Out. Print ("RunTime ");}
21. system. Out. Print ("end ");
22 .}
What is the result?
A. Test end
B. Compilation fails.
C. Test runtime end
D. Test exception end
E. A throwable is thrown by main at runtime.
Answer: d
If the runtimeexception thrown by row 14th is processed by 16 lines, there will be no more than 20 lines.


Question 187
Given:
11. Public static void main (string [] ARGs ){
12. Integer I = new INTEGER (1) + new INTEGER (2 );
13. Switch (I ){
14. Case 3: system. Out. println ("three"); break;
15. Default: system. Out. println ("other"); break;
16 .}
17 .}
What is the result?
A. Three
B. Other
C. An exception is thrown at runtime.
D. Compilation fails because of an error on line 12.
E. Compilation fails because of an error on line 13.
F. Compilation fails because of an error on line 15.
Answer:


Question 188
Given:
21. Class money {
22. Private string Country = "Canada ";
23. Public String GETC () {return country ;}
24 .}
25. Class yen extends money {
26. Public String GETC () {return Super. Country ;}
27 .}
28. Public class euro extends money {
29. Public String GETC (int x) {return Super. GETC ();}
30. Public static void main (string [] ARGs ){
31. system. Out. Print (new yen (). GETC () + "" + new Euro (). GETC ());
32 .}
33 .}
What is the result?
A. Canada
B. null Canada
C. Canada null
D. Canada
E. Compilation fails due to an error on line 26.
F. Compilation fails due to an error on line 29.
Answer: E
Super can only call non-private methods of the parent class ~~

Question 189
Given:
11. Class classa {}
12. Class classb extends classa {}
13. Class classc extends classa {}
And:
21. classa p0 = new classa ();
22. classb p1 = new classb ();
23. classc P2 = new classc ();
24. classa P3 = new classb ();
25. classa P4 = new classc ();
Which three are valid? (Choose three .)
A. p0 = p1;
B. p1 = P2;
C. P2 = P4;
D. P2 = (classc) P1;
E. p1 = (classb) P3;
F. P2 = (classc) P4;
Answer: AEF



Question 190
Which three statements are true? (Choose three .)
A. A final method in Class X can be abstract if and only if X is abstract.
B. A protected method in Class X can be overridden by any subclass of X.
C. A private static method can be called only within other static methods in class X.
D. A non-static public final method in Class X can be overridden in any subclass of X.
E. A public static method in Class X can be called by a subclass of X without explicitly referencing
Class X.
F. A method with the same signature as a private final method in Class X can be implemented in
Subclass of X.
G. A protected method in Class X can be overridden by a subclass of X only if the subclass is in the same
Package as X.
Answer: bef
A: The final method is not an abstract method. C: non-static methods in Class X can also be used. D: The final method cannot be rewritten. F: correct, because the private final method does not inherit from the quilt class, therefore, there can be a method with the same signature in the subclass. Note that this is different from overwriting overrideg: proteted, so it is not a package.

Question 191
Given:
10. interface a {void X ();}
11. Class B implements a {public void X () {} public void y (){}}
12. Class C extends B {public void X (){}}
And:
20. java. util. List <A> List = new java. util. arraylist <A> ();
21. List. Add (New B ());
22. List. Add (New C ());
23. For (A: List ){
24. A. X ();
25. A. Y ();
26 .}
What is the result?
A. The code runs with no output.
B. An exception is thrown at runtime.
C. Compilation fails because of an error in line 20.
D. Compilation fails because of an error in line 21.
E. Compilation fails because of an error in line 23.
F. Compilation fails because of an error in line 25.
Answer: F
The A interface does not have the Y () method.


Question 192
Given:
1. package test;
2.
3. Class target {
4. Public string name = "hello ";
5 .}
What can directly access and change the value of the variable name?
A. Any Class
B. Only the target class
C. Any class in the test package
D. Any class that extends target
Answer: c
Target is the default modifier, package-level visibility ~~


Question 193
Click the exhibit button. What two must the programmer do to correct the compilation errors? (Choose
Two .)
A. Insert a call to this () in the car constructor
B. Insert a call to this () in the mego Constructor
C. Insert a call to super () in the mego Constructor
D. Insert a call to super (VIN) in the mego Constructor
E. Change the wheelcount variable in car to protected
F. Change line 3 in the mego class to super. wheelcount = 3;
Answer: de
Mego will call the car no-parameter constructor ~~ In this case, the wheelcount must be visible to the mego class ~~


Question 194
A team of programmers is involved in reviewing a proposed design for a new utility class. After some
Discussion, they realize that the current design allows other classes to access methods in the Utility Class
That shoshould be accessible only to methods within the utility class itself. What design issue has the team
Discovered?
A. Tight coupling
B. Low cohesion
C. High Cohesion
D. Loose coupling
E. Weak Encapsulation
F. Strong Encapsulation
Answer: E
The class is not well encapsulated ~~

Question 195
Given:
5. Class thingy {meter M = new meter ();}
6. Class component {void go () {system. Out. Print ("C ");}}
7. Class meter extends component {void go () {system. Out. Print ("M ");}}
8.
9. Class deluxethingy extends thingy {
10. Public static void main (string [] ARGs ){
11. deluxethingy dt = new deluxethingy ();
12. dt. M. Go ();
13. thingy T = new deluxethingy ();
14. T. M. Go ();
15 .}
16 .}
Which two are true? (Choose two .)
A. The output is mm.
B. The output is MC.
C. Component is-a meter.
D. component has-a meter.
E. deluxethingy is-a component.
F. deluxethingy has-a component.
Answer: af
Note that both DT. M and T. M are meter instances ~~


Question 196
Given:
10. Interface jumper {public void jump ();}...
20. class animal {}...
30. Class dog extends animal {
31. Tail; 32 .}...
40. Class Beagle extends dog implements jumper {
41. Public void jump (){}
42 .}...
50. Class cat implements jumper {
51. Public void jump (){}
52 .}
Which three are true? (Choose three .)
A. Cat is-a animal
B. Cat is-a jumper
C. Dog is-a animal
D. Dog is-a jumper
E. CAT has-a animal
F. Beagle has-a tail
G. Beagle has-a jumper
Answer: BCF



Question 197
Click the exhibit button. What is the result?
A. value is: 8
B. Compilation fails.
C. Value is: 12
D. value is:-12
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer:

:
Question 198
Given a valid dateformat object named DF, and
16. Date d = new date (0l );
17. String DS = "December 15,200 4 ";
18. // insert code here what updates D's value with the date represented by DS?
A. 18. d = DF. parse (DS );
B. 18. d = DF. getdate (DS );
C. 18. Try {
19. d = DF. parse (DS );
20.} catch (parseexception e ){};
D. 18. Try {
19. d = DF. getdate (DS );
20.} catch (parseexception e ){};
Answer: c
The parse function is used to parses text from the beginning of the given string to produce a date. There is no getdate function ~~
Question 199
Which two scenarios are not safe to replace a stringbuffer object with a stringbuilder object? (Choose
Two .)
A. When using versions of Java technology earlier than 5.0.
B. When sharing a stringbuffer among multiple threads.
C. When using the java. Io class stringbufferinputstream.
D. When you plan to reuse the stringbuffer to build more than one string.
Answer: AB
Stringbuffer is thread-safe, but stringbuilder is not. Stringbuffer is added in JDK 1.5, while stringbuilder is added in JDK ~~

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.