Question: 6.Suppose we have a class A which has a constructor that takes a single integer. After the following statements have been executed, how many A
6.Suppose we have a class A which has a constructor that takes a single integer. After the following statements have been executed, how many A objects will exist (not counting garbage objects) and which objects are they? Explain your answer.
A a = new A(100);
A b = new A(150);
A c = b;
b = a;
a = null;
7.Consider this code that creates some Location objects:
Location a, b, c;
a = new Location(10,20);
b = new Location(10,20);
c = b;
After this code executes, what are the values of these boolean expressions?
a==b
a.equals(b)
a==c
a.equals(c)
b==c
b.equals(c)
8.What does the following program print out. Explain why.
public class Thing
{
public int a;
public int b;
public Thing(int a, int b)
{this.a=a; this.b=b}
}
public class Test
{
public static void f(Thing x, int y)
{
x.a++;
y++;
}
public static void main(String[] args)
{
Thing x = new Thing(1,1);
int y = 1;
f(x, y);
System.out.println("x.a = " + x.a +
}
}
9.Suppose that we have classes A, B, C and D. Suppose that B is a subclass of A, that C is a subclass of B, and D is a subclass of A. Suppose that we make the following declarations.
A a1 = new A();
A a2 = new C();
D d1 = new D();
For each part below, explain what, if any, errors would be caused by the statement in that part. Be sure to consider both compile time and run time errors.
(a) A a3 = new B();
(b) B b1 = new A();
(c) B b2 = (B) a1;
(d) B b3 = (B) a2;
(e) B b4 = (B) d1;
(f) B b5 = (C)(A)new D();
10.Let A be an array of size n 2 containing integers from 1 to n1, inclusive, with exactly one number repeated.
Write a method
public static int findRepeatedNumber(int[] A)
that returns the value of the repeated number in the array A.
What is the running time of the method?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
