Java: 1. When is a try-catch statement used? 2. What is parsing? 3. What is a bl
ID: 3576925 • Letter: J
Question
Java:
1. When is a try-catch statement used?
2. What is parsing?
3. What is a block of code, and how does it relate to the life (or “scope”) of a variable?
4. What is encapsulation and how is it implemented in Java?
5. What is the function of access modifiers public, private, protected…, and how are they used with variables and with methods? What happens when we omit an access modifier?
6. What is inheritance and how is it implemented in Java?
7. What is the effect of the final modifier on variables, methods, and classes?
8. What is the purpose of a constructor? What is a copy constructor?
9. What are “accessor” and “mutator” methods, and what makes a class “mutable”?
10. What does the == operator mean when comparing objects?
11. What does a return statement do and when is it required?
Explanation / Answer
Answer: -
1)Java try block is used to enclose the code that might throw an exception. It must be used within the method.Java try block must be followed by either catch or finally block.
2)Parsing is used to convert the value of one data type to a value of another data type. int i = Integer.parseInt( testString );
4)Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class.
5)Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. Private access modifier is the most restrictive access level. Class and interfaces cannot be private. Variables that are declared private can be accessed outside the class, if public getter methods are present in the class. A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe. Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.
6)Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class)
7)When applied to a method definition the final modifier indicates that the method may not be overridden in a derived class.When applied to a class, the final modifier indicates that the class can not be used as a base class to derive any class from it.
9)A constructor resembles an instance method, but it differs from a method in that it has no explicit return type, it is not implicitly inherited and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring class. They have the task of initializing the object's data members and of establishing the invariant of the class.The copy constructor is a constructor which creates an object by initializing it with an object of the same class. The copy constructor is used to: Initialize one object from another of the same type.
10)The == and can be used to compareobject references,Checks if the values of two operands are equal or not, if yes then condition becomes true.
11)The return statement terminates the execution of a function and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can also return a value to the calling function.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.