Absolute Java 5th Edition By Walter Savitch – Test Bank
To Purchase
this Complete Test Bank with Answers Click the link Below
https://tbzuiqe.com/product/absolute-java-5th-edition-by-walter-savitch-test-bank-2/
If face any problem or
Further information contact us At tbzuiqe@gmail.com
Sample
Questions
Chapter 4
Defining Classes I
·
n Multiple Choice
o The
new operator:
§ allocates
memory
§ is
used to create an object of a class
§ associates
an object with a variable that names it.
§ All
of the above.
§ D
§ A
method that performs some action other than returning a value is called a
__________ method.
§ null
§ void
§ public
§ private
§ B
§ The
body of a method that returns a value must contain at least one _________
statement.
§ void
§ invocation
§ thows
§ return
§ D
§ A variable
whose meaning is confined to an object of a class is called:
§ instance
variable
§ local
variable
§ global
variable
§ none
of the above
§ A
§ A
variable whose meaning is confined to a method definition is called an/a
§ instance
variable
§ local
variable
§ global
variable
§ none
of the above
§ B
§ In
Java, a block is delimited by:
§ ( )
§ /* */
§ “ “
§ { }
§ D
§ In
Java, call-by-value is only used with:
§ objects
§ primitive
types
§ this
§ all
of the above
§ B
§ The
parameter this refers to
§ instance
variables
§ local
variables
§ global
variables
§ the
calling object
§ D
§ When
you want the parameters in a method to be the same as the instance variables
you can use the _____ parameter.
§ String
§ hidden
§ default
§ this
§ D
§ Two
methods that are expected to be in all Java classes are:
§ getName
and setName
§ toString
and equals
§ compareTo
and charAt
§ toLowerCase
and toUpperCase
§ B
§ A
program whose only task is to test a method is called a:
§ driver
program
§ stub
§ bottom-up
test
§ recursive
method
§ A
§ Java
has a way of officially hiding details of a class definition. To hide details,
you mark them as _________.
§ public
§ protected
§ private
§ all
of the above
§ C
§ A set
method is:
§ an
accessor method
§ a
mutator method
§ a
recursive method
§ none
of the above
§ B
§ Accessor
methods:
§ return
something equivalent to the value of an instance variable.
§ promotes
abstraction
§ both
A and B
§ none
of the above
§ C
§ A
more eloquent approach in implementing mutator methods is to return a ________
value.
§ int
§ char
§ boolean
§ double
§ C
§ A
_________ states what is assumed to be true when the method is called.
§ prescript
§ postscript
§ precondition
§ postcondition
§ C
§ The
name of a method and the list of ________ types in the heading of the method
definition is called the method signature.
§ parameter
§ argument
§ return
§ primitive
§ A
§ n True/False
§ An
object of class A is an instance of class A.
§ True
§ An
invocation of a method that returns a value can be used as an expression any
place that a value of the Type_Returned can be used.
§ True
§ The
Java language supports global variables.
§ False
§ In a
method invocation, there must be exactly the same number of arguments in
parentheses as there are formal parameters in the method definition heading.
§ True
§ When
you give a command to run a Java program, the runtime system invokes the class
constructor.
§ False
§ Inside
a Java method definition, you can use the keyword this as a name for the
calling object.
§ True
§ Boolean
expressions may be used to control if-else or while statements.
§ True
§ The
modifier private means that an instance variable can be accessed by name
outside of the class definition.
§ False
§ It is
considered good programming practice to validate a value passed to a mutator
method before setting the instance variable.
§ True
§ Mutator
methods can return integer values indicating if the change of the instance
variable was a success.
§ False
§ Method
overloading is when two or more methods of the same class have the same name
but differ in number or types of parameters.
§ True
§ Java
supports operator overloading.
§ False
§ Only
the default constructor has the this
§ False
§ n Short
Answer/Essay
§ Write
a Java method that prints the phrase “Five purple people eaters were seen
munching Martians”.
§
/** void method that prints a phrase */
public void printPhrase()
{
System.out.println(“Five purple people eaters were seen munching
Martians!”);
}
·
What is the purpose of the new operator?
o The
new operator is used to create an object of a class and associate the object
with the variable that names it. Memory is also allocated for that object.
·
Write a Java method that returns the value of PI, where PI =
3.1415926535.
o
/** method that returns the value of PI */
public double piValue()
{
return 3.1415926535;
}
·
Define the terms arguments and parameters. How are they
different?
o Parameters,
also referred to as formal parameters, are used to define the list of variables
included in the parenthesis in a method signature. An argument is the value
actually sent to the method upon invocation. The terms parameters and
argument are very similar in meaning and are often used interchangeably by
programmers. The terms differ slightly in that an argument is the actual
value that replaces the parameter within the method.
·
Write a method called power the computes xn where
x and n and positive integers. The method has two integer parameters and
returns a value of type long.
o
/** x and n are nonnegative integers */
public long power(int x, int n)
{
long result = 1;
/** check for positive numbers */
if((x >= 0) && (n >= 0))
{
/** raise x to the nth power */
for(int i = n; i > 0; i–)
result *= x;
}
else
{
result = 0;
System.out.println(“Fatal error…….positive integers required!”);
}
return result;
}
·
Write a method called Greeting that displays a personalized
greeting given a first name.
o
/** method to display a personalized greeting */
public static void greeting(String name)
{
System.out.println(“Hello ” + name);
}
·
Discuss a situation in which it is appropriate to use the this
o One
common situation that requires the use of the this parameter occurs when you
have parameters of the method with the same name as the instance variables of
the class. Otherwise, if an instance variable is referenced directly by name
within its class, this is
understood.
·
Write a method called isEqual that returns a Boolean value. The
method compares two integers for equality.
o
/** method to compare to integers for equality */
public static boolean isEqual(int x, int y)
{
return x == y;
}
·
Discuss the public and private modifiers in context of methods
and instance variables.
o The
modifiers public and private can both be used with methods and instance
variables. Any instance variable can be labeled either public or private. The
modifier public means that there are no restrictions on where the instance
variable can be used. The modifier private means that the instance
variable cannot be accessed by name outside of the class definition.
The modifiers public and private before a method definition have
a similar meaning. If the method is labeled public, there are no
restrictions on its usage. If the method is labeled private, the method
can only be used in the definition of another method of the same class.
Normal good programming practices require that all instance
variables be private and typically most methods be public.
·
Create a class named Appointment that contains instance variables
startTime, endTime, dayOfWeek (valid values are Sunday through Saturday), and a
date which consists of a month, day and year. All times should be in military
time, therefore it is appropriate to use integers to represent the time.
Create the appropriate accessor and mutator methods.
o
public class Appointment
{
private int startTime;
private int endTime;
private String dayOfWeek;
private String month;
private int day;
private int year;
/** Mutator methods */
public void setStartTime(int st)
{
/** valid range for military time is 0-2400 */
if((st >= 0) && (st <= 2400))
startTime = st;
}
public void setEndTime(int et)
{
/** valid range for military time is 0-2400 */
if((et >= 0) && (et <= 2400))
endTime = et;
}
public void setDayOfWeek(String dow)
{
/** Valid values are the strings Sunday – Saturday */
if(checkDayOfWeek(dow))
dayOfWeek = dow;
else
System.out.println(“Fatal error….invalid day of week value!”);
}
public void setMonth(String m)
{
/** Valid values are strings January – December */
if(checkMonth(m))
month = m;
else
System.out.println(“Fatal error…invalid month value!”);
}
public void setDay(int d)
{
/** Valid days in a date are the integers 1 – 31 */
if((d >= 1) && (d <= 31))
day = d;
}
public void setYear(int y)
{
if(y >= 0)
year = y;
}
/** Accessor methods */
public int getStartTime()
{
return startTime;
}
public int getEndTime()
{
return endTime;
}
public String getDayOfWeek()
{
return dayOfWeek;
}
public String getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int getYear()
{
return year;
}
/** Facilitator methods */
private boolean checkDayOfWeek(String d)
{
if(d.equalsIgnoreCase(“Sunday”) || d.equalsIgnoreCase(“Monday”)
||
d.equalsIgnoreCase(“Tuesday”) || d.equalsIgnoreCase(“Wednesday”)
||
d.equalsIgnoreCase(“Thursday”) || d.equalsIgnoreCase(“Friday”)
||
d.equalsIgnoreCase(“Saturday”) || d.equalsIgnoreCase(“Sunday”))
return true;
else
return false;
}
private boolean checkMonth(String month)
{
if(month.equalsIgnoreCase(“January”) ||
month.equalsIgnoreCase(“February”) ||
month.equalsIgnoreCase(“March”) ||
month.equalsIgnoreCase(“April”) ||
month.equalsIgnoreCase(“May”) || month.equalsIgnoreCase(“June”)
||
month.equalsIgnoreCase(“July”) ||
month.equalsIgnoreCase(“August”) ||
month.equalsIgnoreCase(“September”) ||
month.equalsIgnoreCase(“October”) ||
month.equalsIgnoreCase(“November”) ||
month.equalsIgnoreCase(“December”))
return true;
else
return false;
}
}
·
Discuss the importance of accessor and mutator methods and how
they apply to the abstraction concept.
o It is
considered good programming practice to make all instance variables private,
but there are times when the values of the instance variables need to be
accessed or updated. Accessor methods allow you to obtain the data, while
mutator methods allow you to change the data in a class object. A mutator
method can verify the validity of a value before updating the instance
variable.
·
Write preconditions and postconditions for the power method you
wrote in question #4.
o
/** Precondition: All instance variables of the calling
object have values.
Postcondition: A positive integer value equal to x to the
power n is returned.
*/
·
Add two constructors to the Appointment class created in
question #9. Include a default constructor and a constructor to initialize an
Appointment to suitable arguments.
o
/** Class constructors */
public Appointment()
{
startTime = 0;
endTime = 0;
dayOfWeek = “”;
month = “”;
day = 0;
year = 0;
}
public Appointment(int st, int et, String dow, String m, int d,
int y)
{
setStartTime(st);
setEndTime(et);
setDayOfWeek(dow);
setMonth(m);
setDay(d);
setYear(y);
}
·
Write a complete Java class that uses the console window to
prompt the user for a sentence to parse. Use the StringTokenizer class to echo
the tokens back to the console.
import java.util.Scanner;
import java.util.StringTokenizer;
public class Parser
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
System.out.print(“Enter a sentence and I’ll display each word
you entered: “);
String sentence = keyboard.nextLine();
//Parse the string into tokens and echo back to the user
StringTokenizer tk = new StringTokenizer(sentence, ” “);
System.out.println(“Here are the tokens: “);
while(tk.hasMoreTokens())
{
System.out.println(tk.nextToken());
}
}
}
·
Write a Java class that represents a Student with instance
variables name, id, and gpa. Include constructors, accessor, mutator and any
facilitator methods you may need.
o
public class Student
{
private String name;
private String id;
private double gpa;
/** Constructors */
public Student()
{
name = null;
id = null;
gpa = 0.0;
Comments
Post a Comment