Semester 1 Unit 5

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/44

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

45 Terms

1
New cards

How many objects can you create from a class? (choose the best answer)

  • 1,000,000

  • 10

  • As many as needed

  • 1

  • As many as needed

2
New cards

You can only have one constructor method in your class.

True

False

False

3
New cards

A class always starts with a default constructor.

True

False

True

4
New cards

What keyword is used to create a new object from a class?

class

object

null

new

new

5
New cards

Consider the following class declarations.

public class Point 
{ 
  private double x; // x-coordinate 
  private double y; // y-coordinate

  public Point() 
  { 
    x = 0; 
    y = 0; 
  } 
  public Point(double a, double b) 
  { 
    x = a; 
    y = b; 
  }  
 
 // There may be instance variables, constructors, and methods that are not shown. 
}

public class Circle 
{ 
  private Point center; 
  private double radius; 
  /** Constructs a circle where (a, b) is the center and r is the radius. 
  */ 
  public Circle(double a, double b, double r) 
  { 
    /* missing code */ 
  } 
}

Which of the following replacements for /* missing code */ will correctly implement the Circle constructor?

Option Table

I.

center = new Point(); 
radius = r;

II.

center = new Point(a, b); 
radius = r;

III.

center = new Point(); 
center.x = a; 
center.y = b; 
radius = r;

(Copyright Collegeboard AP 2014-34)

 

II only

 

III only

 

I only

 

I, II, and III

 

II and III only

II only

6
New cards

Which of the following is a proper constructor method for the Vehicle class?

  • private Vehicle() { };

  • public Vehicle() { };

  • public String Vehicle() { };

  • public class Vehicle() { };

public Vehicle() { };

7
New cards

What specifies the data or state for an object in Java?

methods

attributes

object

class

attributes

8
New cards

Consider the definition of the Employee class below. The class uses the instance variable yearsOfService to indicate whether an employee is able to retire with a pension. Employees are eligible to retire with a pension at age 40 with 20 years of service, age 50 with 15 years of service, or age 60 with 10 years of service.

public class Employee {

private String name;

private int age;

private int yearsOfService;

private boolean retirementPension;

public Employee() {

name = “”;

age = 0;

yearsOfService = 0;

retirementPension = false;

}

public Employee(String n, int a, int y) {

name = n;

age = a;

yearsOfService = y;

if ((age >= 40 && yearsOfService >= 20) ||

(age >= 50 && yearsOfService >= 15) ||

(age >= 60 && yearsOfService >= 10)) {

retirementPension = true;

}

else {

retirementPension = false;

}

}

}

Which of the following statements would create an Employee object that represents an employee who can retire with a pension? Select all that apply.

  • Employee e = new Employee("Todd Snider", "62", “22”);

  • Employee e = new Employee("Jim Smith", 42, 22);

  • Employee e = new Employee("Tom Jones", 35, 21);

  • Employee e = new Employee("Molly Ringwald", 55, 17);

  • Employee e = new Employee("Jim Smith", 42, 22);

  • Employee e = new Employee("Molly Ringwald", 55, 17);

9
New cards

Consider the following class definition.

Each object of the class Toy will store the toy’s name as toyName, the toy’s regular price in dollars as toyPrice, and the discount that is applied to the regular price when the toy is on sale as discPercent. For example, a discount of 20% is stored in discPercent as 0.20.

public class Toy {

private String toyName;

private double regPrice;

private double discPercent;

public Toy (String name, double price, double discount) {

toyName = name;

regPrice = price;

discPercent = discount;

}

public Toy(String name, double price) {

toyName = name;

regPrice = price;

discPercent = 0.20;

}

/* Other methods not shown */

}

Which of the following code segments could be used to create a Toy object with a regular price of $10 and a discount of 20% ?

Toy b = new Toy("blanket", 10.0, 0.20);

Toy b = new Toy("blanket", 10.0);

Toy b = new Toy("blanket", 0.20, 10.0);

II only

III only

I and III only

I and II only

I and II only

10
New cards

Consider the following class declaration.

public class Student {

private String myName;

private int myAge;

public Student() {

/* implementation not shown */

}

public Student(String name, int age) {

/* implementation not shown */

}

// No other constructors

}

Which of the following declarations will compile without error?

Student a = new Student();

Student b = new Student("Juan", 15);

Student c = new Student("Juan", "15");

I, II, and III

I and II only

I and III only

I only

II only

I and II only

11
New cards

Consider the following class definition.

public class Calc {

private int num1;

private int num2;

/* missing constructor */

}

The following statement appears in a method in a class other than Calc. It is intended to create a new Calc object obj with its attributes set to 25 and 40.

Calc obj = new Calc(25, 40);

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

public Calc(int first, int second){

num1 = first;

num2 = second;

}

public Calc(int first, int second){

first = 1;

second = 2;

}

public Calc(int first, int second){

first = 25;

second = 40;

}

public Calc(int first, int second){

first = num1;

second = num2;

}

III

IV

I

II

I

12
New cards

Consider the code below. What would be the output?

public class Person{

private int age;

private String name;

public Person{

age = 0;

name = “”;

}

public Person(int a, String n) {

age = a;

name = n;

}

public static void main(String[] args) {

Person lisa = new Person();

Person jim = new Person(16, “Jim”);

System.out.print(jim.name);

System.out.print(jim.age);

System.out.print(lisa.name);

System.out.print(lisa.age);

}

}

Jim 16

jim16 0

Jim 16 0

Jim160

Jim160

13
New cards

Consider the definition of the VideoGame class below. The class uses the instance variable okUnder10 to indicate whether a video game is appropriate for children age 9 or under.

public class VideoGame {

private String gameName;

private String genre;

private String esrbRating;

private boolean okUnder10;

public VideoGame(String n, String g, String rating) {

gameName = n;

genre = g;

esrbRating = rating;

if (esrbRating == “E”) {

okUnder10 = true;

}

else {

okUnder10 = false;

}

}

}

Which of the following statements will create a VideoGame object that represents a video game that is suitable for children age 9 and under?

  • VideoGame v = new VideoGame("Super Football", “Sports”, “E”);

  • VideoGame v = new VideoGame("Race Car", “Racing”, “E-10+”);

  • VideoGame v = new VideoGame("Beyond the World", “Action”, "E-10+");

  • VideoGame v = new VideoGame("Let’s Dance", “Dancing Simulation”, "E-10+");

VideoGame v = new VideoGame("Super Football", “Sports”, “E”);

14
New cards

Consider the definition of the Person class below. The class uses the instance variableadult to indicate whether a person is an adult or not.

public class Person

{

private String name;

private int age;

private boolean adult;

public Person (String n, int a)

{

name = n;

age = a;

if (age >= 18)

{

adult = true;

}

else

{

adult = false;

}

}

}

Which of the following statements will create a Person object that represents an adult person?

  • Person p = new Person ("Homer", "adult");

  • Person p = new Person ("Homer", 17);

  • Person p = new Person ("Homer", "23");

  • Person p = new Person ("Homer", 23);

Person p = new Person ("Homer", 23);

15
New cards

Consider the following partial class declaration.

public class SomeClass {

private int myA;

private int myB;

private int myC;

// Constructor(s) not shown

public int getA() {

return myA;

}

public void setB(int value) {

myB = value;

}

}

Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC?

Make myC public.

Include the method:

public int getC() {

return myC;

}

Include the method:

private int getC() {

return myC;

}

Include the method:

public void getC(int x) {

x = myC;

}

Include the method:

private void getC(int x) {

x = myC;

}

Include the method:

public int getC() {

return myC;

}

16
New cards

Consider the following methods, which appear in the same class.

public void slope(int x1, int y1, int x2, int y2)

{

int xChange = x2 - x1;

int yChange = y2 - y1;

printFraction(yChange, xChange);

}

public void printFraction(int numerator, int denominator)

{

System.out.print(numerator + "/" + denominator);

}

Assume that the method call slope(3, 2, 5, 12) appears in a method in the same class. What is printed as a result of the method call?

7/1

2/10

1/7

10/2

10/2

17
New cards

Consider the following class declaration.

public class Person {

private String myName;

private int myYearOfBirth;

public Person(String name, int yearOfBirth) {

myName = name;

myYearOfBirth = yearOfBirth;

}

public String getName() {

return myName;

}

public void setName(String name) {

myName = name;

}

// There may be instance variables, constructors, and methods

// that are not shown.

}

Assume that the following declaration has been made.

Person student = new Person("Thomas", 1995);

Which of the following statements is the most appropriate for changing the name of student from "Thomas" to "Tom" ?

  • student.setName("Tom");

  • Person.setName("Tom");

  • student.getName("Tom");

  • student.myName = "Tom";

  • student = new Person("Tom", 1995);

  • student.setName("Tom");

18
New cards

Given the following declaration of a field in a class:

public static final String GREETING = "Hi";

Which of these statements is not true?

  • GREETING.toUpperCase().equals("HI")

  • The value of GREETING can not be changed in any methods

  • GREETING.length() == 2

  • Each object of this class can have a different value for GREETING

  • Each object of this class can access GREETING

  • Each object of this class can have a different value for GREETING

19
New cards

Consider the following class definition.

public class RentalCar

{

private double dailyRate; // the fee per rental day

private double mileageRate; // the fee per mile driven

public RentalCar(double daily, double mileage)

{

dailyRate = daily;

mileageRate = mileage;

}

public double calculateFee(int days, int miles)

{

/* missing code */

}

}

The calculateFee method is intended to calculate the total fee for renting a car. The total fee is equal to the number of days of the rental, days, times the daily rental rate plus the number of miles driven, miles, times the per mile rate.

Which of the following code segments should replace /* missing code */ so that the calculateFee method will work as intended?

  • return (days + miles) * (dailyRate + mileageRate);

  • return dailyRate + mileageRate;

  • return (daily days) + (mileage miles);

  • return (days dailyRate) + (miles mileageRate);

return (days dailyRate) + (miles mileageRate);

20
New cards

Consider the following partial class declaration.

public class SomeClass {

private int myA;

private int myB;

private int myC;

// Constructor(s) not shown

public int getA() {

return myA;

}

public void setB(int value) {

myB = value;

}

}

Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC?

Include the method:

private void getC(int x) {

x = myC;

}

Include the method:

public void getC(int x) {

x = myC;

}

Include the method:

private int getC() {

return myC;

}

Make myC public.

Include the method:

public int getC() {

return myC;

}

Include the method:

public int getC() {

return myC;

}

21
New cards

Consider the following partial class declaration.

public class SomeClass {

private int myA;

private int myB;

private int myC;

// Constructor(s) not shown

public int getA() {

return myA;

}

public void setB(int value) {

myB = value;

}

}

The following declaration appears in another class.

SomeClass obj = new SomeClass();

Which of the following code segments will compile without error?

int x = SomeClass.getA();

int x = getA(obj);

int x; obj.getA(x);

int x = obj.getA();

int x = obj.myA;

int x = obj.getA();

22
New cards

Consider the following class definition.

public class WordClass

{

private final String word;

private static String max_word = "";

public WordClass (String s)

{

word = s;

if (word.length() > max_word.length())

{

max_word = word;

}

}

}

Which of the following is a true statement about the behavior of WordClass objects?

A WordClass object can change the value of the variable word more than once.

Every time a WordClass object is created, the max_word variable is referenced.

The value of the max_word variable cannot be changed once it has been initialized.

Every time a WordClass object is created, the value of the max_word variable changes.

Every time a WordClass object is created, the max_word variable is referenced.

23
New cards

The Date class below will contain three int attributes for day, month, and year, a constructor, and a setDate method. The setDate method is intended to be accessed outside the class.

public class Date

{

/* missing code */

}

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

private int day;

private int month;

private int year;

private Date()

{ /* implementation not shown */ }

private void setDate(int d, int m, int y)

{ /* implementation not shown */ }

public int day;

public int month;

public int year;

public Date()

{ /* implementation not shown */ }

public void setDate(int d, int m, int y)

{ /* implementation not shown */ }

private int day;

private int month;

private int year;

public Date()

{ /* implementation not shown */ }

public void setDate(int d, int m, int y)

{ /* implementation not shown */ }

private int day;

private int month;

private int year;

public Date()

{ /* implementation not shown */ }

private void setDate(int d, int m, int y)

{ /* implementation not shown */ }

public Date()

{ /* implementation not shown */ }

public void setDate(int d, int m, int y)

{ /* implementation not shown */ }

24
New cards

The Car class will contain two string attributes for a car’s make and model. The class will also contain a constructor.

public class Car {

/* missing code */

}

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

private String make;

private String model;

public Car(String myMake, String myModel) {

/* implementation not shown */

}

public String make;

public String model;

public Car(String myMake, String myModel) {

/* implementation not shown */

}

public String make;

public String model;

private Car(String myMake, String myModel) {

/* implementation not shown */

}

private String make;

private String model;

private Car(String myMake, String myModel) {

/* implementation not shown */

}

private String make;

private String model;

public Car(String myMake, String myModel) {

/* implementation not shown */

}

25
New cards

Consider the following class declaration.

public class ParkingPass

{

private int numLeft;

// constructor not shown

public int getNumLeft()

{

return numLeft;

}

public String toString()

{

return "Parking pass has " + getNumLeft() + " left";

}

}

If the following declaration appears in a client class.

ParkingPass pass = new ParkingPass();

Which of these statements can be used in the client class?

Option Choices

I.

System.out.println(pass.numLeft);

II.

System.out.println(pass);

III.

System.out.println(pass.getNumLeft());

I only

III only

II only

II and III

I and III

II and III

26
New cards

Consider the following class definition.

public class Password

{

private String password;

public Password (String pwd)

{

password = pwd;

}

public void reset(String new_pwd)

{

password = new_pwd;

}

}

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

Password p = new Password("password");

System.out.println("The new password is " + p.reset("password"));

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

The reset method cannot be called from outside the Password class.

The Password class constructor is invoked incorrectly.

The code segment attempts to access the private variable password from outside the Password class.

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

The new password cannot be the same as the old password.

  • The reset method does not return a value that can be printed.

27
New cards

Consider the following class definition.

public class WordClass

{

private final String word;

private static String max_word = "";

public WordClass (String s)

{

word = s;

if (word.length() > max_word.length())

{

max_word = word;

}

}

}

Which of the following is a true statement about the behavior of WordClass objects?

A WordClass object can change the value of the variable word more than once.

The value of the max_word variable cannot be changed once it has been initialized.

Every time a WordClass object is created, the value of the max_word variable changes.

No two WordClass objects can have their word length equal to the length of max_word.

Every time a WordClass object is created, the max_word variable is referenced.

Every time a WordClass object is created, the max_word variable is referenced.

28
New cards

Consider the following class declaration.

public class Circle

{

private double radius;

public double computeArea()

{

private double pi = 3.14159

public double area = pi radius radius;

return area;

}

// Constructor not shown.

}

Which of the following best explains why the computeArea method will cause a compilation error?

Local variables used inside a method must be declared at the end of the method.

Local variables declared inside a method must all be private.

Local variables used inside a method must be declared before the method header.

Local variables declared inside a method cannot be declared as public or private.

Local variables declared inside a method must all be public.

Local variables declared inside a method cannot be declared as public or private.

29
New cards

The Car class will contain two string attributes for a car's make and model. The class will also contain a constructor.

public class Car

{

/* missing code */

}

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

private String make;

private String model;

public Car(String myMake, String myModel)

{ / implementation not shown */ }

*

private String make;

private String model;

private Car(String myMake, String myModel)

{ /* implementation not shown */ }

public String make;

private String model;

private Car(String myMake, String myModel)

( /* implementation not shown */ }

public String make;

public String model;

public Car(String myMake, String myModel)

{ /* implementation not shown */ }

public String make;

public String model;

private Car(String myMake, String myModel)

{ /* implementation not shown */ }

private String make;

private String model;

public Car(String myMake, String myModel)

{ / implementation not shown */ }

*

30
New cards

Consider the following class definition.

public class Info

{

private String name;

private int number;

public Info(String n, int num)

{

name = n;

number = num;

}

public void changeName(String newName)

{

name = newName;

}

public int addNum(int n)

{

num += n;

return num;

}

}

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

The class is missing an accessor method.

The instance variables name and number should be designated public instead of private.

The variable name is not defined in the changeName method.

The variable num is not defined in the addNum method.

The return type for the Info constructor is missing.

The variable num is not defined in the addNum method.

31
New cards

The following method is intended to return a string containing the character at position n in the string str. For example, getChar("ABCDE", 2) should return "C".

/* missing precondition */

public String getChar(String str, int n)

{

return str.substring(n, n + 1);

}

Which of the following is the most appropriate precondition for the method so that it does not throw an exception?

/* Precondition: 0 <= n <= str.length() - 1 */

/* Precondition: n >= str.length() */

/* Precondition: 0 < n < str.length() - 1 */

/* Precondition: n > str.length() */

/* Precondition: 0 <= n <= str.length() */

/* Precondition: 0 <= n <= str.length() - 1 */

32
New cards

Consider the following class definitions.

public class Class1

{

private int val1;

public Class1()

{

val1 = 1;

}

public void init ()

{

Class2 c2 = new Class2();

c2.init(this, val1);

}

public void update(int x)

{

val1 -= x;

}

public int getVal()

{

return val1;

}

}

public class Class2

{

private int val2;

public Class2()

{

val2 = 2;

}

public void init(Class1 c, int y)

{

c.update(val2 + y);

}

}

The following code segment appears in a method in a class other than Class1 or Class2.

Class1 c = new Class1();

c.init();

System.out.println(c.getVal());

What, if anything, is printed as a result of executing the code segment?

1

Nothing is printed because the code segment does not compile.

-2

0

2

2

33
New cards

Consider the following two methods, which appear within a single class.

public static void changeIt(int[] arr, int val, String word)

{

arr = new int[];

value = 0;

word = word.substring(0, 5);

for(int k = 0; k < arr.length; k++)

{

arr[k] = 0;

}

}

public static void start()

{

int[] nums = (1, 2, 3, 4, 5);

int value = 6;

String name = "blackboard";

changeIt(nums, value, name);

for(int k = 0; k < nums.length; k++)

{

System.out.print(nums[k] + " ")}

}

System.out.print(value + " ");

System.out.print(name);

}

What is printed as a result of the call start() ?

0 0 0 0 0 6 blackboard

1 2 3 4 5 6 black

1 2 3 4 5 6 blackboard

1 2 3 4 5 0 black

0 0 0 0 0 0 black

1 2 3 4 5 6 blackboard

34
New cards

Consider the following class definition.

public class Element

{

public static int max_value = 0;

private int value;

public Element (int v)

{

value = v;

if (value > max_value)

{

max_value = value;

}

}

}

The following code segment appears in a class other than Element.

for (int i = 0; i < 5; i++)

{

int k = (int) (Math.random() * 10 + 1);

if (k >= Element.max_value)

{

Element e = new Element(k);

}

}

Which of the following best describes the behavior of the code segment?

Exactly 5 Element objects are created.

Between 1 and 5 Element objects are created, and Element.max_value is increased for every object created.

Exactly 10 Element objects are created.

Between 0 and 5 Element objects are created, and Element.max_value is increased only for the first object created.

Between 1 and 5 Element objects are created, and Element.max_value is increased for at least one object created.

Between 1 and 5 Element objects are created, and Element.max_value is increased for at least one object created.

35
New cards

Consider the following class that stores information about temperature readings on various dates.

public class TemperatureReading implements Comparable

{

private double temperature; private int month, day, year;

public int compareTo(Object obj)

{

TemperatureReading other = (TemperatureReading)obj;

/* missing code */

}

// There may be instance variables, constructors, and methods that are not shown.

}

Consider the following code segments that are potential replacements for /* missing code */.

I.

Double d1 = new Double(temperature); Double d2 = new Double(other.temperature); return d1.compareTo(d2);

II.

if (temperature < other.temperature)

return -1;

else if (temperature == other.temperature)

return 0;

else

return 1;

III.

return (int) (temperature - other.temperature);

Which of the code segments could be used to replace /* missing code */ so that compareTo can be used to order TemperatureReading objects by increasing temperature value?

I and III only

I and II only

II only

II and III only

I, II, and III

I and II only

36
New cards

Consider the following class definition.

public class Something

{

private static int count = 0;

public Something()

{

count += 5;

}

public static void increment()

{

count++;

}

}

The following code segment appears in a method in a class other than Something.

Something s = new Something();

Something.increment();

Which of the following best describes the behavior of the code segment?

The code segment creates a Something object s. The class Something's static variable count is initially 0 , then increased by 1 .

The code segment creates a Something object s. After executing the code segment, the object s has a count value of 1 .

The code segment creates a Something object s. The class Something's static variable count is initially 0 , then increased by 5 , then increased by 1 .

The code segment creates a Something object s. After executing the code segment, the object s has a count value of 5 .

The code segment does not compile because the increment method should be called on an object of the class Something, not on the class itself.

The code segment creates a Something object s. The class Something's static variable count is initially 0 , then increased by 5 , then increased by 1 .

37
New cards

Consider the following class definition.

public class ItemInventory

{

private int numItems;

public ItemInventory(int num)

{

numItems = num;

}

public updateItems(int newNum)

{

numItems = newNum;

}

}

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

The constructor header is missing a return type.

The constructor should not have a parameter.

The updateItems method should not have a parameter.

The updateItems method is missing a return type.

The instance variable numItems should be public instead of private.

The updateItems method is missing a return type.

38
New cards

Consider the following while loop. Assume that the int variable k has been properly declared and initialized.

while (k < 0)

{

System.out.print("*");

k++;

}

Which of the following ranges of initial values for k will guarantee that at least one " * " character is printed?

I. k < 0

II. k = 0

III. k > 0

I only

III only

I and II only

I, II, and III

II and III only

I only

39
New cards

Consider the following class, which uses the instance variable balance to represent a bank account balance.

public class BankAccount

{

private double balance;

public double deposit(double amount)

{

/* missing code */

}

}

The deposit method is intended to increase the account balance by the deposit amount and then return the updated balance. Which of the following code segments should replace /* missing code */ so that the deposit method will work as intended?

balance = balance + amount;

return amount;

balance = balance + amount;

return balance;

balance = amount;

return balance;

balance = amount;

return amount;

amount = balance + amount;

return amount;

balance = balance + amount;

return balance;

40
New cards

The question refer to the following declarations.

public class Point

{

private double myX;

private double myyY;

// postcondition: this Point has coordinates (0, 0)

public Point ()

{/* implementation not shown */}

// postcondition: this Point has coordinates (x, y)

public Point(double x, double y)

{/* implementation not shown */}

// other methods not shown

}

public class Circle

{

private Point myCenter;

private double myRadius;

// postcondition: this Circle has center at (0, 0) and radius 0.0

public Circle()

{/* implementation not shown */}

// postcondition: this Circle has the given center and radius

public Circle(Point center, double radius)

{/* implementation not shown */}

// other methods not shown

}

Which of the following would be the best specification for a Circle method isInside that determines whether a Point lies inside this Circle?

public void isInside(Point p, boolean found)

public boolean isInside()

public boolean islnside(Point p, Point center, double radius)

public void isInside(boolean found)

public boolean isInside(Point p)

public boolean isInside(Point p)

41
New cards

A rectangular box fits inside another rectangular box if and only if the height, width, and depth of the smaller box are each less than the corresponding values of the larger box. Consider the following three interface declarations that are intended to represent information necessary for rectangular boxes.

I.

public interface RBox

{

/** @return the height of this RBox */

double getHeight();

/** @return the width of this RBox */

double getWidth();

/** @return the depth of this RBox */

double getDepth();

}

II.

public interface RBox

{

/** @return true if the height of this RBox is less than the height of other;

* false otherwise

*/

boolean smallerHeight(RBox other);

/** @return true if the width of this RBox is less than the width of other;

* false otherwise

*/

boolean smallerWidth(RBox other);

/** @return true if the depth of this RBox is less than the depth of other;

* false otherwise

*/

boolean smallerDepth(RBox other);

}

III.

public interface RBox

{

/** @return the surface area of this RBox */

double getSurfaceArea();

/** @return the volume of this RBox */

double getVolume();

}

Which of the interfaces, if correctly implemented by a Box class, would be sufficient functionality for a user of the Box class to determine if one Box can fit inside another?

I and II only

II only

I, II, and III

III only

I only

 

I and II only

42
New cards

Consider the following class definition.

public class SomeClass

{

private int x = 0;

private static int y = 0;

public SomeClass(int pX)

{

x = pX;

y++;

}

public void incrementY()

{ y++; }

public void incrementY(int inc)

{ y += inc; }

public int getY()

{ return y; }

}

The following code segment appears in a class other than SomeClass.

SomeClass first = new SomeClass(10);

SomeClass second = new SomeClass(20);

SomeClass third = new SomeClass(30);

first.incrementY();

second.incrementY(10);

System.out.println(third.getY());

What is printed as a result of executing the code segment if the code segment is the first use of a SomeClass object?

11

14

0

30

1

14

43
New cards

Consider the following class definition.

public class Box

{

private double weight;

/** Postcondition: weight is initialized to w. */

public Box(double w)

{

/* implementation not shown */

}

public double getWeight()

return weight;

public void addWeight(double aw)

{

/* missing statement*/

}

}

The following code segment, which appears in a class other than Box, is intended to create a Box object b1 with a weight of 2.2 units and then increase the weight of b1 by 1.5 units.

Box b1 = new Box(2.2);

b1.addWeight(1.5);

Which of the following statements could replace /* missing statement */ so that the code segment works as intended?

aw += weight;

weight += aw;

return weight + aw;

weight += getWeight () ;

aw += getWeight();

weight += aw;

44
New cards

Consider the following class definition.

public class FishTank

{

private double numGallons;

private boolean saltWater;

public FishTank(double gals, boolean sw)

{

numGallons = gals;

saltWater = sw;

}

public double getNumGallons()

{

return numGallons;

}

public boolean isSaltWater()

{

if (saltWater)

{

return "Salt Water";

}

else

{

return "Fresh Water";

}

}

}

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

The value returned by the isSaltWater method is not compatible with the return type of the method.

The variable numGallons is not declared in the getNumGallons method.

The variable saltwater is not declared in the isSaltWater method.

The isSaltWater method does not return the value of an instance variable.

The value returned by the getNumGallons method is not compatible with the return type of the method.

The value returned by the isSaltWater method is not compatible with the return type of the method.

45
New cards