Term 2: QUIZ 1
Suppose the following line is used to replace /* Missing Code */. System.out.println(list.get(list.size() - 1)); What is printed as a result of executing the code segment?
sweater costs $18.95 this one isn't right,, don't know the right answer
Consider the following code segment: ArrayList<Light> bulbs = new ArrayList<Light>(); bulbs.add(new Light()); bulbs.remove(0); bulbs.add(new Light()); Light b = new Light(); bulbs.add(1, b); bulbs.add(new Light()); bulbs.remove(0); bulbs.add(new Light()); bulbs.remove(2); bulbs.add(new Light()); bulbs.add(1, new Light()); After running the code, what is the size of bulbs?
4
Which of the following is not true about designing classes?
All methods should be declared private.
Suppose the following lines are used to replace /* Missing Code */. list.remove(1);System.out.println(list.get(1)); What is printed as a result of executing the code segment?
CD costs $0.0
Consider the following method that is intended to test if all the Strings in the ArrayList start with an uppercase letter: public static boolean capitalized(ArrayList<String> a) { /* Missing Code */ } Which of the following could replace /* Missing Code */ so that the method works as intended? I. for (String s: a) { if (s.toUpperCase().charAt(0) != s.charAt(0)) { return true;}} return false; II. for (String s: a) { if (s.toUpperCase().charAt(0) != s.charAt(0)) { return false;}} return true; III. int flag = 1; for (String s: a) { if (s.toUpperCase().charAt(0) != s.charAt(0)) { flag = 0;}} return (flag == 1);
I only this one isn't right,, don't know the right answer
Consider the following code segment. ArrayList<String> stuff = new ArrayList<String>(); stuff.add("Z"); stuff.add("f"); stuff.add(2, "W"); stuff.remove(1); stuff.add("x"); System.out.println(stuff); what is printed as a result of running this code segment?
[Z, W, x]
The method that builds a class in memory is called the ______.
constructor
Consider the following method intended to modify the parameter names by removing all instances of the String n. public static void removeNames (ArrayList<String> names, String n) { for (/* Missing Code */) { if (names.get(i).equals(n)) { names.remove(i);} } } Which of the following could correctly replace /* Missing Code */ so that removeNames works as intended?
int i = 0; i < names.size(); i++ this one isn't right,, don't know the right answer
Questions 5 - 7 refer to the following code: public class Whatchamacallit { private double price; private String title;public Whatchamacallit() { this (0, "none"); }public Whatchamacallit(double p, String t) { price = 0; if (p > 0) { price = p;} title = t; }public String toString() { return title + " costs $" + price; } } The following code segment appears in another class: ArrayList<Whatchamacallit> list = new ArrayList<Whatchamacallit>();list.add(new Whatchamacallit()); list.add(new Whatchamacallit(3.5, "book")); list.add(new Whatchamacallit(-17, "CD")); list.add(new Whatchamacallit(18.95, "sweater")); list.add(new Whatchamacallit(5, "notebook"));/* Missing Code */ Suppose the following line is used to replace /* Missing Code */. System.out.println(list.get(0)); What is printed as a result of executing the code segment?
none costs $0.0
A special value that means "no object" is called ______.
null