How many copies of a static field exist when 50 objects of the class have been
created?
Q2
What is the output?
public class Robot {
static int built = 0;
int serial;
Robot() { built++; serial = built; }
public static void main(String[] args) {
new Robot(); new Robot();
Robot r = new Robot();
System.out.print(built + " " + r.serial);
}
}
Q3
Which member can a static method use directly, with no object?
Q4
Why does the following fail to compile?
public class App {
private int value = 5;
public static void main(String[] args) { System.out.print(value); }
}
Q5
Which two changes would each fix the previous program?
Q6
May an instance method access a static field of the same class?
Q7
Which declaration is a correctly written class constant?
Q8
Why is main declared static?
Q9
Which of these is the best example of something that should be a static field?
Q10
Which is the best example of something that should be an instance field?
Q11
What is the output?
public class Shared {
static int total = 0;
int mine = 0;
void add() { total++; mine++; }
public static void main(String[] args) {
Shared a = new Shared(), b = new Shared();
a.add(); a.add(); b.add();
System.out.print(a.mine + " " + b.mine + " " + total);
}
}
Q12
A utility class such as Math:
Q13
Why might a utility class declare a private no-argument constructor?
Q14
Which call is correct for public static double withVat(double a) in class
TaxUtil?
Q15
What is the output?
public class Bank {
private static double rate = 0.05;
public static void setRate(double r) { rate = r; }
public static double getRate() { return rate; }
public static void main(String[] args) {
Bank a = new Bank();
a.setRate(0.10);
System.out.print(Bank.getRate());
}
}
Q16
Calling a static method through an object reference (a.setRate(...) above) is:
Q17
When is a static field initialised?
Q18
What is wrong with this design?
public class Student {
private static String name; // every student shares one name!
public void setName(String n) { name = n; }
}
Q19
High cohesion in a class means:
Q20
Low coupling between two classes means:
Q21
In a well-designed program, main should mostly:
Q22
A method that is 200 lines long and whose comment says “reads the file, validates the
data, computes statistics and prints a report” should be:
Q23
Which javadoc tag documents what a method gives back?
Q24
Which set of test values is the best choice for a method that validates a mark between 0
and 100?
Q25
What is the output?
public class Util {
public static int twice(int n) { return 2 * n; }
public static int quad(int n) { return twice(twice(n)); }
public static void main(String[] args) { System.out.print(quad(3)); }
}
Q26
A static method calling another static method of the same class must write:
Q27
Which statement is true?
Q28
What is printed?
public class Id {
private static int next = 100;
private int id;
public Id() { id = next++; }
public int getId() { return id; }
public static void main(String[] args) {
Id a = new Id(), b = new Id(), c = new Id();
System.out.print(a.getId() + " " + c.getId() + " " + next);
}
}
Q29
Which is the correct way for one class to use a constant declared as
public static final int MAX = 40; in class Config?
Q30
A program is decomposed into InputReader, StatisticsCalculator and
ReportPrinter. This is an example of:
Q31
Which of the following would not be a sensible member of a utility class for string
handling?
Q32
What is the output?
public class Mixed {
static int s = 1;
int i = 1;
void bump() { s++; i++; }
public static void main(String[] args) {
Mixed m1 = new Mixed(), m2 = new Mixed();
m1.bump(); m2.bump(); m2.bump();
System.out.print(m1.i + "" + m2.i + s);
}
}
Q33Short answer
Explain in your own words why a static method cannot use an
instance variable directly, and describe the two ways a static method can nevertheless work
with instance data.
Q34Short answer
A Library class must remember how many books have ever been
borrowed across the whole system, and also how many each individual Member currently holds.
Say which of these is static and which is an instance variable, and justify both choices.
Q35Short answer
Give two arguments for replacing the literal 0.19 scattered
through a program with public static final double VAT = 0.19;.
Q36Programming
Write a utility class StringUtil with static methods
countVowels(String), reverse(String), isPalindrome(String) (ignoring case and
spaces) and capitaliseWords(String), plus a private constructor and a short javadoc
comment for each method. Write a separate driver class that tests all four with at least two
inputs each, including an empty string.