A+ Answers
Instance and core variables
Volatile and non-volatile variables
Instance and class variables
Inheritance and class variables
Question 2.2. Which is the correct syntax for a mutator method? (Points : 1)
public int getWeight()
{
return this.weight;
}
public boolean isDeclawed()
{
return this.declawed;
}
public void getWeight()
{
return this.weight;
}
public void setWeight(double weight)
{
this.weight = weight;
}
Question 3.3. Which statement shows the correct way to declare and initialize a named constant that represents absolute zero? (Points : 1)
final int absoluteZero = -273.15;
final double absoluteZero = -273.15;
final int ABSOLUTE_ZERO = -273.15;
final double ABSOLUTE_ZERO = -273.15;
Question 4.4. In the following class:
public class Height
{
private double height;
private String unitsOfMeasurement;
//************************
public Height()
{
this.unitsOfMeasurement = "in";
}
public void setHeight(double height)
{
this.height = height;
this.unitsOfMeasurement = "cm";
}
public void setHeight(double height, String unitsOfMeasurement)
{
this.height = height;
this.unitsOfMeasurement = unitsOfMeasurement;
}
public void print()
{
System.out.println(this.height + " " + this.unitsOfMeasurement);
}
} // end class Height
Which method is the constructor? (Points : 1)
The print method
The Height method
The setHeight method
This class does not implement a constructor.
Question 5.5. Java primitive types include _________. (Points : 1)
integer, double, string, boolean, long and float
integer, double, float, array, boolean and long
long, integer, string, array, float, double and character
long, double, character, float, integer and boolean
Question 6.6. What is the name for the circled items in the following statement?What is the name for the circled items in the following statement?
(Points : 1)
modulus specifier
format specifier
flags specifier
precision specifier
Question 7.7. Which Java operator is used for concatenation? (Points : 1)
&&
+
.
||
Question 8.8. Given the following code fragment (presume there is a class called Car implemented):
…
Car mazdaCar = new Car();
Car subaruCar = new Car();
mazdaCar.setYear(2008);
mazdaCar.setColor(“black”);
subaruCar = mazdaCar;
subaruCar.setYear(2011);
mazdaCar.printYear();
…
What will display on the screen for mazdaCar’s year when mazdaCar executes printYear()? (Points : 1)
2008
2010
2011
Black
Question 9.9. Given the following method from a class:
…
public double calculateArea(double length, double width)
{ double area; area = length * width; return area; return area;
}
…
What is the scope of the variables length and width? (Points : 1)
Global to the class.
Local to the method.
Global to all objects created from the class.
None of the above.
Question 10.10. In the following code fragment:
…
5 Person person1 = new Person();
6 Person person2 = new Person();
7
8 person1.setName("Bugs Bunny");
9 person2.setName("Daffy Duck");
10 System.out.println(person1.getName() + ", " + person2.getName());
11
12 person1.swapPerson(person2);
13 System.out.println(person1.getName() + ", " + person2.getName());
…
What is being passed to the swapPerson method in line 12: (Points : 1)
an object
an object reference
a class
a class reference
Question 11.11. In the following Java code fragment:
…
int postion1 = 15;
int postion2 = 18;
int distanceApart =
Math.abs(position1 - position2);
…
What type of method is the Math.abs() method? (Points : 1)
Instance method
Class method
Final method
Inherited method
Question 12.12. When a public method needs to access both class members (variables and methods) and instance members (variables and methods), the method must be a / an _________ method. (Points : 1)
instance
helper
class
None of the above.
Question 13.13. In the following class:
public class Height
{
private double height;
private String unitsOfMeasurement;
public Height()
{
this.unitsOfMeasurement = "in";
}
public void setHeight(double height)
{
this.height = height;
this.unitsOfMeasurement = "cm";
}
public void setHeight(double height, String unitsOfMeasurement)
{
this.height = height;
this.unitsOfMeasurement = unitsOfMeasurement;
}
public void print()
{
System.out.println(this.height + " " + this.unitsOfMeasurement);
}
} // end class Height
Which method is overloaded? (Points : 1)
The print method
The Height method
The setHeight method
None of the methods are overloaded.
Question 14.14. What must a method return if the method implements method call chaining? (Points : 1)
The calling object
A class variable
The class
Null
Question 15.15. Which code segment is representative of a properly formatted nested loop? (Points : 1)
while(i<5)
{
…
}
while(j<3)
{
…
}
do
{
while(j<3)
{
…
}
}while(i<5);
do
{
while(j<3)
{
…
}
}while(i<5)
while(i<5)
{
while(j<3)
{
…
}do;
}
Question 16.16. What is the fundamental difference between a do loop and a while loop? (Points : 1)
Code inside a do loop will never be executed.
Code inside a while loop is guaranteed to execute at least once.
Code inside a do loop is guaranteed to execute at least once.
Do loops and while loops are the same.
Question 17.17. What is the meaning of the java statement:
import java.util.Scanner;? (Points : 1)
It tells the java compiler to include the Scanner class from the utility package.
It tells the java compiler to exclude only the Scanner class from the compile process.
It signals the java compiler to scan the source code for errors.
None of the above.
Question 18.18. Given the following code fragment:
…
double interestRate = 0.05;
double balance = 100.5;
int interestInDollars = (int)(interestRate * balance);
System.out.println(interestInDollars);
…
What value will display on the screen? (Points : 1)
5
5.025
5.03
0
Question 19.19. What is the significance of using the access modifier “private” with instance variables? (Points : 1)
It makes instance variables easier to access from outside the object.
Using “private” provides data encapsulation.
It makes the variables inaccessible from within the object.
The “private” access modifier should never be used.
Question 20.20. Given the following code fragment:
…
System.out.println("\"Four score and seven years ago…\"");
…
What will display on the screen? (Points : 1)
“Four score and seven years ago…”
Four score and seven years ago…
\Four score and seven years ago…\
"\"Four score and seven years ago…\""
Question 21.21. Given the following, which is the correct file name and extension to use when saving the file?
public class SomeJavaClass
{
public static void main(String[] args)
{
System.out.print(“Java is ?”);
}//end main
}//end someJavaClass (Points : 1)
someJavaClass.txt
SomeJavaClass.txt
someJavaClass.java
SomeJavaClass.java
Question 22.22. In the context of OOP, objects __________. (Points : 1)
do not encapsulate data
persist forever after being instantiated
contain only accessor methods
have state and behavior
Question 23.23. Given the following code fragment. (presume there is a Hat class with a constructor that requires the hat type, price and color as arguments and there is a accessor method called getPrice that returns the price):
…
Hat hat1 = new Hat(“baseball”, 5.21, “blue”);
int quantityToOrder = 0;
double totalPrice = 0.0;
Scanner userInput = new Scanner(System.in);
System.out.print(“How many hats to order? “);
quantityToOrder = Integer.parseInt(userInput.nextLine());
totalPrice = (hat1.getPrice() * quantityToOrder);
System.out.println(“Order Total = $“ + totalPrice);
…
What will display on the screen when the user orders 4 hats? (Points : 1)
Order Total = $20.84
Order Total = $20
Order Total = $5.21
Order Total = $5
Question 24.24. In the following Java code fragment:
public class SomeClass
{
private static int someVariable;
…
}//end SomeClass
What is the significance of the keyword static? (Points : 1)
It makes the variable someVariable an instance variable
It makes the variable someVariable a class variable
It makes the variable someVariable a local method variable
None of the above
Question 25.25. Given the following code fragment:
…
String name = "Joseph Cool";
System.out.println(name.charAt(5));
…
What will display on the screen? (Points : 1)
h
p
5
e
Question 26.26. Instance methods ___________. (Points : 1)
specify the object attributes (data)
can be used without creating an object
specify the object behaviors
must contain the static keyword in their definition
Question 27.27. Given the following code fragment:
…
int operand1 = 4;
int operand2 = 3;
double result = operand1 / operand2;
System.out.println(result);
…
What is displayed on the screen? (Points : 1)
0
1.0
1.3333333333333333
1
Question 28.28. Given the following code fragment:
…
boolean logicValueA = true;
boolean logicValueB = true;
System.out.println("Boolean Result: " + (logicValueA && logicValueB));
…
What will display on the screen? (Points : 1)
Boolean Result: false
Boolean Result: true
Boolean Result: and
Boolean Result: or
Question 29.29. A class specifically written to use other classes in its main method is called a _________. (Points : 1)
driven class
static class
driver class
main class
Question 30.30. Given the following code fragment:
…
boolean logicValueA = true;
boolean logicValueB = false;
System.out.println("Boolean Result: " + (!logicValueA || logicValueB));
…
What will display on the screen? (Points : 1)
Boolean Result: false
Boolean Result: true
Boolean Result: and
Boolean Result: or
Question 31.31. What is the term for the lower and upper case pattern that must be used for variable and method identifiers? (Points : 1)
straight case
alternating case
camel case
None of the above.
Question 32.32. Given the following code fragment:
…
int x = 5;
int y = 8;
int z = 0;
while(y < 20)
{
z = x + y;
x = y;
y = z;
System.out.println(y);
}
if(y > 22)
{
System.out.println(z);
}
else if(y < 22)
{
System.out.println(x);
}
…
What will display on the screen? (Points : 1)
13
21
13
13
21
21
13
25
13
13
28
28
Question 33.33. Which statement shows the correct way to define a method that does not provide a return value? (Points : 1)
public void displayFraction(int numerator, int denominator)
{
…
}
public int displayFraction(int numerator, int denominator)
{
…
}
public Fraction displayFraction(int numerator, int denominator)
{
…
}
public null displayFraction(int numerator, int denominator)
{
…
}
Question 34.34. The following code generates a compiler error. Which line is causing the error and what is wrong with it?
1 public class Book
2 {
3 private String author;
4 private int numberOfPages;
5 private String title;
6
7 //*******************
8
9 public Book(String author, int numberOfPages, String title)
10 {
11 this.author = author;
12 this.numberOfPages = numberOfPages;
13 this.title = title;
14 }
15
16 public String getAuthor()
17 {
18 return this.author;
19 }
20 }//end Book class
21
22 public class BookDriver
23 {
24 public static void main(String[] args)
25 {
26String bookTitle = "The Relic";
27String bookAuthor = "Douglas Preston";
28int bookTotalPages = 439;
29Book myBook = new Book();
30
31 System.out.println(myBook.getAuthor());
32 }//end main
33 }//end BookDriver (Points : 2)
Question 35.35. The following code generates a compiler error. Which line is causing the error and what is wrong with it?
1 public class TestClass
2 {
3 public static void main(String[] args)
4 {
5 String testString = "Java";
6 for(int i; i < testString.length(); i++)
7 {
8 System.out.println(i);
9 }
10 }//end main
11 }//end TestClass (Points : 2)
Question 36.36. The following code generates a compiler error. Which line is causing the error and what is wrong with it?
1 import java.util.Scanner;
2
3 public class TestClass
4 {
5 public static void main(String[] args)
6 {
7 Scanner userInput = new Scanner(System.in);
8 String inputName;
9
10System.out.print("Enter your name: ");
11Inputname = userInput.nextLine();
12
13System.out.println("Hello " + inputName + "!");
14 }//end main
15 }//end TestClass (Points : 2)
Question 37.37. The following code generates compiler errors. Which line is causing the error and what is wrong with it?
1 public class TestClass
2 {
3 public static void main(String[] args)
4 {
5 char characterToPrint = '*';
6 for(int i = 1; i < 5; i++)
7 {
8 System.out.println(characterToPrint);
9 }
10 System.out.println(i + " stars");
11 }//end main
12 }//end TestClass (Points : 2)
Question 38.38. Write the Hello World program, be precise, include comments, prologue, etc. if needed. (4 points) (Points : 4)
Question 39.39. Write the source code for a class called Temperature that does the following: (5 points)
a. Prompts the user to input a temperature
b. Prints “Too Hot!” when the input temperature is above 75
c. Prints “Too Cold!” when the input temperature is below 40
d. Prints “Just Right!” when the input temperature is 40 to 75
e. Be precise, include comments, prologue, etc. if needed. (Points : 5)
11 years ago
Purchase the answer to view it

- solved228.docx