IN JAVA 1. Consider the following class definition. public class Cylinder { priv
ID: 3583245 • Letter: I
Question
IN JAVA
1. Consider the following class definition.
public class Cylinder
{
private double baseRadius;
private double height;
public Cylinder ()
{
baseRadius = 0;
height = 0;
}
public Cylinder (double l, double h)
{
baseRadius = l;
height = h;
}
public void set(double r, double h)
{
baseRadius = r;
height = h;
}
public String toString()
{
return (baseRadius + " " + height);
}
public double surfaceArea()
{
return 2 * 3.14 * baseRadius * height;
}
public double volume()
{
return 3.14 * baseRadius * baseRadius * height;
}
}
Suppose that you have the following declaration.
Cylinder cyl = new Cylinder(1.5, 10);
Which of the following sets of statements are valid in Java?
(i)
cyl.surfaceArea();
cyl.volume();
cyl.print();
(ii)
print(cyl.surfaceArea);
print(cyl.volume());
A- Only (i)
B - Only (ii)
C - Both (i) and (ii)
D - None of these
2. Constructors are invoked the same way that methods are invoked.. True or False
3. Consider the following incomplete abstract class:
public abstract class Pet
{
public Pet()
{ //iimplementation not shown}
}
The statement:
Pet p - new Pet();
is a valid statement.
True or False
4. The default constructor executes when an object is instantiated and initialized using the default constructor's parameters..
True or False
5. Consider the following class definitions.
public class BClass
{
private int x;
private double y;
public void print() { }
}
public class DClass extends BClass
{
private int a;
private int b;
public void print() { }
}
Suppose that you have the following statement.
DClass dObject = new DClass();
How many instance variables does dObject have?
zero
two
three
four
6. Consider the following class definitions.
public class BClass
{
private int x;
public void set(int a)
{
x = a;
}
public void print()
{
System.out.print(x);
}
}
public class DClass extends BClass
{
private int y;
public void set(int a, int b)
{
//Postcondition: x = a; y = b;
}
public void print(){ }
}
Which of the following correctly redefines the method print of DClass?
(i)
public void print()
{
System.out.print(x + " " + y);
}
(ii)
public void print()
{
super.print();
System.out.print(" " + y);
}
Only (i)
Only (ii)
Both (i) and (ii)
None of these
7. Consider the following class definitions.
public class BClass
{
private int x;
public void set(int a) { x = a; }
public void print(){ }
}
public class DClass extends BClass
{
private int y;
public void set(int a, int b)
{
//Postcondition: x = a; y = b;
}
public void print(){ }
}
Which of the following is the correct definition of the method set of the class DClass?
(i)
public void set(int a, int b)
{
super.set(a);
y = b;
}
(ii)
public void set(int a, int b)
{
x = a;
y = b;
}
Only (i)
Only (ii)
Both (i) and (ii)
None of these
8.
Suppose a class Car and its subclass Honda both have a method called speed as part of the class definition. Additionally, the Honda class was a method called cost. Assume that the variable rentalH refers to an object of the type Honda and the following statements are found in client code:
System.out.println(rentalH.cost()); //statement 1
System.out.println(rentalH.speed()); //statement 2
In the scenario described in the accompanying figure, what will the first statement do?
The cost method in Honda will be called.
The cost method in Car will be called.
Nothing will be called since the code will not compile as a result of multiple definitions of speed.
Overloading will be used to determine which cost method to use.
9.
Consider the following definition of a recursive method.
public static int foo(int n) //Line 1
{ //Line 2
if (n == 0) //Line 3
return 0; //Line 4
else //Line 5
return n + foo(n – 1); //Line 6
}
Which of the statements represent the base case?
Statements in Lines 3 and 4
Statements in Lines 5 and 6
Statements in Lines 3, 4, and 5
None of these
10. If you are building a mission control system, ____.
you should always use iteration
you should always use recursion
use the most efficient solution
use the solution that makes the most intuitive sense
11. public static int exampleRecursion (int n)
{
if (n == 0)
return 0;
else
return exampleRecursion(n - 1) + n * n * n;
}
What is the output of exampleRecursion(0)?
0
3
9
27
A- Only (i)
B - Only (ii)
C - Both (i) and (ii)
D - None of these
Explanation / Answer
1)ans)D
print() method is not available in Cyclinder class object.
2)Ans)False
Constructor Invoked automatically when we create a class object.
But we have to explicitly call the method of a class.
3)Ans)False
We cannot create an object to the abstract class.
4)Ans)true
Default Constructor is Invoked automatically when we create a class object.
5)Ans)four
BClass object has 2 and DClass object has 2.BClass object lies inside DClass Object.
So,total DClass Object has 4 instance variables inside it/
6)Ans)Both (i) and (ii)
7)Ans)only (i)
8)Ans)The cost method in Honda will be called.
9)Ans)
Statements in Lines 3 and 4
10)Ans)0
As the condition inside the function(n==0) is true then it returns 0.
____________Thank You