revised: 03/10/2018


Quiz on Static Methods

Instructions: For each question, choose the single best answer. Make your choice by clicking on its button. You can change your answers at any time. When the quiz is graded, the correct answers will appear in the box after each question.


1. What is a static method?

A.    ... a method that is part of class but is not part of an object of that class.

B.    ... a method that does not change any variables.

C.    ... a method that does not create any objects.

D.    ... a method that runs slowly


2. Can a static method call other static methods of the class?

A.    No, there is only one static method in a class.

B.    No, static methods can only be started from the command line.

C.    Yes, a class might contain several static methods that can call each other if the parameters are correct.

D.    Yes, but only the static main can call other static methods.


3. Should a method that computes a mathematical function like factorial also do user input and output?

A.    No. To be generally useful, a method should only do one clearly-defined thing.

B.    No. If a function does math it cannot do I/O.

C.    Yes. Modules should do as much as possible for efficiency.

D.    Yes. Every module should ask the user for data each time it is called.


4. What is a method called that is incomplete but enables the entire program to compile and be partially debugged?

A.    smug

B.    slug

C.    sub

D.    stub


5. What is the name of the method of parameter passing that Java uses?

A.    call by reference

B.    call by parameter

C.    call by value

D.    pass by parameter


6. Sensible program creation includes testing each module as soon as it is written. Sometimes code is created that tests the module by itself without anything else going on. This is called.

A.    Unit testing

B.    Static testing

C.    Module testing

D.    Class testing


7. What is the scope of a variable?

A.    The scope of a variable is the range of lines in the source code where the variable can be used.

B.    The scope of a variable is the range of values that it can hold.

C.    The scope of a variable is the entire source file in which it was declared.

D.    The scope of a variable is those methods that use the same name in their parameter list.


8. Is there anything wrong with the following code?

public class Scope
{
  static int absValue( int x )
  {
    if ( x < 0 )
      return -x;
    else
      return x;
  }

  public static void main( String[] args )
  {
    x = 23;
    System.out.println("Absolute Value = " + absValue(x) );
  }

}

A.    The two uses of x in the main method are out of scope.

B.    The use of x in the absValue is out of scope.

C.    The parameter x should be listed in main's parameter list.

D.    The program is fine as it is.


9. Is there anything wrong with the following code?

public class Puzzle
{
  int x = 78;

  static int absValue( int x )
  {
    if ( x < 0 )
      return -x;
    else
      return x;
  }

  public static void main( String[] args )
  {
    x = 23;
    System.out.println("Absolute Value = " + absValue(x) );
  }

}

A.    absValue changes the value of the global x, which might cause problems.

B.    The main uses a static variable x, but the x declared in the class is an instance variable.

C.    absValue should not use x in its parameter list because that name is already used for something else.

D.    The class and main both try to initialize x, but only only one of them should do this.


10. What is the output of the following code?

public class Puzzle10
{
  static int x ;

  static int absValue( )
  {
    if ( x < 0 )
      x = -x;
    return x;
  }

  public static void main( String[] args )
  {
    x = -77;
    System.out.println( absValue() + " is the absolute value of " + x );
  }

}

A.    77 is the absolute value of -77

B.    -77 is the absolute value of 77

C.    77 is the absolute value of 77

D.    The code will not compile


The number you got right:       Percent Correct:       Letter Grade:   


Click here