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

Which of the following will print 4 as the output when inserted at line 4?

1.             public class Whizlab{

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

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

4.                                     // insert here

5.                     }

6.             }

 


Options are :

  • System.out.print(array[2][2]);
  • System.out.print(array[1][2]);
  • System.out.print(array[3][2]);
  • System.out.print(array[2][0]); (Correct)
  • System.out.print(array[2][1]);

Answer :System.out.print(array[2][0]);

Which of the following will print output 5 when inserted at line 4?

1.             public class Whizlab{

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

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

4.                                     // insert here

5.                     }

6.             }

 


Options are :

  • System.out.print(array[2][2]);
  • System.out.print(array[1][2]);
  • System.out.print(array[3][2]);
  • System.out.print(array[3][0]);
  • System.out.print(array[2][1]); (Correct)

Answer :System.out.print(array[2][1]);

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

What will be the output of this program?

1.             public class Whizlab {

2.                      static long index = 2;

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

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

5.                              System.out.print(array[index][1]);

6.                      }

7.             }

 

 


Options are :

  • 2
  • 3
  • 5
  • ArrayIndexOutOfBoundsException is thrown.
  • Compilation fails. (Correct)

Answer :Compilation fails.

Given

1.             class Whizlab{

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

3.                             new Whizlab().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.

After which line the object created at line 1 is eligible for garbage collection?

public class Employee {
    private static Employee e;

    public static void main(String[] args) {
        Employee e1 = new Employee();// Line 1
        Employee e2 = new Employee();// Line 2
        e2.join(e1);// Line 3
        e1 = new Employee();// Line 4
        e1 = null;// Line 5
        e2.join(e1);// Line 6
    }

    public void join(Employee s) {
        e = s;
    }

 


Options are :

  • Line 1
  • Line 2
  • Line 3
  • Line 4
  • Line 5
  • Line 6 (Correct)

Answer :Line 6

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

What will be the output of this program?

1.       class Whizlab {

2.                      

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

4.                          try {

5.                                  System.out.println(args[0]);

6.                          }catch (ArrayIndexOutOfBoundsException | ArithmeticException | NullPointerException e) {

7.                                   if (e instanceof ArrayIndexOutOfBoundsException) {

8.                                                   e = new ArrayIndexOutOfBoundsException("Out of bounds");

9.                                  } else if(e instanceof NullPointerException) {

10.                                                 e = new NullPointerException("Null Value");

11.                                } else {

12.                                                 e = new ArithmeticException("Arithmetic");

13.                                }

14.                                System.out.println(e.getMessage());

15.                       }                                   

16.             }

17.     }

 

 

 


Options are :

  • Null
  • Null Value
  • Arithmetic
  • Out of bounds
  • Arithmetic Null
  • Compilation fails (Correct)

Answer :Compilation fails

Which of the following is true about this given code fragment?

public static void main(String[] args){
                    try{
                                    test();
                    }catch(IOException x) {
                                   
                    }
    }
   public static void test(){
                   System.out.println("I'm running at position: " + 0);
  }

 


Options are :

  • Compiler can compile the code.
  • Compiler complains because there is no finally block.
  • An Exception will be thrown.
  • Compiler complains because of the catch block. (Correct)
  • Output would be I'm running at position: 0.

Answer :Compiler complains because of the catch block.

What will be the output while running the program with args as the array ["0", "2"]?

1.          public class Whizlab {

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

3.                            try {

4.                                       int x = args.length;

5.                                       int  v = 10 / x;

6.                                       System.out.print(x);

7.                                       try {

8.                                               if (x == 1)

9.                                                x = x / x - x;

10.                                           if (x == 2) {

11.                                                  int [ ] c = {2};

12.                                                  c[3] = 3;

13.                                           }

14.                                  }

15.                                  catch (ArrayIndexOutOfBoundsException e) {

16.                                            System.out.println("out of bounds.");

17.                                   }

18.                                   catch (ArithmeticException e) {

19.                                            System.out.println("Arithmetic");

20.                                    }

21.                         }

22.                }

23.      }

 

 


Options are :

  • 2
  • 2out of bounds.
  • out of bounds.
  • Arithmetic
  • Compilation fails. (Correct)

Answer :Compilation fails.

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

What will be the output of this program?

1.           public class Whizlab { 

2.            

3.                    public static void run() {

4.                             throw new RuntimeException();

5.                    }

6.            

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

8.                             try {

9.                                    run(); 

10.                               System.out.println("OK"); 

11.                        } catch (RuntimeException runtime) {

12.                               System.out.println("Runtime.");

13.                        } catch (Exception ex1) {

14.                                System.out.println("Exception here.");

15.                        } finally {

16.                                System.out.println("Finally here.");

17.                        }

18.                       System.out.println("Terminated.");

19.               }

20.      }

 

 


Options are :

  • Terminated.
  • Runtime. Finally here. Terminated. (Correct)
  • Compilation error because Exception is subclass of RuntimeException.
  • Runtime.
  • OK Finally here. Terminated.

Answer :Runtime. Finally here. Terminated.

What happens when the following code compiles and executes ?

                public class Whizlab{

                                public static void main(String[] args){

                                                try {

                                                                int a[] = new int[4];

                                                a[3] = (a[0]+a[1])/a[2];

                                                System.out.println(a[3]);

                                                }catch(ArithmeticException e){

                                                                System.out.println("A");

                                                }catch(Exception ex){

                                                                System.out.println("E");

                                                }

                                }

                }

 


Options are :

  • 0
  • The program cannot compile because all the elements in the array "a" are not initialized.
  • A (Correct)
  • E AE

Answer :A

Which of the following exception is thrown by the programmer when trying to convert a string to a number which is not in appropriate number format?   


Options are :

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

Answer :NumberFormatException

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

Which of the following represents the main method signature?   


Options are :

  • public static void main(String args)
  • public void main(String [] args)
  • public static void main(String i[]) (Correct)
  • public static main(String [] args)
  • None of the above

Answer :public static void main(String i[])

What will be the output of this program?

1.      public class Whizlab{

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

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

4.                         sb = sb.append("lab");

5.                         sb.append('s');

6.                         sb.setLength(7);

7.                         System.out.println(sb);

8.         }     

9.      }   

 

 


Options are :

  • Whizlabs
  • Whizlab (Correct)
  • Whiz
  • Whizla
  • An Exception will be thrown

Answer :Whizlab

What are the correct statements? 

Select 2 options.


Options are :

  • Low cohesion is better.
  • Loose coupling is bad.
  • Tight coupling makes it easier to maintain program.
  • High cohesion makes it easier to maintain program. (Correct)
  • High coupling is bad.

Answer :High cohesion makes it easier to maintain program.

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

Which of the following statement is true?

        I.            We can use "#" symbol for single line comment in java.

      II.            We can use "//" for multi-line comments in java.

    III.            We can use "/*" and "*/" for multi-line comments in java.


Options are :

  • Only I.
  • Only II.
  • Only III. (Correct)
  • Only I and III.
  • All the statements are true.

Answer :Only III.

Which of the following is highly required for a class file?   


Options are :

  • A method.
  • A variable.
  • A constructor. (Correct)
  • A main method.
  • None of the above.

Answer :A constructor.

Which of the following is a valid top level class declaration?   


Options are :

  • class switch{ }
  • public static class Test{}
  • protected class Test{ }
  • private class Test{ }
  • None of the above (Correct)

Answer :None of the above

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

Given the following method from the java.util.Arrays

Method Summary: public static void sort(int[ ] a)

And given,

1.              public class Sorter{

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

3.                                          int[ ] a = {3,1,22,5,11,2};

4.                                          sort(a);

5.                          }

6.              }

 

Which of the following will make this code work?


Options are :

  • By adding the import statements "import static java.util.Arrays.sort;" (Correct)
  • By adding the import statement "static import java.util.Arrays.*;"
  • By adding the import statement "import java.util.Arrays.*;"
  • By adding the import statement "import java.util.Arrays.sort;"
  • By adding the import statement "import static java.util.*;"

Answer :By adding the import statements "import static java.util.Arrays.sort;"

Given

1.      class Whizlab {

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

3.                       StringBuilder s =new StringBuilder("Java");

4.                       s.insert(0, "The latest").append("version is").append(" 1.7").delete(27, 28).append("8").substring(4);

5.                      System.out.print(s);

6.               }

7.      }

 

What is the result?


Options are :

  • latest Java version is 1.7
  • latest Java version is 1.8
  • The latest Java version is 1.
  • The latestJavaversion is 1.8 Compilation fails due to an error at line 4. (Correct)

Answer :The latestJavaversion is 1.8 Compilation fails due to an error at line 4.

What will be the output of this program?

1.         class Whizlab {

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

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

4.                              int i = a.length 1;

5.                              while (i >= 0) {

6.                                          if (i%2 != 0) {

7.                                                  System.out.print(a[i]);

8.                                                   i--;

9.                                          }

10.                         }

11.               }

12.     }

 

 


Options are :

  • 6 will be printed and hang in an infinite loop. (Correct)
  • 6
  • 642
  • 135
  • 135 followed by an exception

Answer :6 will be printed and hang in an infinite loop.

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

Consider, this given program:

1.       class WhizLab {

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

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

4.                           // insert code here

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

6.                           }

7.                 }

8.        }

Which of the following will compile successfully when inserted at line 4? 

  I.             for (i : arr) {

 II.             for (int i : arr) {

 III.            for (arr : int i) {

 

 


Options are :

  • I only
  • II only (Correct)
  • III only
  • I and III only
  • All the three

Answer :II only

What will be the output of this program?

1.       class Whizlab {

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

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

4.                           Loop1: for (int i = 0; i < ints.length; i++) {

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

6.                                    for (int y = 0;y<ints[i].length;y++) {

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

8.                                            System.out.print(ints[i][y]);

9.                                    }

10.                      }

11.             }

12.     }

 

 


Options are :

  • 136
  • 1267
  • 123567
  • Compilation fails. (Correct)
  • Will print 13 and then goes to never ending loop.

Answer :Compilation fails.

What will be the output of this program?

1.             class Whizlab{

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

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

4.                     for(int x = 0;++x <4;x++)

5.                                     System.out.print(a[x]);

6.                     }

7.             }

 

 


Options are :

  • 123
  • 23
  • 123 followed by an ArrayIndexOutOfBoundsException
  • 2 followed by an ArrayIndexOutOfBoundsException (Correct)
  • Compilation fails.

Answer :2 followed by an ArrayIndexOutOfBoundsException

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

Consider this given statement:

"ArrayList uses an array to store the elements. Arrays have a fixed size. The array that ArrayList uses has to have a default size,"

What is the default size?


Options are :

  • 0
  • 1
  • 2
  • 10 (Correct)
  • There is no default value

Answer :10

What will be the output of this program?

1.      public class Whizlab {

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

3.                        int x = 0;

4.                        while((x = 0) <= 1) {

5.                                System.out.print(x);

6.                                x++;

7.                        }

8.               }

9.      }

 

 


Options are :

  • 0
  • 01
  • 1
  • 0 will be printed endlessly. (Correct)
  • Compilation error.

Answer :0 will be printed endlessly.

What will be the output of this program?

1.       public class Whizlab {

2.                                     

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

4.                          int x = 0;

5.                          for ( ; x < 5; x += 3) {

6.                                  System.out.print(x);

7.                          }

8.                 }

9.        }

 

 


Options are :

  • 012345
  • 0123
  • 03 (Correct)
  • 0
  • Compilation fails.

Answer :03

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

Given

1.      public class Whizlab{

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

3.                      int ar[][] = {{1,11},{1},{1,11}};

4.                      for(int x = 0;x < ar.length;x++){

5.                                      //insert here

6.                      }

7.           }

8.      }

 

Which of the following can be inserted at line 5, will print all elements of the array?


Options are :

  • for(x : ar[x]) System.out.print(x);
  • for(int y : ar) System.out.print(y);
  • for(int y : ar[x]) System.out.print(y); (Correct)
  • System.out.print(ar[x]);
  • None of above

Answer :for(int y : ar[x]) System.out.print(y);

What will be the output of this program?

1.       

1.             class Whizlab{

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

3.                               short s=9;

4.                               double d = 10;

5.                               String str = s==d?"s=d":(s==10)?"s=10":"nothing";

6.                               System.out.print(str);

7.                   }

8.             }

 

 


Options are :

  • s=10
  • s=d
  • nothing (Correct)
  • s=d s=10
  • Compilation fails.

Answer :nothing

What will be the output of this program?

1.      class Whizlab{

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

3.                                int y=10;

4.                                int x = 10;

5.                                if(x!=10 && y++==1);

6.                                if(y==11 | ++x==11) y+=10;

7.                                                System.out.print(y);

8.                      }

9.      }

 

 


Options are :

  • 11
  • 10
  • 20 (Correct)
  • 21
  • Compilation fails.

Answer :20

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

What will be the output of this program?

1.             class Whizlab{

2.             static Integer y;

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

4.                               int x = 10;

5.                               if(x++>10 & y++==1)y+=10;

6.                                               System.out.print(y);

7.                               }

8.             }

 

 


Options are :

  • 10
  • 11
  • 20
  • An exception will be thrown. (Correct)
  • Compilation fails.

Answer :An exception will be thrown.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions