unit Flashcards

1
Q

Consider the Cat class which will contain a String and an int attribute for a cat’s name and age and a constructor.
Which of the following replacements for /* missing code / is the most appropriate implementation of the class?
A.
public String name;
private int age;
private Cat(String name, int age)
{ /
implementation not shown */ }

B.
public String name;
public int age;
public Cat(String name, int age)
{ /* implementation not shown */ }

C.
private String name;
private int age;
public Cat(String name, int age)
{ /* implementation not shown */ }

D.
private String name;
private int age;
public Cat(String name, int age)
{ /* implementation not shown */ }

E.
public String name;
public int age;
private Cat(String name, int age)
{ /* implementation not shown */ }

A

D.
private String name;
private int age;
public Cat(String name, int age)
{ /* implementation not shown */ }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Consider the Party class below which will contain three int attributes for numOfPeople, volumeOfMusic, and numOfBoxesOfPizza, a constructor, and a startParty method. The startParty method is intended to be accessed outside the class.
Which of the following replacements for /* missing code */ is the most appropriate implementation of the class?

A.
private int numOfPeople;
private int volumeOfMusic;
private int numOfBoxesOfPizza;
public Party()
{ /* implementation not shown / }
private void startParty()
{ /
implementation not shown */ }

B.
private int numOfPeople;
private int volumeOfMusic;
private int numOfBoxesOfPizza;
public Party()
{ /* implementation not shown / }
public void startParty()
{ /
implementation not shown */ }

C.
public int numOfPeople;
public int volumeOfMusic;
public int numOfBoxesOfPizza;
public Party()
{ /* implementation not shown / }
public void startParty()
{ /
implementation not shown */ }

D.
private int numOfPeople;
private int volumeOfMusic;
private int numOfBoxesOfPizza;
private Party()
{ /* implementation not shown / }
private void startParty()
{ /
implementation not shown */ }

A

B.
private int numOfPeople;
private int volumeOfMusic;
private int numOfBoxesOfPizza;
public Party()
{ /* implementation not shown / }
public void startParty()
{ /
implementation not shown */ }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Consider the definition of the Cat class below. The class uses the instance variable isSenior to indicate whether a cat is old enough to be considered a senior cat or not.
Which of the following statements will create a Cat object that represents a cat that is considered a senior cat?

A. Cat c = new Cat (“Oliver”, 7);
B. Cat c = new Cat (“Max”, “15”);
C. Cat c = new Cat (“Spots”, true);
D. Cat c = new Cat (“Whiskers”, 10);
E. Cat c = new Cat (“Bella”, isSenior);

A

D. Cat c = new Cat (“Whiskers”, 10);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Consider the following class definition. Each object of the class Cat will store the cat’s name as name, the cat’s age as age, and the number of kittens the cat has as kittens. Which of the following code segments, found in a class other than Cat, can be used to create a cat that is 5 years old with no kittens?

A. I only
B. II only
C. III only
D. I and III only
E. I, II and III

A

D. I and III only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The following statement appears in a method in a class other than Cat. It is intended to create a new Cat object c with its attributes set to “black” and true. Which of the following can be used to replace missing constructor code in the class definition so that the object c below is correctly created?
Cat c = new Cat(“black”, true);

A.
public Cat(String c, boolean h)
{
c = “black”;
h = true;
}

B.
public Cat(String c, boolean h)
{
c = “black”;
h = “true”;
}

C.
public Cat(String c, boolean h)
{
c = color;
h = isHungry;
}

D.
public Cat(String c, boolean h)
{
color = black;
isHungry = true;
}

E.
public Cat(String c, boolean h)
{
color = c;
isHungry = h;
}

A

E.
public Cat(String c, boolean h)
{
color = c;
isHungry = h;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Consider the following Party class. The getNumOfPeople method is intended to allow methods in other classes to access a Party object’s numOfPeople instance variable value; however, it does not work as intended. Which of the following best explains why the getNumOfPeople method does NOT work as intended?

A. The getNumOfPeople method should be declared as public.
B. The return type of the getNumOfPeople method should be void.
C. The getNumOfPeople method should have at least one parameter.
D. The variable numOfPeople is not declared inside the getNumOfPeople method.
E. The instance variable num should be returned instead of numOfPeople, which is local to the constructor.

A

A. The getNumOfPeople method should be declared as public.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The accessor method getId is intended to return the id of a Student object. Which of the following best explains why the class does not compile?

A. The id instance variable should be public.
B. The getId method should be declared as private.
C. The getId method requires a parameter.
D. The return type of the getId method needs to be defined as void.
E. The return type of the getId method needs to be defined as int.

A

E. The return type of the getId method needs to be defined as int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

public class Liquid
{
private int currentTemp;

public Liquid(int temp)
{
    currentTemp = temp;
}

public void resetTemp()
{
    currentTemp = newTemp;
} }

Which of the following best identifies the reason the class does not compile?

A. The constructor header does not have a return type.
B. The resetTemp method is missing a return type.
C. The constructor should not have a parameter.
D. The resetTemp method should have a parameter.
E. The instance variable currentTemp should be public instead of private.

A

D. The resetTemp method should have a parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In the Party class below, the addPeople method is intended to increase the value of the instance variable numOfPeople by the value of the parameter additionalPeople. The method does not work as intended.

A. Replace line 12 with numOfPeople = additionalPeople;
B. Replace line 12 with return additionalPeople;
C. Replace line 12 with additionalPeople += 3;
D. Replace line 10 with public addPeople (int additionalPeople)
E. Replace line 10 with public void addPeople(int additionalPeople)

A

Replace line 10 with public void addPeople(int additionalPeople)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The putMoneyInWallet method is intended to increase the dollars in the wallet by the parameter amount and then return the updated dollars in the wallet. Which of the following code segments should replace missing code so that the putMoneyInWallet method will work as intended?

A.
amount += dollars;
return dollars;

B.
dollars = amount;
return amount;

C.
dollars += amount;
return dollars;

D.
dollars = dollars + amount;
return amount;

E.
amount = dollars + amount;
return dollars;

A

C.
dollars += amount;
return dollars;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The isBoiling method is intended to return true if increasing the currentTemp by the parameter amount is greater than or equal to the boilingPoint, or otherwise return false. Which of the following code segments can replace missing code to ensure that the isBoiling method works as intended?

A. I only
B. II only
C. III only
D. I and III only.
E. I, II, III

A

D. I and III only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Consider the class Temperature below which has a static variable. What is the output of the main method below?

A. Max Temp: 0
B. There is a compiler error because the static variable maxTemp cannot be used inside a non-static constructor.
C. Max Temp: 100
D. Max Temp: 75
E. Max Temp: 65

A

C. Max Temp: 100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Click on all the variable declarations that are at Class Level Scope.
public class Name
{
private String first;
public String last;

public Name(String theFirst, String theLast)
{
    String firstName = theFirst;
    first = firstName;
    last = theLast;
} }
A

private String first;
public String last;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Click on all the variable declarations that are at Method Level Scope.
public class Name
{
private String first;
public String last;

public Name(String theFirst, String theLast)
{
    String firstName = theFirst;
    first = firstName;
    last = theLast;
} }
A

String theFirst String theLast

String firstName = theFirst;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Consider the following class definitions. Which of the following best explains why the class will not compile?

public class Party
{
private int boxesOfFood;
private int numOfPeople;

public Party(int people, int foodBoxes)
{
    numOfPeople = people;
    boxesOfFood = foodBoxes;
}

public void orderMoreFood(int additionalFoodBoxes)
{
    int updatedAmountOfFood = boxesOfFood + additionalFoodBoxes;
    boxesOfFood = updatedAmountOfFood;
}

public void eatFoodBoxes(int eatenBoxes)
{
    boxesOfFood = updatedAmountOfFood - eatenBoxes;
} }

A. The class is missing an accessor method.
B. The instance variables boxesOfFood and numOfPeople should be designated public instead of private.
C. The return type for the Party constructor is missing.
D. The variable updatedAmountOfFood is not defined in eatFoodBoxes method.
E. The Party class is missing a constructor

A

D. The variable updatedAmountOfFood is not defined in eatFoodBoxes method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Consider the following class definition.

public class Movie
{
private int currentPrice;
private int movieRating;

public Movie(int p, int r)
{
    currentPrice = p;
    movieRating = r;
}

public int getCurrentPrice()
{
    int currentPrice = 16;
    return currentPrice;
}

public void printPrice()
{
    System.out.println(getCurrentPrice());
} }

A. The private variables currentPrice and movieRating are not properly initialized.
B. The private variables currentPrice and movieRating should have been declared public.
C. The printPrice method should have been declared as private.
D. currentPrice is declared as a local variable in the getCurrentPrice method and set to 16, and will be used instead of the instance variable currentPrice.
E. The currentPrice instance variable does not have a value.

A

currentPrice is declared as a local variable in the getCurrentPrice method and set to 16, and will be used instead of the instance variable currentPrice.

17
Q

Consider the following class definitions.

public class Liquid
{
private int currentTemp;

public Liquid(int ct)
{
    currentTemp = ct;
}

public int getCurrentTemp()
{
    return currentTemp;
}

public void addToJar(LiquidJar j)
{
    j.addLiquid(this);
} }

public class LiquidJar
{
private int totalTemp;

public LiquidJar()
{
    totalTemp = 0;
}

public void addLiquid(Liquid l)
{
    totalTemp += l.getCurrentTemp();
}

public int getTotalTemp()
{
    return totalTemp;
}
// Constructor not shown. } Consider the following code segment, which appears in a class other than Liquid or LiquidJar.

Liquid water = new Liquid(50);
Liquid milk = new Liquid(15);

LiquidJar j = new LiquidJar();
water.addToJar(j);
milk.addToJar(j);
System.out.println(j.getTotalTemp());

A. 50
B. 15
C. 65
D. Nothing, the code segment attempts to access the private variable currentTemp outside of its scope.
E. Nothing, the code segment attempts to access the private variable totalTemp outside of its scope.

A

C. 65