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.                     

3.                      static int x = 2;

4.                      static int z;

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

6.                                      System.out.println(x+z);

7.                      }

8.                     

9.                      static{

10.                                 int x = 3;

11.                                 z = x;

12.                 }

13. }             



Options are :

  • 2
  • 4
  • 5 (Correct)
  • 6
  • Compilation fails

Answer :5

You need to create a class to store information about Books contained in a library. The library class has provided a static method to add Book. When each book is added, the library should update the number of books field, which records the total number of books.

Which of the following variable scope is best suited for that field?


Options are :

  • Method parameter
  • Instance variable
  • Static variable (Correct)
  • Global variable
  • Local variable

Answer :Static variable

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

Which of the following is correct static import statement?


Options are :

  • import java.lang.Math.*;
  • import static java.lang.Math.abs();
  • static import java.lang.Math.abs();
  • static import java.lang.Math;
  • import static java.lang.Math.abs; (Correct)

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

Given :

1.             class Whiz {

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

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

4.                               int i = a.length 1;

5.                            

6.                               while (i >= 0) {

7.                                       if (i == 2) continue;

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

9.                                              i--;         

10.                           }

11.                 }

12.        }

 


Options are :

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

Answer :Will print 654 and then goes to never ending loop.

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.                                                                            

5.                                 for(int j : a) {

6.                                        if (j == 2) continue;

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

8.                                                        x++;

9.                                                }

10.                           }

11.                  }

12.        }

 


Options are :

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

Answer :123123

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

Which of the following will print all the elements of the array when inserted at line 10?

1.      class Whiz{

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

3.       

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

5.                     

6.                                                      outer:for(int x = 0, k=0; x<3; x++){

7.                                                      k=0;

8.                                                      inner:while(true){

9.                                                      System.out.print(array[x][k++]);

10.                                                 // insert here

11.                                                 }

12.                                 }

13.                 }

14. }

 


Options are :

  • if (k == 3) break outer;
  • if (k == 3) break inner ; (Correct)
  • if (k > 3) break;
  • break;
  • None of the above.

Answer :if (k == 3) break inner ;

Which of the following statement is true?


Options are :

  • When we need to execute a certain section, we can use the "while" or the "for" statements.
  • The "do/while" loop allows is allowed for any number of possible execution paths.
  • The "while" and the "do-while" are equivalent.
  • The "while" evaluates its conditional expression at the bottom of the loop
  • The "for" loops has two forms, one of them was designed for looping through collections and arrays (Correct)

Answer :The "for" loops has two forms, one of them was designed for looping through collections and arrays

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.                            int i = a.length - 1;

5.       

6.                            while(true){

7.                                      while(i>=0){

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

9.                                              i--;

10.                                 }

11.                       }

12.            }

13.    }

 


Options are :

  • 1234
  • 4321
  • 4321 and will create never ending loop (Correct)
  • No output
  • Compilation fails

Answer :4321 and will create never ending loop

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

What will be the output of this program?

1.             public class Whiz {

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

3.                                             int x = 1;

4.                                             int y = 10;

5.                                            

6.                                             if((x*=3) == y) {

7.                                                             System.out.println(y);

8.                                             } else {

9.                                                             System.out.println(x);

10.                                    }

11.                    }

12.    }

 


Options are :

  • 1
  • 3 (Correct)
  • 10
  • Compilation fails due to an error at line 6.
  • Compilation fails due to multiple errors.

Answer :3

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

1.      public class Whiz {

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

3.                                     

4.                         // insert here

5.                         final int y = 2;

6.                     

7.                         switch(x+y) {

8.                                                case x+1    : {System.out.print("A");}

9.                                                case 1        : System.out.print("B");

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

11.                                           case y        : System.out.print("C");

12.                    }

13.           }

14.   }



Options are :

  • final int x = -1; (Correct)
  • int x = -1;
  • int x = 1;
  • final int x = 1;
  • All of the above

Answer :final int x = -1;

What will be the output of this program?

1.      public class Whiz {

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

3.                                     

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

5.                                     

6.                         switch(2) {

7.                                case array[0]      : {System.out.print("A");}

8.                                case array[1]      : System.out.print("B");

9.                                default                : System.out.print("default"); break;

10.                           case array[2]      : System.out.print("C");

11.                    }

12.          }

13.    }


Options are :

  • ABdefault
  • default
  • Bdefault
  • C
  • Compilation fails (Correct)

Answer :Compilation fails

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

What will be the output of this program?

1.      public class Whiz {

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

3.                       String out = "0";

4.                        int i = -1, j = -5;

5.                        if (i < 5)

6.                            if (j > 0)

7.                               if (i > j)

8.                                   out += "1";

9.                               else out += "2";

10.                      else out += "3";

11.                  else out += "4";

12.                 System.out.println(out);

13.         }

14.    }



Options are :

  • 01
  • 02
  • 03 (Correct)
  • 04
  • Compile time error.

Answer :03

What will be the output of this program?

1.      public class Whiz {

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

3.                                     

4.                        int whiz = 0;

5.                                     

6.                        if (whiz > 0) {

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

8.                        }

9.                        System.out.println();

10.                   else

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

12.         }

13. }

 

 


Options are :

  • A
  • B
  • A B
  • No output
  • Compile fails (Correct)

Answer :Compile fails

What will be the output of this program?

1.      public class Whiz{

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

3.                                      int marks = 60;

4.                                     

5.                                      if(marks >= 40) System.out.println("C");

6.                                      else if(marks >= 60) System.out.println("B");

7.                                      else if(marks >= 75) System.out.println("A");

8.                                      else System.out.println("D");  

9.                      }

10.    }

 


Options are :

  • A
  • B
  • C (Correct)
  • D
  • Compilation fails

Answer :C

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

Given the variable "point" is an integer and based on its value you need to print "n" or "p", here "n" for negative number while "p" for positive number, which of the following printing statement you can use for that?

Assume zero is a positive integer.


Options are :

  • System.out.println(point >= 0 ?"p":"n"); (Correct)
  • System.out.println(point >= 0 :"p"?"n");
  • System.out.println(if(point >= 0 ) "p";else "n";);
  • System.out.println(point >= 0 :"p";"n");
  • None of the above.

Answer :System.out.println(point >= 0 ?"p":"n");

Which of the following will produce output 8 when inserted at line 7?

1.      public class Whiz {

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

3.                        int x = 1;

4.                        int y = 2;

5.                        int z = 3;

6.                                     

7.                        // insert here   

8.               }

9.      }

 

 


Options are :

  • System.out.println((z / y) + z*2);
  • System.out.println(z / (y + z)*2);
  • System.out.println((z / y + z)*2); (Correct)
  • System.out.println(z / y + z*2);
  • Compilation fails.

Answer :System.out.println((z / y + z)*2);

What will be the output of this program?

1.      public class Whiz{

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

3.                                      Integer i = 10;

4.                                      Double d = 10.0;

5.                                      int ii = 10;

6.                                      double dd = 10.0;

7.                                     

8.                                      System.out.print(i.equals(d) + " ");

9.                                      System.out.print(ii == dd);

10.                 }

11. }

 

 


Options are :

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

Answer :false true

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

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

Consider this given program:

1.             class Whiz {

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

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

4.                            

5.                                 // insert code here

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

7.                                 }

8.                      }

9.             }

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

 I.      for (int i = 0, j = 0; i < a.length; j++) { i = a[j];

II.      for (int i : a) {

III.     for (a : int i) {


Options are :

  • I only
  • II only
  • III only
  • I and II only (Correct)
  • All the statements will compile successfully.

Answer :I and II only

Which of the following is true regarding this given program code?

 I. String s2 = "Rekha";

II. String s2 = new String("Rekha");

1.             public class Whiz{

2.                

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

4.                             String s1 = "Rekha";

5.                             //insert here

6.                

7.                             System.out.print(s1.equals(s2)+" ");

8.                             System.out.print(s1 == s2);

9.                                            

10.        }

11.    }                                                                                                             

 


Options are :

  • Inserting the code statement II at line 6, will produce output "true true"
  • Inserting the code statement I at line 6, will produce output "true false"
  • Inserting the code statement I at line 6, will produce output "false false"
  • Inserting the code statement II at line 6, will produce output "true false" (Correct)
  • None of the above.

Answer :Inserting the code statement II at line 6, will produce output "true false"

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

Which of the following statement is correct about switch?


Options are :

  • Switch statement is more efficient than a set of nested ifs. (Correct)
  • Two case constants in the same switch might be identical.
  • Switch uses equalsIgnoreCase method when working with strings.
  • It is not possible to create a nested switch statements.
  • None of the above.

Answer :Switch statement is more efficient than a set of nested ifs.

Which of the following will override the method run correctly when inserted at line 8?

1.             class A{

2.                             private void run(){

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

4.                             }

5.             }

6.              

7.             class B extends A{

8.                             //override method() here

9.             }                             

 

 


Options are :

  • private void run(){System.out.print("B");}
  • void run(){System.out.print("B");}
  • public void run(){System.out.print("B");}
  • private void run(String s){System.out.print(s);}
  • We can't override the method run (Correct)

Answer :We can't override the method run

What will be the output of this program?

1.          abstract class Animal{

2.              void run(){

3.                          System.out.print("Animal run");

4.              }

5.          abstract void sound();  

6.          }

7.           

8.          class Dog extends Animal{

9.           

10.         void sound(){

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

12.         }

13.                  

14.          public void run(){

15.                   System.out.print(" Dog runs");

16.          }

17.    }

18.   public class Whiz{

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

20.                                   Animal dog =  new Dog();

21.                                   dog.sound();

22.                                   dog.run();

23.                   }

24.   }

 


Options are :

  • Bark Dog runs (Correct)
  • Bark Animal runs
  • Compilation fails due to an error at line 10
  • Compilation fails due to an error at line 21
  • Compilation fails due to multiple errors

Answer :Bark Dog runs

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

What will be the output of this program?

1.          class Animal {

2.                          public void eat() throws Exception { System.out.print("Animal eats");}

3.          }

4.           

5.          class Dog extends Animal{

6.                          public void eat() { System.out.print("Dog eats");}

7.           

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

9.                                          Animal a = new Dog();

10.                                   Dog d = new Dog();

11.                                   d.eat();

12.                                   a.eat();

13.                   }

14.   }             

 

 


Options are :

  • Animal eats Animal eats
  • Dog eats Animal eats
  • Dog eats Dog eats
  • Compilation fails due to an error at line 6
  • Compilation fails due to an error at line 12 (Correct)

Answer :Compilation fails due to an error at line 12

Choose the option that has correct method signature for overridden version of this method.

abstract Number number();       


Options are :

  • private Number number()
  • public void number()
  • public Integer number()throws NumberFormatException (Correct)
  • public Integer number(Integer i)
  • None of the above

Answer :public Integer number()throws NumberFormatException

Which of the following is an advantage of using inheritance?


Options are :

  • It increases reusability
  • We can access both super class content and sub class content at the same time
  • It increases the extensibility
  • All of the above (Correct)

Answer :All of the above

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

Which of the following can be inserted at line 10 to invoke the read method?

1.             class Person{}

2.              

3.             class Student extends Person{

4.                             public void read(){System.out.println("Reading");}

5.             }

6.                            

7.             public class Whiz{

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

9.                                             Person stu =  new Student();

10.                                    //insert here

11.                     }

12.    }             

 

 


Options are :

  • stu.read();
  • (Student)stu.read();
  • (Student)(stu).read();
  • ((Student)stu).read(); (Correct)
  • None of the above.

Answer :((Student)stu).read();

Which of the following will produce the output "Cat" when inserted at line 5?

1.       class Animal {

2.                 Animal(String s){ super(); }

3.       }

4.       class Cat extends Animal {

5.                 // insert code here

6.       }

7.       

8.       public class Whiz {

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

10.                    Animal ab = new Cat();

11.           }

12.  }

 

 

 


Options are :

  • Cat(String s) { super();System.out.print(s); } Cat(){ this("Cat"); }
  • Cat(String s) { super(null);System.out.print(s); } Cat(){ this("Cat"); } (Correct)
  • Cat(String s) { super(" ");System.out.print(s); } Cat(){ super("Cat"); }
  • Cat(String s) { super(null);System.out.print("Cat"); }
  • None of the above.

Answer :Cat(String s) { super(null);System.out.print(s); } Cat(){ this("Cat"); }

Which of the following statement is true?


Options are :

  • Abstract classes can contain default methods
  • Abstract classes can be final
  • Abstract classes do not have constructors
  • Abstract classes can't be instantiated (Correct)
  • None of the above

Answer :Abstract classes can't be instantiated

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

What will be the output of this program?

1.                interface I{

2.                                default boolean equals(Object O){

3.                                                return true;

4.                                }

5.                }

6.                               

7.                class A implements I{

8.                                public boolean equals(Object O){

9.                                                return false;

10.                       }

11.       }

12.                      

13.       public class Whiz{

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

15.                                       A a =  new A();

16.                                       I ia =  new A();

17.                                       I i =  new I(){};

18.                                      

19.                                       System.out.println(a.equals(ia) + " " + i.equals(ia));

20.                       }

21.       }

 


Options are :

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

Answer :Compilation fails

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions