Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

____ 21. Constructors have the same name as the ____. a. member methods c. class

ID: 3633873 • Letter: #

Question

____ 21. Constructors have the same name as the ____.
a. member methods c. class
b. data members d. package


____ 22. Which of the following is used to allocate memory for the instance variables of an object of a class?
a. the reserved word public c. the operator new
b. the reserved word static d. the operator +


____ 23. Consider the following statements.

public class Rectangle
{
private double length;
private double width;

public Rectangle()
{
length = 0;
width = 0;
}

public Rectangle(double l, double w)
{
length = l;
width = w;
}

public void set(double l, double w)
{
length = l;
width = w;
}

public void print()
{
System.out.println("Length = " + length
+ "; Width = " + width + " " +
+ " Area = " + area()
+ "; Perimeter = " + perimeter());
}

public double area()
{
return length * width;
}

public void perimeter()
{
return 2 * length + 2 * width;
}
}

Rectangle bigRect = new Rectangle (14, 10);

What is the output of the following statement?

bigRect.print();
a. Length = length; Width = width
Area = area; Perimeter = perimeter c. Length = 14.0; Width = 10.0
Area = 140.0; Perimeter = 48.0
b. Length = length Width = width
Area = area Perimeter = perimeter d. Length = 14.0 Width = 10.0
Area = 140.0 Perimeter = 48.0


____ 24. In ____ copying, each reference variable refers to its own object.
a. shallow c. method
b. deep d. object


public class Illustrate
{
private int x;
private static int y;
public static int count;
public static int z;

public Illustrate()
{
x = 1;
}

public Illustrate(int a)
{
x = a;
}

public void print()
{
System.out.println("x = " + x + ", y = "
+ y + ", count = " + count);
}

public static void incrementY()
{
y++;
}
}

____ 25. Which of the following statements would you use to declare a new reference variable of the type Illustrate and instantiate the object with a value of 9?
a. Illustrate illObject = new Illustrate(9);
b. illObject = Illustrate(9);
c. Illustrate illObject(9);
d. Illustrate.illObject(9);


____ 26. Which of the following declares an array of int named hits?
a. int hits; c. new int hits[];
b. int[] hits; d. int hits = int[];


int[] num = new int[100];

for (int i = 0; i < 50; i++)
num[i] = i;

num[5] = 10;
num[55] = 100;

____ 27. What is the data type of the array above?
a. int c. list
b. char d. num


____ 28. Consider the following declaration:

double[] sales = new double[50];
int j;

Which of the following correctly initializes all the components of the array sales to 10.

(i)
for (j = 0; j < 49; j++)
sales[j] = 10;

(ii)
for (j = 1; j <= 50; j++)
sales[j] = 10;
a. Only (i) c. Both (i) and (ii)
b. Only (ii) d. None of these


____ 29. What is the value of alpha[2] after the following code executes?

int[] alpha = new int[5];
int j;

for (j = 0; j < 5; j++)
alpha[j] = 2 * j + 1;
a. 1 c. 5
b. 4 d. 6


____ 30. What is stored in alpha after the following code executes?

int[] alpha = new int[5];
int j;

for (j = 0; j < 5; j++)
{
alpha[j] = j + 1;

if (j > 2)
alpha[j - 1] = alpha[j] + 2;
}
a. alpha = {1, 5, 6, 7, 5} c. alpha = {4, 5, 6, 7, 9}
b. alpha = {1, 2, 3, 4, 5} d. None of these


____ 31. What is the output of the following Java code?

int[] alpha = {2, 4, 6, 8, 10};
int j;

for (j = 4; j >= 0; j--)
System.out.print(alpha[j] + " ");
System.out.println();
a. 2 4 6 8 10 c. 4 3 2 1 0
b. 10 8 6 4 2 d. Invalid code


int[] array1 = {1, 3, 5, 7}

for (int i = 0; i <array.length; i++)
if (array1[i] > 5)
System.out.println(i + " " + array1[i]);

____ 32. What is the output of the code fragment above?
a. 0 3 c. 7 3
b. 3 7 d. 5 1


int[] x = new int[10];

x[0] = 34;
x[1] = 88;

System.out.println(x[0] + " " + x[1] + " " + x[10]);

____ 33. What is the output of the code fragment above?
a. 34 88 0
b. 34 88 88
c. 0 34 88
d. This program throws an exception.


____ 34. Suppose that sales is a two dimensional array of 10 rows and 7 columns wherein each component is of the type int , and sum and j are int variables. Which of the following correctly finds the sum of the elements of the fifth row of sales?
a. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[5][j]; c. sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sales[5][j];
b. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[4][j]; d. sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sales[4][j];


____ 35. A subclass can directly access ____.
a. public members of a superclass c. all members of a superclass
b. private members of a superclass d. none of the members of a superclass


____ 36. Consider the following class definitions.

public class BClass
{
private int x;
private double y;

public void print() { }
}

public class DClass extends BClass
{
private int a;
private int b;

public void print() { }
}

Suppose that you have the following statement.

DClass dObject = new DClass();

How many instance variables DObject has?

a. 0 c. 4
b. 2 d. None of these


____ 37. Consider the following class definitions.

public class BClass
{
private int x;

public void set(int a) { x = a; }

public void print(){ }
}

public class DClass extends BClass
{
private int y;

public void set(int a, int b)
{
//Postcondition: x = a; y = b;
}
public void print(){ }
}

Which of the following is the correct definition of the method set of the class Dclass?

(i)
public void set(int a, int b)
{
super.set(a);
y = b;
}

(ii)

public void set(int a, int b)
{
x = a;
y = b;
}

a. Only (i) c. Both (i) and (ii)
b. Only (ii) d. None of these




____ 38. Based on the diagrams above, which of the following is the superclass?
a. Rectangle c. area()
b. Box d. volume()


____ 39. Suppose that the class Mystery is derived from the class Secret. Consider the following statements:

Secret mySecret = new Secret();
Secret secRef;

Mystery myMystery = new Mystery();
Mystery mysRef;

secRef = myMystery;

Which of the following statements is legal in Java?

(i) mysRef = (Mystery) mySecret;
(ii) mysRef = (Mystery) secRef;
a. Only (i) c. Both (i) and (ii)
b. Only (ii) d. None of these


____ 40. Which operator is used to determine if an object is of a particular class type?
a. The operator new c. The instanceof operator
b. The dot (.) operator d. The + operator

Explanation / Answer

21. Constructors have the same name as the ____.

c. class

22. Which of the following is used to allocate memory for the instance variables of an object of a class?

  c. the operator new

23. Output of the code..

c or d.

Length = 14.0 Width = 10.0
Area = 140.0 Perimeter = 48.0

24. In ____ copying, each reference variable refers to its own object.

b. deep

25. Which of the following statements would you use to declare a new reference variable of the type Illustrate and instantiate the object with a value of 9?

a. Illustrate illObject = new Illustrate(9);

26. Which of the following declares an array of int named hits?

  b. int[] hits;

27. What is the data type of the array above?

a. int

28. Consider the following declaration:

  d. None of these

29. What is the value of alpha[2] after the following code executes?

c. 5

30. What is stored in alpha after the following code executes?

d. None of these

31. What is the output of the following Java code?

b. 10 8 6 4 2

32. What is the output of the code fragment above?

  b. 3 7

33. What is the output of the code fragment above?

  a. 34 88 0

34. Suppose that sales is a two dimensional array of 10 rows and 7 columns wherein each component is of the type int , and sum and j are int variables. Which of the following correctly finds the sum of the elements of the fifth row of sales?

  b. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[4][j];

35. A subclass can directly access ____.

a. public members of a superclass

36. Consider the following class definitions.

  a. 0

37. Consider the following class definitions.

  a. Only (i)

38. Based on the diagrams above, which of the following is the superclass?

b. Box (here diagram is not given so its a guess..)

39. Suppose that the class Mystery is derived from the class Secret. Consider the following statements:

c. Both (i) and (ii)

40. Which operator is used to determine if an object is of a particular class type?

  c. The instanceof operator