- ATiger
- BLion
- CDeerLion
- DNo output
Let's analyze the given Java program to determine its output.
int a = 2;
switch (a) {
case 1:
System.out.print("Tiger");
case 2:
System.out.print("Deer");
default:
System.out.print("Lion");
}
int a = 2;
The variable a
is initialized with the value 2.
switch (a) {
The switch
statement checks the value of a
:
Since a
is 2, it does not match case 1
, so the code inside case 1
is not executed.
The value of a
matches case 2
, so the statement inside case 2
is executed:System.out.print("Deer");
This prints Deer.
Since there is no break
statement, the execution falls through to the default
case:System.out.print("Lion");
This prints Lion.
The output of the program is:
DeerLion
The correct answer is: c) DeerLion
Let's analyze the given expression and the output of the Java Math functions:
Math.pow(36, 0.5) + Math.cbrt(125) + Math.ceil(4.2) + Math.floor(7.9)
The Math.pow(x, y)
method returns x
raised to the power of y
.Math.pow(36, 0.5)
calculates the square root of 36.√36 = 6.0
The Math.cbrt(x)
method returns the cube root of x
.Math.cbrt(125)
calculates the cube root of 125.∛125 = 5.0
The Math.ceil(x)
method returns the smallest integer greater than or equal to x
.Math.ceil(4.2)
returns the smallest integer greater than or equal to 4.2.Math.ceil(4.2) = 5.0
The Math.floor(x)
method returns the largest integer less than or equal to x
.Math.floor(7.9)
returns the largest integer less than or equal to 7.9.Math.floor(7.9) = 7.0
Adding these values together:6.0 + 5.0 + 5.0 + 7.0 = 23.0
The output of the expression is: 23.0
The this
keyword in Java is used for a specific purpose related to the current instance of a class. Here's what each option means in the context of the this
keyword:
a) It is used to declare a constant variable.
this
keyword is not used to declare constant variables. Constant variables in Java are declared using the final
keyword.b) It refers to the current instance of the class.
this
keyword is used to refer to the current instance of the class within its methods and constructors. It helps to distinguish between instance variables and parameters with the same name, and to call other constructors or methods within the same class.c) It is used to call static methods.
this
keyword. The this
keyword cannot be used to call static methods.d) It represents the return value of a function.
return
keyword, not this
.b) It refers to the current instance of the class.
Let's determine the final values stored in the variables x
and y
based on the given code:
double a = 6.35;
double b = 14.74;
double x = Math.abs(Math.ceil(a));
double y = Math.rint(Math.max(a, b));
x
:Math.ceil(a)
:
The Math.ceil(x)
method returns the smallest integer greater than or equal to x
.
For a = 6.35
, Math.ceil(6.35)
returns 7.0.
Math.abs(Math.ceil(a))
:
The Math.abs(x)
method returns the absolute value of x
.
Since Math.ceil(a)
is 7.0, and 7.0 is already positive, Math.abs(7.0)
remains 7.0.
So, the value of x
is 7.0.
y
:Math.max(a, b)
:
The Math.max(x, y)
method returns the greater of the two values.
For a = 6.35
and b = 14.74
, Math.max(6.35, 14.74)
returns 14.74.
Math.rint(Math.max(a, b))
:
The Math.rint(x)
method returns the double value that is closest to x
. If two double values are equally close, the even one is returned.
For Math.max(a, b)
which is 14.74, Math.rint(14.74)
returns 15.0 because 15.0 is the nearest integer to 14.74.
So, the value of y
is 15.0.
The final values stored in variables x
and y
are:
Let's analyze the given Java code step by step to determine its output:
char ch;
int x = 97;
do {
ch = (char) x;
System.out.print(ch + " ");
if (x % 10 == 0)
break;
++x;
} while (x <= 100);
- x
starts at 97
.
- In ASCII, 97
corresponds to the character 'a'.
First Iteration:
- ch = (char) x
converts x
to the character 'a'.
- System.out.print(ch + " ");
prints 'a'.
- x % 10
for 97
is 7
(not zero), so the break
statement is not executed.
- ++x
increments x
to 98
.
Second Iteration:
- ch = (char) x
converts x
to the character 'b'.
- System.out.print(ch + " ");
prints 'b'.
- x % 10
for 98
is 8
(not zero), so the break
statement is not executed.
- ++x
increments x
to 99
.
Third Iteration:
- ch = (char) x
converts x
to the character 'c'.
- System.out.print(ch + " ");
prints 'c'.
- x % 10
for 99
is 9
(not zero), so the break
statement is not executed.
- ++x
increments x
to 100
.
Fourth Iteration:
- ch = (char) x
converts x
to the character 'd'.
- System.out.print(ch + " ");
prints 'd'.
- x % 10
for 100
is 0
, so the break
statement is executed, terminating the loop.
The final output of the code is:
Automatic conversion of primitive data into an object of a wrapper class is called autoboxing.
int
, float
, double
, etc.) and their corresponding wrapper classes (such as Integer
, Float
, Double
, etc.).int
to an Integer
when it is assigned to an Integer
object or used in a context where an Integer
is expected.a) autoboxing
Assertion (A): The String
class in Java is immutable.
String
object is created, it cannot be changed. Any modification to a String
object results in the creation of a new String
object.Reason (R): Immutable objects cannot be modified once they are created, and any operation on a String
object results in the creation of a new String
object.
String
objects are immutable, any operation that appears to modify a String
actually creates a new String
object.a) Both Assertion(A) and Reason(R) are true and Reason(R) is correct explanation of Assertion(A).
To stop the execution of a construct, such as a loop or a switch statement, the correct statement is:
b) break
break
: This statement is used to exit from a loop (like for
, while
, or do-while
) or a switch
statement. When break
is encountered, the execution of the loop or switch construct is immediately terminated.
System.exit(0)
: This terminates the entire Java Virtual Machine (JVM), not just a single construct.
stop
: This is not a valid Java statement for stopping execution.
end
: This is not a valid Java statement for stopping execution.
b) break
Let's analyze the given Java code to determine its output:
public static void main(String[] args) {
int a = 5;
a++;
System.out.println(a);
a -= (a--) - (--a);
System.out.println(a);
}
- int a = 5;
initializes a
with the value 5
.
- a++;
increments a
by 1
. After this operation, a
becomes 6
.
- System.out.println(a);
prints the value of a
, which is 6.
- a--
is the post-decrement operator. It returns the value of a
before decrementing it. So, a--
evaluates to 6
, and then a
is decremented to 5
.
- --a
is the pre-decrement operator. It decrements a
first, so --a
evaluates to 4
, and a
becomes 4
.
- Therefore, the expression a -= (a--) - (--a)
translates to a -= 6 - 4
:
6 - 4
evaluates to 2
.a -= 2
is equivalent to a = a - 2
. Since a
is 4
at this point, this becomes 4 - 2
, which results in 2
.- System.out.println(a);
prints the value of a
, which is 4.
The output of the program is:
a++
operation)a -= (a--) - (--a)
operation)
Let's analyze the corrected Java code to determine its type:
class Out {
int a = 5; // Adding a variable to compare with x
int cal() {
int x = 10;
if (x > a) {
return --x; // Decrements x by 1 and returns 9
} else {
return ++x; // Increments x by 1 and returns 11
}
}
public static void main(String[] args) {
Out ob = new Out(); // Create an instance of the class
int x = ob.cal(); // Call the method cal() and store the result in x
System.out.println(x); // Print the value of x
}
}
The method cal()
performs an operation based on the class-level variable a
and modifies the value of x
. It returns 9
in this case, because x
is greater than a
.
- The method is considered an impure method because it depends on and operates based on mutable state.
The program demonstrates an example of an Impure Method.