FAQ
  Java Tutorial
  Questions by Topic
  Sample test
  Other Certification sites
  Certification Tips
  Exam Objectives
  Java jobs
  Java News
  About Java Prepare
   Books
  Certification Books
  SCEA Books
  Online Books
   Tutorial Topics
  Language Fundamentals
  Operator and Assignments
  Declaration and Access Control
  Classes in Java
  AWT
  Event classes
  Threads
  Layout Managers
  Collections
  Files
   Advertisements
 
javaprepare.com
your tool for Java Certification
home | tutorial | questions | test 1

Questions on Assertions

This topic is part of SCJP 1.4 exam but not SCJP 1.2 exam.

  1. What happens when the following code is compiled and run. Select the one correct answer.

  2. for(int i = 1; i < 3; i++)
      for(int j = 3; j > i; j--)
         assert i!=j {System.out.println(i); }

    1. The class compiles and runs, but does not print anything.
    2. The number 1 gets printed with AssertionError
    3. The number 2 gets printed with AssertionError
    4. The number 3 gets printed with AssertionError
    5. The program generates a compilation error.

  3. What happens when the following code is compiled and run. Select the one correct answer.

  4. for(int i = 1; i < 3; i++)
      for(int j = 3; j >= 1; j--)
         assert i!=j : i;

    1. The class compiles and runs, but does not print anything.
    2. The number 1 gets printed with AssertionError
    3. The number 2 gets printed with AssertionError
    4. The number 3 gets printed with AssertionError
    5. The program generates a compilation error.

  5. What happens when the following code is compiled and run. Select the one correct answer.

  6. for(int i = 1; i < 4; i++)
      for(int j = 1; j < 4; j++)
        if(i < j)
           assert i!=j : i;

    1. The class compiles and runs, but does not print anything.
    2. The number 1 gets printed with AssertionError
    3. The number 2 gets printed with AssertionError
    4. The number 3 gets printed with AssertionError
    5. The program generates a compilation error.

  7. Which of the following statement is true about the assert statement. Select the one correct answer.
    1. If a Java class contains assert statements, then it must be compiled with -1.4 option.
    2. When a program having assertions is run, -assertion option must be specified, otherwise the assertions get ignored.
    3. A possible syntax of assert statement is
       assert logical_expression If logical_expression evaluates to true, the program generates an AssertionError.
    4. The program terminates on its first AssertionError.



Answers to questions on Assertions
  1. e. The condition in assert statement must be followed by a semi-colon.
  2. b. When i and j are both 1, assert condition is false, and AssertionError gets generated.
  3. a. When the if condition returns true, the assert statement also returns true. Hence AssertionError does not get generated.
  4. d. The option A is incorrect, as the Java compiler option is -source 1.4 . The option B is incorrect, as the runtime option is -ea or -enableassertions. If the logical expression evaluates to false, then the program generates an AssertionError, hence C is incorrect.

For more questions on Assertions, you may want to visit Khalid Mughal's sample chapter on Assertions.

home | tutorial | questions | test 1