Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Chapter 5 Defining Classes II – absolute java test bank


 

Download  file with the answers

Not a member!
Create a FREE account here to get access and download this file with answers


Chapter 5
Defining Classes II
 Multiple Choice
1) A static method is one that can be used with a _____________.
(a) instance variable
(b) local variable
(c) global variable
(d) the class name as a calling object

2) Static variables are often used:
(a) in arithmetic expressions
(b) to communicate between objects
(c) within looping structures
(d) all of the above

3) Only ______ copy/copies of a static variable are available to objects of a class.
(a) one
(b) two
(c) three
(d) none of the above

4) All of these are methods of Java’s Math class except:
(a) pow
(b) min
(c) random
(d) toString

5) The Math method that returns the nearest whole number that is greater than or equal to its argument is:
(a) round
(b) ceil
(c) floor
(d) all of the above

6) All of the following are wrapper classes except:
(a) String
(b) Integer
(c) Character
(d) Double

7) Converting from a value of primitive type to a corresponding object of its associated wrapper class is called:
(a) Boxing
(b) Unboxing
(c) Converting
(d) Reinstantiating

8) The conversion from an object of a wrapper class to a value of its associated primitive type is called:
(a) Boxing
(b) Unboxing
(c) Converting
(d) Reinstantiating

9) The method trim of the String class trims off:
(a) Leading white space
(b) Trailing white space
(c) Leading and trailing white space
(d) Blanks

10) An example of secondary memory is:
(a) RAM
(b) ROM
(c) hard disk
(d) all of the above

11) When you use the assignment operator with variables of a class type, you are assigning a:
(a) value
(b) primitive type
(c) local variable
(d) reference

12) null can be used:
(a) to indicate a variable has no real value
(b) in a Boolean expression with ==
(c) as a placeholder
(d) all of the above

13) A copy constructor has _________ parameters.
(a) zero
(b) one
(c) two
(d) three

14) A condition that allows a programmer to circumvent the private modifier and change the private instance variable is called:
(a) a copy constructor
(b) a privacy leak
(c) a class invariant
(d) an anonymous object

15) A class that contains public methods that can change the data in the object of a class is called a/an:
(a) mutable class
(b) immutable class
(c) invariant class
(d) none of the above

16) To create a package, you must add a package statement at the ____________ of each class file.
(a) beginning
(b) end
(c) before each method signature
(d) after the import statements

17) The program included in the Java SDK that allows a programmer to separate the class interface from the class implementation is called:
(a) javac
(b) java
(c) javadoc
(d) none of the above

18) Javadoc requires a comment to be delimited by _________ to be included in the extracted class interface.
(a) // //
(b) /* */
(c) /** */
(d) “ “

 True/False
1) In a static method, you may use the this parameter either explicitly or implicitly.

2) A main method can be placed inside a class definition.

3) You may use methods of the Math class without an import statement.

4) Wrapper classes provide a class type corresponding to each of the primitive types so that you can have class types that behave somewhat like primitive types.

5) All versions of Java support automatic boxing.

6) Wrapper classes are provided for all primitive Java types except Boolean.

7) A bit may have the value of either a 1 or 0.

8) Primitive types are reference types.

9) A class invariant is a statement that is always true for every object of the class.

10) You should avoid the use of null as an argument to a method.

11) The String class is a mutable class.

12) To use a package, the program must contain an import statement that names the package.

13) Deprecated methods should be used in new Java code.

 Short Answer/Essay
1) Write a statement that creates and initializes a static variable named salesTax to 7.59.
Answer: private static double salesTax = 7.59;
2) Write a statement that creates a constant variable named TAX_RATE. The tax rate is 8.25%.
Answer: public static final double TAX_RATE = .0825;
3) Write ONE Java statement that computes and displays the value of 25.
Answer: System.out.println(“The value of 2 raised to the 5th power is ” + Math.pow(2,5));
4) Write ONE Java statement that computes and displays a random number between 1 and 25.
Answer: System.out.println(“Random number between 1 and 25 ” + Math.round((Math.random() * 25)));
5) Define boxing and unboxing.
Answer: Boxing and unboxing are terms that refer to either wrapping up a primitive type or unwrapping an object back into a primitive type. More specifically, boxing is the process of going from a value of a primitive type to an object of its wrapper class. Unboxing is the process of going from an object of a wrapper class to the corresponding value of a primitive type.
6) Write a complete Java program that prompts the user for a number and prints back the integer as well as floating point values to the console.
import java.util.Scanner;
public class WrapperDemo
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
System.out.print(“Enter any number >> “);
String strNumber = keyboard.next();
int intNumber = Integer.parseInt(strNumber);
double dblNumber = Double.parseDouble(strNumber);

System.out.println(“The integer value of ” + strNumber + ” is: ” +
intNumber);
System.out.println(“The floating-point value of ” + strNumber + ” is: ” +
dblNumber);
}
}
7) Write a Java method that returns true if and only if a character is a digit or a letter. The method should display appropriate feedback to the console.
Answer:
public boolean isDigitOrLetter(Character c)
{
if(c.isDigit(c.charValue()) || c.isLetter(c.charValue()))
{
System.out.println(“Returning true”);
return true;
}
else
{
System.out.println(“Returning false”);
return false;
}
}
8) Explain in detail how main memory works.
Answer: The main memory is used by the computer when it is running a program. Values stored in a program’s variables are kept in this main memory as well as the byte-code of the program. Main memory consists of a long list of numbered locations called bytes. The number that identifies the byte is called the address. A data item can be stored in one or more of those bytes, and the address of the byte is then used to locate the data item when it is needed.
9) How many bytes are contained within 16-bits, 32-bits, 64-bits?
Answer: 16-bits contain 2-bytes; 32-bits contain 4-bytes, 64-bits contain 8-bytes.
10) When used with objects, what is the equality ( == ) operator really comparing?
Answer: The == operator does not check that the objects have the same values for instance variables. It checks for equality of memory address, so two objects in two different locations in memory would test as being “not equal” when compared using ==, even if their instance variables contain equivalent data.
11) Does an object created with a copy constructor reference the same memory location that the original object references? Explain.
Answer: If properly coded, a copy constructor should create an object that is separate from the original object, that is each object should have distinct memory locations. The new object created by the copy constructor will have instance variables with the same values as the original object.
12) Explain how a package is named in Java.
Answer: A package name is a form of path name to the directory containing the classes in the package. The name of the package specifies the relative path for the directory that contains the package classes. The \ or / (depending on the operating system being used) is replaced by a dot to form a fully qualified package name.
13) Create a Java class named Book with instance variables title, author, ISBN, and yearPublished. Include javadoc style comments to describe your interface. Such a class would normally have methods, but you are not required to supply any methods.
Answer:
/** Class for a book with title, author, ISBN and year published.
Class invariant: A Book always has a title and author.
*/
public class Book
{
/** Instance variables */
private String title;
private String author;
private String ISBN;
private int yearPublished;
}
14) Add accessor and mutator methods to the Book class created in question #13.
Answer:
/** Class for a book with title, author, ISBN and year published.
Class invariant: A Book always has a title and author.
*/
public class Book
{
/** Instance variables */
private String title;
private String author;
private String ISBN;
private int yearPublished;

/** Accessor methods */
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public String getISBN()
{
return ISBN;
}
public int getYearPublished()
{
return yearPublished;
}
/** Mutator methods */
public void setISBN(String isbn)
{
ISBN = isbn;
}
public void setYearPublished(int y)
{
yearPublished = y;
}
/** Facilitator methods */
public String toString()
{
return (title + ” ” + author + ” ” + ISBN + ” ” + yearPublished);
}
public boolean equals(Book otherBook)
{
if(otherBook == null)
return false;
else
return (title.equals(otherBook.title) &&
author.equals(otherBook.author)
&& ISBN.equals(otherBook.ISBN) &&
yearPublished == otherBook.yearPublished);
}
}
15) Add a constructor and a copy constructor to the Book class created in question #13.
Answer:
public Book(String t, String a)
{
title = t;
author = a;
setISBN(null);
setYearPublished(0);
}
public Book(String t, String a, String i, int y)
{
title = t;
author = a;
setISBN(i);
setYearPublished(y);
}
public Book(Book original)
{
if(original == null)
{
System.out.println(“Fatal error”);
System.exit(0);
}
title = original.title;
author = original.author;
ISBN = original.ISBN;
yearPublished = original.yearPublished;
}
16) What is the purpose of Java’s wrapper classes?
Answer: The wrapper classes contain a number of useful constants and static methods. Wrapper classes accomplish two tasks: they produce class objects corresponding to values of primitive types, and they supply useful constants and methods.
17) Write a complete Java program that prompts the user for a phrase. The program converts and displays the phrase in uppercase letters.
import java.util.Scanner;

public class convertToUpper
{
public static void main(String[] args)
{
//Create a scanner object for input
Scanner keyboard = new Scanner(System.in);
System.out.println(“Enter a phrase and I’ll convert it to uppercase.”);
String phrase = keyboard.nextLine();
System.out.println(phrase.toUpperCase());
}
}
18) Write a complete Java program using the StringTokenizer class that computes and displays the average of a list of grades read from the command line. Each grade should be entered on the same line separated by commas. Enter signifies the end of the input.
import java.util.Scanner;
import java.util.StringTokenizer;
public class tokenAverager
{
public static void main(String[] args)
{
//Local variables
Scanner keyboard = new Scanner(System.in);
int sum = 0;
int count = 0;
double average = 0;
System.out.println(“Enter your grades, placing a space between each grade.”);
System.out.println(“Press enter when you are finished.”);
String grades = keyboard.nextLine();
StringTokenizer tk = new StringTokenizer(grades, ” “);
while(tk.hasMoreTokens())
{
sum += Integer.parseInt(tk.nextToken());
count++;
}
average = sum/count;
System.out.println(“The class average is ” + average);
}
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!