Question 2
Answer:
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 |
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.