0% found this document useful (0 votes)
7 views3 pages

AP Computer Science Exam Practice 9

The document presents a series of programming questions related to class definitions and inheritance in Java. It includes scenarios involving constructors, array manipulation, and method overriding. The questions test the understanding of object-oriented programming concepts and syntax in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

AP Computer Science Exam Practice 9

The document presents a series of programming questions related to class definitions and inheritance in Java. It includes scenarios involving constructors, array manipulation, and method overriding. The questions test the understanding of object-oriented programming concepts and syntax in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

30. Consider the following class definitions.

public class Rectangle


{
private int height;
private int width;

public Rectangle()
{
height = 1;
width = 1;
}

public Rectangle(int x)
{
height = x;
width = x;
}

public Rectangle(int h, int w)


{
height = h;
width = w;
}

// There may be methods that are not shown.


}

public class Square extends Rectangle


{
public Square(int x)
{
/* missing code */
}
}
Which of the following code segments can replace /* missing code */ so that the Square class
constructor initializes the Rectangle class instance variables height and width to x ?

(A) super();

(B) super(x);

(C) Rectangle(x);

(D) Square(x, x);

(E) height = x;
width = x;

GO ON TO THE NEXT PAGE.

AP Computer Science A Practice Exam 39


31. Consider an integer array nums, which has been properly declared and initialized with one or more values.
Which of the following code segments counts the number of negative values found in nums and stores the
count in counter ?
I. int counter = 0;
int i = -1;
while (i <= nums.length - 2)
{
i++;
if (nums[i] < 0)
{
counter++;
}
}

II. int counter = 0;


for (int i = 1; i < nums.length; i++)
{
if (nums[i] < 0)
{
counter++;
}
}

III. int counter = 0;


for (int i : nums)
{
if (nums[i] < 0)
{
counter++;
}
}

(A) I only
(B) II only
(C) I and II only
(D) I and III only
(E) I, II, and III

GO ON TO THE NEXT PAGE.

40 AP Computer Science A Practice Exam


32. Consider the following class definitions.
public class ClassA
{
public String getValue()
{
return "A";
}

public void showValue()


{
System.out.print(getValue());
}
}

public class ClassB extends ClassA


{
public String getValue()
{
return "B";
}
}
The following code segment appears in a class other than ClassA or ClassB.
ClassA obj = new ClassB();
obj.showValue();
What, if anything, is printed when the code segment is executed?

(A) A
(B) B
(C) AB
(D) BA
(E) Nothing is printed because the code does not compile.

GO ON TO THE NEXT PAGE.

AP Computer Science A Practice Exam 41

You might also like