Padd Solutions

Converted by Falcon Hive


1

The behavior of an object is defined by the object's

a. instance data
b. constructor
c. visibility modifiers
d. methods
e. all of the above


Your Answer: D       Correct Answer: D

Explanation: The methods dictate how the object reacts when it is passed messages. Each message is implemented as a method, and the method is the code that executes when the message is passed. The constructor is one of these methods but all of the methods combine dictate the behavior. The visibility modifiers do impact the object's performance indirectly.


2

The relationship between a class and an object is best described as

a. classes are instances of objects
b. objects are instances of classes
c. objects and classes are the same thing
d. classes are programs while objects are variables
e. objects are the instance data of classes


Your Answer: D       Correct Answer: B

Explanation: Classes are definitions of program entities that represent classes of things/entities in the world. Class definitions include instance data and methods. To use a class, it is instantiated. These instances are known as objects. So, objects are instances of classes. Program code directly interacts with objects, not classes.


3

To define a class that will represent a car, which of the following definitions is most appropriate?

a. private class car 
b. public class car
c. public class Car
d. public class CAR
e. private class Car


Your Answer: C       Correct Answer: C

Explanation: Classes should be defined to be public so that they can be accessed by other classes. And following Java naming convention, class names should start with a capital letter and be lower case except for the beginning of each new word, so Car is more appropriate than car or CAR.


4

Which of the following reserved words in Java is used to create an instance of a class?

a. class
b. public
c. public or private, either could be used
d. import
e. new


Your Answer: C       Correct Answer: E

Explanation: The reserved word "new" is used to instantiate an object, that is, to create an instance of a class. The statement new is followed by the name of the class. This calls the class' constructor. Example: Car x = new Car( ); will create a new instance of a Car and set the variable x to it.


5

In order to preserve encapsulation of an object, we would do all of the following except for which one?

a. make the instance data private
b. Define the methods in the class to access and manipulate the instance data
c. make the methods of the class public
d. Make the class final
e. All of the above preserve encapsulation


Your Answer: A       Correct Answer: D

Explanation: Encapsulation means that the class contains both the data and the methods needed to manipulate the data. In order to preserve encapsulation properly, the instance data should not be directly accessible from outside of the classes, so the instance data are made private and methods are defined to access and manipulate the instance data. Further, the methods to access and manipulate the instance data are made public so that other classes can use the object. The reserved word "final" is used to control inheritance and has nothing to do with encapsulation.


6

If a method does not have a return statement, then

a. it will produce a syntax error when compiled
b. it must be a void method
c. it can not be called from outside the class that defined the method
d. it must be defined to be a public method
e. it must be an int, double, or String method


Your Answer: B       Correct Answer: B

Explanation: All methods are implied to return something and therefore there must be a return statement. However, if the programmer wishes to write a method that does not return anything, and therefore does not need a return statement, then it must be a void method (a method whose header has "void" as its return type).


7

Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution?

a. m1
b. m2
c. m3
d. m5
e. main


Your Answer: C       Correct Answer: B

Explanation: Once a method terminates, control resumes with the method that called that method. In this case, m2 calls m4, so that when m4 terminates, m2 is resumed.


8

For questions 8-10, use the following class definition

import java.text.DecimalFormat;
public class Student
{
private String name;
private String major;
private double gpa;
private int hours;

public Student(String newName, String newMajor, double newGPA, int newHours)
{
name = newName;
major = newMajor;
gpa = newGPA;
hours = newHours;
}

public String toString( )
{
return name + "\n" + major + "\n" + gpa + "\n" + hours
}
}
Which of the following could be used to instantiate a new Student s1?
a. Student s1 = new Student( );
b. s1 = new Student( );
c. Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);
d. new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33);
e. new Student(s1);






Your Answer: C       Correct Answer: C

Explanation: To instantiate a class, the object is assigned the value returned by calling the constructor preceded by the reserved word new, as in new Student( ). The constructor might require parameters, and for Student, the parameters must be are two String values, a double, followed by an int.




9

Assume that another method has been defined that will compute and return the student's class rank (Freshman, Sophomore, etc). It is defined as:

public String getClassRank( )
Given that s1 is a student, which of the following would properly be used to get s1's class rank?
 a. s1 = getClassRank( );
b. s1.toString( );
c. s1.getHours( );
d. s1.getClassRank( );
e. getClassRank(s1);






Your Answer: D       Correct Answer: D

Explanation: To call a method of an object requires passing that object a message which is the same as the method name, as in object.methodname(parameters). In this situation, the object is s1, the method is getClassRank, and this method expects no parameters. Answers a and e are syntactically illegal while answer b returns information about the Student but not his/her class rank, and there is no "getHours" method so c is also syntactically illegal.




10

Another method that might be desired is one that updates the Student's number of credit hours. This method will receive a number of credit hours and add these to the Student's current hours. Which of the following methods would accomplish this?

a. public int updateHours( )
{
return hours;
}

b. public void updateHours( )
{
hours++;
}

c. public updateHours(int moreHours)
{
hours += moreHours;
}

d. public void updateHours(int moreHours)
{
hours += moreHours;
}

e. public int updateHours(int moreHours)
{
return hours + moreHours;
}






Your Answer: D       Correct Answer: D

Explanation: This method will receive the number of new hours and add this to the current hours. The method in d is the only one to do this appropriately. Answer c is syntactically invalid since it does not list a return type. The answer in e returns the new hours, but does not reset hours appropriately.




11

The Coin class, as defined in Chapter 4, consists of a constructor, and methods flip, isHeads and toString. The method isHeads returns true if the last flip was a Heads, and false if the last flip was a Tails. The toString method returns a String equal to "Heads" or "Tails" depending on the result of the last flip. Using this information, answer questions 16 – 17



What does value in the following code compute?

int num = 0;
for(int j = 0; j < 1000; j++)
{
c.flip( );
if(c.isHeads()) num++;
}
double value = (double) num / 1000;

a. the number of Heads flipped out of 1000 flips
b. the number of Heads flipped in a row out of 1000 flips
c. the percentage of heads flipped out of 1000 flips
d. the percentage of times neither Heads nor Tails were flipped out of 1000 flips
e. nothing at all






Your Answer: A       Correct Answer: C

Explanation: The code iterates 1000 times, flipping the Coin and testing to see if this flip was a 0 ("Heads") or 1 ("Tails"). The variable num counts the number of Heads and the variable value is then the percentage of Heads over 1000.




12

Use the following information to answer questions 19 - 20. The Die class from chapter 4 has two constructors defined as follows. Assume MIN_FACES is an int equal to 4.

public Die( )
{
numFaces = 6;
faceValue = 1;
}

public Die(int faces)
{
numFaces = faces;
faceValue = 1;
}
12)	The instruction Die d = new Die(10); results in
 a. The Die d having numFaces = 6 and faceValue = 1
b. The Die d having numFaces = 10 and faceValue = 1
c. The Die d having numFaces = 10 and faceValue = 10
d. The Die d having numFaces = 6 and faceValue = 10
e. A syntax error






Your Answer: B       Correct Answer: B

Explanation: Since an int parameter is passed to the constructor, the second constructor is executed, which sets numFaces = 10 (since numFaces >= MIN_FACES) and faceValue = 1.




13

The instruction Die d = new Die(10, 0); results in

a. The Die d having numFaces = 6 and faceValue = 1
b. The Die d having numFaces = 10 and faceValue = 1
c. The Die d having numFaces = 10 and faceValue = 10
d. The Die d having numFaces = 6 and faceValue = 10
e. A syntax error






Your Answer: E       Correct Answer: E

Explanation: The Die class has two constructors, one that receives no parameters and one that receives a single int parameter. The instruction above calls the Die constructor with 2 int parameters. Since no constructor matches this number of parameters exists, a syntax error occurs.




14

For questions 14 - 16, use the following class definition:

public class Swapper
{
private int x;
private String y;
public int z;

public Swapper(int a, String b, int c)
{
x = a;
y = b;
z = c;
}

public String swap( )
{
int temp = x;
x = z;
z = temp;
return y;
}

public String toString( )
{
if (x < z) return y;
else return "" + x + z;
}
}
14)	If the instruction Swapper s = new Swapper(0, "hello", 0); is executed followed by s.toString( ); what value is returned from s.toString( )?
 a. "hello"
b. "hello00"
c. "00"
d. "0"
e. 0






Your Answer: C       Correct Answer: C

Explanation: The toString method compares x and z, and if x < y it returns the String y. In this case, x == z, so the else clause is executed, and the String of "" + x + z is returned. This is the String "00".




15

Which of the following criticisms is valid about the Swapper class?

a. The instance data x is visible outside of Swapper
b. The instance data y is visible outside of Swapper
c. The instance data z is visible outside of Swapper
d. All 3 instance data are visible outside of Swapper
e. None of the methods are visible outside of Swapper






Your Answer: C       Correct Answer: C

Explanation: We would expect none of the instance data to be visible outside of the class, so they should all be declared as "private" whereas we would expect the methods that make up the interface to be visible outside of the class, so they should all be declared as "public". We see that z is declared "public" instead of "private".




16

If we have Swapper r = new Swapper (5, "no", 10); then r.swap( ); returns which of the following?

a. nothing
b. "no"
c. "no510"
d. "510"
e. "15"






Your Answer: B       Correct Answer: B

Explanation: The swap method swaps the values of x and z (thus x becomes 10 and z becomes 5) and returns the value of y, which is "no" for r.




17

Consider a method defined with the header: public void foo(int a, int b). Which of the following method calls is legal?

a. foo(0, 0.1); 
b. foo(0 / 1, 2 * 3);
c. foo(0);
d. foo( );
e. foo(1 + 2, 3 * 0.1);






Your Answer: B       Correct Answer: B

Explanation: The only legal method call is one that passes two int parameters. In the case of answer b, 0 / 1 is an int division (equal to 0) and 2 * 3 is an int multiplication. So this is legal. The answers for a and e contain two parameters, but the second of each is a double. The answers for c and d have the wrong number of parameters.




18

Consider a method defined with the header: public void doublefoo(double x). Which of the following method calls is legal?

a. doublefoo(0);
b. doublefoo(0.555);
c. doublefoo(0.1 + 0.2);
d. doublefoo(0.1, 0.2);
e. a, b, and c






Your Answer: E       Correct Answer: E

Explanation: In the case of a, the value 0 (an int) is widened to a double. In the case of c, the addition is performed yielding 0.3 and then doublefoo is called. The parameter list in d is illegal since it contains two double parameters instead of 1.




19

For the free response questions, write the requested portions of a class called BaseballPlayer. This class contains the following instance variables:

   private String name;
private String position;
private int numAtBats;
private int numSingles;
private int numDoubles;
private int numTriples;
private int numHomeRuns;
private double battingAverage;
Write the constructor, which is passed the player's name and position.  






Your Answer:
public BaseballPlayer (String playerName, playerPosition)
{
name = playerName;
position = playerPosition;
}


Correct Answer:
 public BaseballPlayer(String newName, String newPosition)
{
name = newName;
position = newPosition;
numAtBats = 0;
numSingles = 0;
numDoubles = 0;
numTriples = 0;
numHomeRuns = 0;
battingAverage = 0.0
}









20

Write a method that computes the player's batting average, which is the total number of hits (singles, double, triples, home runs) divided by the number of at bats.







Your Answer:
public void batAvg ()
{
battingAverage = ((numSingles+numDoubles+numTriples+numHomeRuns)/(numAtBats))
}


Correct Answer:
public void computeBattingAverage( )
{
battingAverage = (numSingles + numDoubles + numTriples + numHomeRuns) / (double) numAtBats;
}









21

Write a toString method that returns the player's name, position and batting average.







Your Answer:
public String toString ()
{
return (name+" "+position+" "+battingAverage);
}


Correct Answer:
public String toString( )
{
return name + "\t" + position + "\t" + battingAverage;
}















(2) Comments

  1. Anonymous On November 30, 2015 at 8:10 AM

    Did you make these questions or did you find them in a text book?

     
    Unknown On August 26, 2017 at 12:51 PM

    hello.. how to create statement for credit hours on setter method if mark we key in "A" automatically the credit hours is 4.0..
    and automatically calculate it... can you explain for me and show me a coding?? i hope you can.. thank you

     

Post a Comment