Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 19:05:16 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 4: Methods

Sections 4.2-4.4
1   Suppose your method does not return any value, which of the following keywords can be used as return type?

A. void
B. int
C. double
D. public
E. None of the above

2   The signature of a method consists of ____________

A. method name
B. method name and parameter list
C. return type, method name, and parameter list
D. parameter list

3   Arguments to methods always appear within __________.

A. brackets
B. parentheses
C. curly braces
D. quotation marks

4   All Java applications must have a method named __________.

A. Main(String[] args)
B. init()
C. main(String[] args)
D. Init()

Note: Questions 5 to 6 are based on the following method:

static void nPrint(String message, int n) {
   while (n > 0) {
     System.out.print(message);
     n--;
   }
}

5   What is the printout of the call nPrint('a', 4)?

A. aaaaa
B. aaaa
C. aaa
D. invalid call

6   What is k after invoking nPrint("A message", k)?

int k = 2;
nPrint("A message", k);

A. 0
B. 1
C. 2
D. None of the above.

7   Does the return statement in the following method cause syntax errors?

public static void main(String[] args) {
   int max = 0;
   if (max != 0)
     System.out.println(max);
   else
     return;
}

A. Yes
B. No

Section 4.5 Overloading Methods
8   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     System.out.println(xMethod(5, 500L));
   }

   public static int xMethod(int n, long l) {
     System.out.println("int, long");
     return n;
   }

   public static long xMethod(long n, long l) {
     System.out.println("long, long");
     return n;
   }
}

A. The program displays int, long followed by 5.
B. The program displays long, long followed by 5.
C. The program runs fine but displays things other than given in a and b.
D. The program does not compile because the compiler cannot distinguish which xmethod to invoke.
E. None of the above.

9   Analyze the following code:

class Test {
   public static void main(String[] args) {
     System.out.println(xmethod(5));
   }

   public static int xmethod(int n, long t) {
     System.out.println("int");
     return n;
   }

   public static long xmethod(long n) {
     System.out.println("long");
     return n;
   }
}

A. The program displays int followed by 5.
B. The program displays long followed by 5.
C. The program runs fine but displays things other than given in a and b.
D. The program does not compile because the compiler cannot distinguish which xmethod to invoke.
E. None of the above.

10   Analyze the following code.

public class Test {
   public static void main(String[] args) {
     System.out.println(max(1, 2));
   }

   public static double max(int num1, double num2) {
     System.out.println("max(int, double) is invoked");

     if (num1 > num2)
       return num1;
     else
       return num2;
   }
  
   public static double max(double num1, int num2) {
     System.out.println("max(double, int) is invoked");

     if (num1 > num2)
       return num1;
     else
       return num2;
   }
}

A. The program cannot compile because you cannot have the print statement in a non-void method.
B. The program cannot compile because the compiler cannot determine which max method should be invoked.
C. The program runs and prints 2 followed by "max(int, double)" is invoked.
D. The program runs and prints 2 followed by "max(double, int)" is invoked.
E. The program runs and prints "max(int, double) is invoked" followed by 2.

11   Analyze the following code.

public class Test {
   public static void main(String[] args) {
     System.out.println(m(2));
   }

   public static int m(int num) {
     return num;
   }
  
   public static void m(int num) {
     System.out.println(num);
   }
}

A. The program has a syntax error because the two methods m have the same signature.
B. The program has a syntax error because the second m method is defined, but not invoked in the main method.
C. The program runs and prints 2 once.
D. The program runs and prints 2 twice.
E. None of the above.

Section 4.6 The Scope of Local Variables
12   What is k after the following block executes?
{
   int k = 2;
   nPrint("A message", k);
}

A. 0
B. 1
C. 2
D. k is not defined.

Section 4.8 The Math Class
13   Which of the following is a possible output from invoking Math.random()?

A. 3.43
B. 0.5
C. 0
D. 1
E. 1.0

14   What is Math.round(3.6)?

A. 3.0
B. 3
C. 4
D. 4.0
E. None of the above.

15   What is Math.rint(3.6)?

A. 3.0
B. 3
C. 4
D. 4.0
E. None of the above.

16   What is Math.ceil(3.6)?

A. 3.0
B. 3
C. 4
D. 4.0
E. None of the above.

17   What is Math.floor(3.6)?

A. 3.0
B. 3
C. 4
D. 4.0
E. None of the above.

Section 4.10 Recursion (Optional)
18   What is the return value for xMethod(4) after calling the following method?

static int xMethod(int n) {
   if (n == 1)
     return 1;
   else
     return n + xMethod(n - 1);
}

A. 12
B. 11
C. 10
D. 9

19   Analyze the following two programs:

A:

public class Test {
   public static void main(String[] args) {
     xmethod(5);
   }

   public static void xmethod(int length) {
     if (length > 1) {
       System.out.print((length - 1) + " ");
       xmethod(length - 1);
     }
   }
}

B:
public class Test {
   public static void main(String[] args) {
     xmethod(5);
   }

   public static void xmethod(int length) {
     while (length > 1) {
       System.out.print((length - 1) + " ");
       xmethod(length - 1);
     }
   }
}
A. The two programs produce the same output 5 4 3 2 1.
B. The two programs produce the same output 1 2 3 4 5.
C. The two programs produce the same output 4 3 2 1.
D. The two programs produce the same output 1 2 3 4.
E. Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely.