(a) Write a Java expression for the following:

\[ \lvert x^2 + 2xy \rvert \]

(b) Write the return data type of the following functions:

  1. startsWith()
  2. random()

(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;

(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+" ");
}

(e) State a difference between call by value and call by reference.

(f) Give the output of the following:
       Math.sqrt(Math.max(9,16))

(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());

(h) Evaluate the following expression if the value of x=2, y=3 and z=1.

  v=x + --z + y++ + y

(i) String x[] = {"Artificial intelligence", "IOT", "Machine learning", "Big data"};

Give the output of the following statements:

  1. System.out.println(x[3]);
  2. System.out.println(x.length);

(j) What is meant by a package? Give an example.

Single Choice
Views 108

Answer:

Answer (a)

Math.abs(x * x + 2 * x * y)

 

Answer (b)

  1. boolean
  2. double

Answer (c)

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.

Answer (d)

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.

Answer

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))

Answer

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.

 

Answer (g)

The output of above code is:

phoenixland
ISLAND

(h) Evaluate the following expression if the value of x=2, y=3 and z=1.

  v=x + --z + y++ + y

Answer

    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:

  1. System.out.println(x[3]);
  2. System.out.println(x.length);

Answer

  1. Big data
  2. 4

(j) What is meant by a package? Give an example.

Answer

A package is a named collection of Java classes that are grouped on the basis of their functionality. For example, java.util.

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.