Java OCA OCP Practice Question 1771

Question

Assuming the following application is executed with assertions enabled, what is the result?

package mypkg;/*from   www  . j a v a2s.com*/
public class Main {
   private int score;
   public Main() {
      super();
      Main.this.score = 5;
   }
   public static void main(String[] books) {
      final Main m = new Main();
      assert(m.score>2) : m.score++;
      assert m.score>=5 : System.out.print("No mypkg");
      System.out.print("Made it!");
   }
}
  • A. An AssertionError is thrown with a message of 5.
  • B. An AssertionError is thrown with a message of No mypkg.
  • C. Made it! is printed.
  • D. The code does not compile.


D.

Note

The optional second parameter of an assert statement, when used, must return a value.

The second assert statement uses System.out.print() as its second parameter, which has a return type of void.

For this reason, the code does not compile, making Option D the correct answer.

Other than this one line, the rest of the class compiles without issue.




PreviousNext

Related