Question: When answering the next 5 questions, consider the following code: comments and numbers indicate where additional code must be placed. public class MainClass { public

When answering the next 5 questions, consider the following code: comments and numbers indicate where additional code must be placed.

public class MainClass

{

public static void main(String[] args)

{

//(2) call the greeting methodof the MyClass class from here

//(3) creat a MyClass object called myObject

//(4) update myObject instance variables by calling an appropriate method

}

}

class MyClass

{

private int numOfItems;

private String reportTitle;

// (1)MyClass constructor goes here

public static void greetings()// note that greetings() is static

{

//greetings code goes here

}

public void update(int num, String title)

{

//update code goes here

}

public void print()

{

// print code goes here

}

}

  1. Suppose you are writing the constructor of theMyClassclass (1) Which of the following constructors is correct?

  1. public MyClass
  2. public void MyClass()
  3. public MyClass()
  4. public MyClass(void)
  5. public void MyClass

  1. Suppose you wish to call thegreetings() method that prints the greeting, at line (2) Which of the following statements will call this method correctly?

  1. MainClass.greetings();
  2. void result = greetings();
  3. myObject.greetings();
  4. greetings();
  5. MyClass.greetings();

  1. Suppose you wish to construct a MyClass object called myObject at line (3) Which of the following statements will correctly do this?

  1. MyClass myObject;
  2. myObject.MyClass();
  3. MyClass myObject = MyClass();
  4. MyClass myObject = new (MyClass);
  5. MyClass myObject = new MyClass();

  1. Suppose you wish to call the update method at line (4) Which of the following statements will call this method correctly? Assume the myObject object is available form question 3.

  1. update(myObject(3, "Hi!"));
  2. update(3, "Hi!");
  3. MyClass.myObject.update(3, "Hi!");
  4. myObject.update(3, "Hi!");
  5. MyClass.update(3, "Hi!");

  1. Here is the completedupdateThe intent of the method is to update the class' properties with the values provided by the parametersnumandtitle:

public void update(int num, String title)

{

int numOfItems = num;

reportTitle = title;

}

If this method is called, what can we say about the values of the class member variables (properties)numOfItemsandreportTitlewhen that call completes?

  1. Instance variable - numOfItems, takes on the value of num; reportTitle takes on the value of title.
  2. Instance variable - numOfItems, takes on the value of num; reportTitle's value remains unchanged.
  3. Instance variable - numOfItems, value remains unchanged; reportTitle takes on the value of title.
  4. Both instance variables - numOfItems' and reportTitle's values remain unchanged.
  5. A run-time error occurs because of the illegal redefinition of a variable inside the method.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!