Question

In: Computer Science

Which of the following is not true about designing classes? All methods should be declared private....

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

Solutions

Expert Solution

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


Related Solutions

3. Which statement about methods in an interface is true? A.)All methods in an interface must...
3. Which statement about methods in an interface is true? A.)All methods in an interface must be explicitly declared as private or public. B.)All methods in an interface are automatically public. C.)All methods in an interface are automatically private. D.)All methods in an interface are automatically static.
Which of the following are generally true about the challenges of valuing private firms? There is...
Which of the following are generally true about the challenges of valuing private firms? There is a lack of analyses generated by sources outside of the company. Financial reporting systems are often inadequate. Management depth and experience is often limited. Reported earnings are often understated to minimize taxes. All of the above.
Given the following list of classes, attributes and methods, - identify which items are classes, which...
Given the following list of classes, attributes and methods, - identify which items are classes, which items are attributes and which items are methods; - identify which class each attribute and method belongs to; and - suggest a class hierarchy given your list of classes. *Note - no particular capitalization scheme is used in the list below to differentiate between classes, methods, and attributes. LandOnStatue, NumberOfLegs, Height, ShoeSize, Eat, Animal, Speak, WingSpan, Age, Peck, Sleep, Horse, LengthOfMane, Move, BeakLength, LengthOfTail,...
Which of these statements about deadlock are true? (list all that are true.) a. If all...
Which of these statements about deadlock are true? (list all that are true.) a. If all transactions use two-phase locking, they cannot deadlock. b. Once two transactions deadlock, one of them must be aborted to maintain correctness. c. Systems that support update locks (S, X and U modes) cannot deadlock. d. Validation based concurrency control schemes cannot deadlock.
Which of the following statements about recent developments in public and private healthcare financing is true?     ...
Which of the following statements about recent developments in public and private healthcare financing is true?      After increasing for many years, the premiums for group medical expense coverage have finally started to decline at a fairly rapid pace. Preferred Provider Organization s (PPOs) are still the most frequently used healthcare plan and are popular because they offer the most flexibility of any managed care plan After rapidly increasing healthcare costs are a serious burden on the private sector but are...
Which of the following is true about fringe benefits? Which of the following is true about...
Which of the following is true about fringe benefits? Which of the following is true about fringe benefits? They represent additional compensation given for services performed They are only available for employees. The amount of the fringe benefit is never subject to income tax. They represent additional cash paid directly to employees.
Which of the following is true about a credit default swap? A. All three of these...
Which of the following is true about a credit default swap? A. All three of these potential answers here are true about a credit default swap. B. It is basically the same as a mortgage backed security C. It is insurance on an investment D. It helps to reduce the defualt rate on mortgages
Which of the following statements about a monopoly firm are true? (Check all that apply.) It...
Which of the following statements about a monopoly firm are true? (Check all that apply.) It faces a downward-sloping demand curve. Its long-run average total cost curve is always downward sloping. Its marginal revenue is always below the price. It is always profitable in the short run. It is the only seller of a good or service with no close substitutes available. It always experiences economies of scale.
Which of the following are TRUE about the Federal Funds Rate? (Select all that apply.) a....
Which of the following are TRUE about the Federal Funds Rate? (Select all that apply.) a. It is influenced by the Federal Reserve b. It is a rate at which banks can borrow long-term cash reserves c. It is set in the market d. It is the rate at which banks can borrow cash reserves on an overnight basis e. It is the rate at which individual investors may borrow cash from the Federal Reserve
Select ALL of the following which are TRUE about the parasympathetic nervous system (hint- there are...
Select ALL of the following which are TRUE about the parasympathetic nervous system (hint- there are 4): Group of answer choices parasympathetic influence causes blood vessels to constrict preganglionic axons are generally longer than post-ganglionic axons preganglionic axons emerge from thoracic and lumbar nerves preganglionic axons secrete Ach; post-ganglionic axons secrete NE preganglionic neurons emerge from cranial and sacral nerves both preganglionic and post-ganglionic axons secrete Ach parasympathetic influence causes an increase in digestive motility and secretions
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT