Chapter 4: Writing Classes

 

 

Multiple Choice Questions:

 

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

 

 

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

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

 

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

 

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

 

 

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

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

 

 

8)     A variable whose scope is restricted to the method where it was declared is known as a(n)

a)    parameter

b)   global variable

c)    local variable

d)   public instance data

e)    private instance data

 

 

9)     A classÕ constructor usually defines

a)    how an object is initialized

b)   how an object is interfaced

c)    the number of instance data in the class

d)   the number of methods in the class

e)    if the instance data are accessible outside of the object directly

 

 

10)   Having multiple class methods of the same name where each method has a different number of or type of parameters is known as

a)    encapsulation

b)   information hiding

c)    tokenizing

d)   importing

e)    method overloading

 

 

11)   Instance data for a Java class

a)    are limited to primitive types (e.g., int, double, char)

b)   are limited to Strings

c)    are limited to objects(e.g., Strings, classes defined by other programmers)

d)   may be primitive types or objects, but objects must be defined to be private

e)    may be primitive types or objects

 

 

12)   An example of passing a message to a String where the message has a String parameter occurs in which of the following messages?

a)    length

b)   substring

c)    equals

d)   toUpperCase

e)    none of the above, it is not possible to pass a String as a parameter in a message to a String

 

 

For questions 13-15, 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( )

            {

                        DecimalFormat df = new DecimalFormat("xxxx");          // xxxx needs to be replaced

                        return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours

            }

}

 

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

 

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

 

 

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

     }

 

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

 

16)   A set of code has already instantiated c to be a Coin and has input a String guess, from the user asking whether the user guesses that a coin flip will result in ÒHeadsÓ or ÒTailsÓ.  Which of the following sets of code will perform the coin flip and see if the userÕs guess was right or wrong?

a)    c.flip( );

if(c.isHeads( ).equals(guess)) System.out.println("User is correct");

 

b)   if(c.flip( ).equals(guess)) System.out.println("User is correct");

 

c)    if(c.isHeads( ).equals(guess)) System.out.println("User is correct");

 

d)   c.flip( );

if(c.toString( ).equals(guess)) System.out.println("User is correct");

 

e)    c.flip( ).toString( );

     if(c.equals(guess)) System.out.println("User is correct");

 

 

17)   What does 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;

1)     the number of Heads flipped out of 1000 flips

2)     the number of Heads flipped in a row out of 1000 flips

3)     the percentage of heads flipped out of 1000 flips

4)     the percentage of times neither Heads nor Tails were flipped out of 1000 flips

5)     nothing at all

 

18)   In the Rational class, defined in chapter 4, the methods reduce and gcd are declared to be private.  Why?

a)    Because they will never be used

b)   Because they will only be called from methods inside of Rational

c)    Because they will only be called from the constructor of Rational

d)   Because they do not use any of RationalÕs instance data

e)    Because it is a typo and they should be declared as public

 

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( )                                           public Die(int faces)

{                                                          {

            numFaces = 6;                                        if(faces < MIN_FACES) numFaces = 6;

            faceValue = 1;                                         else numFaces = faces;

}                                                                      faceValue = 1;

                                                            }

 

 

 

 

 

 

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

 

 

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