Math.abs(x * x + 2 * x * y)
(b) Write the return data type of the following functions:
- startsWith()
- random()
- boolean
- double
(c) If the value of basic=1500, what will be the value of tax after the following statement is executed?
tax = basic > 1200 ? 200 :100;
Value of tax will be 200. As basic is 1500, the condition of ternary operator — basic > 1200 is true. 200 is returned as the result of ternary operator and it gets assigned to tax.
(d) Give the output of following code and mention how many times the loop will execute?
int i;
for( i=5; i>=1; i--)
{
if(i%2 == 1)
continue;
System.out.print(i+" ");
}
Output of the above code is:
4 2
Loop executes 5 times. The below table shows the dry run of the loop:
| i | Output | Remark |
|---|
| 5 | | 1st Iteration — As i % 2 gives 1 so condition is true and continue statement is executed. Nothing is printed in this iteration. |
| 4 | 4 | 2nd Iteration — i % 2 is 0 so condition is false. Print statement prints 4 (current value of i) in this iteration. |
| 3 | 4 | 3rd Iteration — i % 2 is 1 so continue statement is executed and nothing is printed in this iteration. |
| 2 | 4 2 | 4th Iteration — i % 2 is 0. Print statement prints 2 (current value of i) in this iteration. |
| 1 | 4 2 | 5th Iteration — i % 2 is 1 so continue statement is executed and nothing is printed in this iteration. |
| 0 | 4 2 | As condition of loop (i >= 1) becomes false so loops exits and 6th iteration does not happen. |
(e) State a difference between call by value and call by reference.
In call by value, actual parameters are copied to formal parameters. Any changes to formal parameters are not reflected onto the actual parameters. In call by reference, formal parameters refer to actual parameters. The changes to formal parameters are reflected onto the actual parameters.
(f) Give the output of the following:
Math.sqrt(Math.max(9,16))
The output is 4.0.
First Math.max(9,16) is evaluated. It returns 16, the greater of its arguments. Math.sqrt(Math.max(9,16)) becomes Math.sqrt(16). It returns the square root of 16 which is 4.0.
(g) Write the output for the following:
String s1 = "phoenix"; String s2 ="island";
System.out.println(s1.substring(0).concat(s2.substring(2)));
System.out.println(s2.toUpperCase());
The output of above code is:
phoenixland
ISLAND
s1.substring(0) results in phoenix. s2.substring(2) results in land. concat method joins both of them resulting in phoenixland. s2.toUpperCase() converts island to uppercase.
(h) Evaluate the following expression if the value of x=2, y=3 and z=1.
v=x + --z + y++ + y
v = x + --z + y++ + y
⇒ v = 2 + 0 + 3 + 4
⇒ v = 9
(i) String x[] = {"Artificial intelligence", "IOT", "Machine learning", "Big data"};
Give the output of the following statements:
- System.out.println(x[3]);
- System.out.println(x.length);
- Big data
- 4
(j) What is meant by a package? Give an example.
A package is a named collection of Java classes that are grouped on the basis of their functionality. For example, java.util.