Home / Questions / Question 2
Explanatory Question

Question 2

👁 107 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

Question 2

(a) Differentiate between boxing and unboxing.

Answer

Boxing is the conversion of primitive data type into an object of its corresponding wrapper class. Unboxing is the opposite of Boxing, it is the conversion of wrapper class object into its corresponding primitive data type. Below program highlights the difference between the two:

public class Boxing {
    public static void main(String args[]) {
        int a = 100, b;
        //Boxing
        Integer aWrapped = new Integer(a);
        //Unboxing
        b = aWrapped;
        System.out.println("Boxed Value: " + aWrapped);
        System.out.println("Unboxed Value: " + b);
    }
}

(b) Rewrite the following condition without using logical operators:

if ( a>b || a>c )
    System.out.println(a);

Answer

Condition without using logical operators:

if (a > b)
    System.out.println(a);
else if (a > c)
    System.out.println(a);

(c) Rewrite the following loop using for loop:

while (true)
System.out.print("*");

Answer

Loop rewritten using for:

for (;;)
    System.out.print("*");

(d) Write the prototype of a function search which takes two arguments a string and a character and returns an integer value.

Answer

Below is the function prototype required in the question:

int search(String str, char ch)

(e) Differentiate between = and == operators.

Answer

= ==
It is the assignment operator used for assigning a value to a variable It is the equality operator used to check if a variable is equal to another variable or literal
E.g. int a = 10; assigns 10 to variable a E.g. if (a == 10) checks if variable a is equal to 10 or not