Responsive Ads Here

Friday, February 21, 2020

ocjp dumps | ocjp 8 dumps | ocjp latest dumps | ocjp certification dumps

I am starting to write a 20 sets of ocjp latest dumps questions, each set contains 10 questions. 

This blog will be the set-1 of  ocjp latest dumps, You must practice these questions carefully and understand. If you are planing to clear Oracle Certification stay tuned with these series.

I suggest you to first solve all the given questions then look at the solutions. If you have any doubt in any solution please comment. You can also share your score in comment box below.  





SET-1

1.  Which digits, and in what order, will be printed when the following program is run?

public class MyClass {
 public static void main(String[] args) {
  int k=0; 
  try {
   int i = 5/k;
  } catch (ArithmeticException e) {
   System.out.println("1");
  } catch (RuntimeException e) {
   System.out.println("2");
   return;
  } catch (Exception e) {
   System.out.println("3");
  } finally {
   System.out.println("4"); 
  }
  System.out.println("5"); 
 }
}

Select the one correct answer.
(a) The program will only print 5.
(b) The program will only print 1 and 4, in that order.
(c) The program will only print 1, 2, and 4, in that order.
(d) The program will only print 1, 4, and 5, in that order.
(e) The program will only print 1, 2, 4, and 5, in that order.
(f) The program will only print 3 and 5, in that order.



2. Given the following program, which statements are true?
 public class Exceptions 
{
 public static void main(String[] args) {
  try {
   if (args.length == 0) 
    return; 
   System.out.println(args[0]);
  } finally { 
   System.out.println("The end");
   } 
 }
}
Select the two correct answers.
(a) If run with no arguments, the program will produce no output.
(b) If run with no arguments, the program will print "The end".
(c) The program will throw an ArrayIndexOutOfBoundsException.
(d) If run with one argument, the program will simply print the given argument.
(e) If run with one argument, the program will print the given argument followed by "The end".


3. What will be the result of attempting to compile and run the following program?
public class MyClass {
 public static void main(String[] args) 
 {
  RuntimeException re = null;
  throw re; 
 }
}
Select the one correct answer.
(a) The code will fail to compile because the main() method does not declare that it throws RuntimeException in its declaration.
(b) The program will fail to compile because it cannot throw re.
(c) The program will compile without error and will throw java.lang.Runtime-Exception when run.
(d) The program will compile without error and will throw java.lang.Null-PointerException when run.
(e) The program will compile without error and will run and terminate without any output.



4. Which digits, and in what order, will be printed when the following program is run?
public class MyClass {
 public static void main(String[] args) throws InterruptedException 
 {
  try { 
   f();
   System.out.println("1"); 
  } finally {
   System.out.println("2"); 
  }
  System.out.println("3");
 }

  // InterruptedException is a direct subclass of Exception. 
  static void f() throws InterruptedException {
 throw new InterruptedException("Time to go home."); 
   }
}
Select the one correct answer.
(a) The program will print 2 and throw InterruptedException.
(b) The program will print 1 and 2, in that order.
(c) The program will print 1, 2, and 3, in that order.
(d) The program will print 2 and 3, in that order.
(e) The program will print 3 and 2, in that order.
(f) The program will print 1 and 3, in that order.


5. What, if anything, would cause the following code not to compile?
class A {
 void f() throws ArithmeticException {
  //... 
 }
}
public class MyClass extends A {
 public static void main(String[] args) {
  A obj = new MyClass();
  try { 
   obj.f();
  } catch (ArithmeticException e) { 
   return;
  } catch (Exception e) {
   System.out.println(e);
   throw new RuntimeException("Something wrong here");
  } 
 }
 // InterruptedException is a direct subclass of Exception. 
 void f() throws InterruptedException {
  //... 
 }
}
Select the one correct answer.
(a) The main() method must declare that it throws RuntimeException.
(b) The overriding f() method in MyClass must declare that it throws Arithmetic- Exception, since the f() method in class A declares that it does.
(c) The overriding f() method in MyClass is not allowed to throw Interrupted- Exception, since the f() method in class A does not throw this exception.
(d) The compiler will complain that the catch(ArithmeticException) block shadows the catch(Exception) block.
(e) You cannot throw exceptions from a catch block.
(f) Nothing is wrong with the code, it will compile without errors.


6. What will be the result of compiling and running the following code?
import java.util.Arrays;
public enum Priority {
 ONE(1) { public String toString() { return "LOW"; } }, // (1)
 TWO(2),
 THREE(3) { public String toString() { return "NORMAL"; } },//(2) 
 FOUR(4),
   FIVE(5) { public String toString() { return "HIGH"; } }; //(3)

   private int pValue;
 Priority(int pValue) { 
  this.pValue = pValue;
 }
 public static void main(String[] args) { 
  System.out.println(Arrays.toString(Priority.values()));
 } 
}
Select the one correct answer.
(a) The code compiles, but reports a ClassNotFoundException when run, since an enum type cannot be run as a standalone application.
(b) The compiler reports syntax errors in(1),(2),and(3).
(c) The code compiles and prints: [LOW, TWO, NORMAL, FOUR, HIGH]
(d) The code compiles and prints: [ONE, TWO, THREE, FOUR, HIGH]
(e) None of the above.


7. Which statement about the following program is true?
public enum Scale {
 GOOD('C'), BETTER('B'), BEST('A');
 private char grade;
 
 Scale(char grade) { 
  this.grade = grade;
 }
 abstract public char getGrade();

 public static void main (String[] args) { 
  System.out.println (GOOD.getGrade()); // (1)
 } 
}

Select the one correct answer.
(a) Since the enum type declares an abstract method, the enum type must be declared as abstract.
(b) The method call GOOD.getGrade() in (1) can be written without the enum type name.
(c) An enum type cannot declare an abstract method.
(d) An enum type can declare an abstract method, but each enum constant must provide an implementation.


8. What will be the result of compiling and running the following code?
public enum TrafficLight {
 RED("Stop"), YELLOW("Caution"), GREEN("Go");
 private String action;

 TrafficLight(String action) { 
  this.action = action;
 }
 public static void main(String[] args) { 
  TrafficLight green = new TrafficLight("Go"); 
  System.out.println(GREEN.equals(green));
 } 
}
Select the one correct answer.
(a) The code will compile and print: true.
(b) The code will compile and print: false.
(c) The code will not compile, as an enum type cannot be instantiated.
(d) An enum type does not have the equals() method.


9. Given the following program:
public enum Scale2 {
 GOOD('C') {  public char getGrade() { return 'C'; } }, 
 BETTER('B') { public char getGrade() { return 'B'; } }, 
 BEST('A') { public char getGrade() { return 'A'; } };
 private char grade;
 
 Scale2(char grade) { 
  this.grade = grade;
 }

 // (1) INSERT CODE HERE
 public static void main (String[] args) { 
  System.out.println(GOOD.getGrade());
 } 
}
Which code, when inserted at (1), will make the program print C ? 
Select the two correct answers.
(a) public char getGrade() { return grade; }
(b) public int getGrade() { return grade; }
(c) abstract public int getGrade();
(d) abstract public char getGrade();


10. What will be the result of compiling and running the following code?
public enum Scale5 {
 GOOD, BETTER, BEST;
 public char getGrade() { 
  char grade = '\u0000'; 
 switch(this){
  case GOOD: grade = 'C'; break;
  case BETTER: grade = 'B'; break;
  case BEST: grade = 'A'; break;
  return grade; 
  }
 }
 public static void main (String[] args) { 
  System.out.println(GOOD.getGrade());
 }
}
Select the one correct answer.
(a) The program will not compile, as the switch expression is not compatible with the case labels.
(b) The program will not compile, as enum constants cannot be used as case labels.
(c) The case labels must be qualified with the enum type name.
(d) The program compiles, and when run, prints: C
(e) The program compiles, and when run, prints: GOOD
(f) None of the above.









*************** Solutions  ****************

1.   (d)
Output : 
  1
  4
  5
Explanation : When we run the program, ArithmeticException will be thrown, that will be handled by corresponding catch block, that will print 1 after that finally block will be executed and print 4, after that the last statement will be executed that will print 5. hence option (d) will be correct.


2.  (b), (e) both are correct.

Explanation : (b) : If program run with no argument (args.length == 0) will be true hence further no statement from try block will be executed because of return keyword, and only finally block will be executed and print The end.
(e) : if program will run with one argument then (args.length == 0)  will be false and return statement will not be executed so args[0] will be executed and printed, after that finally block will be executed and print The end.


3.  (d).

Explanation : When program will run it will throw java.lang.NullPointerException because throw keyword will be executed with null reference. Any operation with null reference will cause NullPointerException.

4.  (a). 

Explanation : When program will be run it will  call f() method that throw InterruptedException hence further statement in try block will not be executed then finally block will be executed and print 2 then InterruptedException stack trace will be printed.

5. (c).

Explanation : The program will not compile because overriding f() method in MyClass is not allow to throw InterruptedException since f() method in A class does not throw this Exception.
In case of checked exception overriding method can only throw the same exception or child exception of parent class exception.

If parent method is throwing unchecked exception, child class method can throw any unchecked exception but can not throw checked exceptions.


6. (c).


[LOW, TWO, NORMAL, FOUR, HIGH]

Explanation : The program will print [LOW, TWO, NORMAL, FOUR, HIGH]  because the enum which has toString() method implementation will print return value from this method other print values provided to their constructors.


7. (d)

Explanation : Enum type can declared abstract method but each enum constant must provide a implementation.

example :

public enum Scale {
 GOOD('C') {
  public char getGrade() {
   return 'C';
  }
 },
 BETTER('B') {
  public char getGrade() {
   return 'B';
  }
 },
 BEST('A') {
  public char getGrade() {
   return 'A';
  }
 };

 private char grade;

 Scale(char grade) {
  this.grade = grade;
 }

 abstract public char getGrade();

 public static void main(String[] args) {
  System.out.println(GOOD.getGrade()); // (1)
 }
}



8. (c) 
Explanation : Enum type can not be instantiated


9. (a), (d)
Explanation : Only (a) and (d) will be suitable for the same.

10.  (d)




Click here for Set-2 for SCJP , OCJP, OCA, OCP latest dump



No comments:

Post a Comment