Answer Only (i) Only (ii) Both (i) and (ii) None of these 2. Consider the follow
ID: 3540510 • Letter: A
Question
Answer
Only (i)
Only (ii)
Both (i) and (ii)
None of these
2.
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 void perimeter()
{
return 2 * length + 2 * width;
}
}
Suppose that you have the following declaration.
Rectangle bigRect = new Rectangle();
Which of the following set of statements are valid in Java?
(i) bigRect.set(10, 5};
(ii) bigRect.length = 10;
bigRect.width = 5; Answer
Only (i)
Only (ii)
Both (i) and (ii)
None of these
3.
Consider the following statements.
public class Circle
{
private double radius;
public Circle()
{
radius = 0.0;
}
public Circle(double r)
{
radius = r;
}
public void set(double r)
{
radius = r;
}
public void print()
{
System.out.println(radius + " " + area + " "
+ circumference);
}
public double area()
{
return 3.14 * radius * radius;
}
public double circumference()
{
return 2 * 3.14 * radius;
}
}
Circle myCircle = new Circle();
double r;
Which of the following statements are valid in Java? (Assume that console is Scanner object initialized to the standard input device.)
(i)
r = console.nextDouble();
myCircle.area = 3.14 * r * r;
System.out.println(myCircle.area);
(ii)
r = console.nextDouble();
myCircle.set(r);
System.out.println(myCircle.area()); Answer
Only (i)
Only (ii)
Both (i) and (ii)
None of these
4.
Consider the following declaration:
int[] beta = new int[3];
int j;
Which of the following input statements correctly inputs values into beta? (Assume that console is a Scanner object initialized to the standard input device.)
(i)
beta[0] = console.nextInt();
beta[1] = console.nextInt();
beta[2] = console.nextInt();
(ii)
for (j = 0; j < 3; j++)
beta[j] = console.nextInt(); Answer
Only (i)
Only (ii)
Both (i) and (ii)
None of these
Explanation / Answer
1) only the second program is correct because the first program has a void return type for the constructors whereas the constructors have no return type as in the second program.
2) only the first option is correct as we can access the public functions of a class(as in part (i)), but we cannot access any of the private data members of a class(as in part (ii)). hence only (i) is correct
3) only the second option is correct as we can calculate the area of the circle using the public function area() (as in case (ii)), however there is "no data member" 'area' which can be set using myCircle.area = 3.14 * r * r (as in case (ii)). Hence, only option 2 is correct.
4) Both the options are correct as the second case is equivalent to the first case and the second case executes the same set of statements. And since option 1 is correct (syntax of using arrays), option 2 is also correct as it simply enters the values into the 0th, 1st and 2nd indices of the array as done by option (i). Hence Both are correct in this case.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.