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 following code contains one compilation error, find it? public class Test { Test() { } // line 1 static void Test() { this(); } // line 2 public static void main(String[] args) { // line 3 Test(); // line 4 } }

Answer: Option

Explanation:
a constructor call (super() or this() ) must be the first statement inside a constructor. No constructor calls are allowed inside a method. static void Tester() is a method not a constructor.

2. The implicit return type of a constructor is

Answer: Option B

Explanation:

3. class MyClass{ MyClass(){ System.out.print("one"); } public void myMethod(){ this(); System.out.print("two"); } } public class TestClass{ public static void main(String args[]){ MyClass obj = new MyClass(); obj.myMethod(); } }

Answer: Option D

Explanation:
A method can't have constructor call.

4. Determine output: public class Test{ public static void main(String args[]){ MyClass obj = new MyClass(); obj.val = 1; obj.call(obj); System.out.println(obj.val); } } class MyClass{ public int val; public void call(MyClass ref){ ref.val++; } }

Answer: Option B

Explanation:

5. What will be the result of compiling and running the given code? class A{ int b=10; private A(){ this.b=7; } int f(){ return b; } } class B extends A{ int b; } public class Test{ public static void main(String[] args){ A a = new B(); System.out.println(a.f()); } }

Answer: Option A

Explanation:
Choice A is the correct answer. The code does not compile because the constructor of class A is declared as private. This creates a problem when the subclass constructor makes an implicit super() call to the parent class constructor at the time B is instantiated. Since the code does not compile, all the other choices are incorrect. If the constructor of A had not been private, the output would have been 7.

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

Answer: Option D

Explanation:

7. Given the following piece of code: class Person{ public int number; } public class Test{ public void doIt(int i , Person p){ i = 5; p.number = 8; } public static void main(String args[]){ int x = 0; Person p = new Person(); new Test().doIt(x, p); System.out.println(x + " " + p.number); } } What is the result?

Answer: Option

Explanation:

8. What is the output for the below code? public class Test{ public static void printValue(int i, int j, int k){ System.out.println("int"); } public static void printValue(byte...b){ System.out.println("long"); } public static void main(String... args){ byte b = 9; printValue(b,b,b); } }

Answer: Option B

Explanation:
Primitive widening uses the smallest method argument possible. (For Example if you pass short value to a method but method with short argument is not available then compiler chooses method with int argument). But in this case compiler will prefer the older style before it chooses the newer style, to keep existing code more robust. var-args method is looser than wider.

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

Answer: Option C

Explanation:

10. 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.

11. Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed?

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

12. 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.

13. What is the return type of Constructors?

Answer: Option D

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

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

Answer: Option D

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

15. public class MyClass{ } For the above class(MyClass) what is the correct way of declaring constructor?

Answer: Option D

Explanation:
Choice A,C are the correct answers. The default access specifier for any constructor is same as the access specifier of class.so the options A and C are correct.