Consider the following statements. public class Rectangle { private double lengt
ID: 3803267 • Letter: C
Question
Consider the following statements.
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 = " + length
+ "; Width = " + width + " " +
+ " Area = " + area()
+ "; Perimeter = " + perimeter());
}
public double area()
{
return length * width;
}
public void perimeter()
{
return 2 * length + 2 * width;
}
public void makeCopy(Rectangle otherRect)
{
length = otherRect.length;
width = otherRect.width
}
}
Rectangle tempRect = new Rectangle(14, 10);
Rectangle newRect = new Rectangle(9, 5);
What are the values of the instance variables of newRect after the following statement execute?
newRect.makeCopy(tempRect);
length = 14; width = 10
length = 9; width = 5
length = 14; width = 5
None of these
1 points
Question 2
ADTs illustrate the principle of ____.
information hiding
privacy
encapsulation
overloading
1 points
Question 3
MysteryClass
Which of the following would be a default constructor for the class MysteryClass shown in the accompanying figure?
public MysteryClass(){ setData(0, 0.0); }
public MysteryClass(0, 0.0) { setData(); }
public MysteryClass(0) { setData(0, 0.0); }
private MysteryClass(10){ setData(); }
1 points
Question 4
MysteryClass
According to the UML class diagram in the accompanying figure, which of the following is a private member of the classMysteryClass?
doubleFirst
MysteryClass
second
1 points
Question 5
Which of the following class definitions is correct in Java?
(i)
public class Employee
{
private String name;
private double salary;
private int id;
public Employee()
{
name = "";
salary = 0.0;
id = 0;
}
public Employee(String n, double s, int i)
{
name = n;
salary = s;
id = i;
}
public void print()
{
System.out.println(name + " " + id + " " + salary);
}
}
(ii)
public class Employee
{
private String name;
private double salary;
private int id;
public void Employee()
{
name = "";
salary = 0.0;
id = 0;
}
public void Employee(String n, double s, int i)
{
name = n;
salary = s;
id = i;
}
public void print()
{
System.out.println(name + " " + id + " " + salary);
}
}
Only (i)
Only (ii)
Both (i) and (ii)
Neither is correct
1 points
Question 6
Class members consist of all of the following EXCEPT ____.
named constants
variable declarations
pre-defined methods
methods
length = 14; width = 10
length = 9; width = 5
length = 14; width = 5
None of these
Explanation / Answer
Answers:
(1) length = 14; width = 10
Here, the newRect was initially given (9, 5) as (length, width). After that the makeCopy() method is called by it so the values are changed as per the parameter passed. makecopy() method copies the values of parameter into the object which called the method so newRect values are changed and becomes the same as that of tempRect which are (14, 10). So, length becomes 14 and width becomes 10 after given statements are executed.
(2) information hiding
Abstraction means to give the overall view, not the detailed one. Abstract data type defines this is work it does, but does not say how it works. So, the information is hidden in this case. So this is information hiding.
(3) public MysteryClass(){ setData(0, 0.0); }
Default constructor does not have any parameters. This is its definition. There is only one option which is like this i.e. having no parameters. So, it is the answer.
(4) second
Private member is written before it the '-' sign in the UML diagram. So, here, first and second, only these 2 members are private among which 'second' is present in the options so it is the correct option.
(5) Only (i)
Both the class definitions are differed by the syntax change in the constructor. In (ii), the constructors are defined as having void return type. But, as per the rule of constructor, it does not have any return type because it is not returning anything. So, this way you can't use the constructors, so definition (ii) is not correct. Everything defined in the (i) is in the correct way. So, it is the answer.
(6) pre-defined methods
variables and the methods are but obvious the class members.
named constants are also the variables that are declared as final so that their value can not be changed afterwards.
So, the only possible answer is pre-defined methods. They are already defined in some class so they are not in this class so they are not member of this class.
Please comment if there is any query. Thank you. :)