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

What will be the output of this program code?

1.        class Whizlab {

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

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

4.                                     switch (i) {

5.                                             case 0:

6.                                                         break;

7.                                              case 1:

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

9.                                              case 2:

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

11.                                         case 3:

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

13.                                 }

14.                        }

15.              }

   }                                                                             


Options are :

  • 1 2 3
  • 1 2 3 2 3 (Correct)
  • 1 2 3 1 2 3 1 2 3
  • An Exception is thrown.
  • Compilation fails.

Answer :1 2 3 2 3

Which of the following is TRUE about the switch statement in Java?   


Options are :

  • A default sends execution immediately to the end of the switch statement.
  • A break sends execution immediately to the end of the switch statement. (Correct)
  • A case sends execution immediately to the end of the switch statement.
  • A break sends execution immediately to the end of the next case.
  • The statements in a switch continue to execute as long as the condition at the top of the switch remains true.

Answer :A break sends execution immediately to the end of the switch statement.

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

What will be the output of this program fragment?

Integer x = new Integer(3);

Integer y = new Integer(3);

if ( x == y )

         System.out.print("=");

else

         System.out.print("<>");


Options are :

  • =
  • <> (Correct)
  • =<>
  • An Exception is thrown.
  • The program doesn't compile because == can't be used with references.

Answer :<>

Which of the following java conditional expression can be used for this truth table?

x              y                    x implies y

T              T                              T

T              F                              F

F              T                              T

F              F                              T


Options are :

  • x || y
  • (!x) && (!y)
  • (!x) || y (Correct)
  • x && (!y)
  • (!x) && y

Answer :(!x) || y

What will be the output of this code segment? 

1.         public class Whizlab {

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

3.                           int i = 1;

4.                              

5.                           if ( i++ == 1 )

6.                                 if ( i++ > 2 ) {

7.                                       System.out.print(i);      

8.                                 }

9.                                 else if ( i++ > 3 )

10.                                  System.out.print(i);

11.                       else

12.                            System.out.println(i);

13.             }

14.   }

 

 


Options are :

  • 24
  • 2
  • 3
  • 4 (Correct)
  • No output.
  • Compilation fails.

Answer :4

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

What will be the output of this program code?

1.       public class Whizlab {

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

3.                         float f = 12.0f;

4.                         double d = f++;

5.                         long l = 12;

6.                         d--;

7.                         d -= 0.3;

8.                         System.out.print((f == d) + " ");

9.                         System.out.print(d == l);

10.            }

11.   }

 


Options are :

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

Answer :false false

What will be the output of this program code?

1.       interface Electronic{ }

2.                    

3.       class Device implements Electronic { /* lots of code goes here */ }

4.                    

5.       class MiniDevice extends Device { /* lots of code goes here */ }

6.                    

7.       public class Whizlab {

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

9.                         Device d = new MiniDevice();

10.                    Device d2 = new Device();

11.                    if (d instanceof Electronic) System.out.print("A");

12.                    if (d2 instanceof Object) System.out.print("C");

13.                    if (d2 instanceof MiniDevice) System.out.print("D");

14.           }

15. }

 


Options are :

  • ACD
  • AB
  • AC (Correct)
  • AD
  • Compilation fails.

Answer :AC

Which of the following has higher operator precedence than "/" operator?   


Options are :

  • ! (Correct)
  • *
  • +
  • -
  • None of the above

Answer :!

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

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

1.      interface A{  void print(); }

2.      class B { }

3.      class  Whizlab extends B implements A {

4.                 // insert code here

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

6.                          // some more codes

7.                 }

8.       }

 


Options are :

  • void print(){ }
  • public void print(int i){ }
  • public void print(){ } (Correct)
  • void print(int i){ }
  • public static void print(){ }

Answer :public void print(){ }

Which of the following statement is true?

1.       interface Switchable {

2.              void sw(int i);

3.       }

4.       abstract class Switch {

5.             abstract void sw(int i);

6.       }

7.       class It _ _ _ _ _ _ _ _ _ _ _ _ _ {

8.              

9.              public void sw(int i){ }

10.     

11.         public static void main(String [ ]arg) {

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

13.         }

14. }

 

 


Options are :

  • At line 7, extending the abstract class "Switch" is more appropriate than implementing the interface "Switchable".
  • At line 7, implementing the interface "Switchable" is more appropriate than extending the abstract class "Switch". (Correct)
  • At line 7, implementing the interface or extending the interface won't give us any advantage over another.
  • Since the both the interface and the abstract class are abstract, both of them will provide same flexibility.
  • Implementing or extending will fail this code from compiling.

Answer :At line 7, implementing the interface "Switchable" is more appropriate than extending the abstract class "Switch".

Which of following represents a correct interface?    


Options are :

  • interface I{ protected int x = 10; }
  • interface I{int x = 10; void print(){} }
  • abstract interface I{ int x = 10; void print();} (Correct)
  • interface I{int x = 10; private void print();}
  • None of the above.

Answer :abstract interface I{ int x = 10; void print();}

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

Which of the following is true about this given code?

1.        class Whizlab {

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

3.                             int x = (int)args[0];

4.                             System.out.print(x);

5.                   }

6.        }


Options are :

  • If we use command line invocation, java Whizlab 10, the output will be 10.
  • If we use command line invocation, java Whizlab abc, An ClassCastException will be thrown.
  • If we use command line invocation, java Whizlab, an ArrayIndexOutOfBoundsException will be thrown.
  • Compilation fails due to an error at line 3. (Correct)
  • Compilation fails due to multiple errors.

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

Which of the following statement is true about this class code fragment?

                class Animal{ }

                class Dog extends Animal { }


Options are :

  • Dog is an Animal. (Correct)
  • Animal is a Dog.
  • Dog has an Animal.
  • Animal has a Dog.
  • None of the above.

Answer :Dog is an Animal.

Which of the following can be used to fill in the blank so that the code compiles successfully?

1.        class Animal() {

2.                    protected void makeSound() {

3.                              System.out.println("sound");

4.                    }

5.        }

6.                    

7.        class Dog extends Animal {

8.                     ________ void makeSound() {

9.                               System.out.println("Dog Barking");

10.                }       

11.   }                                                  

 


Options are :

  • private
  • final
  • public (Correct)
  • abtract
  • We can leave it blank

Answer :public

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

You read the following statement in a Java program that compiles and executes.

car.drive(speed);

What observation you can make for sure?


Options are :

  • speed must be a double
  • drive must be a method. (Correct)
  • drive must be the name of an instance field.
  • car must be the name of a class
  • car must be a method.

Answer :drive must be a method.

Suppose you want to generate number between 1 to 6.

Which of the following expression would you select?


Options are :

  • Math.random()*6
  • ((int)Math.random())*6+1
  • (int)(Math.random()*6)+1 (Correct)
  • (int)(Math.random()+6)
  • (int)(Math.random()*6)

Answer :(int)(Math.random()*6)+1

What is garbage collection in the context of Java?   


Options are :

  • The operating system periodically deletes all of the java files available on the system.
  • Any package imported in a program and not used, is automatically deleted.
  • The JVM checks the output of any Java program and deletes anything that doesn't make sense.
  • When there is no any reference to an object, the object is automatically eligible for GC . (Correct)
  • None of the above.

Answer :When there is no any reference to an object, the object is automatically eligible for GC .

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

Which is the best way to represent the 1.2% fee as a constant in the program?   


Options are :

  • public double fee = 0.012;
  • private float fee = 0.012f;
  • public final String FEE = "0.012";
  • public final double FEE = 0.012; (Correct)
  • public final float FEE = 0.012;

Answer :public final double FEE = 0.012;

Given

 

1.  enum Color {

2.   red, blue;

3.   }

4.   class Rectangle {

5.   private Color color;

6.  public Color getColor() {

7.  return color;

8.  }

9.  public void setColor(Color color) {

10. this.color = color;

11. }

12. }



What will be the colors of r1 and r2 (in this order) when this code fragment
 is executed?

Rectangle r1 = new Rectangle();

r1.setColor(Color.blue);

Rectangle r2 = r1;

r2.setColor(Color.red);


Options are :

  • Color.blue and Color.red respectively
  • Both Color.blue
  • Both Color.red (Correct)
  • Color.red and Color.blue respectively
  • This is not enough information to determine.

Answer :Both Color.red

What will be the output of this program code?

1.       class Whizlab {

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

3.                          Integer i1 = 100, i2=128;

4.                          Integer i3 = 128;

5.                          Integer i4 = 100;

6.                            

7.                          if ((i1 == i4) ^ (i2 == i3)) {

8.                                  System.out.print(i1);

9.                          }

10.                     else if ((i1 == i4) | (i2 == i3)) {

11.                             System.out.print(i3);

12.                     }

13.                     else {

14.                              System.out.print(i3+i4);

15.                      }

16.            }

17.   }

 


Options are :

  • 100 (Correct)
  • 128
  • 228
  • An exception is thrown at runtime.
  • Compilation fails.

Answer :100

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

What will be the output of this program code?

1.       

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 :

  • 11
  • 0
  • 1
  • Compilation fails.
  • An exception is thrown at runtime. (Correct)

Answer :An exception is thrown at runtime.

Which of the following statement is true?   


Options are :

  • The non-access modifier final can be used with every class.
  • The access modifier protected can be used with every class.
  • The access modifier private can't be used with top level class. (Correct)
  • The only access modifier can be used with class is public.
  • None of the above.

Answer :The access modifier private can't be used with top level class.

What will be the output of this program?

1.       class Whizlab {

2.                       

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

4.                          System.out.println(Speed.FASTER== Speed.FAST);

5.                 }         

6.       }

7.                       

8.       enum Speed {

9.                 FAST(2), 

10.            FASTER(3),

11.            SLOW (1);  

12.              

13.            private final int speed;

14.              

15.            public Speed(int speedCode) {

16.                     this.speed = speedCode;

17.            }

18.  }

 


Options are :

  • true
  • false
  • An Exception is thrown.
  • Compilation fails. (Correct)

Answer :Compilation fails.

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

What will be the output of this program code while compiling class B?

1.       package first;

2.              

3.       public class A {

4.                protected int j;

5.                // some codes

6.                public void change(){

7.                         j = 12;

8.                }

9.       }

 

1.        package second;

2.       import first.*;

3.         

4.       class B {

5.              

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

7.                        A a = new A();

8.                        a.change();

9.                        System.out.print(a.j);

10.          }

11.  }

Note: two packages defined in two source codes.

 


Options are :

  • 12
  • 0
  • Compilation fails due to an error at line 8.
  • Compilation fails due to an error at line 9 . (Correct)

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

What will be the output of this program code?

1.       class Whizlab {

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

3.                          int i = 8;

4.                          int x = new Whizlab().change(i);

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

6.                 }

7.       

8.                 int change(int i) {

9.                           i = 2;

10.                      return i;

11.            }

12.  }

 


Options are :

  • 4
  • 10 (Correct)
  • 12
  • 16
  • Compilation fails.

Answer :10

What will be the output of this program code?

1.       abstract class A{

2.               A() {  System.out.print("1"); }

3.               abstract void print();

4.       }

5.       

6.       class Whizlab extends A {

7.                 Whizlab() { System.out.print("2");  }

8.       

9.                 void print() {

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

11.             }

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

13.                      new Whizlab().print();

14.             }

15.   }

 


Options are :

  • 123 (Correct)
  • 321
  • 231
  • 12
  • Compilation fails.

Answer :123

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

What will be the output of this program?

1.       public class Whizlab {

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

3.                         Person p = new Student("Livera");

4.                         System.out.println(p.getName());

5.                }

6.       }

7.       

8.       abstract class Person {

9.                protected String name;

10.           Person(String s) {

11.                     name = s;

12.           }

13.           public abstract String getName();

14.                

15.   }

16.  

17.  class Student extends Person { 

18.                

19.           Student(String s) { }

20.                

21.           public String getName() {

22.                    return name;

23.           }             

24.  }

 


Options are :

  • null
  • Livera
  • No output.
  • An Exception will be thrown.
  • Compilation fails. (Correct)

Answer :Compilation fails.

You want to create an instance method isSpeeding to check whether an object of type Car is speeding or not. One of the instance fields of the Car class is a double variable speed equal to the current speed of the Car object.

The class also lists a constant static double field SPEED_LIMIT, equal to the legal speed limit for the driving conditions of the Car object. What method signature would be best for isSpeeding?


Options are :

  • public void isSpeeding(double speed)
  • public boolean isSpeeding() (Correct)
  • public boolean isSpeeding(double speed,double SPEED_LIMIT)
  • public isSpeeding()
  • public boolean isSpeeding(speed,SPEED_LIMIT)

Answer :public boolean isSpeeding()

What is the process by which we can control that part of program which can access the members of a class?   


Options are :

  • Polymorphism
  • Abstraction
  • Encapsulation (Correct)
  • Recursion
  • Cohesion

Answer :Encapsulation

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

What will be the output of this program code?

1.       class Whizlab {

2.       

3.                 public void test(int i) {

4.                          System.out.println(i);

5.                 }

6.       

7.                 public void test(float f) {

8.                          System.out.println(f);

9.                 }

10.  

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

12.                    Whizlab obj = new Whizlab();

13.                    obj.test(1.0);

14.           }

15.  }

  


Options are :

  • 1.0
  • 1
  • No output.
  • An Exception will be thrown.
  • Compilation fails. (Correct)

Answer :Compilation fails.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions