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

Which of the following will compile successfully and produce output A when  inserted independently at line 5?(Choose 3 options)

1.      class Whizlab{

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

3.                          int x = -10;

4.                          int y =  10;

5.                          //insert here

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

7.                }

8.      }

 

 


Options are :

  • if(y++>10 | x%(-3)==1)
  • if(y>=10 & x%(-3)==-1) (Correct)
  • if(y>10 | x%(3)==-1) (Correct)
  • if(++y>10 & x%(-3)==1)
  • if( ++y>10 | x%(-3)==1) (Correct)
  • if(y++>=10 ^ x%(-3)==-1)

Answer :if(y>=10 & x%(-3)==-1) if(y>10 | x%(3)==-1) if( ++y>10 | x%(-3)==1)

What will be the output of this program?

1.              public class Whizlab{

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

3.                           int y = 10;

4.                         

5.                           if(y++ == 10)

6.                                  if(y-- == 10)

7.                                           if(y == 10);

8.                                           else y *=3;

9.                                  else y *=2;

10.                                  

11.                    System.out.println(y);

12.                 }

13.       }

 

 


Options are :

  • 10
  • 20 (Correct)
  • 18
  • 30
  • Compilation fails.

Answer :20

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

What will be the output of this program?

1.         class Whizlab {

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

3.                            double in = (double)(Math.random() * 4);

4.                            switch(in){

5.                                     case 1: {System.out.print("A");} break;

6.                                     case 2: System.out.print("B");

7.                                     case 3: System.out.print("C");

8.                             }

9.                   }

10.     }

      

Note: Math.random() * 4 will assign value between  0 3 to in.


Options are :

  • The output could be ABC
  • The output could be BC
  • There could be no output
  • An exception is thrown
  • Compilation fails (Correct)

Answer :Compilation fails

Choose the correct statement.   


Options are :

  • The default keyword is a must for switch statement.
  • The only legal expression in a switch expression is an integer type expression.
  • Switch statement can evaluate double data type.
  • Using switch is slower than if/else if.
  • None of the above. (Correct)

Answer :None of the above.

Given

1.             class Whizlab{

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

3.                             int y = -10;          

4.                             System.out.println(y); 

5.             }

6.             }

In above code "-"operator has used as?


Options are :

  • Assignment operator.
  • Relational operator.
  • Unary operator. (Correct)
  • Arithmetic operator.

Answer :Unary operator.

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

Given

1.       

1.             class Whizlab{

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

3.                            int y = -10;          

4.                             int x = -3;

5.                             System.out.println(y%x);

6.                 }

7.             }

What is the output?


Options are :

  • -1 (Correct)
  • 1
  • 3
  • -3
  • Compilation fails

Answer :-1

Which of the following method can be inserted to an interface? 

Select 2 options.


Options are :

  • default static void method() { }
  • static int method() {return 1; } (Correct)
  • abstract int method() { };
  • public void method(); (Correct)

Answer :static int method() {return 1; } public void method();

Which of the following will override the go method of Parent class when inserted independently at line 8?

1.      class Parent {

2.                protected void go() throws java.io.FileNotFoundException {

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

4.                 }

5.      }

6.       

7.      class Child extends Parent{

8.                 // override go method here

9.      }

 

 


Options are :

  • private void go() throws IndexOutOfBoundsException{ System.out.print("Child"); }
  • void go() throws IOException{ System.out.print(""); }
  • public void go (String s)throws IndexOutOfBoundsException{ System.out.print(s); }
  • protected void go() throws RuntimeException{ System.out.print("Child"); } (Correct)
  • None of above.

Answer :protected void go() throws RuntimeException{ System.out.print("Child"); }

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

In which of the following situations, you will choose interface inheritance over class inheritance?

1.                 You are asked to create two classes(A,B) which should have some behaviors in common and class B is already a subclass of class C

2.                  You are asked to create two classes (A,B) which should have some behaviors in common and class A is already implements another interface C .

3.                  You are asked to create two classes (A,B) which should have some behaviors in common and both classes (A,B) are already sub classes of other classes (C,D) .

 


Options are :

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

Answer :Only I and III

Which of the following is a valid identifier?   


Options are :

  • _ (Correct)
  • 2a
  • first name
  • new
  • default

Answer :_

Which of the following can be used to invoke randomize() method?

1.      interface Run{

2.                      static int range = 12;

3.                      public static double randomize(){

4.                                      return Math.random()*range;

5.                      }

6.      }

 

 


Options are :

  • new Run().randomize();
  • Run.randomize(); (Correct)
  • It is impossible to access randomize() method without implementing the interface.
  • ArrayIndexOutOfBoundsException is thrown.
  • Given interface is invalid.

Answer :Run.randomize();

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

Which of the following is a valid identifier?   


Options are :

  • whiz labs
  • 2way
  • $_ (Correct)
  • new
  • default

Answer :$_

What will be the output of this program?

1.        class Whizlab {

2.                   int i;

3.                            

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

5.                            Float F = 1.03f;

6.                            new Whizlab().devide(F);

7.                   }

8.              

9.                   void devide(Double D) {

10.                       System.out.print(D/i);

11.               }

12.                   

13.     }

 

 


Options are :

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

Answer :Compilation fails.

What will be the output of this program?

1.             public class Whizlab{

2.                             static int x = 1;

3.                            

4.                             Whizlab( ){x++;}

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

6.                                             System.out.print(x + check(2));

7.                            }             

8.                                                                            

9.                             static int check(int i){

10.                                    return new Whizlab().x*i;

11.                    }

12.    }                             

 

 


Options are :

  • 1
  • 2
  • 4
  • 5 (Correct)
  • Compilation fails.

Answer :5

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

Which of the following will be the correct constructor for Whizlabs class? 

  class Exam {

            Exam(String s){System.out.println(s);}

   }

   class Whizlabs extends Exam {

            // What would be correct constructor for this class

   }         

 


Options are :

  • private Whizlabs(String x){ }
  • Whizlabs(){ }
  • Whizlabs(String s){ super(s);} (Correct)
  • Whizlabs(){ this("OCAJP"); }
  • None of the above.

Answer :Whizlabs(String s){ super(s);}

What will be the output of this program?

1.             public class Whizlab{

2.                     int x = 10;

3.                     static int y = x;

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

5.                                    int x = 2;

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

7.                     }

8.             }

 

 


Options are :

  • 4
  • 12
  • 20
  • Compilation fails due to an error at line 3. (Correct)
  • Compilation fails due to an error at line 6.
  • Compilation fails due to multiple errors.

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

We use the following method in the class "Whiz"

public static int test(int i)

And we know that the following line has been executed

Whiz t = new Whiz();

Which of the following code is accepted?


Options are :

  • System.out.println(Whiz.test(0) + t.test(1999)); (Correct)
  • short s = t.test(1) ;
  • System.out.println(t.test(9.0));
  • System.out.println(Whiz.test(0) + t.test(1999*1.0));
  • None of the above.

Answer :System.out.println(Whiz.test(0) + t.test(1999));

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

Which of the following can be considered a valid statement?

1.         public class Rectangle {

2.                  public int width;

3.                  public int height;

4.                  public int area;

5.                

6.                  public Rectangle() { }

7.                

8.                  protected Rectangle(int width, int height) {

9.                          this.width  = width;

10.                     this.height = height;

11.             }

12.       

13.             private Rectangle(int area){ this.area = area; }

14.    }

15.     

16.    class Square extends Rectangle { }

 

 


Options are :

  • Square s = new Square(); (Correct)
  • Square s = new Square(0, 0);
  • Square s = new Square(0);
  • Square s = new Rectangle(0,0);
  • Square s = (Square) (new Rectangle(0,0));

Answer :Square s = new Square();

What will be the output of this program?

1.             public class WhizLab{

2.             int  _ = $;

3.             static int $ = 2;

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

5.                                     System.out.print($ +new WhizLab()._);

6.                     }

7.             }

 

 


Options are :

  • 2
  • 4 (Correct)
  • Compilation fails due to an error at line 2.
  • Compilation fails due to an error at line 5.
  • Compilation fails due to multiple errors.

Answer :4

Which of the following is a valid statement about this interface?

1.      interface Movable{

2.                      static int speed = 12;

3.                      String s = "speed: ";

4.      }

 

 


Options are :

  • new Movable().s;
  • Movable.speed = 10;
  • System.out.println(Movable.s); (Correct)
  • Given interface is invalid.
  • None of the above.

Answer :System.out.println(Movable.s);

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

What will be the output of this program?

1.          public class Whizlab {

2.                

3.                    static boolean b = false;

4.                          

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

6.                                      String str;

7.                                                                                          

8.                                       if (b = true) { str = "true"; }

9.                                                          

10.                                  str = b?str:"false";

11.                                                  

12.                                  System.out.println(str);

13.                       }                                             

14.       }

 

 


Options are :

  • null
  • false
  • true
  • Compilation fails due to an error at line 10. (Correct)
  • Compilation fails due to multiple errors.

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

What will be the output of this program?

1.              public class Whizlab{

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

3.                                                          Integer x = 8;                                    

4.                                                          System.out.println(x.SIZE +x.BYTES);

5.                                          }                                             

6.              }

 

 

 


Options are :

  • 3
  • 11
  • 35
  • 36 (Correct)
  • None of the above

Answer :36

Which of the following will create a valid Boolean object without auto-boxing ?   


Options are :

  • Boolean b = Boolean.valueOf(true); (Correct)
  • Boolean b = Boolean.parseBoolean("yes");
  • boolean b = Boolean.getBoolean("FALSE");
  • All of the above.
  • None of the above.

Answer :Boolean b = Boolean.valueOf(true);

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

Given

1.       import java.time.LocalDate;

2.       import java.time.LocalDateTime;

3.       import java.time.LocalTime;

4.        

5.       public class Whiz{

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

7.                                       LocalTime lt = LocalTime.of(12, 30);

8.                                       LocalDate ld = LocalDate.of(2015, 2, 21);

9.                                       LocalDateTime ldt = lt.atDate(ld);

10.                                   ldt.minusYears(2);

11.                                   ldt.minusDays(2);

12.                                   System.out.println(ldt.getYear() + "/"+ ldt.getMonthValue()+"/" + ldt.getDayOfWeek());

13.                   }

14.   } 


Options are :

  • 2013/2/SATURDAY
  • 2015/2/SATURDAY (Correct)
  • 2015/2/21
  • An exception is thrown.
  • Compilation fails

Answer :2015/2/SATURDAY

What will be the output of this program?

1.             public class Whizlab{

2.             int _ = $;

3.             static int $ = 5;

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

5.                                     System.out.print($ +_);

6.                     }

7.             }

 

 

 


Options are :

  • 5
  • 10
  • Compilation fails due to an error at line 2.
  • Compilation fails due to an error at line 5. (Correct)
  • Compilation fails due to multiple errors.

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

What will be the output of this program?

1.      public class Whizlab {

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

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

4.                                     

5.                                      list.add("A");

6.                                      list.add("C");

7.                                      list.add("E");

8.                                      list.add("D");

9.                                     

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

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

12.  

13.                                 System.out.println(list);

14.          }

15.    }   

 

 


Options are :

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

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

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

What will be the output of this program?

import java.time.Period;

 public class Whizlab{

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

2.                 Period p1 = Period.ofYears(1);

3.                 Period p2 = Period.of(0, 1, 0);

4.                 Period p3 = p1.plus(p2);

5.                 System.out.println(p3.getDays());

6.         }     

7.     

 

 


Options are :

  • 397
  • 396
  • 0 (Correct)
  • An Exception will be thrown.
  • Compilation fails.

Answer :0

Given scenario

                Value of the integer variable x is equal to 0 then "0" should be printed.

                Value of the integer variable x is greater than 0 then ">" should be printed.

                Value of the integer variable x is less than 0 then "<" should be printed.


Options are :

  • System.out.println(x==0?"0":x>0?">":"<"); (Correct)
  • System.out.println(x==0?"0",x>0?">":"<");
  • System.out.println(x==0?"0",x>0?">","<");
  • System.out.println(x==0:"0"?x>0:">"?"<");
  • None of the above.

Answer :System.out.println(x==0?"0":x>0?">":"<");

What will be the output of this program?

1    import java.util.function.*;
2    public class Employee {
3             int id;
4             public static void main(String[] args) {
5                      Employee e = new Employee();
6                      e.id = 3;
7                      check(e, p -> p.id < 10);    
8             }
9             private static void check(Employee e, Predicate<Employee> pr) {
10                    String result = pr.test(e) ? "match" : "not match";
11                    System.out.print(result);
12           }
13   }

 


Options are :

  • match (Correct)
  • not match
  • Compiler error at line 7.
  • Compiler error at line 10.
  • None of the above.

Answer :match

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

Which of the following are the correct syntax to overload the method on line 4?


(Choose three options.)




4. public void greet(String greeting) {


5.  /* more codes */


6. }


   


Options are :

  • public String greet() {//more codes} (Correct)
  • public void greet() {String greeting,name;} (Correct)
  • public void greet(Object greeting){} (Correct)
  • private void greet(String greeting) {}
  • private String greet(String greet) {}

Answer :public String greet() {//more codes} public void greet() {String greeting,name;} public void greet(Object greeting){}

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions