You are on page 1of 7

Check Point for Chapter 1

1. What are the differences between constructors and methods?


- If the constructer is not present a default value to the constructer is made by java automatically
whereas methods does not have a default value
- Constructer must have the same name as the class name but methods should not be .
- Constructor is used to initialize an object whereas method is used to exhibits functionality
of an object.

2. When will a class have a default constructor?


- When there is no constructor in the class.
-
3. What is wrong with each of the following programs?
a) There is no constructor for int 5

b) There is no method x

c) The value of c has not been initialized

d)

1 public class ShowErrors { 1 public class ShowErrors {


2 public static void main(String[] args) { 2 public static void main(String[] args) {
3 ShowErrors t = new ShowErrors(5); 3 ShowErrors t = new
4 } ShowErrors(); 4 t.x();
5 } 5 }
6 }

(a) (b)
1 public class ShowErrors { 5 public class ShowErrors {
2 public void method1() { 6 public void method1() {
3 Circle c; 7 Circle c;
4 System.out.println("What is radius " 8 System.out.println("What is radius "
5 + c.getRadius()); 5 + c.getRadius());
6 c = new Circle(); 6 c = new Circle();
7 } 7 }
8} 8}

(c) (d)
4. 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 newS) {
12 s = newS;
13 }
14
15 public void print() {
16 System.out.print(s);
17 }
18 }

-They didn’t initialise the value of s . it must be A a = new A(the value of s)


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

- false
6. A 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 C {
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 }

- Run time error because the value of c has not been initialized before it is printed .

7. Suppose the class F is defined in (a). Let f be an instance of F. Which of the statements in (b) are correct?
Justify your answer.

public class F { System.out.println(f.i);


int i; System.out.println(f.s);
static String s; f.imethod();
void imethod() { f.smethod();
} System.out.println(F.i);
static void smethod(){ System.out.println(F.s);
} F.imethod();
} F.smethod(); int

(a) (b)

8. Add the static keyword in the place of ? if appropriate.


public class Test {
int count;
public ? static 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;
}
}

9. What are the benefits of data field encapsulation?

10. In the following code, radius is private in the Circle class, and myCircle is an objectof the Circle class.
Does the highlighted 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);
}
}
no because you cannot use it in another class since it is private .
11. 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++;
}
}

public class Count {


public int count;
public Count (int c) {
count = c;
}
public Count () {
count = 1;
}
}

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

public class Test { public class Test {


public static void main(String[] args) { public static void main(String[] args) {
T t = new T(); T t1 = new T();
swap(t); T t2 = new T();
System.out.println("e1 = " + t.e1+ " e2 = " + t.e2); System.out.println("t1's i = " + t1.i + " and j = " + t1.j);
} System.out.println("t2's i = " + t2.i + " and j = " + t2.j);
public static void swap(T t) { }
int temp = t.e1; }
t.e1 = t.e2; class T {
t.e2 = temp; static int i = 0;
} int j = 0;
} T() {
class T { i++;
int e1 = 1; j = 1;
int e2 = 2; }
} }

(c) (d)

14. Is the following class immutable? Justify your answer.


public class A {
private int[] values;
public int[] getValues() {
return values;
}
}

15. What is the output of the following program? Justify your answer.
public class Test {
private static int i = 0;
private 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);
}
}
16. 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 }

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

18. Design a class named Account that contains:


• A private int data field named id for the account (default 0).
• A private double data field named balance for the account (default 0).
• A private double data field named annualInterestRate that stores the current interest rate (default 0).
Assume that all accounts have the same interest rate.
• A private Date data field named dateCreated that stores the date when the account was created.
• A no-arg constructor that creates a default account.
• A constructor that creates an account with the specified id and initial balance.
• The accessor and mutator methods for id, balance, and annualInterestRate.
• The accessor method for dateCreated.
• A method named getMonthlyInterestRate() that returns the monthly interest rate.
• A method named getMonthlyInterest() that returns the monthly interest.
• A method named withdraw that withdraws a specified amount from the account.
• A method named deposit that deposits a specified amount to the account.

Draw the UML diagram for the class then implement the class. (Hint: The method getMonthlyInterest() is
to return monthly interest, not the interest rate. Monthly interest is balance * monthlyInterestRate.
monthlyInterestRate is annualInterestRate / 12. Note annualInterestRate is a percentage, for example
4.5%. You need to divide it by 100.)
Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000, and
an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to
deposit $3,000, and print the balance,the monthly interest, and the date when this account was created.
19. Design a class named Location for locating a maximal value and its location in a two-dimensional array.
The class contains public data fields row, column, and maxValue that store the maximal value and its
indices in a two-dimensional array with row and column as int types and maxValue as adouble type.
Write the following method that returns the location of the largest element in a two-dimensional array:

public static Location locateLargest(double[][] a)

The return value is an instance of Location. Write a test program that prompts the user to enter a two-
dimensional array and displays the location of the largest element in the array. Here is a sample run:

You might also like