logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Assertions See What Others Are Saying!
  • Question
  • Which three statements are true?

    1. Assertion checking is typically enabled when a program is deployed.
    2. It is never appropriate to write code to handle failure of an assert statement.
    3. Assertion checking is typically enabled during program development and testing.
    4. Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis.
    5. Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.


  • Options
  • A. 1, 2 and 4
  • B. 2, 3 and 5
  • C. 3, 4 and 5
  • D. 1, 2 and 5

  • Correct Answer
  • 2, 3 and 5 

    Explanation
    (1) is wrong. It's just not true.

    (2) is correct. You're never supposed to handle an assertion failure.

    (3) is correct. Assertions let you test your assumptions during development, but the assertion code?in effect?evaporates when the program is deployed, leaving behind no overhead or debugging code to track down and remove.

    (4) is wrong. See the explanation for (5) below.

    (5) is correct. Assertion checking can be selectively enabled or disabled on a per-package basis. Note that the package default assertion status determines the assertion status for classes initialized in the future that belong to the named package or any of its "subpackages".

    The assertion status can be set for a named top-level class and any nested classes contained therein. This setting takes precedence over the class loader's default assertion status, and over any applicable per-package default. If the named class is not a top-level class, the change of status will have no effect on the actual assertion status of any class.


    More questions

    • 1. What will be the output of the program?
      public class A
      { 
          void A() /* Line 3 */
          {
              System.out.println("Class A"); 
          } 
          public static void main(String[] args) 
          { 
              new A(); 
          } 
      }
      

    • Options
    • A. Class A
    • B. Compilation fails.
    • C. An exception is thrown at line 3.
    • D. The code executes with no output.
    • Discuss
    • 2. What will be the output of the program?
      class Test 
      {
          public static void main(String [] args) 
          {
              Test p = new Test();
              p.start();
          }
      
          void start() 
          {
              boolean b1 = false;
              boolean b2 = fix(b1);
              System.out.println(b1 + " " + b2);
          }
      
          boolean fix(boolean b1) 
          {
              b1 = true;
              return b1;
          }
      }
      

    • Options
    • A. true true
    • B. false true
    • C. true false
    • D. false false
    • Discuss
    • 3. What will be the output of the program?
      class Test 
      {
          static int s;
          public static void main(String [] args) 
          {
              Test p = new Test();
              p.start();
              System.out.println(s);
          }
      
          void start() 
          {
              int x = 7;
              twice(x);
              System.out.print(x + " ");
          }
      
          void twice(int x) 
          {
              x = x*2;
              s = x;
          }
      }
      

    • Options
    • A. 7 7
    • B. 7 14
    • C. 14 0
    • D. 14 14
    • Discuss
    • 4. You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

    • Options
    • A. public
    • B. private
    • C. protected
    • D. default access
    • Discuss
    • 5. What will be the output of the program?
      class Super
      { 
          public int i = 0; 
      
          public Super(String text) /* Line 4 */
          {
              i = 1; 
          } 
      } 
      
      class Sub extends Super
      {
          public Sub(String text)
          {
              i = 2; 
          } 
      
          public static void main(String args[])
          {
              Sub sub = new Sub("Hello"); 
              System.out.println(sub.i); 
          } 
      }
      

    • Options
    • A. 0
    • B. 1
    • C. 2
    • D. Compilation fails.
    • Discuss
    • 6. What will be the output of the program (in jdk1.6 or above)?
      public class BoolTest 
      {
          public static void main(String [] args) 
          {
              Boolean b1 = new Boolean("false");
              boolean b2;
              b2 = b1.booleanValue();
              if (!b2) 
              {
                  b2 = true;
                  System.out.print("x ");
              }
              if (b1 & b2) /* Line 13 */
              {
                  System.out.print("y ");
              }
              System.out.println("z");
          }
      }
      

    • Options
    • A. z
    • B. x z