JAVA FOUNDATIONS 3rd Ed By JOHN LEWIS – Test Bank

$20.00

Pay And Download
Complete Test Bank With Answers
 
 
Sample Questions Posted Below

 

 

 

 

Chapter 5: Writing Classes

 

Multiple Choice Questions:

 

1)  A ________________ diagram helps us visualize the contents of and relationships among the classes of a program.

 

  1. a) class and object
  2. b) UML
  3. c) object-oriented
  4. d) public
  5. e) private

 

Answer: b

Explanation:  A UML diagram helps us visualize the contents and relationships among the classes of a program.  The other choices do not refer to any type of diagram.

 

2)  Which of the following object-oriented principles refers to the fact that an object should have its data guarded from inappropriate access?

 

  1. a) encapsulation
  2. b) inheritance
  3. c) polymorphism
  4. d) instance variables
  5. e) methods

 

Answer:  a

Explanation:  Encapsulation is the object-oriented principle that specifies that an objects data should be guarded from inappropriate access.  Therefore choice a is correct.  Inheritance and polymorphism are features of object-oriented programming that allow for class flexibility and re-use.  Instance variables and methods play important roles in object-oriented programming, but are not fundamental principles.

 

3)  When applied to instance variables, the ________________ visibility modifier enforces encapsulation.

 

  1. a) static
  2. b) final
  3. c) public
  4. d) private
  5. e) none of the above

 

Answer:  d

Explanation:  The private visibility modifier guards against inappropriate data access, and therefore promotes encapsulation.  Choices a and b are not visibility modifiers, and choice c is a visibility modifier that allows public access to an objects data, which violates the principle of encapsulation.

 

4)  Which of the following types of methods do not have any return type (not even a void return type)?

 

  1. a) methods declared as static
  2. b) methods declared as public
  3. c) methods declared as private
  4. d) constructors
  5. e) all of the above have return types

 

Answer: d

Explanation:  Constructors are the only methods that do not have any return type.  They do not even have a void return type.  All of the other methods must specify a return type or be declared as void.

 

5)  Which of the following method headers is most likely a header for a mutator method?

 

  1. a) public int getAge()
  2. b) public double computeSalary()
  3. c) public Person()
  4. d) public void setAge(int newAge)
  5. e) none of these are headers for a mutator method

 

Answer: d

Explanation:  Mutators are methods that change the value of an instance variable, and are often referred to as “setters.”  Therefore, choice d is the correct answer.  Choice a is an example of a header for a accessor method, often referred to as a “getter.”  Choice c is a constructor, and choice b is a class method.

 

6)  A _______________ variable is shared among all instances of a class.

 

  1. a) static
  2. b) final
  3. c) public
  4. d) private
  5. e) none of the above

 

Answer: a

Explanation:  A static variable is shared among all instances of a class.  A final variable is a constant, a public variable is one that is accessible from outside the object and a private variable is one that cannot be accessed outside of the object.

 

7)  __________________ parameters are the values that are used when calling a method.

 

  1. a) formal
  2. b) actual
  3. c) useful
  4. d) informal
  5. e) none of the above

 

Answer: b

Explanation:  Actual parameters are sent in when calling a method.  Formal parameters are used when defining a method.

 

8)  The ________________ reference always refers to the currently executing object.

 

  1. a) null
  2. b) static
  3. c) final
  4. d) actual
  5. e) this

 

Answer: e

Explanation:  The this reference always refers to the currently executing object.  A null reference is a reference that is not pointing to any object.  The other three choices are not special references in Java.

 

9)  A method that has multiple definitions is an __________________ method.

 

  1. a) overloaded
  2. b) overridden
  3. c) overlooked
  4. d) overclocked
  5. e) none of the above

 

Answer: a

Explanation:  A method that has multiple definitions is an overloaded method.  The versions of an overloaded method are distinguished by the number, type and order of their parameters.  Overridden methods are methods that have been redefined later in an inheritance hierarchy.  They will be studied in more detail later.  Choice c and d are not types of methods in Java.

 

10)  A(n) ________________ is a step-by-step process for solving a problem.

 

  1. a) UML diagram
  2. b) aggregate object
  3. c) class
  4. d) algorithm
  5. e) none of the above

 

Answer: d

Explanation:  An algorithm is a step-by-step solution for solving a problem.  A UML diagram is a way of visually representing how classes and objects interact.  An aggregate object is an object that is composed, in part, of other objects.  A class can be thought of as a blueprint for a set of objects.

 

11)  All methods (with the exception of constructors) must specify a return type.  What is the return type for a method that does not return any values?

 

  1. a) int
  2. b) public
  3. c) double
  4. d) void
  5. e) none of the above

 

Answer: d

Explanation:  Methods that do not need to return any data should have void specified as the return type.  A method cannot have public specified as its return type, so choice b is incorrect.  Choice a and choice c specify a return type, and therefore they must return data of that type.

 

12)  Methods that can be called directly through the class name and do not need to have an object instantiated are called _________________.

 

  1. a) final
  2. b) public
  3. c) static
  4. d) private
  5. e) none of the above

 

Answer: c

Explanation: Methods that can be called directly through the class name must be declared as static.  Choice b and choice d are visibility modifiers for a method.  Methods declared as final cannot be overridden.

 

13)  A(n) ___________________ object is one that is made up, at least in part, of other objects.

 

  1. a) static
  2. b) aggregate
  3. c) encapsulated
  4. d) private
  5. e) public

 

Answer: b

Explanation: An aggregate object is one that is made up of other objects.  Choice a, d and e do not refer to types of objects.  Encapsulated objects may be made up of primitive types or object types.

 

14)  If a service is so complex that it cannot be reasonably be implemented using one method, it is often helpful to decompose it to make use of ________________ support methods.

 

  1. a) static
  2. b) aggregate
  3. c) private
  4. d) public
  5. e) final

 

Answer: c

Explanation: Private support methods are useful when a service is too complex to be defined in a single method.  Therefore choice d is correct.

 

15)  The versions of an overloaded method are distinguished by ___________________________.

 

  1. a) the number, type and order of their parameters
  2. b) their identifiers
  3. c) their classes
  4. d) the number and type of their parameters
  5. e) the number of their parameters

 

Answer: a

Explanation:  Overloaded methods are two methods in the same class that have the same identifier, but a different number, type or order of parameters.  Therefore, choice a is correct and the rest are incorrect.
True/False Questions:

 

1)   A variable can always be referenced anywhere in a program.

Answer:  False

Explanation:  The scope of a variable determines where it can be referenced.  It depends on where the variable is declared.

 

2)   An object can be thought of as a blueprint for a set of classes.

Answer: False

Explanation: A class can be thought of as a blueprint for a set of objects; not the other way around.

 

3)   A return statement is not required at the end of every method.

Answer:  True

Explanation:  A return statement is not required at the end of a constructor or any method declared with the return type void.

 

4)   When an object is passed to a method, the actual and formal parameters become aliases.

Answer: True

Explanation:  Actual parameters are the datum that are sent into the method.  The formal parameters are used in the method definition.  When objects are sent to a method, both of these values are references, and they become aliases of one another.

 

5)  There are times when it is appropriate to return data from a method of a type that is inconsistent with the return type specified in the method header.

Answer:  False

Explanation:  The value returned from a method must be consistent with the return type specified in the method header.

 

6)   A main method can only access static or local variables.

Answer:  True

Explanation:  A main method cannot access non-static and non-local variables because it is a static method.  In particular, it cannot access any variables declared at the class level.

 

7)   In a class that has variables called height and width, methods called getHeight() and getWidth() are examples of accessor methods.

Answer:  True

Explanation:  Accessor methods get the value of instance variables.  In contrast, mutators set the values.

 

8)   A programmer is required to define a constructor for every class.

Answer:  False

Explanation:  Every class automatically has a default constructor that doesn’t take any parameters.

 

9)   Variables that are declared as static are shared among all instances of a class.

Answer:  True

Explanation:  Static variables are sometimes called class variables because they are shared among all instances of a class.

 

10)  Aggregation is sometimes described as a has-a relationship.

Answer:  True

Explanation: Aggregate objects are made up of other objects.  Therefore the relationship between an object that is an aggregate of other objects is often described as a has-a relationship.
Short Answer Questions:

 

1)  What is the difference between an object and a class?

 

Answer:  A class can be thought of as the blueprint for an object.  In other words, the object is the embodiment of a class.

 

2)  Explain the difference between actual parameters and formal parameters.

 

Answer:  Formal parameters are used in the definition of a method, while actual parameters are the values that are used in the method call.

 

3)  What is encapsulation?  How can it be enforced in Java?

 

Answer:  Encapsulation is the principle that objects should be self-governing.  In other words, an object’s internal data should be protected from outside access.  Encapsulation can be enforced in Java by making instance variables private.

 

4)  Write a method called maxOfThree that accepts three integer parameters and returns the largest of the three.

 

Answer:

 

public int maxOfThree(int firstInt, int secondInt, int thirdInt)  {

if(firstInt >= secondInt && firstInt >= thirdInt)

return firstInt;

else if(secondInt >= firstInt && secondInt >= thirdInt)

return secondInt;

else

return thirdInt;

}

 

5)  Write a method called randomInRange that takes in two numbers representing a range.  Print an error message and return zero if the second parameter is less than the first.  Otherwise, the method should return a randomly generated integer in that range (inclusive).  You may assume that the class has a static Random object called generator already declared and instantiated.

 

Answer:

 

public int randomInRange(int a, int b)  {

if(b < a)  {

System.out.println(“Error, invalid range!”);

return 0;

}

 

return generator.nextInt(b – a + 1) + a;

}

 

6)  Write a method called isPalindrome that accepts a String as a parameter and returns true if the String is a palindrome, and false otherwise.  You may assume that the entered String consists entirely of lowercase letters (meaning it contains no numbers, spaces, punctuation, etc).  Hint:  write code that creates a new string that is the original string reversed, and then check to see if the two strings are equal.

 

Answer:

 

public boolean isPalindrome(String s)  {

String reverseS = new String(“”);

 

for(int i = s.length()-1; i >= 0; i–)

reverseS += s.charAt(i);

 

return reverseS.equals(s);

}

 

7)  Write a method called countSpaces that takes in a String as a parameter and returns an integer that represents the number of space characters (‘ ‘) contained in the string, false otherwise..

 

Answer:

 

public int countSpaces(String s)  {

int count = 0;

for(int i = 0; i < s.length(); i++)

if(s.charAt(i) == ‘ ‘)

count++;

 

return count;

}

 

8)  Write a method called containsPair that takes in three integer parameters and returns true if any two of the input parameters are the same.

 

Answer:

 

public boolean containsPair(int a, int b, int c)  {

return (a == b || a == c || b == c);

}

 

9)  Write a method called threeOfAKind that takes in three integer parameters and returns true if all three of them are the same, false otherwise.

 

Answer:

 

public boolean threeOfAKind(int a, int b, int c)  {

return (a == b && b == c);

}

 

10)  Write a method called randomAverage that generates 100 random integers in the range 1 to 100 (inclusive) and returns their average.  You may assume that the class has a static Random object called generator already declared and instantiated.

 

Answer:

 

public double randomAverage()  {

int sum = 0;

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

sum += generator.nextInt(100) + 1;

 

return (double) sum/100.0;

}

 

11)  Explain why a static method cannot refer to an instance variable.

 

Answer:  Static methods may be called without instantiating an object.  Therefore, an instance variable may not have an assigned value when a static method is called, so a static method cannot reference it.

 

12)  Write a method that prints your name, age, and major.  The method should accept no parameters and return no value.

 

Answer:

 

public void myInfo()  {

System.out.println(“Name:\tJohn Smith”);

System.out.println(“Age:\t30”);

System.out.println(“Major:\tBasket Weaving”);

}

 

 

13)  Explain why method overloading is useful.

 

Answer:  Method overloading is useful because it allows multiple methods to have the same name as long as they have different parameter types.  This allows for more flexibility in choosing identifiers for methods, since the alternative would require the programmer to have different method identifiers for every method that took in a different type of parameter.

 

14)  Consider the following method.

 

public void changeValues(int i, Card c)  {

i = 15;

c.setSuit(“Clubs”);

}

 

Now consider the following snippet of code that calls the method defined above.

 

int num = 12;

Card fiveOfSpades = new Card(5, “Spades”);

 

System.out.println(“Before method call:”);

System.out.println(num);

System.out.println(fiveOfSpades.getSuit());

 

changeValues(num, fiveOfSpades);

 

System.out.println(“After method call:”);

System.out.println(num);

System.out.println(fiveOfSpades);

 

What is the output of this code?

 

Answer:

 

Before method call:

12

Spades

After method call:

12

Clubs

 

15)  Write a method called square that takes in an integer value and returns the integer squared.  Your method should use the Math.pow() method.

 

Answer:

 

public int square(int a)  {

return Math.pow(a, 2);

}

 

 

There are no reviews yet.

Add a review

Be the first to review “JAVA FOUNDATIONS 3rd Ed By JOHN LEWIS – Test Bank”

Your email address will not be published. Required fields are marked *

Category:
Updating…
  • No products in the cart.