Question : Which three statements are true about the structure of a Java class? A. A class can have only one private constructor. B. A method can have the same name as a field. C. A class can have overloaded static methods. D. A public class must have a main method. E. The methods are mandatory components of a class. F. The fields need not be initialized before use.
Correct Answer : Get Lastest Questions and Answer : Explanation: A: Private constructors prevent a class from being explicitly instantiated by its callers. If the programmer does not provide a constructor for a class, then the system will always provide a default, public no-argument constructor. To disable this default constructor, simply add a private no-argument constructor to the class. This private constructor may be empty.
B: The following works fine: int counter() { int counter=0; return (1); }
C: We can overload static method in Java. In terms of method overloading static method are just like normal methods and in order to overload static method you need to provide another static method with same name but different method signature.
Incorrect: Not D: Only a public class in an application need to have a main method. Not E: Example: class A { public string something; public int a; } Q: What do you call classes without methods? Most of the time: An anti pattern. Why? Because it faciliates procedural programming with "Operator" classes and data structures. You separate data and behaviour which isn't exactly good OOP. Often times: A DTO (Data Transfer Object) Read only datastructures meant to exchange data, derived from a business/domain object.
Well sometimes, you just gotta have those structures to hold data that is just plain and simple and has no operations on it.
Not F: Fields need to be initialtized. If not the code will not compile. Example: Uncompilable source code - variable counter might not have been initialized
Question : Which two items can legally be contained within a java class declaration? A. An import statement B. A field declaration C. A package declaration D. A method declaration
C: You can declare two kinds of classes: top-level classes and inner classes. You define an inner class within a top-level class. Depending on how it is defined, an inner class can be one of the following four types: Anonymous, Local, Member and Nested toplevel. A nested top-level class is a member classes with a static modifier. A nested top-level class is just like any other top-level class except that it is declared within another class or interface. Nested top-level classes are typically used as a convenient way to group related classes without creating a new package. The following is an example: public class Main { static class Killer {
1. 4W 100 Auto 4W 150 Manual 2. Null 0 Auto 4W 150 Manual 3. Compilation fails only at line n1 4. Compilation fails only at line n2 5. Compilation fails at both line n1 and line n2
1. Sum is 600 2. Compilation fails at line n1. 3. Compilation fails at line n2. 4. A ClassCastException is thrown at line n1. 5. A ClassCastException is thrown at line n2.