Q1. Suppose your method does not return any value, which of the following keywords can be used as a return type?
void
Q2. Does the method call in the following method cause compile errors?
No
Q3. You should fill in the blank in the following code with ________.
void
Q4. You should fill in the blank in the following code with ________.
char
Q5. Given the following method
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n–;
}
}
What is the output of the call nPrint(‘a’, 4)?
invalid call
Q6. Given the following method
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n–;
}
}
What is k after invoking nPrint(“A message”, k)?
int k = 2;
nPrint(“A message”, k);
2
Q7. 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;
}
}
The program cannot compile because the compiler cannot determine which max method should be invoked.
Q8. (char)(‘a’ + Math.random() * (‘z’ – ‘a’ + 1)) returns a random character ________.
between ‘a’ and ‘z
Q9. Which of the following is the best for generating random integer 0 or 1?
(int)(Math.random() + 0.5)
Q10. The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as ________.
encapsulation
information hiding
Other Links:
See other websites for quiz:
Check on QUIZLET