Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 22

What will be the output of this program?

1.        class Sup {

2.                 String s = "";

3.                 Sup() {

4.                         s += "sup ";

5.                 }

6.        }

7.              

8.        class Sub extends Sup {

9.                 Sub() {

10.                    s += "sub ";

11.            }             

12.    }

13.     

14.    class SubSub extends Sup {

15.             SubSub() {

16.                     s += "subsub ";

17.             }             

18.                   

19.             public static void main(String args [ ] ) {

20.                      System.out.print(new SubSub().s);

21.             }         

22.    } 

 

 


Options are :

  • subsub sup
  • sup sub subsub
  • sup subsub (Correct)
  • subsub sub sup
  • Compilation fails.

Answer :sup subsub

What will be the output of this program?

1.             public class Whiz {

2.              

3.                       static int x;

4.                            

5.                       public static void main(String [ ] args) {

6.                                Whiz w1 = new Whiz ();

7.                                Whiz w2 = new Whiz ();

8.                                Whiz w3 = new Whiz ();

9.                                w1.method();

10.                           w2.method();

11.                           w3.method();

12.                 }

13.                   

14.                 public void method() {

15.                           while ( ++x < 3 ) {

16.                                   System.out.print("A");

17.                            }

18.                 }

19.        }                             

 


Options are :

  • AAAAAA
  • AA (Correct)
  • No output.
  • Compilation fails due to an error at line 15.
  • Compilation fails due to multiple errors.

Answer :AA

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 26

What is the output of this program code?

1.               import java.time.LocalDate;

2.                

3.               public class Whiz {

4.                           public static void main(String [ ] args) {

5.                                           LocalDate date = LocalDate.ofYearDay(2015, 363);

6.                                           date.plusWeeks(2);

7.                                           System.out.println(date + " : " + date.isLeapYear());

8.                           }

9.               }                                                         

 


Options are :

  • 2016-01-12 : true
  • 2015-12-29 : false (Correct)
  • An Exception
  • Compilation fails due to an error at line 5.
  • Compilation fails due to multiple errors.

Answer :2015-12-29 : false

Which of the following method from the String class can modify the object on which they are called?


Options are :

  • charAt(int index)
  • concat(String str)
  • toLowerCase()
  • split(String regex)
  • None of the above. (Correct)

Answer :None of the above.

What will be the output of this program?

1.              public class Whiz {

2.                          public static void main(String [ ] args) {

3.                                   int marks = 75;

4.                                   String s = "Result ";

5.                                         

6.                                   if ( marks > 75 ) {

7.                                           s.concat("A");

8.                                    } else if ( marks > 65 ) {

9.                                            s.concat("B");

10.                               } else if ( marks > 50 ) {

11.                                       s.concat("C");

12.                               } else {

13.                                       s.concat("D");

14.                               }

15.        

16.                               System.out.println(s);

17.                     }

18.         }                                                         



Options are :

  • Result (Correct)
  • Result A
  • Result B
  • Result D
  • Compilation fails.

Answer :Result

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 22

Which of the following method can be found in both StringBuilder and String classes?


Options are :

  • concat(String str)
  • isEmpty()
  • charAt(int index) (Correct)
  • toUpperCase()
  • None of the above.

Answer :charAt(int index)

What will be the output of this program?

1.              public class Whiz {

2.                         

3.                          public static void main(String args [ ] ) {

4.                                          char c [ ] = new char[]{'a','b','c'};

5.                                          String cd = "abcdef".substring(4);

6.                                          String s1 = new String(c);

7.                                          s1 += cd;

8.                                          System.out.print(s1);

9.                          }

10.       }                                                                                                                                         

 


Options are :

  • abcef (Correct)
  • abcdef
  • An exception will be thrown.
  • Compilation fails due to an error at line 5.
  • Compilation fails due to an error at line 6.

Answer :abcef

Which of the following can be filled the blank to print the number of days for the month on line 7?

1.      import java.time.LocalDate;

2.             

3.      class Whizlab {

4.                public static void main(String args[]) {   

5.                         LocalDate ld  = LocalDate.of(2010,10,10);

6.                         int days = ld._____________;

7.                         System.out.println(days);

8.                }

9.      }



Options are :

  • length();
  • dayCount();
  • lengthOfMonth(); (Correct)
  • sizeOfMonth();
  • days();

Answer :lengthOfMonth();

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 13

Given the following,

long result;

which options are correct declarations of methods that accept two String arguments and an int argument and whose return value can be assigned to the variable result? (Choose all that apply)


Options are :

  • A. Short myMethod1(String str1, int str2, String str3) (Correct)
  • D. Float myMethod4(String val1, val2, int val3)
  • F. Long myMethod6(String... val1, int val2)
  • B. Int myMethod2(String val1, int val2, String val3)
  • E. Long myMethod5(int str2, String str3, String str1) (Correct)
  • C. Byte myMethod3(String str1, str2, int a)
  • G. Short myMethod7(int val1, String... val2) (Correct)

Answer :A. Short myMethod1(String str1, int str2, String str3) E. Long myMethod5(int str2, String str3, String str1) G. Short myMethod7(int val1, String... val2)

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 19

What is the output of the following code? (Choose one)


Options are :

  • Runtime exception (Correct)
  • Spring, Autumn, Winter,
  • Autumn, Winter,
  • Spring, Summer, Winter,
  • Compilation error

Answer :Runtime exception

Given the following definition of the classes Animal, Lion, and Jumpable, select the correct combinations of assignments of a variable that don't result in compilation errors or runtime exceptions (Choose all that apply)


Options are :

  • E. Jumpable var5 = new Lion(); (Correct)
  • F. Jumpable var6 = (Jumpable)(new Animal());
  • B. Animal var2 = new Animal(); (Correct)
  • C. Lion var3 = new Animal();
  • D. Jumpable var4 = new Animal();
  • A. Jumpable var1 = new Jumpable();

Answer :E. Jumpable var5 = new Lion(); B. Animal var2 = new Animal();

What is the output of the following code? (Choose one)


Options are :

  • Paper:0
  • Paper:1
  • Paper:0 Paper:1
  • Paper:1 Paper:0 (Correct)

Answer :Paper:1 Paper:0

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 49

Which statement is true for the following code? (Choose one)


Options are :

  • The code prints Circle.
  • The code prints Circle Square Triangle
  • The code fails to compile. (Correct)
  • The code prints Square.
  • The code prints Triangle.
  • The code prints Triangle Circle Square

Answer :The code fails to compile.

What is the output of the following code? (Choose one)


Options are :

  • B. 6
  • E. 12
  • C. 10
  • H. Runtime exception
  • A. 5
  • G. No output
  • D. 11 (Correct)
  • F. Compilation error

Answer :D. 11

What is the output of the following code? (Choose one)


Options are :

  • 12
  • Compilation error (Correct)
  • No output
  • 10

Answer :Compilation error

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 14

What is the output of the following code? (Choose one)


Options are :

  • 200 200
  • 100 200
  • The code fails to compile.
  • 100 100 (Correct)

Answer :100 100

A class Student is defined as follows:

The creator of the class later changes the method getName as follows:

What are the implications of this change? (Choose all that apply)


Options are :

  • The classes that were using the class Student will fail to compile.
  • The class Student is an example of a well-encapsulated class. (Correct)
  • The classes that were using the class Student will work without any compilation issues. (Correct)
  • The class Student exposes its instance variable outside the class.

Answer :The class Student is an example of a well-encapsulated class. The classes that were using the class Student will work without any compilation issues.

What is the output of the following code? (Choose one)


Options are :

  • Call-Phone Call-Phone (Correct)
  • Call-Phone null
  • Call-Phone Call-SmartPhone
  • null Call-SmartPhone

Answer :Call-Phone Call-Phone

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 22

Given the following code, which option, if used to replace //INSERT CODE HERE, will define an overloaded rideWave method? (Choose one)


Options are :

  • D. default StringBuilder rideWave (StringBuffer a) { return null; }
  • C. private void rideWave(int value, String value2) {} (Correct)
  • A. public String[] rideWave() { return null; }
  • B. protected void riceWave(int a) {}

Answer :C. private void rideWave(int value, String value2) {}

Given the following code,

what is the output? (Choose one)


Options are :

  • 123
  • Compilation error
  • 111 (Correct)
  • Runtime exception
  • 333
  • 222

Answer :111

Given the following code, which option, if used to replace /* INSERT CODE HERE */, will make the code print the value of the variable pagesPerMin? (Choose one)


Options are :

  • B. myPrinter.pagesPerMin
  • D. ((LaserPrinter)myPrinter).pagesPerMin (Correct)
  • A. (LaserPrinter)myPrinter.pagesPerMin
  • C. LaserPrinter.myPrinter.pagesPerMin

Answer :D. ((LaserPrinter)myPrinter).pagesPerMin

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 20

Examine the following code and select the correct statement (choose one).


Options are :

  • The code throws a runtime exception and the code execution never reaches line 8 or line 9. (Correct)
  • The object referred to by object e isn't eligible for garbage collection because its member variable mgr isn't set to null.
  • The object referred to by object e is eligible for garbage collection on line 9.
  • The object referred to by object e is eligible for garbage collection on line 8.

Answer :The code throws a runtime exception and the code execution never reaches line 8 or line 9.

What is the output of the following code? (Select 1 option.)


Options are :

  • The while loop will execute indefinitely, printing all even numbers, starting from 0.
  • The while loop will execute indefinitely, printing all even numbers, starting from 2. (Correct)
  • The while loop will execute indefinitely, printing all odd numbers, starting from 3.
  • The while loop will execute indefinitely, printing all numbers, starting from 1.
  • The while loop won't execute; nothing will be printed.
  • The while loop will execute indefinitely, printing all odd numbers, starting from 1.

Answer :The while loop will execute indefinitely, printing all even numbers, starting from 2.

Given the following code, which option, if used to replace /* REPLACE CODE HERE */, will make the code print the name of the phone with the position at which it's stored in the array phones? (Choose one)


Options are :

  • System.out.println(phones.count + ":" + phone);
  • System.out.println(phones.getPosition() + ":" + phone);
  • System.out.println(phones.getCount() + ":" + phone);
  • None of the above (Correct)
  • System.out.println(phones.pos + ":" + phone);

Answer :None of the above

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 34

What is the output of the following code? (Choose one)


Options are :

  • Runtime exception
  • 2 GB
  • Compilation error
  • 1 GB (Correct)

Answer :1 GB

Examine the following code and select the correct statements (choose all that apply).


Options are :

  • The class compiles successfully—a base class can use reference variables of its derived class as method parameters. (Correct)
  • The class Bottle can access only one constructor. (Correct)
  • A base class can't pass reference variables of its defined class as method parameters in constructors.
  • The class Bottle defines two overloaded constructors.

Answer :The class compiles successfully—a base class can use reference variables of its derived class as method parameters. The class Bottle can access only one constructor.

Given the following definitions of the classes ColorPencil and TestColor, which option, if used to replace //INSERT CODE HERE, will initialize the instance variable color of the reference variable myPencil with the String literal value "RED"? (Choose one)


Options are :

  • A. this.color = color; (Correct)
  • B. color = color;
  • D. this.color = RED;
  • C. color = RED;

Answer :A. this.color = color;

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 43

Given that the following classes Animal and Forest are defined in the same package, examine the code and select the correct statements (Choose all that apply).


Options are :

  • A. The class Forest prints Lion. (Correct)
  • B. If the code on line 2 is changed as follows, the class Forest will print Lion: private void printKing() { System.out.println("Lion"); }
  • C. If the code on line 2 is changed as follows, the class Forest will print Lion: void printKing() { System.out.println("Lion"); } (Correct)
  • D. If the code on line 2 is changed as follows, the class Forest will print Lion: default void printKing() { System.out.println("Lion"); }

Answer :A. The class Forest prints Lion. C. If the code on line 2 is changed as follows, the class Forest will print Lion: void printKing() { System.out.println("Lion"); }

Given the following code,

which of the options, when inserted at //INSERT CODE HERE, will define a valid use of the method write in the method article? (Choose all that apply)


Options are :

  • D. try { new Pen().write("story"); } catch (RuntimeException e) {}
  • A. try { new Pen().write("story"); } catch (NoInkException e) {} (Correct)
  • B. try { new Pen().write("story"); } finally {}
  • C. try { write("story"); } catch (Exception e) {} (Correct)

Answer :A. try { new Pen().write("story"); } catch (NoInkException e) {} C. try { write("story"); } catch (Exception e) {}

Given the following statements,

     ? com.ejava is a package

     ? class Person is defined in package com.ejava

     ? class Course is defined in package com.ejava

which of the following options correctly import the classes Person and Course in the class MyEJava? (Choose all that apply)


Options are :

  • B. import com.ejava; class MyEJava {}
  • D. import com.ejava.Person; import com.ejava.*; class MyEJava {} (Correct)
  • A. import com.ejava.*; class MyEJava {} (Correct)
  • C. import com.ejava.Person; import com.ejava.Course; class MyEJava {} (Correct)

Answer :D. import com.ejava.Person; import com.ejava.*; class MyEJava {} A. import com.ejava.*; class MyEJava {} C. import com.ejava.Person; import com.ejava.Course; class MyEJava {}

Oracle Java SE 8 Certification - OCAJP 1Z0-808 Practice Tests Set 22

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions