CS 4A: Chapter 9 - Objects & Classes
Which of the following statements are true? (choose all that apply)
- Data fields have default values. - Local variables do not 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.
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 a compilation 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").
Which of the following statements are correct?
- 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.
Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true? (choose all that apply)
- dates[0] is null. - dates = new java.util.Date[5] is fine, which assigns a new array to dates.
You use the ________ operator to access members of an object.
.
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
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
________ is invoked to create an object.
A constructor
________ represents an entity in the real world that can be distinctly identified.
An object
The java.util.Date class is introduced in Section 7.4. Which of the following code 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(); } }
Both
Suppose TestCircle and Circle in Listing 7.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
Every instance data field f in the class can be referenced using this.f in a static method the same class. True/False
False
If the parameter is of a primitive type, both formal parameter and actual parameter reference to the same memory. True/False
False
Java assigns a default value to a local variable in a method if the variable is not initialized. True/False
False
The default value for a data member of boolean type is true. True/False
False
You can declare variables of the same name in a method even though they are in the same block. True/False
False
A static method in a class can access the class variables in the same class. True/False
True
All data fields in an object have default values. True/False
True
Array variable are reference variables. True/False
True
Each class in the file is compiled into a separate bytecode file. True/False
True
Every instance data field f in the class can be referenced using this.f in an instance method the same class. True/False
True
You can declare variables of the same name in a method if they are in non-nesting blocks. True/False
True
You cannot use modifiers on local variables inside a method except final. True/False
True
You use the plus sign (+) to denote public data or methods. True/False
True
How many JFrame objects can you create and how many can you display?
Unlimited
Suppose you declare Date d. d is now called ________.
a reference variable for an object
The default value for data field of a boolean type, numeric type, object type is ________, respectively.
false, 0, null
To declare a constant MAX_LENGTH as a member of the class, you write:
final static double MAX_LENGTH = 99.98;
An immutable class cannot have ________.
public data fields
When invoking a method with an object argument, ________ is passed.
the reference of the object
Java uses ________ to reference the current object.
this
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.