Constructors And Methods(Java Program ) Questions and Answers

Question 1. 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();
}
}
  1.    Animal Wild
  2.    Wild Animal
  3.    Runtime Exception
  4.    Compilation Error
Explanation:-
Answer: Option D. -> Compilation Error
super() call must be the first statement in a constructor.

Question 2. 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);
}
}
  1.    Won't compile because of line (1); constructor can't be private
  2.    Won't compile because of line (5); constructor can't be final
  3.    50
  4.    10 50
Explanation:-
Answer: Option B. -> Won't compile because of line (5); constructor can't be final
Only public, protected, private and default(no modifier) are legal when declaring constructors.

Question 3. In which area of memory, the system stores parameters and local variables whenever a method is invoked?
  1.    Heap
  2.    Storage Area
  3.    Stack
  4.    Array
Explanation:-
Answer: Option C. -> Stack

Question 4. The variables declared in a class for the use of all methods of the class are called
  1.    reference variables
  2.    objects
  3.    instance variables
  4.    None of these
Explanation:-
Answer: Option C. -> instance variables

Question 5. Which of the modifier can't be used for constructors?
  1.    public
  2.    private
  3.    static
  4.    protected
Explanation:-
Answer: Option C. -> static

Question 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);
}
}
  1.    Won't compile because of line (1), constructor can't be private
  2.    10 50
  3.    50
  4.    Won't compile because of line (5), constructor can't be static
Explanation:-
Answer: Option D. -> Won't compile because of line (5), constructor can't be static
Only public, protected, private and default (no access modifier) are legal while declaring constructors.

Question 7. 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
}
}
  1.    At line 1, constructor Tester must be marked public like its class
  2.    At line 2, constructor call
  3.    At line 3, compilation error, ambiguity problem, compiler can't determine whether a constructor
  4.    At line 4
Explanation:-
Answer: Option B. -> At line 2, constructor call
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.

Question 8. Which of the following options is the best for generating random integer 0 or 1?
  1.    (int)Math.random()
  2.    (int)Math.random() + 1
  3.    (int)(Math.random() + 0.5)
  4.    (int)(Math.random() + 0.2)
Explanation:-
Answer: Option C. -> (int)(Math.random() + 0.5)

Question 9. What is Math.floor(3.6)?
  1.    3.0
  2.    3
  3.    4
  4.    4.0
Explanation:-
Answer: Option B. -> 3
The method floor gives the largest integer that is less than or equal to the argument.

Question 10. What will be the return type of a method that not returns any value?
  1.    void
  2.    int
  3.    double
  4.    None of the above
Explanation:-
Answer: Option A. -> void