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. Determine Output: class A{ public static void method(int i){ System.out.print("Method 1"); } public static int method(String str){ System.out.print("Method 2"); return 0; } } public class Test{ public static void main(String args[]){ A.method(5); } }

Answer: Option A

Explanation:
Overloading depends only on the method or constructor name and its parameter list i.e. method(int i) and method(String str) So, overloading does not depend on access modifiers(public, private, protected), access specifiers(static, final) and return type.

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

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

Answer: Option C

Explanation:

4. What is the output of the above program ? class Num { Num(double x ){ System.out.println( x ) ; } } public class Test extends Num { public static void main(String[] args){ Num num = new Num( 2 ) ; } }

Answer: Option C

Explanation:

5. 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:

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

7. he variables declared in a class for the use of all methods of the class are called

Answer: Option

Explanation:

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

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

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

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

Answer: Option C

Explanation:

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

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

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

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