182). What is the return type of Constructors?
[A]int
[B]float
[C]void
[D]none of the mentioned
Show Answer
Correct Answer: none of the mentioned
Notes:
Answer: d
Explanation: Constructors does not have any return type, not even void.
183). Which keyword is used by the method to refer to the object that invoked it?
[A]import
[B]catch
[C]abstract
[D]this
Show Answer
Correct Answer: this
Notes:
Answer: d
Explanation: this keyword can be used inside any method to refer to the current object. this is always a reference to the object on which the method was invoked.
184). Which of the following is a method having same name as that of its class?
[A]finalize
[B]delete
[C]class
[D]constructor
Show Answer
Correct Answer: constructor
Notes:
Answer: d
Explanation: A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.
185). Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed?
[A]delete
[B]free
[C]new
[D]none of the mentioned
Show Answer
Correct Answer: none of the mentioned
Notes:
Answer: d
Explanation: Java handles deallocation of memory automatically, we do not need to explicitly delete an element. Garbage collection only occurs during execution of the program. When no references to the object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed.
186). Which function is used to perform some action when the object is to be destroyed?
[A]finalize()
[B]delete()
[C] main()
[D]none of the mentioned
Show Answer
Correct Answer: finalize()
Notes:
Answer: a
Explanation: None.
187). What will be the output of the following Java code?
class box
{
int width;
int height;
int length;
int volume;
box()
{
width = 5;
height = 5;
length = 6;
}
void volume()
{
volume = width*height*length;
}
}
class constructor_output
{
public static void main(String args[])
{
box obj = new box();
obj.volume();
System.out.println(obj.volume);
}
}
[A]100
[B]150
[C]200
[D]250
Show Answer
Correct Answer: 150
Notes:
Answer: b
Explanation: None.
output:$ constructor_output.java
$ constructor_output
150
188). What will be the output of the following Java code?
class San
{
San()throws IOException
{
}
}
class Foundry extends San
{
Foundry()
{
}
public static void main(String[]args)
{
}
}
[A]compile time error
[B]run time error
[C]compile and runs fine
[D]unreported exception java.io.IOException in default constructor
Show Answer
Correct Answer: compile time error
Notes:
Answer: a
Explanation: If parent class constructor throws any checked exception, compulsory child class constructor should throw the same checked exception as its parent, otherwise code won’t compile.
189). What will be the output of the following Java code?
class box
{
int width;
int height;
int length;
int volume;
void finalize()
{
volume = width*height*length;
System.out.println(volume);
}
protected void volume()
{
volume = width*height*length;
System.out.println(volume);
}
}
class Output
{
public static void main(String args[])
{
box obj = new box();
obj.width=5;
obj.height=5;
obj.length=6;
obj.volume();
}
}
[A]150
[B]200
[C]Run time error
[D]Compilation error
Show Answer
Correct Answer: 150
Notes:
Answer: a
Explanation: None.
output:
$ javac Output.java
$ java Output
150
190). Which of the following statements are incorrect?
class box
{
int width;
int height;
int length;
int volume;
void finalize()
{
volume = width*height*length;
System.out.println(volume);
}
protected void volume()
{
volume = width*height*length;
System.out.println(volume);
}
}
class Output
{
public static void main(String args[])
{
box obj = new box();
obj.width=5;
obj.height=5;
obj.length=6;
obj.volume();
}
}
[A]default constructor is called at the time of object declaration
[B] constructor can be parameterized
[C]finalize() method is called when a object goes out of scope and is no longer needed
[D]finalize() method must be declared protected
Show Answer
Correct Answer: finalize() method is called when a object goes out of scope and is no longer needed
Notes:
Answer: c
Explanation: finalize() method is called just prior to garbage collection. it is not called when object goes out of scope.
191). What will be the output of the following Java code?
class area
{
int width;
int length;
int area;
void area(int width, int length)
{
this.width = width;
this.length = length;
}
}
class Output
{
public static void main(String args[])
{
area obj = new area();
obj.area(5 , 6);
System.out.println(obj.length + " " + obj.width);
}
}
[A] 0 0
[B]5 6
[C]6 5
[D] 5 5
Show Answer
Correct Answer: 6 5
Notes:
Answer: c
Explanation: this keyword can be used inside any method to refer to the current object. this is always a reference to the object on which the method was invoked.
output:
$ javac Output.java
$ java Output
6 5