Java OCA OCP Practice Question 978

Question

Which of these classes compile and use a default constructor?

Choose all that apply

  • A. public class Main { }
  • B. public class Main { public main() {} }
  • C. public class Main { public main(String name) {} }
  • D. public class Main { public Main() {} }
  • E. public class Main { Main(String name) {} }
  • F. public class Main { private Main(int age) {} }
  • G. public class Main { void Main() { }


A, G.

Note

Options B and C don't compile because the constructor name must match the classname.

Since Java is case sensitive, these don't match.

Options D, E, and F all compile and provide one user-defined constructor.

Since a constructor is coded, a default constructor isn't supplied.

Option G defines a method, but not a constructor.

Option A does not define a constructor, either.

Since no constructor is coded, a default constructor is provided for options A and G.




PreviousNext

Related