placeholder image to represent content

Chp. 8, Methods

Quiz by DEREK HOWARD

Our brand new solo games combine with your quiz, on the same screen

Correct quiz answers unlock more play!

New Quizalize solo game modes
20 questions
Show answers
  • Q1
    Assuming myMethod( ) returns a string, which of the following statements is valid?
    All of these are valid
    System.out.println(myMethod());
    myMethod();
    String result = "Return value is " + myMethod();
    30s
  • Q2
    Given the following function declaration: private static double mystery(double a, int b){ return a *b; } What is the result of executing the following code? int a = 3; double b = 2.0; System.out.println(mystery(a,b));
    6.0 will be printed to the console
    Nothing, a run-time exception will be thrown
    6 will be printed to the console
    Nothing, there is a compile-time error
    30s
  • Q3
    . Given the following method definitions: private static void mystery(double a) { System.out.print("double! "); } private static void mystery(int a) { System.out.print("int! "); } What will be the output of the following code? mystery(1); mystery(1.0);
    int! double!
    double! int!
    It is impossible to predict
    Duplicate function names results in a compiler error
    30s
  • Q4
    Given the following method: private static void mystery(int a) { a = 3; System.out.print(a + " "); } What is the result printed to the console when the following code is executed? int a = 4; mystery(a); System.out.print(a + " ");
    4 3
    4 4
    3 4
    3 3
    30s
  • Q5
    What does the following method return if a = -8.0 and b = 2.0? private static double mystery(double a, double b) { if (a > 0) return a * b; else if (a < 0) return a / b; return 0; }
    a * b
    0
    a / b
    An exception will be thrown
    30s
  • Q6
    What is wrong with the following declaration for the empty() method? public class Tester { } public static void empty() { }
    All methods other than main() must be declared as private
    It is not enclosed within the curly braces of the Tester class
    All methods must return some value
    The method does not receive any input parameters
    30s
  • Q7
    What is wrong with the following function declaration? static void errorProne(int a, double a) return a + a;
    It has duplicate parameter names
    It is missing curly braces
    It is declared as a void but returns some data
    All of these things are wrong
    30s
  • Q8
    What keyword must be used on any method (function) on your class that is called from your main() method?
    private
    static
    public
    void
    30s
  • Q9
    Which of the following method names are invalid?
    _1a()
    1_a()
    _a1()
    a_1()
    30s
  • Q10
    Which of the following statements best reflects “functional decomposition”?
    Careful analysis of your code to ensure it is error-free
    Break a large task into a series of smaller, simpler methods
    Keep your method bodies as long as possible
    Get rid of old methods you haven’t used in a while
    30s
  • Q11
    What three words are often used interchangeably to represent a function?
    function, method, subroutine
    function, loop, flow control
    function, main, declaration
    function, purpose, task
    30s
  • Q12
    What two symbols define the beginning and ending of the function body?
    { and }
    ( and )
    [ and ]
    /* and */
    30s
  • Q13
    Which of the following locations are valid places to add a function definition?
    These are all valid locations
    Outside a class body
    Inside a class body, but outside other function bodies
    Inside other function bodies
    30s
  • Q14
    Which of the following functions correctly declares a double and a string parameter?
    static void myFunction(double p1, string p2)
    static void myFunction(double, string)
    static void myFunction(p1, p2)
    static void myFunction(int myDouble, int myString)
    30s
  • Q15
    What can you do with function parameters within a function body?
    You can change their data type
    You can read the parameters but not change their local value
    You can write the parameters but not read their value
    The same things you can do with any other locally declared variable
    30s

Teachers give this quiz to your class