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

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 101

Given:
12. Import java. util .*;
13. Public class assumer1 {
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 (609 );
21. system. Out. println (S + "" + subs );
22 .}
23 .}
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. [608,609,610,612] [608,610]
D. [608,609,610,612] [608,609,610]
E. [606,608,609,610,612] [608,610]
F. [606,608,609,610,612] [608,609,610]
Answer: F

For duplicate questions, the subset method returns view instead of copy

Question 102
Given:
23. object [] myobjects = {
24. New INTEGER (12 ),
25. New String ("foo "),
26. New INTEGER (5 ),
27. New Boolean (true)
28 .};
29. arrays. Sort (myobjects );
30. For (INT I = 0; I <myobjects. length; I ++ ){
31. system. Out. Print (myobjects [I]. tostring ());
32. system. Out. Print ("");
33 .}
What is the result?
A. Compilation fails due to an error in line 23.
B. Compilation fails due to an error in line 29.
C. A classcastexception occurs in line 29.
D. A classcastexception occurs in line 31.
E. The value of all four objects prints in natural order.
Answer: c

The sort method must be equivalent to the same object that can be converted into the same and implement the comparable excuse.

Question 103
Given:
1. Public class donkey {
2. Public static void main (string [] ARGs ){
3. boolean assertson = false;
4. Assert (assertson): assertson = true;
5. If (assertson ){
6. system. Out. println ("assert is on ");
7 .}
8 .}
9 .}
If class donkey is invoked twice, the first time without assertions enabled, and the second time
Assertions enabled, what are the results?
A. No output
B. No output
Assert is on
C. Assert is on
D. No output
An assertionerror is thrown.
E. Assert is on
An assertionerror is thrown.
Answer: d

Test assert. If the assert is valid, that is, assert (false), an assertionerror is thrown.

Question 104
Given:
11. Float Pi = new float (3.14f );
12. If (PI> 3 ){
13. system. Out. Print ("Pi is bigger than 3 .");
14 .}
15. else {
16. system. Out. Print ("Pi is not bigger than 3 .");
17 .}
18. Finally {
19. system. Out. println ("have a nice day .");
20 .}
What is the result?
A. Compilation fails.
B. Pi is bigger than 3.
C. An exception occurs at runtime.
D. Pi is bigger than 3. Have a nice day.
E. Pi is not bigger than 3. Have a nice day.
Answer:

Finally is not used in such a place, but only in try/catch/finally.

Question 105
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

Nullpointerexception is a subclass of exception. In the compiler, a warning cannot be obtained (not available.

Question 106
Given:
22. Public void go (){
23. String o = "";
24. Z:
25. For (INT x = 0; x <3; X ++ ){
26. For (INT y = 0; y <2; y ++ ){
27. If (x = 1) break;
28. If (x = 2 & Y = 1) Break Z;
29. O = O + x + y;
30 .}
31 .}
32. system. Out. println (O );
33 .}
What is the result when the go () method is invoked?
A. 00
B. 0001
C. 000120
D. 00012021.
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: c

Test break, when x = 0, y = 0, O = 00;

When x = 0, y = 1, O = 0001;

When x = 1, y = 0, break falls into the inner loop, O = 0001;

When x = 2, y = 0, O = 000120;

When x = 2, y = 1, break drops the External Loop at the breakpoint, O = 000120;

Question 107
Given:
12. Public class test {
13. Public Enum dogs {collie, Harrier };
14. Public static void main (string [] ARGs ){
15. Dogs mydog = dogs. Collie;
16. Switch (mydog ){
17. Case Collie:
18. system. Out. Print ("Collie ");
19. Case Harrier:
20. system. Out. Print ("Harrier ");
21 .}
22 .}
23 .}
What is the result?
A. Collie
B. Harrier
C. Compilation fails.
D. Collie Harrier
E. An exception is thrown at runtime.
Answer: d

The switch parameter can be an enumeration constant without a break. It is executed from row 17.

Question 108

Click the exhibit button. given:

31. Public void method (){
32. a A = new ();
33. A. Method1 ();
34 .}
Which statement is true if a testexception is thrown on line 3 of Class B?
A. Line 33 must be called within a try block.
B. The exception thrown by Method1 in Class A is not required to be caught.
C. The method declared on line 31 must be declared to throw a runtimeexception.
D. on line 5 of Class A, the call to method2 of Class B does not need to be placed in a try/Catch Block.
Answer: B

Since a runtimeexception is thrown and does not need to be processed, try blocks or throws or B pairs are not required.

Class A throws a testexception that inherits the exception and must be processed.

Question 109
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 fifth line is 5. x = I + Y;, and it has not been assigned an I value (I is null at this time.

Question 110
Given:
11. Static Class {
12. Void process () throws exception {Throw new exception ();}
13 .}
14. Static Class B extends {
15. Void process () {system. Out. println ("B ");}
16 .}
17. Public static void main (string [] ARGs ){
18. New B (). Process ();
19 .}
What is the result?
A. B
B. The code runs with no output.
C. Compilation fails because of an error in line 12.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 18.
Answer:

Question 111
Given:
1. Public class Venus {
2. Public static void main (string [] ARGs ){
3. Int [] x = {1, 2, 3 };
4. Int y [] = {4, 5, 6 };
5. New Venus (). Go (x, y );
6 .}
7. Void go (INT []... z ){
8. For (INT [] A: Z)
9. system. Out. Print (A [0]);
10 .}
11 .}
What is the result?
A. 1
B. 12
C. 14
D. 123.
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: c

Int []... z indicates a variable parameter, while variable indicates the number of arrays. There are two arrays to output the first element of each array.

Question 112
Given:
10. Public class Foo {
11. Static int [];
12. Static {A [0] = 2 ;}
13. Public static void main (string [] ARGs ){}
14 .}
Which exception or error will be thrown when a programmer attempts to run this code?
A. java. Lang. stackoverflowerror
B. java. Lang. illegalstateexception
C. java. Lang. exceptionininitializererror
D. java. Lang. arrayindexoutofboundsexception
Answer: c

If it is not new, assign a value. Initialization error.

Question 113
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:

Polymorphism: first, call the Foo () function in subb, and execute the Foo () function in subb first. foo (); that is, call the Foo () function of the parent class to output X. Then, an exception is thrown and an exception is returned.Method for throwing an exception. B is not output

Question 121
A company has a business application that provides its users with your different reports:
Receivables reports, payables reports, revenue projects, and so on. The company has just purchased
Some new, state-of-the-art, wireless printers, and a programmer has been assigned the task of enhancing
All of the reports to use not only the company's old printers, but the new wireless printers as well. When
Programmer starts looking into the application, the programmer discovers that because of the design of
Application, it is necessary to make changes to each report to support the new printers. Which two design
Concepts most likely explain this situation? (Choose two .)
A. Inheritance
B. Low cohesion
C. Tight coupling
D. High Cohesion
E. Loose coupling
F. Object immutability
Answer: BC

Low cohesion low cohesion tight coupling high coupling: both of the above are software engineering to avoid.

Question 122
Given:
2. Public class Hi {
3. Void M1 (){}
4. Protected void () m2 {}
5 .}
6. Class Lois extends Hi {
7. // insert code here
8 .}
Which four code fragments, inserted independently at line 7, will compile? (Choose four .)
A. Public void M1 (){}
B. Protected void M1 (){}
C. Private void M1 (){}
D. Void m2 (){}
E. Public void m2 (){}
F. Protected void m2 (){}
G. Private void m2 (){}
Answer: ABEF

Subclass rewriting, access modifier cannot reduce

Question 124
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 employee class only has a constructor with parameters and does not have a default constructor. Pay attention to this. Therefore, super () is not allowed, and super (name, basesalary) with parameters must also be explicitly called for the parent class. It must appear in the first sentence of the constructor.

Question 125
A team of programmers is reviewing a proposed API for a new utility class. After some discussion, they
Realize that they can reduce the number of methods in the API without losing any functionality. If they
Implement the new design, which two oo principles will they be promoting?
A. Looser Coupling
B. Tighter Coupling
C. Lower cohesion
D. Higher cohesion
E. Weaker Encapsulation
F. Stronger Encapsulation
Answer:

Cohesion: the closeness of each element in a module.
Coupling: a measurement of the degree of interconnection between different modules in a software architecture

This reduces the number of Apis but does not lose any function. It means that the connection between modules is very small, the degree of interconnection is very low, and it is a low coupling.


Question 126
Given:
1. Class classa {
2. Public int numberofinstances;
3. Protected classa (INT numberofinstances ){
4. This. numberofinstances = numberofinstances;
5 .}
6 .}
7. Public class extendeda extends classa {
8. Private extendeda (INT numberofinstances ){
9. Super (numberofinstances );
10 .}
11. Public static void main (string [] ARGs ){
12. extendeda ext = new extendeda (420 );
13. system. Out. Print (ext. numberofinstances );
14 .}
15 .}
Which statement is true?
A. 420 is the output.
B. An exception is thrown at runtime.
C. All constructors must be declared public.
D. constructors cannot use the private modifier.
E. constructors cannot use the protected modifier.
Answer:

Question 127
Given:
5. class building {}
6. Public class barn extends building {
7. Public static void main (string [] ARGs ){
8. Building build1 = new building ();
9. Barn barn1 = new barn ();
10. Barn barn2 = (Barn) build1;
11. Object obj1 = (object) build1;
12. String str1 = (string) build1;
13. Building build2 = (Building) barn1;
14 .}
15 .}
Which is true?
A. If line 10 is removed, the compilation succeeds.
B. If line 11 is removed, the compilation succeeds.
C. If line 12 is removed, the compilation succeeds.
D. If line 13 is removed, the compilation succeeds.
E. More than one line must be removed for compilation to succeed.
Answer: c

This forced type conversion (string) build1; will not succeed

Question 128
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 129
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

T. Start () is to start thread t. It has already started and cannot start () again (). It will throw an invalid state exception, illegalthreadstateexception.

Question 130
Which two statements are true? (Choose two .)
A. It is possible for more than two threads to deadlock at once.
B. The JVM implementation guarantees that multiple threads cannot enter into a deadlocked state.
C. Deadlocked threads release once their sleep () method's sleep duration has expired.
D. deadlocking can occur only when the wait (), Y (), and policyall () methods are used incorrectly.
E. It is possible for a single-threaded application to deadlock if synchronized blocks are used incorrectly.
F. If a piece of code is capable of deadlocking, you cannot eliminate the possibility of deadlocking
Inserting invocations of thread. Yield ().
Answer: af

A: deadlocks on more than two threads may occur.

B: The JVM does not ensure that multiple threads do not enter the Deadlock State.

C: The deadlocked thread will be released only related to resources. When there are fewer resources, it will be deadlocked, and it has nothing to do with sleep. Sleep is just to sleep the thread.

D: No. In addition to these methods, there are other methods, such as await, signal, and signalall.

E: How can applications with a single thread experience deadlocks? Are there other threads to grab resources.

F: if a piece of code can be deadlocked, you cannot insert thread. Yield () to eliminate the possibility of deadlocks.

Question 132
Given classes defined in two different files:
1. Package util;
2. Public class bitutils {
3. Public static void process (byte [] B) {/* more code here */}
4 .}
1. Package app;
2. Public class someapp {
3. Public static void main (string [] ARGs ){
4. byte [] bytes = new byte [256];
5. // insert code here
6 .}
7 .}
What is required at line 5 in class someapp to use the process method of bitutils?
A. Process (bytes );
B. bitutils. Process (bytes );
C. util. bitutils. Process (bytes );
D. someapp cannot use methods in bitutils.
E. Import util. bitutils. *; process (bytes );
Answer: c

Question 133
A developer is creating a class book, that needs to access class paper. The paper class is deployed in
Jar named mylib. Jar. Which three, taken independently, will allow the developer to use the paper class
While compiling the book class? (Choose three .)
A. the JAR file is located at $ java_home/JRE/classes/mylib. jar.
B. the JAR file is located at $ java_home/JRE/lib/EXT/mylib. jar ..
C. The JAR file is located at/Foo/mylib. jar and a classpath environment variable is set that includes des/Foo/
Mylib. Jar/paper. Class.
D. the JAR file is located at/Foo/mylib. jar and a classpath environment variable is set that includes des/Foo/
Mylib. jar.
E. The JAR file is located at/Foo/mylib. jar and the book class is compiled using javac-CP/Foo/mylib. Jar/
Paper book. java.
F. The JAR file is located at/Foo/mylib. jar and the book class is compiled using javac-D/Foo/mylib. Jar
Book. Java
G. The JAR file is located at/Foo/mylib. jar and the book class is compiled using javac-classpath/Foo/
Mylib. Jar book. Java
Answer: BDG

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.