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

Which one of the following statements is correct about a valid .java file?


Options are :

  • It can only contain one class declaration
  • It may define at most one public class (Correct)
  • It can contain one public class declaration and one public interface definition
  • It must define at least one public class

Answer :It may define at most one public class

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

Choose the options that meets the following specification: Create a well encapsulated class Pencil with one instance variable model. The value of model should be accessible and modifiable outside Pencil.


Options are :

  • class Pencil { public String model; public String getModel() { return model; } public void setModel(String val) { model = val; } }
  • class Pencil { private String model; public String getModel() { return model; } }
  • class Pencil { private String model; public String getModel() { return model; } public void setModel(String val) { model = val; } } (Correct)
  • class Pencil { private String model; public String getModel() { } public void setModel(String val) { } }

Answer :class Pencil { private String model; public String getModel() { return model; } public void setModel(String val) { model = val; } }

Given the following code, which option, if used to replace /* INSERT CODE HERE */, will cause the code to print 110? 


Options are :

  • return super.pages + this.interviews*5;
  • return super.pages + interviews*5
  • return this.pages + super.interviews*5
  • None of the above (Correct)

Answer :None of the above

What is the output of the following code? 


Options are :

  • m1
  • m2
  • m3
  • The code fails to compile (Correct)

Answer :The code fails to compile

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

What is the output of the following application?

package pond;

abstract class Duck {

protected int count;

public abstract int getDuckies();

}

public class Ducklings extends Duck {

private int age;

public Ducklings(int age) { this.age = age; }

public int getDuckies() { return this.age/count; }

public static void main(String[] pondInfo) {

Duck itQuacks = new Ducklings(5);

System.out.print(itQuacks.getDuckies());

}

}


Options are :

  • 0
  • 5
  • The code does not compile.
  • The code compiles but throws an exception at runtime (Correct)

Answer :The code compiles but throws an exception at runtime

What is the output of the following class?


Options are :

  • 2
  • 3
  • 4
  • None of the above (Correct)

Answer :None of the above

How many lines does this code output?


Options are :

  • One
  • Two
  • None. The code does not compile. (Correct)
  • None. The code throws an exception at runtime.

Answer :None. The code does not compile.

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

Which of the following statement(s) is/are correct?

  I.  Private members of a class can only be accessed by the members of the same class.

 II.  Protected members of a class can be inherited only by a subclass of another package.

III.  Protected members of a class can be inherited by a subclass of an another package, and become private members of that subclass.


Options are :

  • I only (Correct)
  • II only
  • I and II only
  • I and III only
  • None of the statement is true

Answer :I only

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

What will be the output of this program?

1.      class Whiz {

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

3.                        int x = 1;

4.                        int y = new Whiz().change(x);

5.                       System.out.print(x+y);

6.               }

7.               int change(int x) {

8.                       x = 2;

9.                       return x;

10.          }

11.    }

 


Options are :

  • 2
  • 3 (Correct)
  • 4
  • Compilation fails
  • An exception is thrown at runtime

Answer :3

What will be the output of this program?

1.      public class Whiz{

2.       

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

4.                                      char[] chars = {'1','Z','0','-','8','1'};

5.                                      StringBuilder sb = new StringBuilder();

6.                                      sb.append(chars,0,chars.length-1);

7.                                      sb.append('0');

8.                                      sb.append("8");

9.                                      System.out.print(sb);

10.                 }

11. }

 

 


Options are :

  • 1Z0-808 (Correct)
  • 1Z0-8108
  • 1Z0-810
  • Compilation fails due to an error at line 6
  • Compilation fails due to an error at line 7

Answer :1Z0-808

What will be the output of this program?

1.      class Whiz {

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

3.                               StringBuilder sb = new StringBuilder("Whiz");

4.                               sb.append("Labs");

5.                               System.out.print(sb.length() + sb.capacity());

6.                         }

7.     

 

 


Options are :

  • 16
  • 40
  • 28 (Correct)
  • 24
  • Compilation fails

Answer :28

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

What will be the output of this program?

1.         class Whiz {

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

3.                            String s = "1Z";

4.                            s.concat("0");

5.                            s += "1";

6.                            System.out.println(s + "-808");

7.                 }

8.         }

 


Options are :

  • 1Z01-808
  • 1Z0-808
  • 1Z1-808 (Correct)
  • An Exception is thrown at runtime
  • Compilation fails

Answer :1Z1-808

Which of the following is valid array declaration?   


Options are :

  • String strs[][] = new String[][1];
  • String strs[][] = new String[3][]; (Correct)
  • String strs[2][] = new String[][];
  • String strs[][] = new String[3];
  • String strs[][] = new String[][];

Answer :String strs[][] = new String[3][];

What is the total number of integers that array nums can hold in this code fragment?

int nums[][] = new int[3][3];

nums[0] = new int[2];


Options are :

  • 6
  • 8 (Correct)
  • 9
  • Not valid array statement

Answer :8

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

Which of the following is a correct anonymous array?


Options are :

  • new Integer[4]{1,2,3,4};
  • new Integer{1,2,3,4};
  • new Integer[3]{1,2,3,4};
  • new Integer[]{1,2,3,4}; (Correct)
  • None of the above.

Answer :new Integer[]{1,2,3,4};

What will be the output of this program?

1.             public class Whiz{

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

3.              

4.                             final int [ ]ints = new int[3];

5.                             int len = ints.length;

6.                             ints[1]++;

7.                             for(int i : ints)

8.                                             System.out.print(i);

9.                 }

10.    }             

 


Options are :

  • 000
  • 100
  • 010 (Correct)
  • An Exception is thrown.
  • Compilation fails due to an error at line 6.

Answer :010

What will be the output of this program?

1.             public class Whiz{

2.              

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

4.              

5.                                             int [][]ints = new int[3][2];

6.                                             ints[0] = new int[3];

7.                                             ints[2] = {1,2,3};

8.                                             System.out.print(ints[0].length + ints[2].length);

9.                             }

10.    }                             

 

 


Options are :

  • 4
  • 5
  • 6
  • An ArrayIndexOutOfBoundsException is thrown
  • Compilation fails (Correct)

Answer :Compilation fails

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

What will be the output of this program?

1.             import java.util.Arrays;

2.              

3.             public class Whiz {

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

5.                                int [ ][ ] ints = new int[2][];

6.              

7.                                Arrays.sort(ints[1]);

8.                            

9.                                System.out.print(Arrays.toString(ints[1]));

10.                 }

11.       }             

 


Options are :

  • [0, 0, 0]
  • [null,null,null]
  • null
  • NullPointerException (Correct)
  • Compilation fails

Answer :NullPointerException

What will be the output of this program?

1.             import java.util.Arrays;

2.              

3.             public class Program{

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

5.                            

6.                             int[] ints = {2,-1,4,5,3};

7.                             Arrays.sort(ints);

8.                             System.out.print(Arrays.binarySearch(ints, -1));

9.                 }

10.        }             

 



Options are :

  • 0 (Correct)
  • 1
  • -1
  • true
  • Compilation fails

Answer :0

What will be the output of this program?

1.             class Whiz{

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

3.                             new Whiz().meth();                      

4.                             }

5.                            

6.             public void meth()throws Exception{

7.                             for(int x=0;x>5;x++)

8.                                             System.out.print(x);

9.                             }

10.    }

 

 


Options are :

  • 01234
  • An exception is thrown at runtime.
  • Code will cause a never ending loop
  • Compilation fails (Correct)
  • No output

Answer :Compilation fails

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

What will be the output of this program?

1.             class Whiz {

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

3.                                int x = 5, y=10;                                                                  

4.                                        try {

5.                                                   y /=x;

6.                                             }

7.                                             catch(Exception e){

8.                                                   System.out.print("error");

9.                                             } finally {

10.                                              System.out.print("finally");

11.                                        }

12.                 }

13.       }

 


Options are :

  • error finally
  • error
  • Compilation fails
  • finally (Correct)
  • No output

Answer :finally

Which of the following exception is thrown by JVM when code uses a negative size while initializing an array?


Options are :

  • NullPointerException
  • NumberFormatException
  • IllegalArgumentException
  • NegativeArraySizeException (Correct)
  • ArrayIndexOutOfBoundsException

Answer :NegativeArraySizeException

What will be the output of this program?

1.             class Whiz {

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

3.                                 try {

4.                                           new Whiz().meth();

5.                                 } catch(ArithmeticException e) {

6.                                           System.out.print("Arithmetic");

7.                                 } finally {

8.                                           System.out.print("final 1");

9.                                 } catch(Exception e) {

10.                                      System.out.print("Exception");

11.                            } finally {

12.                                      System.out.print("final 2");

13.                            }

14.                  }

15.                   

16.                 public void meth()throws  ArithmeticException {

17.                          for(int x = 0; x < 5; x++) {

18.                                int y = (int) 5/x;

19.                                System.out.print(x);

20.                          }

21.                 }

22.         }


Options are :

  • Arithmetic final 1
  • Exception final 2
  • Arithmetic final 2
  • Exception
  • Compilation fails (Correct)

Answer :Compilation fails

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

What will be the output of this program?

1.             interface I{

2.             void meth();

3.             }

4.              

5.             class A implements I{

6.                             void A(String s){

7.                                             }

8.                             public void meth(){

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

10.                    }

11.    }

12.     

13.    class C extends A implements I{

14.                    public void meth(){

15.                                    System.out.print("C");

16.                    }

17.    }

18.     

19.    class Whiz{

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

21.                                     A a = new A();

22.                                    C c1 = (C)a;

23.                                    c1.meth();

24.                     }

25.    }

 


Options are :

  • A
  • C
  • An exception will be thrown at runtime (Correct)
  • Compilation fails due to an error at line 6
  • Compilation fails due to multiple errors

Answer :An exception will be thrown at runtime

Java source code will be compiled in to _ _ _ _ _  _ _ _ _ file.


Options are :

  • java
  • class (Correct)
  • exe
  • byte
  • None of the above

Answer :class

Which of the following command can be used to compile the code?   


Options are :

  • javac (Correct)
  • java
  • javadoc
  • jar
  • compile

Answer :javac

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

Question text

What will be the output of this program?

1.      public class Whiz{

2.                     

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

4.                                      int x = 10, y = 12;

5.                                      System.out.println("Answer is :" + x +y);

6.                      }

7.      }             

 


Options are :

  • Answer is : 22
  • Answer is : 1012 (Correct)
  • Answer is :
  • Compilation fails

Answer :Answer is : 1012

Which of the following statement is true?


Options are :

  • class declaration should come before import statement.
  • Package statement should come after class statement.
  • Comments can come before package statement. (Correct)
  • Constructor should appear before any other statement of a class.
  • None of the above.

Answer :Comments can come before package statement.

which of the following statement compiles successfully?


Options are :

  • final int / array[] = {1,2,3};
  • final int // array[] = {1,2,3};
  • final int /** */ array[] = {1,2,3} ; (Correct)
  • All of the above
  • None of the above

Answer :final int /** */ array[] = {1,2,3} ;

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

What will be the output of this program?

1.      public class Whiz{

2.                     

3.                      static int x = 2;

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

5.                                      if( x > 1){

6.                                                      x++;

7.                                                      int x = 4;

8.                                      }

9.                                      System.out.println(x);

10.                                 final int x = 10;

11.                 }

12.    }             

 


Options are :

  • 2
  • 3 (Correct)
  • 4
  • 10
  • Compilation fails

Answer :3

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions