Write the out in the corresponding Public class Test Public
Solution
1. Solution ->
Note: Whenever we refer to an array we start the index from 0. So, Array[0] indicates 0th position and Array[3] indicates 4th position.
Initially an array A is declared with elements 1, 2, 3 and 4.
Then, a variable of type integer is declared and assigned a value of 7. => x = 7
Then a function method(x, A) is called which increases the value of x by 1 and changes the array value at position 3. Now the value of variable won\'t change outside the function as the value is passed by \"call by value method\". On the other hand, arrays are always passed by \"call by reference\" method. Hence, the value in the array will change outside the function.
So, the value of x outside the function is 7 itself and the array A comprises of {1, 2, 3, 5}.
Then, A new array B is declared as Integer and size of the array is defined as 3 => int []B = new int[3];
Further, a 2D array is created named list which contains heterogeneous array size. So, list array has 2 1D arrays with different size. The individual 1D array in the list array has 2 and 3 elements respectively.
Next, Array A is assigned to B. Now, Array A and Array B comprises of {1, 2, 3, 5}.
As, B[2] = 6. So, the value at 3rd position in array B is changed from 3 to 6. As A was assigned to B, both A and B are referring to the same array and hence A[2] is also 6. Now, the array A and array B comprises of {1, 2, 6, 5}.
Hence, the output for the following variables will be as follows -
B.length = 4; (as finally B contains 1, 2, 6, 5)
x = 7; (as it is not changed outside the function method1(x, A))
B[3] = 5; (as the fourth element in array B {1, 2, 6, 5} is 5)
A[2] = 6; (as the second element in array A {1, 2, 6, 5} is 6)
list.length = 2; (as list contains 2 individual 1D arrays)
list[1][2] = 1; (as the second 1D array has elements {3, 8, 1} and the 3rd element in this array is 1)
2. Solution ->
This is an example of method overloading. Having the same function name with different parameters can be referred to as Method Overloading. In the above question, the fun function is declared many times, although with different parameters. This can increase the code readiness as you don\'t have to devise various fucntions with different parameters.
As a practical example, let us consider the ability to fly. Now, different creatures/things fly in different way. As an example, birds fly differently, some reptiles fly in the form of gliding, aeroplanes and helicopters fly in a different fashion. Suppose you create a generic class FlyingAbility, then you can call the function fly for all creatures/things by passing separate arguments.
Sometimes, type promotions occurs in this case. A data type can be promoted to next higher data type by the compiler if the type match is not found. Type demotion is not feasible as it can cause data loss. Fox example, 5.3 (double) cannot be demoted to 5 (int) as it will cause the loss of 0.3. Type promotion is feasible as it does not cause any data loss. Fox example, 5 (int) is converted to 5.0 (double) which does not result in data loss.
Hence, the output of the above is as follows -
Houstan (as first parameter is int and second is char type)
Edison (as both the parameters are of double type)
Edison (as first parameter is int and second is double type. So promotion has occurred and int is promoted to double and hence, it is treated as fun(double, double))
Newark (as first parameter is double and second is int type)
Boston (as both the parameters are of int type)
3. Solution ->
In the above question, a simple for loop is applied. Initially, the variables are initialized as follows
i = 0, j = 0 and key = 3
An array list is created with elements {1, 2, 3, 4, 5, 6}
The for loop is used to iterate through the list.
The iteration starts from i i.e 0 and goes till list.length i.e 6.
So, while iteration, a comparison takes place and compares every element of list to the key.
As, list[2] == 3 i.e key, hence the loop enters the condition and sets j to 2.
In the last iteration i = 5 and list.length = 6. As 5 < 6, it checks the condition. As it doesn\'t satisfy the condition, the value of i is incremented and again checked with the termination condition of for loop.
Now, i = 6. As, 6 is not less than list.length i.e 6, it comes out of the loop and prints the values of i and j as follows
i = 6
j = 2
4. Solution ->
In the above question, initially all variables are initialized to different values.
Initially,
i , x, y, z = 0
j = 2
k = 3
Then, we compare i and j. Here, i = 0 and j = 2; So, i < j, hence it does not fulfill the first condition.
Then, it checks the else condition. As, i != j hence, it goes to the else condition and sets z = 5;
Now j = 0 and k = 3
As, j < k, hence it sets y = 7.
Thus, the outputs are as follows -
x = 0 (wasn\'t changed after it was initialized as 0)
y = 7 (set as j < k)
z = 5 (set as i < j)
5. Solution ->
In the above question two classes are defined, one being the Test class and other the Hello class.
The program starts by initiating an instance of class Hello by calling its constructor.
The first time, default constructor is invoked and the second time parameterized constructor is called.
Hello a = Hello(); calls the default constructor and the value of bal associated with a is set to 1000 and id is set to 0 as nothing is specified.
Hello b = Hello(1, 300); calls the parameterized constructor and the value of bal associated with b is set to 300and id is set to 1 as both these values are passed while calling the constructor of the class Hello.
In Hello class, \"this\" keyword is used in constructor to refer to the same object that invoked the constructor.
When a calls print, bal associated with a is printed i.e. 1000.
When b calls print, bal associated with b is printed i.e. 300.
The output of various objects of class Hello is printed as below -
a.id = 0 (as id associated with a is 0)
a.bal = 1000 (as bal associated with a is 1000)
b.id = 1 (as id associated with b is 1)
b.bal = 300 (as bal associated with b is 300)
![Write the out in the corresponding Public class Test {Public static void main(string[] args} {int [] A = {1, 2, 3, 4); int x = 7; method(x, A); int [] B = new Write the out in the corresponding Public class Test {Public static void main(string[] args} {int [] A = {1, 2, 3, 4); int x = 7; method(x, A); int [] B = new](/WebImages/4/write-the-out-in-the-corresponding-public-class-test-public-978709-1761502248-0.webp)
![Write the out in the corresponding Public class Test {Public static void main(string[] args} {int [] A = {1, 2, 3, 4); int x = 7; method(x, A); int [] B = new Write the out in the corresponding Public class Test {Public static void main(string[] args} {int [] A = {1, 2, 3, 4); int x = 7; method(x, A); int [] B = new](/WebImages/4/write-the-out-in-the-corresponding-public-class-test-public-978709-1761502248-1.webp)
![Write the out in the corresponding Public class Test {Public static void main(string[] args} {int [] A = {1, 2, 3, 4); int x = 7; method(x, A); int [] B = new Write the out in the corresponding Public class Test {Public static void main(string[] args} {int [] A = {1, 2, 3, 4); int x = 7; method(x, A); int [] B = new](/WebImages/4/write-the-out-in-the-corresponding-public-class-test-public-978709-1761502248-2.webp)