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

Which of the following statement is true?

1.        public class Whizlab {

2.                 static int i = 2;

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

4.                          int array[] = new int[i];

5.                          array[1] = 7;

6.                          array[2] = 8;

7.                          System.out.print(array[1]);

8.                 }

9.         }                                                         

 


Options are :

  • The output will be 7.
  • The output will be 0.
  • The output will be 8.
  • An Exception will be thrown at the runtime. (Correct)
  • Compilation fails as we cannot assign int values to the elements of a char array.

Answer :An Exception will be thrown at the runtime.

Which will be the output of this program?

1.      public class Whiz{

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

3.           

4.                        final int []ints = new int[3];

5.                        int len = ints.length;

6.                        ints[1]++;

7.                        for(int i : ints)

8.                          System.out.print(i);

9.               }

10.  }               

 


Options are :

  • 000
  • 100
  • 010 (Correct)
  • An Exception is thrown.
  • Compilation fails due to an error at line 6.

Answer :010

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

What will be the output of this program?

1.         public class Whizlab {

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

3.                           long [ ][ ] l2d;

4.                           long [ ] l1d = {1,2,3};

5.                           Object o = l1d;

6.                           l2d = new long[3][3];

7.                           l2d[0][0] = (long[])o;

8.                  }

9.          }                                                         

 


Options are :

  • Compilation fails due to an error at line 5.
  • Compilation fails due to an error at line 6.
  • Compilation fails due to an error at line 7. (Correct)
  • Compilation succeeds and the code runs without exception.
  • Compilation succeeds and an exception is thrown at runtime.

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

Which method is used to print the description of the exception?   


Options are :

  • printStackTrace() (Correct)
  • printExceptionMessage()
  • printStackMessage()
  • printExceptionTrace()
  • None of the above

Answer :printStackTrace()

Which of the following exception is thrown by the JVM when code uses a negative size while initializing an array?   


Options are :

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

Answer :NegativeArraySizeException

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

What will be the output of this program?

1.       import java.io.IOException;

2.       class Whizlab {

3.                            

4.                 static Integer i;

5.                            

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

7.                          print();

8.                  }

9.                           

10.             public static void print() {

11.                       try {

12.                                 System.out.println(i++);

13.                             }catch(IOException ex) {

14.                                  System.out.println("error");

15.                             }

16.                       }

17.              }

 


Options are :

  • error
  • 0
  • 1
  • Null
  • Compilation fails (Correct)

Answer :Compilation fails

Which of the following statement is an incorrect statement?          

I.   java.lang.Exception class has only 4 constructors.

II.  One constructor of java.lang.Exception takes a String as the parameter.

III. If we invoke the "Exception()" constructor of the java.lang.Exception class, we can Construct a new exception with the specified detail message.

IV. If we invoke the "Exception(String message)" constructor of the java.lang.Exception class, we can Construct a new exception with the specified detail message.


Options are :

  • Only I & II
  • Only II and IV
  • Only II and III
  • Only III
  • Only I and III. (Correct)
  • All the statements are incorrect.

Answer :Only I and III.

What will be the output of this program?

1.      class Whizlab {

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

3.                         new Whizlab().go();

4.                }

5.                void go() {

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

7.                          try {

8.                                  run(0);

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

10.                      }catch (Exception e) {

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

12.                      }finally {

13.                              System.out.print("D ");

14.                      }

15.            }      

16.            void run (int i) {

17.                      try {

18.                              System.out.print("E ");

19.                               int x = 5/i;

20.                               System.out.print("F ");

21.                      }catch(NumberFormatException e2) {

22.                                   System.out.print("G ");

23.                      }finally {

24.                                   System.out.print("H ");

25.                      }

26.            }

27.   }

 

 


Options are :

  • A E G H C D
  • A E H B C D
  • A E H C D (Correct)
  • A E H C D followed by an uncaught exception.
  • Compilation fails.

Answer :A E H C D

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

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

1.        public class Whizlab { 

2.                  // insert code here

3.                              new Whizlab().doIt();  

4.                              new Whizlab().didIt();

5.                  }

6.                              

7.                  static void doIt()throws java.io.IOException {

8.                              throw new java.io.IOException();

9.                   }

10.                                     

11.              static void didIt()throws ClassNotFoundException{

12.                         throw new SecurityException();

13.              }

14.   }                                         

 

 


Options are :

  • public static void main(String[] args)throws java.io.IOException, ClassNotFoundException { (Correct)
  • public static void main(String[] args)throws java.io.IOException, SecurityException {
  • public static void main(String[] args)throws java.io.IOException|SecurityException {
  • public static void main(String[] args)throws ClassNotFoundException {
  • None of the above.

Answer :public static void main(String[] args)throws java.io.IOException, ClassNotFoundException {

What will be the output of this program code?

public class Whiz {


    public static void main(String[] args) {


        Integer a = new Integer(127);


        Integer b = new Integer(127);


        Integer c = 127;


        Integer d = 127;


        Integer e = new Integer(200);


        Integer f = new Integer(200);


        Integer g = 200;


        Integer h = 200;


        System.out.println((a == b) + " " + (c == d) + " " + (e==f)+ " "+ (g == h));



    }


}


Options are :

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

Answer :false true false false

What will be the output of this program code?

1.        class Whizlab {

2.                                     

3.                  int x = 5;

4.                  int y = 5;

5.                                     

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

7.                           new Whizlab().print(2);

8.                  }

9.                                     

10.             public void print(int x) {

11.                      System.out.println(x+y);

12.                      int y = 4;

13.             }

14.  }

 


Options are :

  • 7 (Correct)
  • 9
  • 10
  • 6
  • Compilation fails.

Answer :7

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

 Which of the following compiles successfully when inserted at line 1? 

1.      // insert code here

2.               class Whizlab{

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

4.                                  System.out.println(MIN_VALUE);

5.                          }

6.               }

 


Options are :

  • import static java.lang.Integer.*; (Correct)
  • import java.lang.*;
  • import static java.lang.Integer;
  • import static java.lang.Integer.*_VALUE;
  • import java.lang.Integer.MIN_VALUE;

Answer :import static java.lang.Integer.*;

In a Java program, you read the following statement that compiles and executes.

Person p = new Student("Malshi");

What can you conclude?


Options are :

  • Student is the superclass of Person.
  • Reference type of the p is Student.
  • Person class has the default constructor.
  • Student class doesn't have default constructor. (Correct)
  • None of the above.

Answer :Student class doesn't have default constructor.

There can be more than one ____________ per source code.

  I.   Public classes (top level).

 II.   Package statements.

III.   Import statements.


Options are :

  • Only I.
  • Only III. (Correct)
  • Only I and II.
  • Only I and III.
  • None of the above.

Answer :Only III.

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

Consider the following content of a source code file :

import myLibrary.*;

public class Whiz{

          // code for the class...

}

 

class Lab{

           // code for the class...

}

               

What could be the name of the java file containing this source?


Options are :

  • Lab.java
  • Whiz.java (Correct)
  • myLibrary.java
  • This can't be included in one source.
  • This can be included in any file name with having java file extension.

Answer :Whiz.java

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

1.      The scope of a local variable and block variable is same  when the block is outside of method.

2.      The member variables are only visible to the methods in which they are declared.

3.       If the scopes are different we can have variables of the same name.


Options are :

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

Answer :Only I and III.

What is the extension of the file generated by the java compiler ?   


Options are :

  • java
  • jar
  • bin
  • class (Correct)
  • javac

Answer :class

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

Given :

1.        public class Whizlab {

2.              

3.                 private int value;

4.                 public void setValue(int value) {

5.                           /* code */

6.                  }

7.        }

The method setValue assigns the value of the argument to the instance field value.

What could you write for the implementation of setValue?


Options are :

  • value = value;
  • value = this.value;
  • this.value = value; (Correct)
  • this.value = this.value
  • Any of the above.

Answer :this.value = value;

Consider the following statement.

   "Java application or algorithms  will run identically on Windows and Linux."

Which of the following feature is related to above statement?


Options are :

  • Coupling
  • Object orientation
  • Platform independence (Correct)
  • Encapsulation
  • Polymorphism

Answer :Platform independence

What will be the output of this program code?

1.       class Whizlab {

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

3.                           int y = 10;

4.                           try {

5.                                   int x = 0;                              

6.                                   int z = y/x;

7.                                   System.out.println(z);

8.                            }catch(ArithmeticException ex) {

9.                                   System.out.println("Arithmetic : divided by " + x);

10.                       }catch(NumberFormatException nex) {

11.                              System.out.println("Number Format");

12.                       }

13.             }

14.     }

 

 


Options are :

  • 0
  • Arithmetic : divided by 0 Exception
  • Number Format Exception
  • An Uncaught exception is thrown.
  • Compilation fails. (Correct)

Answer :Compilation fails.

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

What will be the output for this statement?   


Options are :

  • Hello, \nworld
  • Hello, world
  • Hello, world (Correct)
  • "Hello, \nworld"
  • Given statement is illegal.

Answer :Hello, world

What will be the output of this program?

1.             package whizlabs;

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

3.             import static pack.SpeedMeter.*;

4.              

5.             class Whizlab {

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

7.                                             System.out.println(MAX_VALUE);

8.                             }

9.             }

10.    package pack;

11.    public class SpeedMeter{

12.                   public final static int MAX_VALUE = 180;

13.    }

 

Note that above two codes are in two different source files.

 


Options are :

  • 0
  • 180
  • 2147483647
  • An Exception will be thrown.
  • Compilation fails. (Correct)

Answer :Compilation fails.

How many import statement(s) is/are unnecessary 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
  • No output.
  • Compilation fails.

Answer :2

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

What will be the output of this code segment when executed as a program? 

1.       import java.util.*;

2.       public class Whizlab {

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

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

5.                         int y =  a.length;

6.                         while(y >= 0) {

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

8.                         }             

9.                 }

10.  }         


Options are :

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

Answer :0123 followed by an exception.

What will be the output of this program code?

1.        public class Whizlab {

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

3.                          int y = 0;

4.                          while (y-- < 10) { continue;}

5.                          String message = y >10 ? "Greater than" : "Less than";

6.                          System.out.println(message+" 10");

7.                  }

8.        }

 


Options are :

  • Greater than 10 (Correct)
  • Less than 10
  • Infinite loop
  • Compilation fails due to an error at line 4.
  • Compilation fails due to an error at line 5.

Answer :Greater than 10

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")) ;

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

What will be the output of this program code?

1.       class Whizlab {

2.                    

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

4.                           int a = 1;

5.                           while (a <= 3) {

6.                                                     if (a++  == 2)

7.                                                         continue; 

8.                                                         System.out.print(a + " ");

9.                           }

10.             }

11.  }                                                                                                                                         


Options are :

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

Answer :2 4

What will be the output of this program code?

1.        public class Whizlab {

2.                    

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

4.                                              

5.                          String[ ] sts = {"A","B","C"};

6.                    

7.                          for (String i : sts) {

8.                                    continue;

9.                                    System.out.print(i);

10.                     }

11.            }

12.  }                                                                                                                                         


Options are :

  • 1
  • 1234
  • No output.
  • An Exception is thrown.
  • Compilation fails. (Correct)

Answer :Compilation fails.

What will be the output of this program code?

1.      class Whizlab {

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

3.                         int x = 1;

4.                         int y = 2;

5.                                     

6.                         int i = (++x * y--)*--y;

7.                                     

8.                         System.out.println(i);

9.                }

10.  }

 


Options are :

  • 10
  • 5
  • 2
  • 0 (Correct)
  • Compilation fails.

Answer :0

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

Which of the following ternary statement can be used to replace if statement in this program code?

int x = 10;

double d = 0;

if (x > 100) {

    d = 2.0;

} else if (x > 50) {

                d = 1.0;

} else {

                d = 0.0;

}


Options are :

  • d = x > 100 ? 2.0 : x > 50 ? 1.0 : 0.0 ; (Correct)
  • d = x > 100 : 2.0 : x > 50 : 1.0 : 0.0 ;
  • d = x > 100 ? 2.0 ? x > 50 ? 1.0 : 0.0 ;
  • d = x > 100 ? 2.0 ; x > 50 ? 1.0 ; 0.0 ;
  • None of the above.

Answer :d = x > 100 ? 2.0 : x > 50 ? 1.0 : 0.0 ;

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions