Chapter 9 Study Guide

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

an instance method

A method that is associated with an individual object is called ________.

an instance method

A method that is associated with an individual object is called __________.

false

A static method in a class can access the instance variables in the same class.

Add static in the main method and in the factorial method because these two methods don't need reference any instance objects or invoke any instance methods in the Test class.

Add the static keyword in the place of ? if appropriate. public class Test { int count; public ? void main(String[] args) { ... } public ? int getCount() { return count; } public ? int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) result *= i; return result; } }

*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).

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

The program has a compile error because TempClass does not have a constructor with an int argument. Explanation: The program would be fine if the void keyword is removed from public void TempClass(int j).

Analyze 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); } }

The program has a compile error because Test does not have a default constructor. Explanation: Note that a default no-arg constructor is provided only if no constructors are explicitly defined in the class. In this case, a constructor Test(String t) is already defined. So, there is no default no-arg constructor in the Test class.

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

The program has a runtime NullPointerException because test is null while executing test.x.

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

The code has a compile error because xMethod is not declared static.

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++; } }

The program has a runtime NullPointerException because test is null while executing test.x.

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

The program will compile.

Analyze the following code: class Circle { private double radius; public Circle(double radius) { radius = radius; } }

*The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0. Explanation: You have replaced radius = radius by this.radius = radius

Analyze the following code: class Circle { private double radius; public Circle(double radius) { radius = radius; } } A. The program has a compile error because it does not have a main method. B. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0. C. The program has a compile error because you cannot assign radius to radius. D. The program does not compile because Circle does not have a default constructor.

*this.t() may be replaced by t(). *this(1) must be called before System.out.println("Default constructor").

Analyze the following code: class Test { private double i; public Test(double i) { this.t(); this.i = i; } public Test() { System.out.println("Default constructor"); this(1); } public void t() { System.out.println("Invoking t"); } }

t is non-static and it cannot be referenced in a static context in the main method.

Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } }

The program has a compile error because class A does not have a default constructor. The program would compile and run if you change A a = new A() to A a = new A("5").

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 s) { this.s = s; } void print() { System.out.println(s); } }

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

Analyze the following code: 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); } }

*dates[0] is null. *dates = new java.util.Date[5] is fine, which assigns a new array to dates.

Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true? 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.

You can invoke a static method or reference a static variable from an instance method.

Can you invoke a static method or reference a static variable from an instance method?

You cannot invoke an instance method or reference an instance variable from a static method.

Can you invoke an instance method or reference an instance variable from a static method?

Java uses "pass by value" to pass parameters to a method. When passing a variable of a primitive type to a method, the variable remains unchanged after the method finishes. However, when passing a variable of a reference type to a method, any changes to the object referenced by the variable inside the method are permanent changes to the object referenced by the variable outside of the method. Both the actual parameter and the formal parameter variables reference to the same object. The output of the program is as follows: count 101 times 0

Describe the difference between passing a parameter of a primitive type and passing a parameter of a reference type. Show the output of the following programs: 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("count is " + myCount.count); System.out.println("times is " + times); } public static void increment(Count c, int times) { c.count++; times++; } } class Count { public int count; public Count(int c) { count = c; } public Count() { count = 1; } }

'this' refers to the object itself

Describe the role of the 'this' keyword.

x contains a reference to a Circle object. Explanation: x is a reference variable that can reference a Circle object or null if it does not reference any object.

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.

x contains a reference to an array and each element in the array can hold 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.

*Use the Date's no-arg constructor to create a Date for the current time. *Use the Date's toString() method to display a string representation for the Date.

How do you create a Date for the current time? How do you display the current time?

*You can create a Point2D object using its constructor Point2D(x, y) for a point at (x, y). *Use p1.distance(p2) to obtain the distance between p1 and p2. *Use p1.midPoint(p2) to obtain the mid-point between p1 and p2.

How do you create a Point2D? Suppose p1 and p2 are two instances of Point2D, how do you obtain the distance between the two points? How do you obtain the midpoint between the two points?

The syntax to create an object is: new ClassName();

How do you create an object?

The syntax to declare a reference variable for an object is: ClassName v;

How do you declare an object's reference variable?

Not necessarily. To be immutable, the class must also contain no getter methods that would return a reference to a mutable data field object.

If a class contains only private data fields and no setter methods, is the class immutable?

Yes

If all the data fields in a class are private and of primitive types, and the class doesn't contain any setter methods, is the class immutable?

Not a problem. Though radius is private, myCircle.radius is used inside the Circle class. Thus, it is fine.

In the following code, radius is private in the Circle class, and myCircle is an object of the Circle class. Does the code cause any problems? If so, explain why. public class Circle { private double radius = 1; /** Find the area of this circle */ public double getArea() { return radius * radius * Math.PI; } public static void main(String[] args) { Circle myCircle = new Circle(); System.out.println("Radius is " + myCircle.radius); } }

*An array is an object. *An array can contain elements of an object type. *The default value for the elements of an array is 0 for numeric, false for boolean, '\u0000' for char, null for object element type

Is an array an object or a primitive type value? Can an array contain elements of an object type? Describe the default value for the elements of an array.

No, because values is a reference type.

Is the following class immutable? public class A { private int[] values; public int[] getValues() { return values; } }

(a) a[0] = 1 a[1] = 2 (b) a[0] = 2 a[1] = 1

Show the output of the following code: (a) public class Test { public static void main(String[] args) { int[] a = {1, 2}; swap(a[0], a[1]); System.out.println("a[0] = " + a[0] + " a[1] = " + a[1]); } public static void swap(int n1, int n2) { int temp = n1; n1 = n2; n2 = temp; } } (b) public class Test { public static void main(String[] args) { int[] a = {1, 2}; swap(a); System.out.println("a[0] = " + a[0] + " a[1] = " + a[1]); } public static void swap(int[] a) { int temp = a[0]; a[0] = a[1]; a[1] = temp; } }

After swap1: circle1 = 1.0 circle2 = 2.0 After swap2: circle1 = 2.0 circle2 = 1.0 Explanation: The reference value of circle1 is passed to x and the reference value of circle2 is passed to y. The contents of the objects are not swapped in the swap1 method. circle1 and circle2 are not swapped.

Show the output of the following program: public class Test { public static void main(String[] args) { Circle circle1 = new Circle(1); Circle circle2 = new Circle(2); swap1(circle1, circle2); System.out.println("After swap1: circle1 = " + circle1.radius + " circle2 = " + circle2.radius); swap2(circle1, circle2); System.out.println("After swap2: circle1 = " + circle1.radius + " circle2 = " + circle2.radius); } public static void swap1(Circle x, Circle y) { Circle temp = x; x = y; y = temp; } public static void swap2(Circle x, Circle y) { double temp = x.radius; x.radius = y.radius; y.radius = temp; } } class Circle { double radius; Circle(double newRadius) { radius = newRadius; } }

Both compile fine.

Suppose TestCircle and Circle in Listing 9.1 are in two separate files named TestCircle.java and Circle.java, respectively. What is the outcome of compiling TestCircle.java and then Circle.java?

*System.out.println(f.i); *System.out.println(f.s); *f.imethod(); *f.smethod(); *System.out.println(F.s); *F.smethod();

Suppose that the class F is defined in (a). Let f be an instance of F. Which of the statements in (b) are correct? (a) public class F { int i; static String s; void imethod() { } static void smethod() { } } (b) *System.out.println(f.i); *System.out.println(f.s); *f.imethod(); *f.smethod(); *System.out.println(F.i); *System.out.println(F.s); *F.imethod(); *F.smethod();

a static method

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 static method or an instance method

Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class. public MyClass() { xMethod(); }

public boolean isFinished()

Suppose you wish to provide an accessor method for a boolean property finished, what should the signature of this method?

false, 0, null

The default value for data field of a boolean type, numeric type, object type is ___________, respectively.

class

The keyword __________ is required to declare a class.

true

The order of methods in a class is immaterial.

final static double MAX_LENGTH = 99.98;

To declare a constant MAX_LENGTH as a member of the class, you write...

*new Point2D(40, 50).distance(5.5, 4.4) *new Point2D(40, 50).distance(new Point2D(5.5, 4.4)) *new Point2D(5.5, 4.4).distance(40, 50) *new Point2D(5.5, 4.4).distance(new Point2D(40, 50))

To obtain the distance between the points (40, 50) and (5.5, 4.4), use _________.

use the private modifier on the constructor.

To prevent a class from being instantiated, _____________________

class variables

Variables that are shared by every instance of a class are __________.

static variables

Variables that are shared by every instances of a class are ________.

Two benefits: (1) for protecting data and (2) for easy to maintain the class.

What are the benefits of data field encapsulation?

*Constructors are special kinds of methods that are called when creating an object using the new operator. *Constructors do not have a return type-not even void.

What are the differences between constructors and methods?

test.date

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(_________________); } }

k is 2

What is the output for the second statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } }

j is 0

What is the output for the third statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } }

(a) null (b) 1234567

What is the output of the following programs? (a) import java.util.Date; public class Test { public static void main(String[] args) { Date date = null; m1(date); System.out.println(date); } public static void m1(Date date) { date = new Date(); } } (b) import java.util.Date; public class Test { public static void main(String[] args) { Date date = new Date(1234567); m1(date); System.out.println(date.getTime()); } public static void m1(Date date) { date = new Date(7654321); } }

(c) 7654321 (d) 1234567

What is the output of the following programs? (c) import java.util.Date; public class Test { public static void main(String[] args) { Date date = new Date(1234567); m1(date); System.out.println(date.getTime()); } public static void m1(Date date) { date.setTime(7654321); } } (d) import java.util.Date; public class Test { public static void main(String[] args) { Date date = new Date(1234567); m1(date); System.out.println(date.getTime()); } public static void m1(Date date) { date = null; } }

(c) e1 = 2 e2 = 1 (d) t1's i = 2 t1's j = 1 t2's i = 2 t2's j = 1

What is the output of the following programs? (c) public class Test { public static void main(String[] args) { T t = new T(); swap(t); System.out.println("e1 = " + t.e1 + " e2 = " + t.e2); } public static void swap(T t) { int temp = t.e1; t.e1 = t.e2; t.e2 = temp; } } class T { int e1 = 1; int e2 = 2; } (d) public class Test { public static void main(String[] args) { T t1 = new T(); T t2 = new T(); System.out.println("t1's i = " + t1.i + " and j = " + t1.j); System.out.println("t2's i = " + t2.i + " and j = " + t2.j); } } class T { static int i = 0; int j = 0; T() { i++; j = 1; } }

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.

What is the output of 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++; } }

f3.i is 1 f3.s is 3

What is the output of 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++; } }

101

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; } }

0

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; } }

Swap line 5 with line 6 Lines 10, 14, should be this.p = p;

What is wrong in the following code? 1 public class C { 2 private int p; 3 4 public C() { 5 System.out.println("C's no-arg constructor invoked"); 6 this(0); 7 } 8 9 public C(int p) { 10 p = p; 11 } 12 13 public void setP(int p) { 14 p = p; 15 } 16 }

The program does not compile because new A() is used in class Test, but class A does not have a default constructor.

What is wrong in the following code? class Test { public static void main(String[] args) { A a = new A(); a.print(); } } class A { String s; A(String newS) { s = newS; } public void print() { System.out.print(s); } }

(a) The main method is static and cannot invoke the instance method method1. (b) c is an instance variable, which cannot be accessed from the static context in method2.

What is wrong in the following code? public class C { Circle c = new Circle(); public static void main(String[] args) { method1(); } public void method1() { method2(); } public static void method2() { System.out.println("What is radius " + c.getRadius()); } }

Test.id = 45; This is wrong, since id is an instance member and cannot be accessed from a class.

What is wrong in the following code? public class Test { private int id; public void m1() { this.id = 45; } public void m2() { Test.id = 45; } }

Line 4 prints null since dates[0] is null. Line 5 causes a NullPointerException since it invokes toString() method from the null reference.

What is wrong in the following code? 1 public class Test { 2 public static void main(String[] args) { 3 java.util.Date[] dates = new java.util.Date[10]; 4 System.out.println(dates[0]); 5 System.out.println(dates[0].toString()); 6 } 7 }

the reference of the object

When invoking a method with an object argument, ___________ is passed.

A class has a default constructor only if the class does not define any constructor.

When will a class have a default constructor?

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

Which is the advantage of encapsulation?

this

Which of the following can be placed in the blank line in the following code? public class Test { private int id; public void m1() { _____.id = 45; } } A. this B. Test

A. & B. Explanation: Both (A) and (B) are fine. In A, an object is created without explicit reference. This is known as anonymous object.

Which of the following code 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(); } } A. A. B. B. C. Neither

*A reference variable references to an object. *A data field in a class can be of an object type.

Which of the following statements are correct? A. A reference variable is an object. B. A reference variable references to an object. C. A data field in a class must be of a primitive type. D. A data field in a class can be of an object type.

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

Which of the following statements are correct? 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.

*The contents of an immutable object cannot be modified. *All properties of an immutable object must be private. *A readable object type property in an immutable object must also be immutable. *An immutable object contains no mutator methods.

Which of the following statements are true about an immutable object? A. The contents of an immutable object cannot be modified. B. All properties of an immutable object must be private. C. All properties of an immutable object must be of primitive types. D. A readable object type property in an immutable object must also be immutable. E. An immutable object contains no mutator methods.

*Local variables do not have default values. *Data fields have default values. *A variable of a primitive type holds a value of the primitive type. *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? 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 default constructor is provided automatically if no constructors are explicitly declared in the class. *The default constructor is a no-arg constructor.

Which of the following statements are true? 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.

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

Which of the following statements are true? 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.

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

Which of the following statements are true? 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.

*Date is in the java.util package. *Random is in the java.util package. *Point2D is in the javafx.geometry package. *System and Math are in the java.lang package.

Which packages contain the classes Date, Random, Point2D, System, and Math?

false

You can always use the default constructor even though the non-default constructors are defined in the class.

True

You can declare a local variable in a method that has same name as an instance variable in the class.

different methods in a class

You can declare two variables with the same name in __________.

true

You can declare variables of the same name in a method if they are in non-nesting blocks.

true

You cannot use the private modifier in front of keyword class.

in line 4 Explanation: The square method should be static because it does not reference any instance data or invoke any instance method.

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 return age; 10 } 11 }

true

You use underline in UML to denote static variables and methods.

A class

_______ is a construct that defines objects of the same type.

An object

__________ represents an entity in the real world that can be distinctly identified.

Accessor method (getter)

___________________ is for retrieving private data value. The naming convention is getDataFieldName() for non-boolean values and isDataFieldName() for boolean values.

Mutator method (setter)

______________________ is for changing private data value. The naming convention is setDataFieldName(value).

true

All data fields in an object have default values.

class

An object is an instance of a __________.

The program has a compile error because the NClass class has a private constructor.

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() { } }

The syntax to define a class is: public class ClassName { }

How do you define a class?

a reference variable for an object

Suppose you declare Date d. d is now called ________.

A NullPointerException occurs when a null reference variable is used to access the members of an object.

What is NullPointerException?

An anonymous object is the one that does not have a reference variable referencing it.

What is an anonymous object?

i + j is 23 Explanation: The first + operator in the expression 'i + j is ' + i + j is evaluated.

What is the output for the first statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } }

false

What is the output of the following code? public class A { boolean x; public static void main(String[] args) { A a = new A(); System.out.println(a.x); } }

1234567 7654321

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

The member access operator is used to access a data field or invoke a method from an object.

Which operator is used to access a data field or invoke a method from an object?

A constructor

________ is invoked to create an object.


Set pelajaran terkait

UCONN Marcus. W. Garcia SOCI 1501 EXAM 2

View Set

Chapter 01: Professional Nursing Practice

View Set