• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 4 out of 242 pages
Exam (elaborations)

OCP 17 Java Final Exam Questions With Guaranteed Pass Solutions.

Document preview thumbnail
Preview 4 out of 242 pages

What is the result of the following program? 1: public class Squares { 2: ....public static long square(int x) { 3: ....var y = x * (long) x; 4: ....x = -1; 5: ....return y; 6: } 7: public static void main(String[] args) { 8: ...var value = 9; 9: ...var result = square(value); 10: ..Sln(value); 11: } } A. -1 B. 9 C. 81 D. Compiler error on line 9 E. Compiler error on a different line - Answer B. Since Java is pass-by-value and the variable on line 8 never gets reassigned, it stays as 9. In the method square, x starts as 9. The y value becomes 81, and then x gets set to -1. Line 9 does set result to 81. However, we are printing out value, and that is still 9, making option B correct. Which of the following are legal entry point methods that can be run from the command line? (Choose all that apply.) A. private static void main(String[] args) B. public static final main(String[] args) C. public void main(String[] args) D. public static final void main(String[] args) E. public static void main(String[] args) F. public static main(String[] args) - Answer D, E. Option E is the canonical main() method signature. You need to memorize it. Option D is an alternate form with the redundant final. Option A is incorrect because the main() method must be public. Options B and F are incorrect

Content preview

OCP 17 Java Final Exam Questions With
Guaranteed Pass Solutions.
What is the result of the following program?

1: public class Squares {

2: ....public static long square(int x) {

3: ....var y = x * (long) x;

4: ....x = -1;

5: ....return y;

6: }

7: public static void main(String[] args) {

8: ...var value = 9;

9: ...var result = square(value);

10: ..System.out.println(value);

11: } }



A. -1

B. 9

C. 81

D. Compiler error on line 9

E. Compiler error on a different line - Answer B. Since Java is pass-by-value and the variable on
line 8 never gets reassigned, it stays as 9. In the method square, x starts as 9. The y value
becomes 81, and then x gets set to -1. Line 9 does set result to 81. However, we are printing out
value, and that is still 9, making option B correct.



Which of the following are legal entry point methods that can be run from the command line?
(Choose all that apply.)



A. private static void main(String[] args)

B. public static final main(String[] args)

C. public void main(String[] args)

D. public static final void main(String[] args)

,because the main() method must have a void return type. Option C is incorrect because the
main() method must be static.



Which answer options represent the order in which the following statements can be assembled
into a program that will compile successfully? (Choose all that apply.)



X: class Rabbit {}

Y: import java.util.*;

Z: package animals;



A. X, Y, Z

B. Y, Z, X

C. Z, Y, X

D. Y, X

E. Z, X

F. X, Z

G. None of the above - Answer C, D, E. The package and import statements are both optional.
If both are present, the order must be package, then import, and then class. Option A is
incorrect because class is before package and import. Option B is incorrect because import is
before package. Option F is incorrect because class is before package.



Which of the following are true? (Choose all that apply.)

public class Bunny {

public static void main(String[] x) {

Bunny bun = new Bunny();

}}



A. Bunny is a class.

B. bun is a class.

C. main is a class.

D. Bunny is a reference to an object.

E. bun is a reference to an object.

F. main is a reference to an object.

,Which of the following are valid Java identifiers? (Choose all that apply.)



A. _

B. _helloWorld$

C. true

D. java.lang

E. Public

F. 1980_s

G. _Q2_ - Answer B, E, G.

Option A is invalid because a single underscore is not allowed. Option C is not a valid identifier
because true is a Java reserved word. Option D is not valid because a period (.) is not allowed in
identifiers. Option F is not valid because the first character is not a letter, dollar sign ($), or
underscore (_). Options B, E, and G are valid because they contain only valid characters.



Which statements about the following program are correct? (Choose all that apply.)



2: public class Bear {

3: ....private Bear pandaBear;

4:.... private void roar(Bear b) {

5: ..........System.out.println("Roar!");

6: ..........pandaBear = b;

7: }

8: public static void main(String[] args) {

9: ..Bear brownBear = new Bear();

10: ..Bear polarBear = new Bear();

11: ...brownBear.roar(polarBear);

12: ..polarBear = null;

13: ..brownBear = null;

14:... System.gc(); } }



A. The object created on line 9 is eligible for garbage collection after line 13.

, F. Garbage collection might or might not run.

G. The code does not compile. - Answer A, D, F. Garbage collection is never guaranteed to run,
making option F correct and option E incorrect. Next, the class compiles and runs without issue,
so option G is incorrect. The Bear object created on line 9 is accessible until line 13 via the
brownBear reference variable, which is option A. The Bear object created on line 10 is
accessible via both the polarBear reference and the brownBear.pandaBear reference. After line
12, the object is still accessible via brownBear.pandaBear. After line 13, though, it is no longer
accessible since brownBear is no longer accessible, which makes option D the final answer.



Assuming the following class compiles, how many variables defined in the class or method are
in scope on the line marked on line 14?



1: public class Camel {

2:..... { int hairs = 3_000_0; }

3: .....long water, air=2;

4: ......boolean twoHumps = true;

5:...... public void spit(float distance) {

6: ..........var path = "";

7: ..........{ double teeth = 32 + distance++; }

8: ..........while(water > 0) {

9: ..................int age = twoHumps ? 1 : 2;

10:................ short i=-1;

11: .......................for(i=0; i<10; i++) {

12: .....................................var Private = 2;

13: .......................}

14: ................// SCOPE

15:......... }

16: ......}

17: }



A. 2

B. 3

C. 4

Document information

Uploaded on
May 18, 2025
Number of pages
242
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$18.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
TestSolver9
3.6
(182)
Sold
1040
Followers
131
Items
32812
Last sold
14 hours ago




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions