본문 바로가기
자바(Java)/자바 AP

[자바AP연습문제]05.Writing Classes

by 긱펀 2024. 3. 24.
반응형

[자바AP연습문제]05.Writing Classes

 

문제 정답은 제일 아래 "더보기" 클릭

 

 

*05.Writing Classes 단원의 복습 내용은 아래 "더보기" 클릭

더보기
  • Class - A class defines a type and is used to define what all objects of that class know and can do.
  • Compiler - Software that translates the Java source code (ends in .java) into the Java class file (ends in .class).
  • Compile time error - An error that is found during the compilation. These are also called syntax errors.
  • Constructor - Used to initialize fields in a newly created object.
  • Field - A field holds data or a property - what an object knows or keeps track of.
  • Java - A programming language that you can use to tell a computer what to do.
  • Main Method - Where execution starts in a Java program.
  • Method - Defines behavior - what an object can do.
  • Object - Objects do the actual work in an object-oriented program.
  • Syntax Error - A syntax error is an error in the specification of the program.
  • public - a visibility keyword which is used to control the classes that have access. The keyword public means the code in any class has direct access.
  • private - a visibility keyword which is used to control the classes that have access. The keyword private means that only the code in the current class has direct access.
  • Getters / Setters - review link: https://wooduino.tistory.com/214
  • Static member - review link: https://wooduino.tistory.com/174
  • Scope and Access - review link: https://wooduino.tistory.com/215
  • this keyword - review link: https://wooduino.tistory.com/216

 

 

*Common Mistakes

  • Forgetting to declare an object to call its methods.
  • Forgetting to write get/set methods for private instance variables.
  • Forgetting to write a constructor.
  • Mismatch in name, number, type, order of arguments and return type between the method definition and the method call.
  • Forgetting data types for every argument in the method definition.
  • Forgetting to use what the method returns.

 

[문제1]

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?

1
2
3
4
public class Cat
{
    /* missing code */
}
cs

 

 

 

[문제2]

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?

1
2
3
4
public class Party
{
    /* missing code */
}
cs

 

 

 

[문제3]

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Cat
{
    private String name;
    private int age;
    private boolean isSenior;
 
    public Cat(String n, int a)
    {
        name = n;
        age = a;
        if (age >= 10)
        {
            isSenior = true;
        }
        else
        {
            isSenior = false;
        }
    }
}
cs

 

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);

 

 

[문제4]

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Cat
{
    private String name;
    private int age;
    private int kittens;
 
    public Cat(String n, int a, int k)
    {
        name = n;
        age = a;
        kittens = k;
    }
    public Cat(String n, int a)
    {
        name = n;
        age = a;
        kittens = 0;
    }
    /* Other methods not shown */
}
 
I.   Cat c = new Cat("Sprinkles"50);
II.  Cat c = new Cat("Lucy"05);
III. Cat c = new Cat("Luna"5);
cs

 

A. I only

B. II only

C. III only

D. I and III only

E. I, II and III

 

 

[문제5]

Consider the following class definition.

1
2
3
4
5
6
public class Cat
{
    private String color;
    private boolean isHungry;
    /* missing constructor */
}
cs

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. 

1
2
Cat c = new Cat("black"true);
 
cs

 

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?

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[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;
}
cs

 

 

[문제6]

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Party
{
    private int numOfPeople;
 
    public Party(int num)
    {
        numOfPeople = num;
    }
 
    private int getNumOfPeople()
    {
        return numOfPeople;
    }
}
cs

 

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.

 

 

[문제7]

Consider the following class definition. The class does not compile.

1
2
3
4
5
6
7
8
9
10
public class Student
{
    private int id;
 
    public getId()
    {
        return id;
    }
    // Constructor not shown
}
cs

 

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.

 

 

[문제8]

Consider the following class definition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Liquid
{
    private int currentTemp;
 
    public Liquid(int temp)
    {
        currentTemp = temp;
    }
 
    public void resetTemp()
    {
        currentTemp = newTemp;
    }
}
cs

 

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.

 

 

[문제9]

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Party
{
    private int numOfPeople;
 
    public Party(int n)
    {
        numOfPeople = n;
    }
 
    public int addPeople(int additionalPeople) // Line 10
            {
        numOfPeople += additionalPeople; // Line 12
    }
}
cs

 

Which of the following changes should be made so that the class definition compiles without error and the method addPeople works 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)

 

 

[문제10]

Consider the following class, which uses the instance variable dollars to represent the money in a wallet in dollars.

1
2
3
4
5
6
7
8
9
public class Wallet
{
    private double dollars;
 
    public double putMoneyInWallet(int amount)
    {
        /* missing code */
    }
}
cs

 

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?

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[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;
cs

 

 

[문제11]

Consider the Liquid class below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Liquid
{
    private int currentTemp;
    private int boilingPoint;
 
    public Liquid(int ct, int bp)
    {
        currentTemp = ct;
        boilingPoint = bp;
    }
 
    public boolean isBoiling(int amount)
    {
        /* missing code */
    }
}
cs

 

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
I.   if (currentTemp + amount < boilingPoint)
     {
         return false;
     }
     else
     {
         return true;
     }
II.  if (amount > currentTemp)
     {
         return false;
     }
     else
     {
         return currentTemp;
     }
III. if (amount + currentTemp >= boilingPoint)
     {
         return true;
     }
     else
     {
         return false;
     }
cs

 

A. I only

B. II only

C. III only

D. I and III only.

E. I, II, III

 

 

[문제12]

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
    }
}
cs

 

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

 

[문제13]

Consider the following class definition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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());
    }
}
cs

 

Which of the following reasons explains why the printPrice method is “broken” and only ever prints out a value of 16?

 

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.

 

 

[문제14]

Consider the following class definitions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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.
}
cs

 

Consider the following code segment, which appears in a class other than Liquid or LiquidJar.

1
2
3
4
5
6
7
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());
cs

 

What, if anything, is printed out after the execution of the code segment?

 

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.

 

 

[문제15]

The Liquid class will contain two double attributes for a liquid’s boiling point temperature and freezing point temperature. The class will also contain a constructor.

1
2
3
4
public class Liquid
{
   /* missing code */
}
cs

 

Which of the following replacements for /* missing code */ is the most appropriate implementation of the class?

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[A]
private double boilingPoint;
private double freezingPoint;
public Liquid(double boilingPoint, double freezingPoint)
/* implementation not shown */ }
 
[B]
private double boilingPoint;
private double freezingPoint;
private Liquid(double boilingPoint, double freezingPoint)
/* implementation not shown */ }
 
[C]
private double boilingPoint;
public double freezingPoint;
private Liquid(double freezingPoint, double boilingPoint)
/* implementation not shown */ }
 
[D]
public double boilingPoint;
public double freezingPoint;
private Liquid(double boilingPoint, double freezingPoint)
/* implementation not shown */ }
 
[E]
public double freezingPoint;
public double boilingPoint;
public Liquid(double freezingPoint, double boilingPoint)
/* implementation not shown */ }
cs

 

 

[문제16]

The Cat class below will contain two String attributes and one int attribute for name, color, and age; a constructor; and an adoptCat method. The adoptCat method is intended to be accessed outside the class.

1
2
3
4
public class Cat
{
  /* missing code */
}
cs

 

Which of the following replacements for /* missing code */ is the most appropriate implementation of the class?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[A]
private String name;
private String color;
private int age;
public Cat()
/* implementation not shown */ }
private void adoptCat(String n, String c, int a)
/* implementation not shown */ }
 
[B]
private String name;
private String color;
private int age;
public Cat()
/* implementation not shown */ }
public void adoptCat(String n, String c, int a)
/* implementation not shown */ }
 
[C]
public String name;
public String color;
public int age;
private Cat()
/* implementation not shown */ }
private void adoptCat(String n, String c, int a)
/* implementation not shown */ }
 
[D]
public String name;
public String color;
public int age;
private Cat()
/* implementation not shown */ }
public void adoptCat(String n, String c, int a)
/* implementation not shown */ }
 
[E]
public String name;
public String color;
public int age;
public Cat()
/* implementation not shown */ }
public void adoptCat(String n, String c, int a)
/* implementation not shown */ }
cs

 

 

[문제17]

Consider the definition of the Party class below. The class uses the instance variable numOfPeople to indicate how many people are at the party.

1
2
3
4
5
6
7
8
9
10
11
public class Party
{
    private int numOfPeople;
    private String partyHost;
 
    public Party (String name, int people)
    {
        partyHost = name;
        numOfPeople = people;
    }
}
cs

 

Which of the following statements will create a Party object that represents a party that has three people at it?

 

A. Party p = new Party (“Billie”, “2+1”);

B. Party p = new Party (“Emillio”, “three”);

C. Party p = new Party (“Eduardo”, 3);

D. Party p = new Party (“Bob”, three);

E. Party p = new Party (“Natasha”, “3”);

 

[문제18]

Consider the following class definition. Each object of the class Party will store the party host’s name as partyHost, the number of people as numOfPeople, and the capacity that the event can hold as capacity. Which of the following code segments, found in a class other than Party, can be used to create a party hosted by Charlie without anyone there initially, but the place can hold 78 people ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Party
{
    private String partyHost;
    private int numOfPeople;
    private int capacity;
 
    public Party(String name, int num, int cap)
    {
        partyHost = name;
        numOfPeople = num;
        capacity = cap;
    }
    public Party (String name, int cap)
    {
        partyHost = name;
        numOfPeople = 0;
        capacity = cap;
    }
    /* Other methods not shown */
}
 
I.   Party b = new Party("Charlie"78);
II.  Party b = new Party("Charlie"070+8);
III. Party b = new Party("Charlie"078);
cs

 

A. I and III

B. I only

C. I, II, and III

D. II and III

E. I and II

 

[문제19]

Consider the following class definition.

1
2
3
4
5
6
public class Party
{
    private int numOfPeople;
    private double volumeOfMusic;
    /* missing constructor */
}
cs

 

The following statement appears in a method in a class other than Party. It is intended to create a new Party object p with its attributes set to 10 and 5.0.

1
Party p = new Party(105.0);
cs

 

Which of the following can be used to replace /* missing constructor */ so that the object p is correctly created?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[A]
public Party(int first, double second)
{
    numOfPeople = 10;
    volumeOfMusic = 5.0;
}
 
[B]
public Party(int first, double second)
{
    first = numOfPeople;
    second = volumeOfMusic;
}
 
[C]
public Party(int first, double second)
{
    first = 10;
    second = 5.0;
}
 
[D]
public Party(int first, double second)
{
    numOfPeople = first;
    volumeOfMusic = second;
}
cs

 

[문제20]

Consider the following class definition that defines a Liquid class with a boilingPoint, a currentTemperature, and a freezingPoint. For example, Liquid water = new Liquid(100, 50, 0); defines a water object with a boiling point of 100, a current temperature of 50, and a freezing temperature of 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Liquid
{
    private int boilingPoint;
    private int currentTemp;
    private int freezingPoint;
 
    public Liquid(int bp, int ct, int fp)
    {
        boilingPoint = bp;
        currentTemp = ct;
        freezingPoint = fp;
    }
    /* Other methods not shown */
}
cs

 

Which of the following preconditions is reasonable for the Liquid constructor?

A. Precondition: fp > 0

B. Precondition: fp < ct < bp

C. Precondition: fp > ct > bp

D. Precondition: currentTemp > 0

 

[문제21]

Consider the following Cat class, with the cat’s age stored in the method’s int attribute. The getAge method is intended to allow methods in other classes to access a Cat object’s age value; however, it does not work as intended. Which of the following best explains why the getAge method does NOT work as intended?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Cat
{
    private int age;
 
    public Cat(int a)
    {
        age = a;
    }
 
    public int getAge()
    {
        return a;
    }
}
cs

 

A. The variable age is not declared inside the getAge method.

B. The getAge method should be declared as private.

C. The return type of the getAge method should be void.

D. The instance variable age should be returned instead of a, which is local to the constructor.

E. The getAge method should have at least one parameter.

 

[문제22]

Consider the following Liquid class. The currentTemperature is stored in the method’s int attribute. The getCurrentTemp method is intended to allow methods in other classes to access a Liquid object’s currentTemperature value; however, it does not work as intended. Which of the following best explains why the getCurrentTemperature method does NOT work as intended?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Liquid
{
    private int currentTemperature;
 
    public Liquid(int ct)
    {
        currentTemperature = ct;
    }
 
    public void getCurrentTemperature()
    {
        return currentTemperature;
    }
}
cs

 

A. The instance variable ct should be returned instead of currentTemperature.

B. The variable currentTemperature is not declared inside of the getCurrentTemperature method.

C. The getCurrentTemperature method should have at least one parameter.

D. The return type of the getCurrentTemperature method should be int.

E. The getCurrentTemperature method should be declared as private.

 

[문제23]

Consider the following class definition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Liquid
{
    private int currentTemp;
 
    public Liquid(int temp)
    {
        currentTemp = temp;
    }
 
    public int getTemp()
    {
        return currentTemp;
    }
 
    public void resetTemp(int new_temp)
    {
        currentTemp = new_temp;
    }
}
cs

 

Consider the following code segment, which appears in a method in a class other than Liquid. The code segment does not compile.

1
2
Liquid liq = new Liquid(50);
System.out.println("The temperature of the liquid is " + liq.currentTemp);
cs

 

Which of the following best identifies the reason the code segment does not compile?

A. The resetTemperature method does not return a value that can be printed.

B. currentTemp does not have a value.

C. The Liquid class constructor should not have a parameter.

D. The private instance variable cannot be accessed from outside the class unless the accessor method is used.

E. The getTemp method cannot be called from outside the Liquid class.

 

[문제24]

In the Liquid class below, the raiseTemperature method is intended to increase the value of the instance variable currentTemp by the value of the parameter increase. The method does not work as intended.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Liquid
{
    private int currentTemp;
 
    public Liquid(int ct)
    {
        currentTemp = ct;
    }
 
    public void raiseTemperature(int increase) // Line 10
            {
        return currentTemp + increase; // Line 12
    }
}
cs

 

Which of the following changes should be made so that the class definition compiles without error and the method raiseTemperature works as intended?

A. Replace line 12 with increase += currentTemp;

B. Replace line 12 with currentTemp += increase;

C. Replace line 10 with public raiseTemperature(int increase)

D. Replace line 12 with return currentTemp += increase;

E. Replace line 10 with public int raiseTemperature(int increase)

 

[문제25]

Consider the following class definition. The calculatePizzaCostPerPerson method is intended to calculate the amount each person at the party must pay for pizza. The amount is equal to the total price of all the pizza boxes divided by the number of people at the party. Which of the following code segments should replace missing code so that the calculatePizzaCostPerPerson method will work as intended?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Party
{
    private int numOfPeople; // number of people at the party
 
    public Party(int people)
    {
        numOfPeople = people;
    }
 
    public double calculatePizzaCostPerPerson(
            int numOfBoxes, double priceOfOnePizzaBox)
            {
        /* missing code */
    }
}
cs

 

A. return numOfPeople / (numOfBoxes * priceOfOnePizzaBox);

B. return numOfPeople * numOfBoxes * priceOfOnePizzaBox;.

C. return (numOfBoxes + priceOfOnePizzaBox) / numOfPeople;

D. return (numOfBoxes * priceOfOnePizzaBox) / numOfPeople;

E. return numOfBoxes / priceOfOnePizzaBox / numOfPeople;

 

[문제26]

Consider the Party class below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Party
{
    private int numOfPeople; // number of people at the party
    private int capacity; // total capacity of people at party
 
    public Party(int people, int cap)
    {
        numOfPeople = people;
        capacity = cap;
    }
 
    public boolean updateNumOfPeople(int additionalPeople)
    {
        /* missing code */
    }
}
cs

 

The class contains the updateNumOfPeople method, which is intended to update the instance variable numOfPeople under certain conditions and return a value indicating whether the update was successful. If adding additionalPeople to the current number of people would lead to the number going over the capacity, then the update would be unsuccessful. Otherwise, if adding the number of additional people is still below or at the capacity, the update is successful. Which of the following code segments can replace missing code to ensure that the updateNumOfPeople method works as intended?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
I.  if (numOfPeople + additionalPeople > capacity)
    {
        return false;
    }
    else
    {
        numOfPeople += additionalPeople;
        return true;
    }
II. if (numOfPeople + additionalPeople <= capacity)
    {
        numOfPeople += additionalPeople;
        return true;
    }
    else
    {
        return false;
    }
III. if (numOfPeople += additionalPeople <= capacity)
     {
        return true;
     }
     else
     {
        return false;
     }
cs

 

A. III only

B. I only

C. II only

D. I, II, and III

E. I and II only

 

[문제27]

Consider the following class definition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Liquid
{
    private int currentTemp;
    private int boilingPoint;
 
    public Liquid(int ct, int bp)
    {
        currentTemp = ct;
        boilingPoint = bp;
    }
 
    public void changeTemp(int newTemp)
    {
        currentTemp = newTemp;
    }
 
    public void increaseTemp(int howMuch)
    {
        currentTemp = newTemp + howMuch;
    }
}
cs

 

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

A. The class is missing an accessor method.

B. The instance variables currentTemp and boilingPoint should be public instead of private.

C. The Liquid constructor needs a return type.

D. The Liquid class is missing a constructor.

E. The variable newTemp is not defined in the increaseTemp method.

 

[문제28]

Consider the following class definition for Party. The following code segment appears in a method in a class other than Party. The code segment is intended to print the value 30, but does not print the correct value because of an error in the Party class. Which of the following best explains why the correct value isn’t printed?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Party p = new Party(2015);
p.orderMoreFood(20);
p.eatFood(5);
System.out.println(p.getBoxesOfFood());
 
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 int getNumOfPeople() {
     return numOfPeople;
 }
 
 public int getBoxesOfFood() {
     return boxesOfFood;
 }
 
 public void eatFood(int eatenBoxes)
 {
     boxesOfFood = updatedAmountOfFood - eatenBoxes;
 }
}
cs

 

A. The private variables boxesOfFood and numOfPeople are not properly initialized.

B. The private variables boxesOfFood and numOfPeople should have been declared public.

C. The public method getBoxesOfFood should have been declared private.

D. The variable updatedAmountOfFood in the eatFood method is not declared in this method.

E. The variables boxesOfFood and numOfPeople in the updatedAmountOfFood method are local variables.

 

[문제29]

Consider the following class definitions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class Party
{
    private String partyHost;
    private int monthOfParty;
    private int partyStartTime;
 
    public Party(String h, int month, int startTime)
    {
        partyHost = h;
        monthOfParty = month;
        partyStartTime = startTime;
    }
 
    public int getMonth()
    {
        return monthOfParty;
    }
 
    public int getStartTime()
    {
        return partyStartTime;
    }
 
    public String getHost()
    {
        return partyHost;
    }
 
    public void addToOptions(PartyOptions o)
    {
        o.addParty(this);
    }
}
 
public class PartyOptions
{
    private int onlyThisMonth;
 
    public PartyOptions(int month)
    {
        onlyThisMonth = month;
    }
 
    /* A Party should only be added to this PartyOption if the party’s month matches onlyThisMonth */
    public void addParty(Party p)
    {
        if (p.getMonth() == onlyThisMonth)
        {
            System.out.print("Party by " + p.getHost() + " accepted; ");
        }
        else
        {
            System.out.print("Party by " + p.getHost() + " rejected; ");
        }
    }
}
cs

 

Consider the following code segment, which appears in a class other than Party or PartyOptions.

What is the most appropriate output?

1
2
3
4
5
6
Party p1 = new Party("Kerry"107);
Party p2 = new Party("Jules"96);
 
PartyOptions options = new PartyOptions(10);
p1.addToOptions(options);
p2.addToOptions(options);
cs

 

A. Party by Kerry rejected; Party by Jules rejected;

B. Party by Kerry rejected; Party by Jules accepted;

C. Party by Kerry accepted; Party by Jules rejected;

D. Party by Kerry accepted; Party by Jules accepted;


 

 

[문제 정답은 아래 "더보기" 클릭]

더보기

[문제1 정답]

C

(The instance variables are private and the constructor is public.)

 

[문제2 정답]

B

(instance variables should be private and the methods should be public.)

 

[문제3 정답]

D

 

[문제4 정답]

D

 

[문제5 정답]

E

 

[문제6 정답]

A

(accessor methods should be public so they can be accessed from outside the class)

 

[문제7 정답]

 E

(Accessor methods have a return type of the instance variable they are returning)

 

[문제8 정답]

D

(The resetTemp method should have a parameter for the newTemp value to set the currentTemp)

 

[문제9 정답]

E

(Mutator methods should have a void return type)

 

[문제10 정답]

C

 

[문제11 정답]

D

 

[문제12 정답]

D

(There is a scope violation. The updatedAmountOfFood variable is a local variable in another method)

 

[문제13 정답]

D

 

[문제14 정답]

C

(The liquid water with a temperature of 50 and then the liquid milk with a temperature of 15 are added to the jar.)

 

[문제15 정답]

A

(The instance variables should be private and the constructor and methods should be public)

 

[문제16 정답]

B

(Method and constructor should be public, and instance variables should be private)

 

[문제17 정답]

 C

 

[문제18 정답]

 C

(I, II, and III can successfully create the Party instance)

 

[문제19 정답]

 D

 

[문제20 정답]

 B

 

[문제21 정답]

D

(The accessor method getAge should return the instance variable age)

 

[문제22 정답]

D

(The return type should match the type of the variable being returned)

 

[문제23 정답]

 D

(The currentTemp instance variable is private and cannot be accessed outside of the class but the public accessor method getTemp() can be used instead)

 

[문제24 정답]

 B

(This void mutator method should just change the value of currentTemp and not return a value)

 

[문제25 정답]

 D

(Assume you have 5 boxes at $10 each for a total cost of $50. If you had 10 people at the party, you would need to divide $50 by 10 to get $5 per person)

 

[문제26 정답]

E

(in case of III, you cannot put a shortcut assignment operator(+=) in the conditional test of an if statement)

 

[문제27 정답]

 E

(newTemp is defined in a different method. The instance variable currentTemp should be used instead)

 

[문제28 정답]

 D

(The variable updatedAmountOfFood in the eatFood method is not declared in this method. It could be replaced by the boxesOfFood instance variable)

 

[문제29 정답]

 C

(Kerry’s party is accepted because it is in the 10th month, and Jules’ party is not)

 

728x90
반응형

댓글