In the heading public static int total(int a, int b), what is int a?
Q2
Which part of a method heading tells the compiler what kind of value the method sends back?
Q3
What must a method whose return type is double do?
Q4
Which statement about a void method is correct?
Q5
What is the output?
public class Pass {
public static void change(int n) { n = 100; }
public static void main(String[] args) {
int x = 5;
change(x);
System.out.print(x);
}
}
Q6
Why can a Java method not swap two int variables belonging to its caller?
Q7
A method receives an array as a parameter and changes arr[0]. The caller:
Q8
The same method then executes arr = new int[5];. After the call, the caller's array:
Q9
Which of the following is a valid overload of void print(int x)?
Q10
Which is not a valid way to overload a method?
Q11
What will the following program output?
class Calculator {
void multiply(int a, int b) { System.out.print("Hi"); }
void multiply(double a, double b) { System.out.print("HD"); }
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.multiply(5, 3.0);
System.out.print(" and ");
calc.multiply(5.0, 3);
}
}
Q12
What will the following program output?
class Calculator {
int add(int a, int b) { return a + b; }
double add(int a, int b) { return a + b; }
public static void main(String[] args) {
System.out.println(new Calculator().add(2, 3));
}
}
Q13
What is the output of the following application?
public class ChooseWisely {
public int choose(int choice) { return 5; }
public int choose(short choice) { return 2; }
public int choose(long choice) { return 11; }
public static void main(String[] path) {
System.out.print(new ChooseWisely().choose((byte)2 + 1));
}
}
Q14
Which statement about static methods is correct?
Q15
Which of these is the best candidate to be declared static?
Q16
A utility class such as Math:
Q17
What is the value of Math.pow(2, 3)?
Q18
Which line does not compile?
Q19
What is the value of Math.ceil(4.1)?
Q20
What is the value of Math.floor(-4.1)?
Q21
What is the value of Math.round(4.5)?
Q22
What is the range of values returned by Math.random()?
Q23
Which expression produces a random integer from 1 to 6 inclusive?
Q24
Which expression gives a random integer between 10 and 20 inclusive?
Q25
Which method converts the String"42" into the int42?
Q26
What is the wrapper class for char?
Q27
What does Character.isDigit('7') return?
Q28
What does Character.toUpperCase('a') return?
Q29
What is Integer.MAX_VALUE used for?
Q30
Autoboxing refers to:
Q31
What does System.out.printf("%.2f", 3.14159) print?
Q32
Which format specifier prints a whole number?
Q33
What does the minus sign in %-10s mean?
Q34
What is printed?
public class Helper {
private static int square(int n) { return n * n; }
public static void main(String[] args) {
System.out.print(square(square(2)));
}
}
Q35
What is the output?
public class Counter {
static int count = 0;
static void bump() { count++; }
public static void main(String[] args) {
bump(); bump(); bump();
System.out.print(count);
}
}
Q36
Which method call is illegal, given
public static double area(double w, double h)?
Q37
What is the output?
public class Early {
public static void check(int n) {
if (n < 0) return;
System.out.print("ok");
}
public static void main(String[] args) { check(-5); check(5); }
}
Q38Short answer
Explain, with a small example, why a method that takes an int
parameter cannot change the caller's variable, while a method that takes an array parameter can
change the caller's data.
Q39Short answer
State the rule that decides whether two methods are valid overloads,
and explain why int f(int x) and double f(int x) cannot coexist.
Q40Programming
Write a utility class MathUtil containing: a public static final double named GOLDEN_RATIO; a static method isPrime(int n) returning a
boolean; a static method max3(int a, int b, int c); and two overloaded
static methods average — one taking two ints and one taking three. Add a
main method that tests each of them and prints the results with printf, showing two
decimal places for the averages.