In: Computer Science
Which of the following is not true about designing classes?
All methods should be declared private.
To test for equality the programmer should define an equals method.
In order for class information to be printed instead of a memory address, a toString method should be declared.
All class variables should be declared as private.
The constructor must be declared as public.
The method that builds a class in memory is called the ______.
constructor
equals
toString
return
private
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?
2
3
4
5
6
A special value that means "no object" is called ______.
reference
void
null
garbage collection
class
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?
notebook costs $5.0
CD costs $0.0
book costs $3.5
none costs $0.0
sweater costs $18.95
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?
notebook costs $5.0
CD costs $0.0
book costs $3.5
none costs $0.0
sweater costs $18.95
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?
notebook costs $5.0
CD costs $0.0
book costs $3.5
none costs $0.0
sweater costs $18.95
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, f, W, x]
[W, Z, f, x, ]
[f, W, x]
[Z, W, x]
[Z, f, x]
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
II only
III only
II and III
I, II and III
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 = names.size() - 1; i >= 9; i++
int i = 0; i < names.size(); i--
int i = 0; i < names.size(); i++
int i = names.size() - 1; i >=0; i--
none of the above
1.Which of the following is not true about designing classes?
Ans- All methods should be declared private.
explanation- Methods can be declared public also.
2.The method that builds a class in memory is called the
Ans- Constructor
explanation- This is the function of the constructor
3.After running the code, what is the size of bulbs?
Ans- 3
explanation- Add function is 7 times and remove function is 3 times but last line add function is overwriting previous bulb so no extra bulb , hence 3.
4.A special value that means "no object" is called ______.
Ans- Null
5.What is printed as a result of executing the code segment?
Ans- none costs $0.0
explanation- First element of the list is (0, "none")
6.What is printed as a result of executing the code segment?
Ans- notebook costs $5.0
explanation- Last element of the list is (5, "notebook")
7.What is printed as a result of executing the code segment?
Ans- CD costs $0.0
explanation- list.remove(1); will remove element at index 1 and all elements after 1 will shift left, for example- element at index 2 will come at 1 , element at index 3 will come at 2.
So the answer is the element at index 2 which will come at index 1 after remove command.
8.what is printed as a result of running this code segment?
Ans- [Z, W, x]
explanation- Remove command removes the element at mentioned index and all elements after that index will shift left.(subtracts one from their indices).
9.Consider the following method that is intended to test if all the Strings in the ArrayList start with an uppercase letter:
Ans- II and III
explanation- for every string in the ArrayList, we first convert that string to uppercase and check if the first character of original string matches with the uppercased string, if it is not true for at least one string also then we return False and if it true for all strings then we return True.
10.Consider the following method intended to modify the parameter names by removing all instances of the String n.
Ans-
int i = 0; i < names.size(); i++
int i = names.size() - 1; i >=0; i--
explanation- int i = 0; i < names.size(); i++ . In this i will start from 0 and will increment until it becomes equal to names.size()
int i = names.size() - 1; i >=0; i-- . In this i will start from size of list and decrement till i becomes equal to 0