logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Assertions Comments

  • Question
  • Which of the following statements is true?


  • Options
  • A. In an assert statement, the expression after the colon ( : ) can be any Java expression.
  • B. If a switch block has no default, adding an assert default is considered appropriate.
  • C. In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.
  • D. It is appropriate to handle assertion failures using a catch clause.

  • Correct Answer
  • If a switch block has no default, adding an assert default is considered appropriate. 

    Explanation
    Adding an assertion statement to a switch statement that previously had no default case is considered an excellent use of the assert mechanism.

    Option A is incorrect because only Java expressions that return a value can be used. For instance, a method that returns void is illegal.

    Option C is incorrect because the expression after the colon must have a value.

    Option D is incorrect because assertions throw errors and not exceptions, and assertion errors do cause program termination and should not be handled.


    Assertions problems


    Search Results


    • 1. Which two can be used to create a new Thread?

      1. Extend java.lang.Thread and override the run() method.
      2. Extend java.lang.Runnable and override the start() method.
      3. Implement java.lang.Thread and implement the run() method.
      4. Implement java.lang.Runnable and implement the run() method.
      5. Implement java.lang.Thread and implement the start() method.

    • Options
    • A. 1 and 2
    • B. 2 and 3
    • C. 1 and 4
    • D. 3 and 4
    • Discuss
    • 2. Which statement is true?

    • Options
    • A. A static method cannot be synchronized.
    • B. If a class has synchronized code, multiple threads can still access the nonsynchronized code.
    • C. Variables can be protected from concurrent access problems by marking them with the synchronized keyword.
    • D. When a thread sleeps, it releases its locks.
    • Discuss
    • 3. Which statement is true?

    • Options
    • A. The notifyAll() method must be called from a synchronized context.
    • B. To call wait(), an object must own the lock on the thread.
    • C. The notify() method is defined in class java.lang.Thread.
    • D. The notify() method causes a thread to immediately release its locks.
    • Discuss
    • 4. Which statement is true?

    • Options
    • A. If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution.
    • B. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.
    • C. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call.
    • D. If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.
    • Discuss
    • 5. The following block of code creates a Thread using a Runnable target:
      Runnable target = new MyRunnable();
      Thread myThread = new Thread(target);
      
      Which of the following classes can be used to create the target, so that the preceding code compiles correctly?

    • Options
    • A. public class MyRunnable extends Runnable{public void run(){}}
    • B. public class MyRunnable extends Object{public void run(){}}
    • C. public class MyRunnable implements Runnable{public void run(){}}
    • D. public class MyRunnable implements Runnable{void run(){}}
    • Discuss
    • 6. Which of the following statements is true?

    • Options
    • A. If assertions are compiled into a source file, and if no flags are included at runtime, assertions will execute by default.
    • B. As of Java version 1.4, assertion statements are compiled by default.
    • C. With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time.
    • D. When evaluating command-line arguments, the VM gives -ea flags precedence over -da flags.
    • Discuss
    • 7. 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
    • Discuss
    • 8. Which statement is true?

    • Options
    • A. Assertions can be enabled or disabled on a class-by-class basis.
    • B. Conditional compilation is used to allow tested classes to run at full speed.
    • C. Assertions are appropriate for checking the validity of arguments in a method.
    • D. The programmer can choose to execute a return statement or to throw an exception if an assertion fails.
    • Discuss
    • 9. Which of the following statements is true?

    • Options
    • A. It is sometimes good practice to throw an AssertionError explicitly.
    • B. Private getter() and setter() methods should not use assertions to verify arguments.
    • C. If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.
    • D. It is proper to handle assertion statement failures using a catch (AssertionException ae) block.
    • Discuss
    • 10. Which statement is true about assertions in the Java programming language?

    • Options
    • A. Assertion expressions should not contain side effects.
    • B. Assertion expression values can be any primitive type.
    • C. Assertions should be used for enforcing preconditions on public methods.
    • D. An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method.
    • Discuss


    Comments

    There are no comments.

Enter a new Comment