Compile Time Errors

Help Questions

AP Computer Science A › Compile Time Errors

Questions 1 - 10
1

Consider the following code:

public static class Rectangle {

private double width, height;

public Rectangle(double w,double h) {

width = w;

height = h;

}

``

public double getArea() {

return width * height;

}

``

public double getPerimeter() {

return 2 * width + 2 * height;

}

}

``

public static class Square extends Rectangle {

public Square(double s) {

super(s,s);

}

}

public static void main(String[] args) {

Rectangle[] rects = new Rectangle[6];

for(int i = 0; i < 6; i++) {

if(i % 2 == 0) {

rects[i] = new Rectangle(i+10,i + 20);

} else {

rects[i] = new Square(i+20);

}

}

Square s = rects[1];

}

What is the error in the code above?

The final assignment operation cannot be done.

There is an array overrun.

You cannot assign a Square object to an array of Rectangle objects.

There is an error in the declaration of the Rectangle array.

You need to use "implements", not "extends" for the class Square.

Explanation

This code fills up the 6 member array with alternating Rectangle and Square objects. You can do this because the Square class is a subclass of Rectangle. That is, since Squares are Rectangles, you can store Square objects in Rectangle variables. However, even though rects[1] is a square, you CANNOT immediately reassign that to a Square object. The code has now come to consider all of the objects in the array as being Rectangle objects. You would need to explicitly type cast this to get the line to work:

Square s = (Square)(rects[1]);

2

Consider the following code:

public static class Rectangle {

private double width, height;

public Rectangle(double w,double h) {

width = w;

height = h;

}

``

public double getArea() {

return width * height;

}

``

public double getPerimeter() {

return 2 * width + 2 * height;

}

}

``

public static class Square extends Rectangle {

public Square(double s) {

super(s,s);

}

}

public static void main(String[] args) {

Rectangle[] rects = new Rectangle[6];

for(int i = 0; i < 6; i++) {

if(i % 2 == 0) {

rects[i] = new Rectangle(i+10,i + 20);

} else {

rects[i] = new Square(i+20);

}

}

Square s = rects[1];

}

What is the error in the code above?

The final assignment operation cannot be done.

There is an array overrun.

You cannot assign a Square object to an array of Rectangle objects.

There is an error in the declaration of the Rectangle array.

You need to use "implements", not "extends" for the class Square.

Explanation

This code fills up the 6 member array with alternating Rectangle and Square objects. You can do this because the Square class is a subclass of Rectangle. That is, since Squares are Rectangles, you can store Square objects in Rectangle variables. However, even though rects[1] is a square, you CANNOT immediately reassign that to a Square object. The code has now come to consider all of the objects in the array as being Rectangle objects. You would need to explicitly type cast this to get the line to work:

Square s = (Square)(rects[1]);

3

Consider the following code:

public static class Rectangle {

private double width, height;

public Rectangle(double w,double h) {

width = w;

height = h;

}

``

public double getArea() {

return width * height;

}

``

public double getPerimeter() {

return 2 * width + 2 * height;

}

}

``

public static class Square extends Rectangle {

public Square(double s) {

super(s,s);

}

}

public static void main(String[] args) {

Rectangle[] rects = new Rectangle[6];

for(int i = 0; i < 6; i++) {

if(i % 2 == 0) {

rects[i] = new Rectangle(i+10,i + 20);

} else {

rects[i] = new Square(i+20);

}

}

Square s = rects[1];

}

What is the error in the code above?

The final assignment operation cannot be done.

There is an array overrun.

You cannot assign a Square object to an array of Rectangle objects.

There is an error in the declaration of the Rectangle array.

You need to use "implements", not "extends" for the class Square.

Explanation

This code fills up the 6 member array with alternating Rectangle and Square objects. You can do this because the Square class is a subclass of Rectangle. That is, since Squares are Rectangles, you can store Square objects in Rectangle variables. However, even though rects[1] is a square, you CANNOT immediately reassign that to a Square object. The code has now come to consider all of the objects in the array as being Rectangle objects. You would need to explicitly type cast this to get the line to work:

Square s = (Square)(rects[1]);

4

What is the error in the following code?

int val1 = -14,val2 = 4;

final int val3 = 9;

double val4 = 4.1;

double val5 = 3.1;

val1 = val2 * val3;

val3 = val1 * 12;

val5 = val1 - val3;

val4 = val2 + val5;

You cannot assign the new integer value to val3.

You cannot perform mixed addition or subtraction with doubles and integers.

You cannot multiply val3 by val2.

You must define val2 on its own line.

You cannot assign the result of an integer subtraction to val5.

Explanation

The only error among the options given is the fact that this code assigns a new value to the variable val3, which is defined as a constant. (This is indicated by the keyword final before the rest of its declaration.) You cannot alter constants once they have been declared. Thus, the following line will cause a compile-time error:

val3 = val1 * 12;

5

What is the error in the following code?

int val1 = -14,val2 = 4;

final int val3 = 9;

double val4 = 4.1;

double val5 = 3.1;

val1 = val2 * val3;

val3 = val1 * 12;

val5 = val1 - val3;

val4 = val2 + val5;

You cannot assign the new integer value to val3.

You cannot perform mixed addition or subtraction with doubles and integers.

You cannot multiply val3 by val2.

You must define val2 on its own line.

You cannot assign the result of an integer subtraction to val5.

Explanation

The only error among the options given is the fact that this code assigns a new value to the variable val3, which is defined as a constant. (This is indicated by the keyword final before the rest of its declaration.) You cannot alter constants once they have been declared. Thus, the following line will cause a compile-time error:

val3 = val1 * 12;

6

What is the error in the following code?

int val1 = -14,val2 = 4;

final int val3 = 9;

double val4 = 4.1;

double val5 = 3.1;

val1 = val2 * val3;

val3 = val1 * 12;

val5 = val1 - val3;

val4 = val2 + val5;

You cannot assign the new integer value to val3.

You cannot perform mixed addition or subtraction with doubles and integers.

You cannot multiply val3 by val2.

You must define val2 on its own line.

You cannot assign the result of an integer subtraction to val5.

Explanation

The only error among the options given is the fact that this code assigns a new value to the variable val3, which is defined as a constant. (This is indicated by the keyword final before the rest of its declaration.) You cannot alter constants once they have been declared. Thus, the following line will cause a compile-time error:

val3 = val1 * 12;

7

public interface ServerInstance {

byte[] readBytes();

boolean writeBytes(byte[]b);

boolean wake();

boolean status();

void sleep();

}

``

public class MyHost implements ServerInstance {

boolean running = false;

public boolean wake() {

// Other logic code here...

return running;

}

public boolean status() {

// Other logic code here...

return running;

}

public byte[] readBytes() {

byte[] buffer = null;

// Other logic code here...

return buffer;

}

public void sleep() {

// Other logic code here...

running = false;

}

public byte[] writeBytes(byte[] b) {

// Other logic code here...

return b;

}

// Other methods...

}

What is the error in the code above?

The writeBytesmethod is not properly defined

The sleep method does not return anything

The readBytes method initializes the variable buffer to null

There is no error

The word extends should be used, not implements

Explanation

When you implement an interface, all of the methods defined in that interface must be written in the class that is proposing to be such an implementation. (Or, if they are not implemented there, you need to involve abstract classes—but that is not our concern here.) The methods must match the prototypes proposed in the interface. In the example code, ServerInstance has a method writeBytes that returns a boolean value. However, the MyHost class has implemented this method as returning a byte[] value. Since you cannot have different types of return values for methods with the same parameter set, Java interprets this as being the proposed implementation for writeBytes(byte[] b), and this method must return a boolean if MyHost is to implement ServerInstance.

8

Given:

const int x = 10;

Which of the following will compile?

const int * r = &x

int * p = &x

int * const q = &x

All of the above

None of the above

Explanation

First we take a look at the given statement:

const int x = 10;

the const in front of "int" means that x will always hold the value of 10 and it will not change.

Let's observe all the choices.

int *p =&x

This line says to assign the address of x (in memory) to the pointer p. This however, will not compile because int * p is not marked as const. x is marked as a const so this forces int * p to be a const as well.

int * const q = &x

There is a const in this case but it is in the wrong place

const int * r = &x

The const is in the correct place and this is the correct answer

9

public interface ServerInstance {

byte[] readBytes();

boolean writeBytes(byte[]b);

boolean wake();

boolean status();

void sleep();

}

``

public class MyHost implements ServerInstance {

boolean running = false;

public boolean wake() {

// Other logic code here...

return running;

}

public boolean status() {

// Other logic code here...

return running;

}

public byte[] readBytes() {

byte[] buffer = null;

// Other logic code here...

return buffer;

}

public void sleep() {

// Other logic code here...

running = false;

}

public byte[] writeBytes(byte[] b) {

// Other logic code here...

return b;

}

// Other methods...

}

What is the error in the code above?

The writeBytesmethod is not properly defined

The sleep method does not return anything

The readBytes method initializes the variable buffer to null

There is no error

The word extends should be used, not implements

Explanation

When you implement an interface, all of the methods defined in that interface must be written in the class that is proposing to be such an implementation. (Or, if they are not implemented there, you need to involve abstract classes—but that is not our concern here.) The methods must match the prototypes proposed in the interface. In the example code, ServerInstance has a method writeBytes that returns a boolean value. However, the MyHost class has implemented this method as returning a byte[] value. Since you cannot have different types of return values for methods with the same parameter set, Java interprets this as being the proposed implementation for writeBytes(byte[] b), and this method must return a boolean if MyHost is to implement ServerInstance.

10

Given:

const int x = 10;

Which of the following will compile?

const int * r = &x

int * p = &x

int * const q = &x

All of the above

None of the above

Explanation

First we take a look at the given statement:

const int x = 10;

the const in front of "int" means that x will always hold the value of 10 and it will not change.

Let's observe all the choices.

int *p =&x

This line says to assign the address of x (in memory) to the pointer p. This however, will not compile because int * p is not marked as const. x is marked as a const so this forces int * p to be a const as well.

int * const q = &x

There is a const in this case but it is in the wrong place

const int * r = &x

The const is in the correct place and this is the correct answer

Page 1 of 3
Return to subject