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

Which of the following code will create a two-dimensional array of integers correctly?   


Options are :

  • int array[ ][ ] = new int[10,10];
  • int array [ ][ ] = new int[10];
  • int array [ ][ ] = new int[10][ ]; (Correct)
  • int [ ] array [ ] = new int[ ][10];
  • int array[10][ ] = new int[ ][ ];

Answer :int array [ ][ ] = new int[10][ ];

Select the statement that is equal to this code fragment.

               int array[ ][ ][ ] = new int[1][2][3];

                array[0][0][0] = 8;

                array[0][0][2] = 12;

                array[0][0][1] = 10;

                array[0][1][0] = 40;

                array[0][1][2] = 11;

                array[0][1][1] = 21;


Options are :

  • int array1[ ][ ]= {{8,10,12},{40,21,11}};
  • int array[ ][ ][ ] = {{{8,10,12},{40,21,11}}}; (Correct)
  • int array[ ][ ][ ] = {{8,10,12},{40,21,11}};
  • int array[ ][ ][ ] = {{8,12,10},{40,11,21}};
  • int array[ ][ ][ ] = {{{8,12,10},{40,11,21}}};

Answer :int array[ ][ ][ ] = {{{8,10,12},{40,21,11}}};

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

What will be the output of this program code?

1.             import java.util.Arrays;

2.              

3.             public class Program {

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

5.                             String[ ] strings = {"N","L","n","O","S"};

6.                             Arrays.sort(strings);

7.                                             for(String s :  strings){

8.                                                             System.out.print(s);

9.                                             }                             

10.        }

11.    }             

 


Options are :

  • NLnOS
  • LnNOS
  • nLNOS
  • LNOSn (Correct)
  • Compilation fails

Answer :LNOSn

What will be the output of this program code?

1.             import java.util.Arrays;

2.              

3.             public class Program {

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

5.                            

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

7.                             int[ ] a2 = {2,-1,4,5,3};

8.                            

9.                             System.out.print((a1 == a2) + " ");

10.                        System.out.print(Arrays.equals(a1, a2) + " " );

11.                        System.out.print(Arrays.deepEquals(a1, a2) + " " );

12.          }

13.    }             

 


Options are :

  • false true true
  • true true true
  • false true false
  • true false false
  • Compilation fails (Correct)

Answer :Compilation fails

Which of the following exception/error is thrown by the Java Virtual Machine (JVM)?   


Options are :

  • IllegalArgumentException
  • ExceptionInInitializerError (Correct)
  • IOException
  • FileNotFoundException
  • NumberFormatException

Answer :ExceptionInInitializerError

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

What will be the output of this program?

1.          import java.io.FileNotFoundException;

2.          import java.io.IOException;

3.           

4.          public class Whiz {

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

6.                             try {

7.                                      throw method();

8.                                   } catch(IOException e) {

9.                                      System.out.println("caught");

10.                         }

11.               }

12.                  

13.               public static IOException method() {

14.                        try {

15.                                   return new IOException();

16.                         } catch(FileNotFoundException e) {

17.                                   return new FileNotFoundException();

18.                         }

19.                }

20.     }

 


Options are :

  • caught
  • No output
  • An uncaught exception is thrown
  • Compilation fails (Correct)

Answer :Compilation fails

What will be the output of this program?

1.          import java.io.FileNotFoundException;

2.          import java.io.IOException;

3.           

4.          public class Whiz {

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

6.                             try {

7.                                       method();

8.                             } catch(FileNotFoundException fne) {

9.                                      System.out.println("File Not Found Exception");

10.                        } catch(IOException ioe) {

11.                                 System.out.println("IO Exception");

12.                        }

13.               }

14.                  

15.               public static void method()throws IOException {

16.                        throw new FileNotFoundException();

17.               }

18.     }

 

what   will be the output of this program?


Options are :

  • File Not Found Exception (Correct)
  • IO Exception
  • File Not Found Exception, IO Exception
  • No output
  • Compilation fails

Answer :File Not Found Exception

Which of the following exception is thrown when a programmer converts a string to a numeric type but the string doesn't have an appropriate format?   


Options are :

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

Answer :NumberFormatException

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

Which of the following exceptions can be thrown by this code fragment?

(Choose 2 options)

                public int size(Object obj) {

                                return ((int[])obj).length;

                 }

 

 


Options are :

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

Answer :ClassCastException NullPointerException

What will be the output of this program?

1.          public class Whiz {  

2.                         

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

4.                              try {

5.                                        print();

6.                                    } catch(Exception e){}

7.                   }

8.                                       

9.                   static void print() {

10.                         try {

11.                                   throw new NullPointerException();

12.                              } catch(ClassCastException e) {

13.                                   System.out.print("Class Cast ");

14.                              } finally {

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

16.                              }

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

18.              }

19.   }

 


Options are :

  • Class Cast Final OCAJP
  • Class Cast OCAJP
  • Final OCAJP
  • Final (Correct)
  • Final followed by a NullPointerException.
  • Compilation fails.

Answer :Final

Which of the following will create an appropriate catch block for this given try block?   


try { int x = Integer.parseInt("one"); } 


Options are :

  • ClassCastException
  • IllegalStateException
  • IllegalArgumentException (Correct)
  • ExceptionInInitializerError
  • ArrayIndexOutOfBoundsException

Answer :IllegalArgumentException

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

Which of the following will import all static members of java.util.Arrays class?                                                        


Options are :

  • import static java.util.Arrays;
  • import java.util.Arrays;
  • import static java.util.Arrays.*; (Correct)
  • static import java.util.Arrays;
  • import java.util.Arrays.*;

Answer :import static java.util.Arrays.*;

Consider the following directory structure.  (Assume that current directory is the same directory where "com" is located.)

                             com - - -|

                                           |- Abc.class

                                           |- -foo - -|

                                                          |- One.class

                                                          |- Two.class                    

                                                          |- bar - -|

                                                                        |- Test.class 

Now consider the following import statement -

                                import com.foo.*;

Which of the following is the set of classes that will be imported on the execution of above statement?


Options are :

  • Abc, One, Two, Test
  • One, Two, Test
  • One, Two (Correct)
  • Abc, One, Two
  • None of the above

Answer :One, Two

Find the number of unnecessary import statements in this program code.

1.          import java.lang.Math.*;

2.          import static java.lang.Math.*;

3.          import java.lang.*;

4.           

5.          public class Whiz {

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

7.                                          System.out.println(PI);

8.                          }

9.          }

 

 


Options are :

  • 1
  • 2 (Correct)
  • 3
  • None of the statement can be removed.
  • Compilation fails

Answer :2

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

Which of the following options can appear before the package statements in a single code file?   


Options are :

  • Comments and another package statement.
  • Class statements and/or comments.
  • Another package statement.
  • Import statements and/or class statement.
  • Comments (Correct)

Answer :Comments

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

I.    The scope of a local variable is wider than the scope of an instance variable.

II.   The instance variables are only visible to the methods in which they are declared.

III.  The variable "x" could be an instance variable in the declaration "final int x = 10". 


Options are :

  • Only I.
  • Only III. (Correct)
  • Only I and II.
  • Only I and III.
  • None

Answer :Only III.

What will be the output of this program code?

1.          public class Whiz {

2.                          public static void main(int [ ] i) {

3.                                          System.out.print("main1");

4.                          }

5.                          public static void main(String... c) {

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

7.                          }

8.                          public static void main(String c) {

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

10.                   }

11.   }

 

 


Options are :

  • main1
  • main2 (Correct)
  • main3
  • An Error is thrown at the runtime, stating that, Main method not found in class Whiz.
  • Compilation fails.

Answer :main2

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

What will be the output of this program?

1.          public class Whiz {

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

3.                                          int x = 10;

4.                                          if ( x > 10 ) {

5.                                                          int y = 20;

6.                                          } else {                   

7.                                                          int y = 30;

8.                                          }

9.                                          System.out.println(y);

10.                   }

11.         }

 



Options are :

  • 20
  • 30
  • Compilation fails due to an error at line 7.
  • Compilation fails due to an error at line 9. (Correct)
  • Compilation fails due to multiple errors.

Answer :Compilation fails due to an error at line 9.

What will be the output of this program?

1.          public class Whiz {

2.                          static int x = 50;

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

4.                                          Integer [ ] a = new Integer[2];

5.                                          a[1] = 10;

6.                                          for (Integer I:a)

7.                                                          System.out.print(I);

8.                          }

9.          }

 


Options are :

  • null10
  • 10
  • A null pointer exception is thrown.
  • Compilation fails. (Correct)
  • None of the above.

Answer :Compilation fails.

What will be the output of this program?

1.              public class Whiz {

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

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

4.                                    int y =  a.length;

5.                                    int x = y - 1;

6.                         

7.                                    while ( y >= 0 ) {

8.                                        System.out.print( a[--y] );

9.                                    }             

10.                   }

11.         }         

 

 


Options are :

  • 0123
  • 3210
  • Never ending loop after printing 3210.
  • 0123 followed by an exception. (Correct)
  • Compilation fails.

Answer :0123 followed by an exception.

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

What will be the output of this program?

1.              class Whiz {

2.               

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

4.                       String s = "A";

5.                       String c1 = "A";

6.                       String c2 = "B";

7.                       String c3 = "C";

8.               

9.                       switch(s) {

10.                                   case c1  : {System.out.print("A");};

11.                                   default : {System.out.print("default ");};

12.                                   case c2  : {System.out.print("B");}; break;           

13.                                   case c3  : {System.out.print("C");};         

14.                                   }

15.                   }

16.       }

 

 


Options are :

  • A
  • Adefault
  • AdefaultB
  • Compilation fails as Strings can't be used as the case constant.
  • Compilation fails due to multiple errors. (Correct)

Answer :Compilation fails due to multiple errors.

Which of the following will result in false?


Options are :

  • true | (false ^ true);
  • (true | false ^ true);
  • true | false ^ true;
  • (true | false) ^ true; (Correct)
  • None of the above.

Answer :(true | false) ^ true;

What will be the output of this program?

1.              public class Whiz {

2.                          static String out = "";

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

4.                         

5.                                   int x = 5, y = 8;

6.               

7.                                   if( x ++== 5 )

8.                                           out += "1";

9.                                   if( x != 6 ) { }

10.                              else if (x > 9) { out += "2"; }

11.                              else if (y < 9) { out += "3"; }

12.                              else if (x == 6) { out += "4"; }

13.                              else { out += "5"; }

14.                  

15.                             System.out.println(out);

16.                    }

17.     }                                                                                                                         

 


Options are :

  • 1
  • 134
  • 15
  • 13 (Correct)
  • Compilation fails.

Answer :13

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

What will be the output of this program?

1.              class Whiz {

2.               

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

4.               

5.                                   int value1 = 1;

6.                                   int value2 = 2;

7.               

8.                                   if (value1 == value2)

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

10.                              if (value1 != value2)

11.                                    System.out.print("2");

12.                              if (value1 > value2)

13.                                    System.out.print(3);

14.                              if (value1 < value2)

15.                                    System.out.print(4);

16.                              if (value1 => value2)

17.                                    System.out.print("5");

18.        

19.                    }

20.          }                                                                         

 

 


Options are :

  • 245
  • 24
  • Compilation fails due to an error at line 13.
  • Compilation fails due to an error at line 16. (Correct)
  • Compilation fails due to multiple errors

Answer :Compilation fails due to an error at line 16.

Which of the following is true?

1.          public class Whiz {

2.            

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

4.                         

5.                             int y = 5;

6.                          

7.                             if (false && y++==11)

8.                                      System.out.print(y);

9.                              else if(true || --y==4)

10.                                 System.out.print(y);

11.                         else( y==5){}

12.                }

13.       }

 


Options are :

  • The output will be 6.
  • The output will be 4.
  • The output will be 5.
  • There is no output.
  • Compilation fails. (Correct)

Answer :Compilation fails.

What will be the output of this program code?

1.              class Whiz {

2.               

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

4.                            int x = 20;

5.                            int y = 13;

6.                            System.out.print("" + x + y + " ");

7.                            System.out.print( x + y + " ");

8.                            System.out.print(x + " " + y);

9.                  }

10.       }                                                                         

 


Options are :

  • 33 33 33
  • 2013 33 20 13 (Correct)
  • 33 33 20 13
  • 2013 2013 2013
  • Compilation fails.

Answer :2013 33 20 13

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

Which of the following statement is true?


Options are :

  • There is no difference in using "==" or the "equals()" method for testing equality between objects.
  • Using "==" to test equality for primitives, will always provide accurate result except for float/double values (Correct)
  • If "equals()" method returns true then "==" test will also result true.
  • We can overload the "equals()" method to define how the two objects are going to be equal.
  • Using the "equals()" method to test equality for primitives, will always provide accurate result.

Answer :Using "==" to test equality for primitives, will always provide accurate result except for float/double values

Which of the following will compile successfully?


Options are :

  • for ( int j = 0, int k = 5; j < k; k-- ) ;
  • for ( ; ; System.out.print("a") ) ; (Correct)
  • for ( );
  • for ( int k = 10; k--; k > 0 ) ;
  • None of the above.

Answer :for ( ; ; System.out.print("a") ) ;

What will be the output of this program code?

1.              class Whiz {

2.               

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

4.                                   int x = 0;

5.                                   String [ ] animal = new String[3];

6.                                         

7.                                   do{animal[x] = "Cat"; x++;} while(false);

8.                                   do{animal[x] = "Dog";} while(x>animal[x++].length());

9.                                   do{animal[x] = "Rat";} while(x>3);

10.                                  

11.                              for(String s:animal){

12.                                     System.out.print(s + " ");

13.                              }

14.        

15.                   }

16.       }                                                         

 

 


Options are :

  • Cat Dog Rat (Correct)
  • Dog Rat
  • A run-time exception is thrown.
  • Compilation fails due to an error at line 7.
  • Compilation fails due to an error at line 8.

Answer :Cat Dog Rat

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

Which of the following will print all elements of the array "arr"?

          int arr[ ][ ] = {{1, 3, 5},{7,8}};


Options are :

  • for(int [ ]a : arr) for(int i: a) System.out.print(i + " "); (Correct)
  • for(int [ ]a : arr); for(int i: a) System.out.print(i + " ");
  • for(int a : arr) System.out.print(a + " ");
  • None of the above.

Answer :for(int [ ]a : arr) for(int i: a) System.out.print(i + " ");

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions