1. Question: Methods must include all of the following except -

    A
    a declaration

    B
    a call to another method

    C
    curly braces

    D
    a body

    Note: Not available
    1. Report
  2. Question: All method declaration must contain -

    A
    the keyword "static"

    B
    one or more aceess modifiers

    C
    arguments

    D
    parantheses

    Note: Not available
    1. Report
  3. Question: Which of the method declarations is correct for a method named displayFacts() if the method receives an int argument?

    A
    public void int displayFacts()

    B
    public void displayFacts(int)

    C
    public void displayFacts(int data)

    D
    public void displayFacts()

    Note: Not available
    1. Report
  4. Question: The information provided within the parenthesis is called arguments.

    A
    False

    B
    True

    Note: Not available
    1. Report
  5. Question: Arguments are optional in any method.

    A
    True

    B
    False

    Note: Not available
    1. Report
  6. Question: A method is a series of statements that carry out a task.

    A
    True

    B
    False

    Note: Not available
    1. Report
  7. Question: What will be the output of the program? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) ? "assertion failed" : "assertion passed" ; System.out.println("finished"); } }

    A
    finished

    B
    Compiliation fails.

    C
    An AssertionError is thrown and finished is output.

    D
    An AssertionError is thrown with the message "assertion failed."

    Note: Compilation Fails. You can't use the Assert statement in a similar way to the ternary operator. Don't confuse.
    1. Report
  8. Question: public class Test { public void foo() { assert false; /* Line 5 */ assert false; /* Line 6 */ } public void bar() { while(true) { assert false; /* Line 12 */ } assert false; /* Line 14 */ } } What causes compilation to fail?

    A
    Line 5

    B
    Line 6

    C
    Line 12

    D
    Line 14

    Note: Option D is correct. Compilation fails because of an unreachable statement at line 14. It is a compile-time error if a statement cannot be executed because it is unreachable. The question is now, why is line 20 unreachable? If it is because of the assert then surely line 6 would also be unreachable. The answer must be something other than assert. Examine the following: A while statement can complete normally if and only if at least one of the following is true: - The while statement is reachable and the condition expression is not a constant expression with value true. -There is a reachable break statement that exits the while statement. The while statement at line 11 is infinite and there is no break statement therefore line 14 is unreachable. You can test this with the following code: public class Test80 { public void foo() { assert false; assert false; } public void bar() { while(true) { assert false; break; } assert false; } }
    1. Report
  9. Question: What will be the output of the program? public class Test { public static int y; public static void foo(int x) { System.out.print("foo "); y = x; } public static int bar(int z) { System.out.print("bar "); return y = z; } public static void main(String [] args ) { int t = 0; assert t > 0 : bar(7); assert t > 1 : foo(8); /* Line 18 */ System.out.println("done "); } }

    A
    bar

    B
    bar done

    C
    foo done

    D
    Compilation fails

    Note: The foo() method returns void. It is a perfectly acceptable method, but because it returns void it cannot be used in an assert statement, so line 18 will not compile.
    1. Report
  10. Question: What will be the output of the program (when you run with the -ea option) ? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) : "assertion failed"; /* Line 6 */ System.out.println("finished"); } }

    A
    finished

    B
    Compilation fails.

    C
    An AssertionError is thrown.

    D
    An AssertionError is thrown and finished is output.

    Note: An assertion Error is thrown as normal giving the output "assertion failed". The word "finished" is not printed (ensure you run with the -ea option) Assertion failures are generally labeled in the stack trace with the file and line number from which they were thrown, and also in this case with the error's detail message "assertion failed". The detail message is supplied by the assert statement in line 6.
    1. Report
Copyright © 2024. Powered by Intellect Software Ltd