Log In Register

Units


Constructors & Garbage Collection - Questions and Answers

 

Practice these Constructors & Garbage Collection under Java Programming walk-in interview Questions/ examination questions with best tricks and short cuts with solution. Student (candidate) who want to crack the walk in interview, competitive exams and want to find short cuts and tricks to solve questions on Constructors & Garbage Collection for following purpose.


– Anyone who is wishing to increase their perfect knowledge of Constructors & Garbage Collection of Java Programming.
– Anyone who is preparing for aptitude test and increase his in depth aptitude knowledge Java Programming specifically Constructors & Garbage Collection.
– Anyone who is preparing for interviews (competitive examinations, govt examinations and bank examinations, off-campus or on campus interviews, walk-in interview and company interviews) having questions on Constructors & Garbage Collection under Java Programming.

– Anyone who is preparing for aptitude test or competitive exam or entrance exam.
– Anyone who is preparing for entrance examinations and other competitive examinations.
– All Experienced, Freshers and Students.

Here is list of Questions and Answers covering all key area of  Constructors & Garbage Collection topic of Java Programming:

 

1. The finalize() method is called just prior to

Answer: Option D

Explanation:

2. What is the return type of Constructors?

Answer: Option D

Explanation:
Constructors does not have any return type, not even void.

3. What will be the return type of a method that not returns any value?

Answer: Option

Explanation:

4. What is the output for the below code ? class A{ public A(){ System.out.println("A"); } public A(int i){ this(); System.out.println(i); } } class B extends A{ public B(){ System.out.println("B"); } public B(int i){ this(); System.out.println(i+3); } } public class Test{ public static void main (String[] args){ new B(5); } }

Answer: Option

Explanation:
Constructor of class B call their superclass constructor of class A (public A()), which execute first, and that constructors can be overloaded. Then come to constructor of class B (public B (int i)).

5. The main method should be static for the reason

Answer: Option C

Explanation:

6. Which of the modifier can't be used for constructors?

Answer: Option C

Explanation:

7. public class Test { } What is the prototype of the default constructor?

Answer: Option D

Explanation:
A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class). D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.

8. What is the expected output? public class Profile { private Profile(int w) { // line 1 System.out.print(w); } public final Profile() { // line 5 System.out.print(10); } public static void main(String args[]) { Profile obj = new Profile(50); } }

Answer: Option B

Explanation:
Only public, protected, private and default(no modifier) are legal when declaring constructors.

9. The implicit return type of a constructor is

Answer: Option B

Explanation:

10. Which of the following options is the best for generating random integer 0 or 1?

Answer: Option C

Explanation:

11. What is the expected output? class Animal { Animal() { System.out.println("Animal"); } } class Wild extends Animal{ Wild() { System.out.println("Wild"); super(); } } public class Test { public static void main(String args[]) { Wild wild = new Wild(); } }

Answer: Option D

Explanation:
super() call must be the first statement in a constructor.

12. Which of the following is a method having same name as that of its class?

Answer: Option 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.

13. What will be the output? public class Test{ public static void main(String[] args){ String value = "abc"; changeValue(value); System.out.println(value); } public static void changeValue(String a){ a = "xyz"; } }

Answer: Option A

Explanation:
Java pass reference as value. passing the object reference, and not the actual object itself. Simply reassigning to the parameter used to pass the value into the method will do nothing, because the parameter is essentially a local variable.

14. What is the output of the program? class Test{ public int display(int x, int y){ return ("The sum of x and y is " + x + y); } public static void main(String args[]){ Test test = new Test(); System.out.println(test.display(4,5)); } }

Answer: Option

Explanation:
Return type of method display is int and we are trying to return string.

15. Which keyword is used by method to refer to the object that invoked it?

Answer: Option 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.