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

public class Secret { private int x; private static int y; public static int cou

ID: 3803270 • Letter: P

Question

public class Secret
{
    private int x;
    private static int y;
    public static int count;
    public int z;
      
    public Secret()
    {
        x = 0;
        z = 1;
    }

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

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

    public String toString()
    {
        return ("x = " + x + ", y = " + y + ",
                 count = " + count);
    }

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

How many constructors are present in the class definition in the accompanying figure?

zero

one

two

three

1 points   

Question 8

Which of the following is a constructor without parameters?

copy constructor

default constructor

finalizer

modifier

1 points   

Question 9

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

shallow

deep

method

object

1 points   

Question 10

Consider the following class definition.

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 + " " + width);
    }

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

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

Which of the following statements correctly instantiates the Rectangle object myRectangle?

(i) myRectangle = new Rectangle(12.5, 6);      
(ii) Rectangle myRectangle = new Rectangle(12.5, 6);
(iii) class Rectangle myRectangle = new Rectangle(12.5, 6);

Only (i)      

Only (ii)

Only (iii)

Both (ii) and (iii)

1 points   

Question 11

Suppose that Book is class with two instance variables-name of type String and numOfBooks of type int. Which of the following would be appropriate syntax for the heading of a copy constructor for the class called Book?

public Book(String n, int num)

public Book()

public Book(Book b)

public copyBook(String n, int num)

1 points   

Question 12

What is the default definition of the method toString?

There is no default definition; you must define the method yourself.

It creates a string that is the name of the object’s class, followed by the hash code of the object.

It creates a string that is the name of the program.

It creates a string that is the name of the package, followed by the name of the class, followed by the hash of the object.

1 points   

Question 13

What is the main use of inner classes?

To alter the behavior of the outer class

To handle events

To implement cascaded method calls

To provide information hiding

zero

one

two

three

Explanation / Answer

How many constructors are present in the class definition in the accompanying figure?
Answer:
three

Which of the following is a constructor without parameters?
Answer:
default constructor

In ____ copying, each reference variable refers to its own object.
Answer:
deep

Which of the following statements correctly instantiates the Rectangle object myRectangle?
Answer:
Only (ii)

Suppose that Book is class with two instance variables-name of type String and numOfBooks of type int. Which of the following would be appropriate syntax for the heading of a copy constructor for the class called Book?
Answer:
public Book(String n, int num)

What is the default definition of the method toString?
Answer:
It creates a string that is the name of the package, followed by the name of the class, followed by the hash of the object.

What is the main use of inner classes?
Answer:
To alter the behavior of the outer class