Java Chapter 9

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true?

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

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

0

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

an instance method

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

different methods in a class

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

false, 0, null

To prevent a class from being instantiated, _____________________

use the private modifier on the constructor.

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

An object

Which of the following statements are correct?

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

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

B. new Point2D(40, 50).distance(5.5, 4.4) C. new Point2D(40, 50).distance(new Point2D(5.5, 4.4)) D. new Point2D(5.5, 4.4).distance(40, 50) E. new Point2D(5.5, 4.4).distance(new Point2D(40, 50)

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

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.

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

1234567 7654321

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

A class

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 D. Encapsulating data fields helps prevent programming errors.

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.

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 TempClass does not have a constructor with an int argument.

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.

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

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

class variables

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

f2.i is 1 f2.s is 2

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

f3.i is 1 f3.s is 3

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 }

in line 4

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

j is 0

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

k is 2

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 & B

________ is invoked to create an object.

A constructor

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

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

Which is the advantage of encapsulation?

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

Which of the following statements are true about an immutable object?

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.

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 compile error because Test does not have a default constructor.

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

a static method or an instance method

An object is an instance of a __________.

class

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

test.date

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

the reference of the object

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

this

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

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

Which of the following statements are true?

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.

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.

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 code has a compile error because xMethod is not declared static.

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 program has a compile error because the NClass class has a private constructor.

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 has a runtime NullPointerException because test is null while executing test.x.

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

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

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

i + j is 23

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

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

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

public boolean isFinished()

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

final static double MAX_LENGTH = 99.98;

The keyword __________ is required to declare a class.

Class

Given the declaration Circle x = new Circle(), which of the following statement is most accurate.

x contains a reference to a Circle object.

Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate?

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

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

101

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.

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?

Both compile fine.


Ensembles d'études connexes

Ch. 26: Growth and Development of the Toddler

View Set

International Nutrition Midterm 1

View Set