7.1 | Describe the relationship between an object and its defining class. How do you declare a class? How do you declare an object reference variable? How do you create an object? How do you declare and create an object in one statement? |
7.2 | What are the differences between constructors and methods ? |
7.3 | Is an array an object or a primitive type value? Can an array contain elements of an object type as well as a primitive type? Describe the default value for the elements of an array. |
7.4 | What is wrong with the following program? |
7.5 | What is wrong in the following code? 1 class Test { 2 public static void main(String[] args) { 3 A a = new A(); 4 a.print(); 5 } 6 } 7 8 class A { 9 String s; 10 11 A(String s) { 12 this .s = s; 13 } 14 15 public void print(){ 16 System.out.print(s); 17 } 18 } |
| |
7.6 | What is the printout of the following code? public class Foo { private boolean x; public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); } } |
7.7 | How do you create a Date for the current time? How do you display the current time? |
7.8 | How do you create a Random object and obtain a random int value, double value, and boolean value? |
7.9 | Which packages contain the classes Date , Random , JOptionPane , System , and Math ? |
7.10 | Suppose that the class Foo is defined in (a). Let f be an instance of Foo . Which of the statements in (b) are correct?
|
7.11 | Add the static keyword in the place of ? if appropriate. public class Test { private 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; } } |
| |
7.12 | Can you invoke an instance method or reference an instance variable from a static method? Can you invoke a static method or reference a static variable from an instance method? What is wrong in the following code? 1 public class Foo { 2 public static void main(String[] args) { 3 method1(); 4 } 5 6 public void method1() { 7 method2(); 8 } 9 10 public static void method2() { 11 System.out.println( "What is radius " + c.getRadius()); 12 } 13 14 Circle c = new Circle(); 15 } |
7.13 | What is an accessor method? What is a mutator method? What are the naming conventions for accessor methods and mutator methods? |
7.14 | What are the benefits of data field encapsulation? |
7.15 | In the following code, radius is private in the Circle class, and myCircle is an object of the Circle class. Does the following highlighted code cause any problems? Explain why. public class Circle { private double radius = 1.0 ; /** Find the area of this circle */ double getArea() { return radius * radius * Math.PI; } public static void main(String[] args) { Circle myCircle = new Circle(); System.out.println( "Radius is " + myCircle.radius ); } } |
7.16 | If a class contains only private data fields and no set methods, is the class immutable? |
7.17 | If all the data fields in a class are private and primitive type, and the class contains no set methods, is the class immutable? |
7.18 | 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 program: |
7.19 | 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; } } |
| |
7.20 | Show the printout of the following code: |
7.21 | What is the output of the following program? public class Foo { static int i = ; static int j = ; 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); } } |
7.22 | Describe the role of the this keyword. What is wrong in the following code? 1 public class C { 2 int p; 3 4 public void setP( int p) { 5 p = p; 6 } 7 } |
7.23 | 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[ ]); 5 System.out.println(dates[ ].toString()); 6 } 7 } |
7.24 | If you redefine the Loan class without set methods, is the class immutable? |
7.25 | Is the StackOfIntegers class immutable? |