Intro. to Java Programming, Ninth Edition - Ch.8

Ace your homework & exams now with Quizwiz!

Which of the following statements are true? (choose more than one) A. Use the private modifier to encapsulate data fields. B. Encapsulating data fields makes the program easy to maintain. C. Encapsulating data fields makes the program short. D. Encapsulating data fields helps prevent programming errors.

A. Use the private modifier to encapsulate data fields. B. Encapsulating data fields makes the program easy to maintain. D. Encapsulating data fields helps prevent programming errors.

__________ represents an entity in the real world that can be distinctly identified. A. A class B. An object C. A method D. A data field

B. An object

Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class. public MyClass() { xMethod(); } A. a static method B. an instance method C. a static method or an instance method

C. a static method or an instance method

What is the value of times displayed? public class Test { __public static void main(String[] args) { ____Count myCount = new Count(); ____int times = 0; ____for (int i=0; i<100; i++) ______increment(myCount, times); ___System.out.println( ______"myCount.count = " + myCount.count); ___System.out.println("times = "+ times); } __public static void increment(Count c, int times) { ____c.count++; ____times++; } } class Count { __int count; __Count(int c) { ____count = c; } __Count() { ____count = 1; } } A. 101 B. 100 C. 99 D. 98 E. 0

E. 0

To declare a constant MAX_LENGTH as a member of the class, you write A. final static MAX_LENGTH = 99.98; B. final static float MAX_LENGTH = 99.98; C. static double MAX_LENGTH = 99.98; D. final double MAX_LENGTH = 99.98; E. final static double MAX_LENGTH = 99.98;

E. final static double MAX_LENGTH = 99.98;

Suppose TestCircle1 and Circle1 in Listing 8.1 are in two separate files named TestCircle1.java and Circle1.java, respectively. What is the outcome of compiling TestCircle.java and then Circle.java? A. Only TestCircle1.java compiles. B. Only Circle1.java compiles. *C. Both compile fine.* D. Neither compiles successfully.

*C. Both compile fine.* //I've looked for this "Listing 8.1" but can't find it. Its not mentioned in the book so maybe this was a class assignment.

What is the value of myCount.count displayed? public class Test { __public static void main(String[] args) { ____Count myCount = new Count(); ____int times = 0; ____for (int i=0; i<100; i++) _______increment(myCount, times); ____System.out.println("myCount.count = " + myCount.count); ____System.out.println("times = "+ times); } __public static void increment(Count c, int times) { ____c.count++; ____times++; } } class Count { __int count; __Count(int c) { ____count = c; } __Count() { ____count = 1; } } A. 101 B. 100 C. 99 D. 98

A. 101 //p.312 - An instance variable is tied to a specific instance of the class; it is not shared among objects of the same class. So in this specific case time is an instance variable and count is a passed variable.

_______ is a construct that defines objects of the same type. A. A class B. An object C. A method D. A data field

A. A class

________ is invoked to create an object. A. A constructor B. The main method C. A method with a return type D. A method with the void return type

A. A constructor

Which of the following statements are true? (choose more than one) A. A default constructor is provided automatically if no constructors are explicitly declared in the class. B. At least one constructor must always be defined explicitly. C. Every class has a default constructor. D. The default constructor is a no-arg constructor.

A. A default constructor is provided automatically if no constructors are explicitly declared in the class. D. The default constructor is a no-arg constructor.

The java.util.Date class is introduced in this section. Analyze the following code and choose the best answer: Which of the following code in A or B, or both creates an object of the Date class: A: public class Test { __public Test() { _____new java.util.Date(); } } B: public class Test { __public Test() { _____java.util.Date date = new java.util.Date(); } } (its possible to choose more than one) A. A. B. B. C. Neither

A. A. B. B.

Which of the following statements are true? (choose more than one) A. Local variables do not have default values. B. Data fields have default values. C. A variable of a primitive type holds a value of the primitive type. D. A variable of a reference type holds a reference to where an object is stored in the memory. E. You may assign an int value to a reference variable.

A. Local variables do not have default values. B. Data fields have default values. C. A variable of a primitive type holds a value of the primitive type. D. A variable of a reference type holds a reference to where an object is stored in the memory.

Which of the following statements are true? (choose more than one) A. Multiple constructors can be defined in a class. B. Constructors do not have a return type, not even void. C. Constructors must have the same name as the class itself. D. Constructors are invoked using the new operator when an object is created.

A. Multiple constructors can be defined in a class. B. Constructors do not have a return type, not even void. C. Constructors must have the same name as the class itself. D. Constructors are invoked using the new operator when an object is created.

Analyze the following code: public class Test { __public static void main(String args[]) { _____NClass nc = new NClass(); _____nc.t = nc.t++; } } class NClass { __int t; __private NClass() { } } A. The program has a compilation error because the NClass class has a private constructor. B. The program does not compile because the parameter list of the main method is wrong. C. The program compiles, but has a runtime error because t has no initial value. D. The program compiles and runs fine.

A. The program has a compilation error because the NClass class has a private constructor. //p.321 - Private data can be accessed only within their defining class, so you cannot use myCircle.radius in the client program. A compile error would occur if you attempted to access private data from a client.

Analyze the following code: (quizlet automatically bolds text, just ignore) public class Test { __public static void main(String[] args) { _____double radius; _____final double PI= 3.15169; _____double area = radius ** radius ** PI; _____System.out.println("Area is " + area); } } A. The program has compile errors because the variable radius is not initialized. B. The program has a compile error because a constant PI is defined inside a method. C. The program has no compile errors but will get a runtime error because radius is not initialized. D. The program compiles and runs fine.

A. The program has compile errors because the variable radius is not initialized.

Which of the following statements are correct? (choose more than one) A. When creating a Random object, you have to specify the seed or use the default seed. B. If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical. C. The nextInt() method in the Random class returns the next random int value. D. The nextDouble() method in the Random class returns the next random double value.

A. When creating a Random object, you have to specify the seed or use the default seed. B. If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical. C. The nextInt() method in the Random class returns the next random int value. D. The nextDouble() method in the Random class returns the next random double value.

Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is _________ in the class. public static void main(String[] args) { xMethod(); } A. a static method B. an instance method C. a static method or an instance method

A. a static method //public *static* void main(String[] args) { xMethod(); }

You should add the static keyword in the place of ? in Line ________ in the following code: 1 public class Test { 2 __private int age; 3 4 __public ? int square(int n) { 5 _____return n * n; 6 } 7 8 __public ? int getAge() { 9 } 10} A. in line 4 B. in line 8 C. in both line 4 and line 8 D. none

A. in line 4f //Barely mentioned in the C169 WGU material, so most likely won't be on the test. A static method is not invoked on an object. Sometimes a class defines methods that are not invoked on an object. Such a method is called a static method. A typical example of a static method is the sqrt method in the Math class. *Because numbers aren't objects, you can't invoke methods on them.*

What code may be filled in the blank without causing syntax or runtime errors: public class Test { java.util.Date date; public static void main(String[] args) { Test test = new Test(); System.out.println(_________________); } } A. test.date B. date C. test.date.toString() D. date.toString()

A. test.date

What is the output of the following program? import java.util.Date; public class Test { __public static void main(String[] args) { ____Date date = new Date(1234567); ____m1(date); ____System.out.print(date.getTime() + " "); ____m2(date); ____System.out.println(date.getTime()); } __public static void m1(Date date) { ____date = new Date(7654321); } __public static void m2(Date date) { _____date.setTime(7654321); } } A. 1234567 1234567 B. 1234567 7654321 C. 7654321 1234567 D. 7654321 7654321

B. 1234567 7654321

Which of the following statement is most accurate? (choose more than one) A. A reference variable is an object. B. A reference variable refers to an object. C. An object may contain other objects. D. An object may contain the references of other objects.

B. A reference variable refers to an object. D. An object may contain the references of other objects.

Analyze the following code. public class Test { __public static void main(String[] args) { _____int n = 2; _____xMethod(n); _____System.out.println("n is " + n); } __void xMethod(int n) { _____n++; } } A. The code has a compile error because xMethod does not return a value. B. The code has a compile error because xMethod is not declared static. C. The code prints n is 1. D. The code prints n is 2. E. The code prints n is 3.

B. The code has a compile error because xMethod is not declared static. //p.315 - However, a static method cannot invoke an instance method or access an instance data field, since static methods and static data fields don't belong to a particular object. __public static void main(String[] args) { *//* Static method _____int n = 2; _____xMethod(n); *//* Instance method ... __void xMethod(int n) { *//* Fix this problem by changing this line to: static void xMethod

What is wrong in the following code? class TempClass { _int i; _public void TempClass(int j) { _int i = j; } } public class C { _public static void main(String[] args) { _TempClass temp = new TempClass(2); } } A. The program has a compilation error because TempClass does not have a default constructor. B. The program has a compilation error because TempClass does not have a constructor with an int argument. C. The program compiles fine, but it does not run because class C is not public. D. The program compiles and runs fine.

B. The program has a compilation error because TempClass does not have a constructor with an int argument. //poorly worded question. What they are referencing is the void keyword. If you take out void the program works fine. _public void TempClass(int j) should be _public TempClass(int j) which then makes int j active. You could also remove the 2 but that isn't what its looking for.

Analyze the following code: public class Test { __public static void main(String[] args) { ____A a = new A(); ____a.print(); } } class A { __String s; __A(String newS) { ____s = newS; } __void print() { ____System.out.println(s); } } (choose more than one) A. The program has a compilation error because class A is not a public class. B. The program has a compilation error because class A does not have a no-arg constructor. C. The program compiles and runs fine and prints nothing. D. The program would compile and run if you change A a = new A() to A a = new A("5").

B. The program has a compilation error because class A does not have a no-arg constructor. D. The program would compile and run if you change A a = new A() to A a = new A("5").

An object is an instance of a __________. A. program B. class C. method D. data

B. class

Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true? (choose more than one) A. dates is null. B. dates[0] is null. C. dates = new java.util.Date[5] is fine, which assigns a new array to dates. D. dates = new Date() is fine, which creates a new Date object and assigns to dates.

B. dates[0] is null. C. dates = new java.util.Date[5] is fine, which assigns a new array to dates. //java.util.Date[] dates means its an array with the name dates. So [0] in an array is null and new java.util.Date[5] makes a new array.

What will be displayed by the second println statement in the main method? public class Foo { __int i; __static int s; __public static void main(String[] args) { _____Foo f1 = new Foo(); _____System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); _____Foo f2 = new Foo(); _____System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); _____Foo f3 = new Foo(); _____System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } __public Foo() { _____i++; _____s++; } } A. f2.i is 1 f2.s is 1 B. f2.i is 1 f2.s is 2 C. f2.i is 2 f2.s is 2 D. f2.i is 2 f2.s is 1

B. f2.i is 1 f2.s is 2 //Explanation: i is an instance variable and s is static, shared by all objects of the Foo class.

The default value for data field of a boolean type, numeric type, object type is ___________, respectively. A. true, 1, Null B. false, 0, null C. true, 0, null D. true, 1, null E. false, 1, null

B. false, 0, null //p.305 - The default value of a data field is null for a reference type, 0 for a numeric type, false for a boolean type, and \u0000 for a char type.

Which is the advantage of encapsulation? A. Only public methods are needed. B. Making the class final causes no consequential changes to other code. C. It changes the implementation without changing a class's contract and causes no consequential changes to other code. D. It changes a class's contract without changing the implementation and causes no consequential changes to other code.

C. It changes the implementation without changing a class's contract and causes no consequential changes to other code.

Analyze the following code and choose the best answer: public class Foo { __private int x; __public static void main(String[] args) { _____Foo foo = new Foo(); _____System.out.println(foo.x); } } A. Since x is private, it cannot be accessed from an object foo. B. Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code. C. Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code. D. You cannot create a self-referenced object; that is, foo is created inside the class Foo.

C. Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code. //Explanation: (A) is incorrect, since x can be accessed by an object of Foo inside the Foo class. (B) is incorrect because x is non-static, it cannot be accessed in the main method without creating an object. (D) is incorrect, since it is permissible to create an instance of the class within the class. The best choice is (C).

A method that is associated with an individual object is called __________. A. a static method B. a class method C. an instance method D. an object method

C. an instance method

The keyword __________ is required to declare a class. A. public B. private C. class D. All of the above.

C. class

What will be displayed by the third println statement in the main method? public class Foo { _int i; __static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } A. f3.i is 1 f3.s is 1 B. f3.i is 1 f3.s is 2 C. f3.i is 1 f3.s is 3 D. f3.i is 3 f3.s is 1 E. f3.i is 3 f3.s is 3

C. f3.i is 1 f3.s is 3

Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be? A. public void getFinished() B. public boolean getFinished() C. public boolean isFinished() D. public void isFinished()

C. public boolean isFinished() //p.320 - If the returnType is boolean, the get method should be defined as follows by convention: public boolean isPropertyName() A set method has the following signature: public void setPropertyName(dataType propertyValue)

Analyze the following code: public class Test { __private int t; __public static void main(String[] args) { _____int x; _____System.out.println(t); } } A. The variable t is not initialized and therefore causes errors. B. The variable t is private and therefore cannot be accessed in the main method. C. t is non-static and it cannot be referenced in a static context in the main method. D. The variable x is not initialized and therefore causes errors. E. The program compiles and runs fine.

C. t is non-static and it cannot be referenced in a static context in the main method. //Private data can be accessed only within their defining class //private != static

When invoking a method with an object argument, ___________ is passed. A. the contents of the object B. a copy of the object C. the reference of the object D. the object is copied, then the reference of the copied object

C. the reference of the object

To prevent a class from being instantiated, _____________________ A. don't use any modifiers on the constructor. B. use the public modifier on the constructor. C. use the private modifier on the constructor. D. use the static modifier on the constructor.

C. use the private modifier on the constructor.

Given the declaration Circle x = new Circle(), which of the following statement is most accurate. A. x contains an int value. B. x contains an object of the Circle type. C. x contains a reference to a Circle object. D. You can assign an int value to x.

C. x contains a reference to a Circle object.

Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate? A. x contains an array of ten int values. B. x contains an array of ten objects of the Circle type. C. x contains a reference to an array and each element in the array can hold a reference to a Circle object. D. x contains a reference to an array and each element in the array can hold a Circle object.

C. x contains a reference to an array and each element in the array can hold a reference to a Circle object.

Analyze the following code. public class Test { __int x; __public Test(String t) { _____System.out.println("Test"); } __public static void main(String[] args) { _____Test test = new Test(); _____System.out.println(test.x); } } A. The program has a compile error because System.out.println method cannot be invoked from the constructor. B. The program has a compile error because x has not been initialized. C. The program has a compile error because you cannot create an object from the class that defines the object. D. The program has a compile error because Test does not have a default constructor.

D. The program has a compile error because Test does not have a default constructor. //research again

Variables that are shared by every instances of a class are __________. A. public variables B. private variables C. instance variables D. class variables

D. class variables

How many JFrame objects can you create and how many can you display? A. one B. two C. three D. unlimited

D. unlimited

Analyze the following code. public class Test { __int x; __public Test(String t) { _____System.out.println("Test"); } _public static void main(String[] args) { ____Test test = null; ____System.out.println(test.x); } } A. The program has a compile error because test is not initialized. B. The program has a compile error because x has not been initialized. C. The program has a compile error because you cannot create an object from the class that defines the object. D. The program has a compile error because Test does not have a default constructor. E. The program has a runtime NullPointerException because test is null while executing test.x.

E. The program has a runtime NullPointerException because test is null while executing test.x. //NullPointerException is a common runtime error. It occurs when you invoke a method on a reference variable with a null value. // In redli0nswift speak, you pointed to a reference variable which wasn't initialized "int x" in the place of a method like you should have. So its pointing to the wrong code.


Related study sets

Chapter 2 Developing Marketing Strategies and a Marketing Plan

View Set

Chapter 14 - Firms In Competitive Markets

View Set

Sports Finance Quiz 1 (T/F & Multiple Choice)

View Set