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
}
}
- Suppose you are writing the constructor of theMyClassclass (1) Which of the following constructors is correct?
- public MyClass
- public void MyClass()
- public MyClass()
- public MyClass(void)
- public void MyClass
- Suppose you wish to call thegreetings() method that prints the greeting, at line (2) Which of the following statements will call this method correctly?
- MainClass.greetings();
- void result = greetings();
- myObject.greetings();
- greetings();
- MyClass.greetings();
- Suppose you wish to construct a MyClass object called myObject at line (3) Which of the following statements will correctly do this?
- MyClass myObject;
- myObject.MyClass();
- MyClass myObject = MyClass();
- MyClass myObject = new (MyClass);
- MyClass myObject = new MyClass();
- 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.
- update(myObject(3, "Hi!"));
- update(3, "Hi!");
- MyClass.myObject.update(3, "Hi!");
- myObject.update(3, "Hi!");
- MyClass.update(3, "Hi!");
- 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?
- Instance variable - numOfItems, takes on the value of num; reportTitle takes on the value of title.
- Instance variable - numOfItems, takes on the value of num; reportTitle's value remains unchanged.
- Instance variable - numOfItems, value remains unchanged; reportTitle takes on the value of title.
- Both instance variables - numOfItems' and reportTitle's values remain unchanged.
- 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started