Errors and where to find them

Report a typo

Below are four code snippets (A-D) with the class Rectangle, and each is incorrect. Match the snippets with the errors made in them.

Tip: The signature of a constructor includes the number, types, and order of its parameters.

A:

class Rectangle {
    val width: Int
    val length: Int

    constructor(_width: Int, _length: Int) {
        width = _width
        length = _length
    }
}

val rectangle = Rectangle()

B:

class Rectangle {
    val width: Int
    val length: Int
    
    constructor(_width: Int, _length: Int) {
        width = _width
        length = _length
    }

    constructor(_sizeA: Int, _sizeB: Int) {
        width = _sizeA
        length = _sizeB
    }
}

C:

class Rectangle {
    val width: Int
    val length: Int

    constructor(width: Int, length: Int) {
        width = width
        length = length
    }
}

D:

class Rectangle() {
    var width: Int = 1
    var length: Int = 1

    constructor(width: Int, length: Int) {
        this.width = width
        this.length = length
    }
}
Match the items from left and right columns
A
B
C
D
Primary constructor call is absent
No values passed for constructor parameters
Signature uniqueness violation
Reassigning val because of name collision
___

Create a free account to access the full topic