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

Consider the following code:

1.      class Whiz{

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

3.                      try{

4.                                      int i = Integer.parseInt(args[0]);

5.                                      System.out.print(i);

6.                      }catch(ArrayIndexOutOfBoundsException e){

7.                                      System.out.println("Array Index Out Of Bounds Exception");

8.                      }catch(NumberFormatException e){

9.                                      System.out.println("Number Format Exception");

10.                 }

11. }

12. }

 

And the command-line invocations are                        

1.                                  java Whiz

2.                                  java Whiz 12

3.                                  java Whiz A

4.                                  java Whiz 12 24 

Which of the following are true? (Choose 3)


Options are :

  • Invocation 1 will print Array Index Out Of Bounds Exception. (Correct)
  • Invocation 2 will print will print 12. (Correct)
  • Invocation 3 will print Array Index Out Of Bounds Exception.
  • Invocation 3 will print Number Format Exception. (Correct)
  • Invocation 4 will print Array Index Out Of Bounds Exception.

Answer :Invocation 1 will print Array Index Out Of Bounds Exception. Invocation 2 will print will print 12. Invocation 3 will print Number Format Exception.

Which of the following is checked exception?   


Options are :

  • ClassCastException
  • NullPointerException
  • ExceptionInInitializerError
  • IllegalArgumentException
  • None of the above. (Correct)

Answer :None of the above.

Which of the following will be thrown by the JVM when a method calls itself too many times?   


Options are :

  • NullPointerException
  • NumberFormatException
  • IllegalArgumentException
  • ArrayIndexOutOfBoundsException
  • None of the above (Correct)

Answer :None of the above

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

What will be the output of this program code?

1.             public class Whiz{

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

3.                                try {

4.                                       print();

5.                                }catch(Exception e){}

6.                      }

7.                                            

8.                      static void print() {

9.                               try {

10.                                 throw new ClassCastException();

11.                          }catch(ClassCastException e) {

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

13.                          }finally {

14.                                  System.out.print("B ");

15.                          } finally {

16.                                  System.out.print("B 2");

17.                          }

18.                          System.out.print("OCAJP ");

19.                 }

20.       }

 

 

 


Options are :

  • A B B2 OCAJP
  • AB 2 OCAJP
  • B OCAJP
  • B B2
  • B followed by a NullPointerException.
  • Compilation fails. (Correct)

Answer :Compilation fails.

What will be the output of this program code?

1.      class Whiz{

2.                      int a=10;

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

4.                                      new Whiz().print();

5.                      }

6.                      public void print(){

7.                                      int a = 8;

8.                                      System.out.print(a + " ");

9.                      }

10.     }             

 

 


Options are :

  • 10
  • 18
  • 8 (Correct)
  • Compilation fails

Answer :8

Which will compile successfully when inserted at line 3?

1.      abstract interface Movable{

2.                      int x = 10;

3.                        //insert code here

4.                      void run();

5.       }

 

 


Options are :

  • private int x = 10;
  • abstract int i = 5;
  • final static float c = 6.0;
  • final short s=10; (Correct)
  • None of the above.

Answer :final short s=10;

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

Which of the following uses a fully qualified name in java?   


Options are :

  • class MyDate extends java.util.Date{ //statement; } (Correct)
  • import java.util.*; class MyDate extends Date{ //statement; }
  • import java.util.Date.*; class MyDate extends Date{ //statement; }
  • None of the above.

Answer :class MyDate extends java.util.Date{ //statement; }

What will be the output of this program code?

1.       

1.          import java.util.*;

2.              

3.          class Whiz {

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

5.                             System.out.println(new Date());

6.                    }

7.          }

 

 


Options are :

  • Prints today date (Correct)
  • An Exception
  • Compile Error at line 1
  • Compile Error at line 5.

Answer :Prints today date

Which of the following import statement will be inserted at line 1?

1.            // insert here

2.              

3.            public class Whiz {

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

5.                              System.out.println(pow(5,5));

6.                     }

7.            }             

 

 


Options are :

  • import java.lang.Math.*;
  • import static java.Math.*;
  • import static java.lang.Math.pow; (Correct)
  • import java.Math.pow;
  • No import statement is needed.

Answer :import static java.lang.Math.pow;

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

A java source code file can have -   


Options are :

  • Only two non-public classes.
  • Few public classes.
  • Few package statements.
  • Methods with same names in different classes (Correct)
  • None of the above.

Answer :Methods with same names in different classes

What will be the output of this program?

1.      class Whiz{

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

3.                                      int n = 808;

4.                                      System.out.format("%d", n);     

5.                                      System.out.format("%06d", n); 

6.                      }

7.     

 

 

 


Options are :

  • 808000808 (Correct)
  • 808000000808
  • 808808
  • An Exception is thrown.
  • Compilation fails.

Answer :808000808

What will be the output of this program?

1.        class Whiz {

2.       

3.                static int i;          

4.                int j;

5.                Whiz() { j=i++; }

6.       

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

8.                          Whiz s = new Whiz();

9.                          Whiz s1= new Whiz();

10.                      Whiz s2= new Whiz();

11.                     System.out.print( "i = "+s.i);

12.                     System.out.print( ", j = "+s.j);

13.           }

14. }

What is the result?


Options are :

  • i = 3, j = 3
  • i = 1, j = 0
  • i = 3, j = 0 (Correct)
  • An Exception is thrown.
  • Compilation fails.

Answer :i = 3, j = 0

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

What will be the output of this program?

1.      class Whiz{

2.       

3.      final int i;

4.                     

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

6.                                      Whiz s= new Whiz();

7.                                      System.out.println( "i = "+s.i);

8.                      }

9.      }

 

 


Options are :

  • Prints i = 0
  • Prints i = 1
  • Prints i = Null
  • Runtime Exception is thrown.
  • Compile-time Error. (Correct)

Answer :Compile-time Error.

Consider this program -

1.      class Whiz {

2.       

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

3.                          for (int x = 0; args.length > x++;) {

4.                                      System.out.print(args[x]+ " ");

5.                          }

6.                }

7.      }

 

And the command-line invocations are?

 I.             java Whiz

 II.            java Whiz 10

III.            java Whiz 10 11

 

Which of the following is true?


Options are :

  • Only the invocation I will complete without throwing exceptions (Correct)
  • Only Invocation I will throw an exception.
  • Invocation II will produce he output 12.
  • Invocation III will produce the output 12 32.
  • All the invocations will not throw exceptions.

Answer :Only the invocation I will complete without throwing exceptions

What will be the output of this program?

1.      class Whiz{

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

3.                        new Whiz().iterator(new int[]{10,12,13});

4.                }

5.                void iterator(int [ ]i) {

6.                        for(int x=0;x<i.length;System.out.print(i[x] + " "))x++;                

7.                }

8.      }

 

 


Options are :

  • 10 12 13
  • 12 13
  • 10 12.
  • 12 13 followed by an exception (Correct)
  • Compilation fails

Answer :12 13 followed by an exception

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

What will be the output of this program?

1.       class Whiz {

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

3.                          int [ ] a = {1,2,3,4};

4.                                                                            

5.                          for (int j : a) {

6.                                  if ( j==2 ) continue;

7.                                           for (int x = 0; x < 3; System.out.print(x)) { x++;  }

8.                          }

9.                 }

10.    }

 

 


Options are :

  • 123
  • 123123
  • 123123123 (Correct)
  • Compilation fails.
  • Will print 123 and then goes to never ending loop.

Answer :123123123

What will be the output of this program?

1.         class Whiz {

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

3.                                                                                                                                            

4.                              Loop1: for (int x = 0;x<5; x++) {

5.                                          if (y==3) break Loop1;

6.                                          for (int y = 0; y<3; y++) {

7.                                                   if (y==2) continue Loop1;

8.                                                       System.out.print(y);

9.                                           }

10.                           }

11.               }

12.     }

 

 


Options are :

  • 0101010101
  • 123123
  • 123123123
  • Compilation fails. (Correct)
  • Will print 123 and then goes to never ending loop.

Answer :Compilation fails.

What will be the output of this program?

1.        class Whiz {

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

3.                                                                                                                                            

4.                             Loop1:for (int x = 0; x < 5 ;x++) {

5.                                       if (x == 3) continue;

6.                                       for (int y = 0; y < 3; y++) {

7.                                              if (y == 1) continue Loop1;

8.                                                  System.out.print(y);

9.                                        }

10.                        }

11.              }

12.    }

 

 


Options are :

  • 012012012012
  • 0000 (Correct)
  • 00000
  • Compilation fails.
  • Will print 000 and then goes to never ending loop.

Answer :0000

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

What will be the output of this program?

1.        class Whiz {

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

3.                                                                                                                                            

4.                             Loop1: for (int x = 0; x < 3; x++) {

5.                                       for (int y = 0; y < 3; y++) {

6.                                                if ( y==2 ) continue;

7.                                                  if ( y==2 && x==2 ) break Loop1;

8.                                                     System.out.print(y);

9.                                        }

10.                        }

11.               }

12.     }

 

 


Options are :

  • 01010101
  • 000
  • 010101 (Correct)
  • Compilation fails.

Answer :010101

Which of the following can be used to iterate through all elements of this array?

                double [] dlbs = {1,5.1,2.0,7};


Options are :

  • for(double d : double dlbs[]){ }
  • for(double d ; dlbs){ }
  • for(dbs ; dlbs){ }
  • for(dbs : double dlbs){ }
  • for(double d : dlbs){ } (Correct)

Answer :for(double d : dlbs){ }

What will be the output of this program?

1.      public class Whiz {

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

3.                         do {

4.                                   int i = 3;

5.                                   System.out.print(i++);

6.                               } while (i <= 6);

7.                }

8.      }

 

  


Options are :

  • 333
  • 3456
  • 333333
  • Infinite loop of 3
  • Compilation fails (Correct)

Answer :Compilation fails

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

What will be the output of this program?

1.      public class Whiz {

2.                     

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

4.                        String out = "";

5.                         z: for (int i = 3; i< 8; i++) {                            

6.                                   if (i == 3)

7.                                       continue;

8.                                    if (i== 3)

9.                                         break z;

10.                               out += i;

11.                      }

12.                      System. out .println(out);

13.           }

14.    }

 

 


Options are :

  • 4
  • 4567 (Correct)
  • 34567
  • No output will be produced.
  • Compilation fails due to multiple errors.

Answer :4567

What will be the output of this program?

1.        class Whiz {

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

3.                           for (int x = 10; x > 5; x++) {

4.                                   if (x == 16) x -= 11;

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

6.                            }             

7.                  }

8.        }

 

 


Options are :

  • 10 11 12 13 14 15 3.
  • 10 11 12 13 14 15
  • The program will hang without ever completing. (Correct)
  • Compilation fails due to an error at line 4.
  • Compilation fails due to multiple errors.

Answer :The program will hang without ever completing.

Which of the following has higher precedence than "=="?   


Options are :

  • !=
  • &
  • ^
  • +=
  • -- (Correct)

Answer :--

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

Which of the following will compile and produce BC when inserted at line 4?

1.        class Whiz {

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

3.                             final int s = 2;

4.                             // insert code here

5.                             switch(s) {

6.                                     case 1       : System.out.print("A");break;

7.                                     case x-1    :System.out.print("B");

8.                                     case x       : System.out.print("C");break;

9.                                     case x+1   : System.out.print("D");break;

10.                                default      : System.out.print("F");

11.                       }

12.            }

13.      }

 

 


Options are :

  • int x=2;
  • final int x=2;
  • int x=3;
  • private final int x = 3;
  • None of the above. (Correct)

Answer :None of the above.

Which of the following method of String class has been introduced in java SE 8?   


Options are :

  • split
  • replaceFirst
  • append
  • set
  • join (Correct)

Answer :join

Which of the following method was introduced in ArrayList in java SE 8?   


Options are :

  • length
  • spliterator (Correct)
  • retainAll
  • listIterator
  • None of the above

Answer :spliterator

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

What will be the output of this program?

1.      public class Whiz{

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

3.                         java.util.List<String> list = new java.util.ArrayList<>();

4.                         list.add("A");

5.                         list.add("C");

6.                         list.add("E");

7.                         list.add("D");

8.                         list.add(1, "B");

9.                         list.set(4, "F");

10.                   System.out.println(list);

11.        }

12.   

 

 


Options are :

  • [A, B, C, E, F] (Correct)
  • [A, C, E, F]
  • [A, B, C, F, E]
  • An Exception is thrown.
  • Compilation fails.

Answer :[A, B, C, E, F]

Which of the following statement can be used to declare and instantiate an ArrayList to hold only Book instances with a default capacity of 10 elements?   


Options are :

  • ArrayList book= new ArrayList<>();
  • ArrayList book= new ArrayList<>(10);
  • ArrayList book= new ArrayList(10);
  • ArrayList book= new ArrayList();
  • All of the above. (Correct)

Answer :All of the above.

Which of the following can be used to get the number of elements in an ArrayList object?   


Options are :

  • capacity
  • length
  • size()
  • length()
  • None of the above. (Correct)

Answer :None of the above.

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

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions