1. Given the following int i,j-4; int[] a, b- 11, 2, 3; using picture to illustr
ID: 3753994 • Letter: 1
Question
1. Given the following int i,j-4; int[] a, b- 11, 2, 3; using picture to illustrate the difference of the following statements 2. What is a state of an object? What is the state of a class? 3. When should we declare a method to be static? 4. Given assignment statement (a) Ifx and y are primitive variables, when w a compilation error? it generate (b) Ifx and y are reference variables and the statement is valid (no compilation error), how are the types of x and y related to each other? 5. Suppose you have the following function prototype and variable declaration: public void goop (int[ ] z) int [ ] x new int [10]; Which is the correct way to call the goop method with x as the argument .A. goop(x); .B. goop(x D; C. goop(x[10]); .D. goop(&x); E. goop(&x[ );Explanation / Answer
Below are your answers: -
1. The meaning of both statements are as below: -
i = j; - > copy of value of j is assigned to i
a = b; - > address of first element of b is assigned to a. So a now contains 3 elements copied from b i.e. 1,2,3
2. State of the object is value of the object. i.e. if a = 3, then a is the object and 3 is the state of the object.
State of class is the reference or the obejct assigned to that class
3. We should declare a method static when the execution of method is not dependent on the object or instance of class. So the method can be called without creating an object of the class.
4. a) x = y, when x,y are primitive, will generate error when both are of different types, for eg, x is double and y is boolean. We need to resolve this error by type casting.
b) x = y, x,y are reference type, this will be valid if and only if x and y are objects of same class or x is parent class of y. (inheritance)
5.a) goop(x) is the correct way.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.