Ocjp (1z0-851) Simulated question analysis (9)

Source: Internet
Author: User
Tags comparable

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 261
Given:
3. Class employee {
4. string name; double basesalary;
5. employee (string name, double basesalary ){
6. This. Name = Name;
7. This. basesalary = basesalary;
8 .}
9 .}
10. Public class salesperson extends employee {
11. Double Commission;
12. Public salesperson (string name, double basesalary, double commission ){
13. // insert code here
14 .}
15 .}
Which two code fragments, inserted independently at line 13, will compile? (Choose two .)
A. Super (name, basesalary );
B. This. Commission = Commission;
C. Super ();
This. Commission = Commission;
D. This. Commission = Commission;
Super ();
E. Super (name, basesalary );
This. Commission = Commission;
F. This. Commission = Commission;
Super (name, basesalary );
G. Super (name, basesalary, Commission );
Answer: AE
The constructor that calls the parent class must use super (name, basesalary) in the first line and cannot use super (), because the parent class does not have a constructor without parameters ~~


Question 262
Given:
11. Class {
12. Public void process () {system. Out. Print (",");}
13. Class B extends {
14. Public void process () throws ioexception {
15. Super. Process ();
16. system. Out. Print ("B ,");
17. Throw new ioexception ();
18 .}
19. Public static void main (string [] ARGs ){
20. Try {New B (). Process ();}
21. Catch (ioexception e) {system. Out. println ("exception ");}
22 .}
What is the result?
A. Exception
B. A, B, exception
C. Compilation fails because of an error in line 20.
D. Compilation fails because of an error in line 14.
E. A nullpointerexception is thrown at runtime.
Answer: d
The subclass throws the checked exceptions that are not present in the parent class ~~


Question 263
Given a method that must ensure that its parameter is not null:
11. Public void somemethod (object Value ){
12. // check for null value...
20. system. Out. println (value. getclass ());
21 .}
What, inserted at line 12, is the appropriate way to handle a null value?
A. Assert value = NULL;
B. Assert value! = NULL, "value is null ";
C. If (value = NULL ){
Throw new assertionexception ("value is null ");
}
D. If (value = NULL ){
Throw new illegalargumentexception ("value is null ");}
Answer: d
An invalid parameter exception should be thrown ~~


Question 264
Given:
11. Public static void main (string [] ARGs ){
12. Try {
13. ARGs = NULL;
14. ARGs [0] = "test ";
15. system. Out. println (ARGs [0]);
16.} catch (exception ex ){
17. system. Out. println ("exception ");
18.} catch (nullpointerexception NPE ){
19. system. Out. println ("nullpointerexception ");
20 .}
21 .}
What is the result?
A. Test
B. Exception
C. Compilation fails.
D. nullpointerexception
Answer: c
The Catch Block of nullpointerexception may cause compilation errors that cannot reach (cannot reach ~~


Question 265
Given:
11. Public static iterator reverse (list ){
12. Collections. Reverse (list );
13. Return list. iterator ();
14 .}
15. Public static void main (string [] ARGs ){
16. List list = new arraylist ();
17. List. Add ("1"); list. Add ("2"); list. Add ("3 ");
18. For (Object OBJ: reverse (list ))
19. system. Out. Print (OBJ + ",");
20 .}
What is the result?
A. 3, 2, 1,
B. 1, 2, 3,
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: c
The second parameter of the for-reach loop must be of the OBJ [] type.


Question 267
Given:
11. Class X {public void Foo () {system. Out. Print ("X ");}}
12.
13. Public class subb extends X {
14. Public void Foo () throws runtimeexception {
15. Super. Foo ();
16. If (true) throw new runtimeexception ();
17. system. Out. Print ("B ");
18 .}
19. Public static void main (string [] ARGs ){
20. New subb (). Foo ();
21 .}
22 .}
What is the result?
A. X, followed by an exception.
B. No output, and an exception is thrown.
C. Compilation fails due to an error on line 14.
D. Compilation fails due to an error on line 16.
E. Compilation fails due to an error on line 17.
F. X, followed by an exception, followed by B.
Answer:
Although the subclass throws a runtimeexception, the parent class does not have to handle runtimeexception, because any class has the ability to process runtimeexception ~~


:
Question 274
Given:
11. Public class person {
12. Private string name, comment;
13. Private int age;
14. Public Person (string N, int A, string c ){
15. Name = N; age = A; Comment = C;
16 .}
17. Public Boolean equals (Object O ){
18. If (! (O instanceof person) return false;
19, person P = (person) O;
20. Return age = P. Age & name. Equals (P. Name );
21 .}
22 .}
What is the appropriate definition of the hashcode method in class person?
A. Return super. hashcode ();
B. Return name. hashcode () + age * 7;
C. Return name. hashcode () + comment. hashcode ()/2;
D. Return name. hashcode () + comment. hashcode ()/2-age * 3;
Answer: B
See equals. It is only related to age and name ~~ To conform to the hashcode () return value, the two elements must be different, that is, the return value of equals must be false ~~



Question 275
A programmer must create a generic class MINMAX and the type parameter of MINMAX must implement
Comparable. Which Implementation of MINMAX will compile?

A.

Class MINMAX <E extends comparable <e >> {
E min = NULL;
E max = NULL;
Public MINMAX (){}
Public void put (e value) {/* store min or Max */}


B.

Class MINMAX <e implements comparable <e >> {
E min = NULL;
E max = NULL;
Public MINMAX (){}
Public void put (e value) {/* store min or Max */}


C.

Class MINMAX <E extends comparable <e >> {
<E> E min = NULL;
<E> E max = NULL;
Public MINMAX (){}
Public <E> void put (e value) {/* store min or Max */}


D.

Class MINMAX <e implements comparable <e >> {
<E> E min = NULL;
<E> E max = NULL;
Public MINMAX (){}
Public <E> void put (e value) {/* store min or Max */}
Answer:
Simple generic ~~


Question 276
Given:
3. Import java. util .*;
4. Public class G1 {
5. Public void takelist (list <? Extends string> List ){
6. // insert code here
7 .}
8 .}
Which three code fragments, inserted independently at line 6, will compile? (Choose three .)
A. List. Add ("foo ");
B. Object o = List;
C. String S = list. Get (0 );
D. List = new arraylist <string> ();
E. List = new arraylist <Object> ();
Answer: BCD
The list type is list <? Extends string>, according to the PECS rule, that is, the list must be the producer ~~ List is a string class or its subclass that implements the list interface ~~


Question 277
Given:
1. Public class drink implements comparable {
2. Public string name;
3. Public int compareto (Object O ){
4. Return 0;
5 .}
6 .}
And:
20. Drink one = new drink ();
21. Drink two = new drink ();
22. One. Name = "coffee ";
23. Two. Name = "tea ";
24. treeset set = new treeset ();
25. Set. Add (one );
26. Set. Add (two );
A programmer iterates over the treeset and prints the name of each drink object. What is the result?
A. Tea
B. Coffee
C. Coffee
Tea
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: B
Drink objects are equal when compared to each other, because public int compareto (Object O) {return 0 ;}, and the add () method of the treeset class uses compareto () method To compare the size of each object, and then add the object to the data structure of the red/black tree by size. If the comparison result is equal, the system rejects the addition. The add method returns false ~~

 

:
Question 279
Given:
1. Public class lineup {
2. Public static void main (string [] ARGs ){
3. Double D = 12.345;
4. // insert code here
5 .}
6 .}
Which code fragment, inserted at line 4, produces the output | 12.345 |?
A. System. Out. printf ("| % 7D | \ n", d );
B. System. Out. printf ("| % 7f | \ n", d );
C. System. Out. printf ("| % 3.7d | \ n", d );
D. system. Out. printf ("| % 3.7f | \ n", d );
E. System. Out. printf ("| % 7.3d | \ n", d );
F. system. Out. printf ("| % 7.3f | \ n", d );
Answer: F
Format output ~~ The same as C language ~~


Question 280
Given that the current directory is empty, and that the user has read and write privileges to the current
Directory, and the following:
1. Import java. Io .*;
2. Public class maker {
3. Public static void main (string [] ARGs ){
4. File dir = new file ("dir ");
5. File F = new file (Dir, "F ");
6 .}
7 .}
Which statement is true?
A. Compilation fails.
B. Nothing is added to the file system.
C. Only a new file is created on the file system.
D. Only a new directory is created on the file system.
E. Both a new file and a new directory are created on the file system.
Answer: B



Question 281
Given:
1. d is a valid, non-null date object
2. df is a valid, non-null dateformat object set to the current locale what outputs the current locale's
Country name and the appropriate version of D's date?
A. locale loc = locale. getlocale ();
System. Out. println (loc. getdisplaycountry ()
+ "" + DF. Format (d ));
B. locale loc = locale. getdefault ();
System. Out. println (loc. getdisplaycountry ()
+ "" + DF. Format (d ));
C. locale loc = locale. getlocale ();
System. Out. println (loc. getdisplaycountry ()
+ "" + DF. setdateformat (d ));
D. locale loc = locale. getdefault ();
System. Out. println (loc. getdisplaycountry ()
+ "" + DF. setdateformat (d ));
Answer: B

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.