- A10
- B12
- C20
- D11
Time Taken:
Correct Answer:
Wrong Answer:
Percentage: %
Let's break down the two string method operations in Java for the string "MISSISSIPPI":
"MISSISSIPPI".indexOf('S')The indexOf('S') method returns the index of the first occurrence of the character 'S' in the string "MISSISSIPPI".
The string "MISSISSIPPI" has the following indices:
M I S S I S S I P P I 0 1 2 3 4 5 6 7 8 9 10
So, "MISSISSIPPI".indexOf('S') returns 2.
"MISSISSIPPI".lastIndexOf('I')The lastIndexOf('I') method returns the index of the last occurrence of the character 'I' in the string "MISSISSIPPI".
So, "MISSISSIPPI".lastIndexOf('I') returns 10.
Now, adding these two results together:
indexOf('S') + lastIndexOf('I') = 2 + 10 = 12
So, the correct answer is:
12
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 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:
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
The parseLong() function is a member of the Long wrapper class.
parseLong() is a static method provided by the Long wrapper class in Java.String into a long value.a) Long wrapper class
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 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.
Class and Main Method:
MCQ contains the main method, which is the entry point for the program.main method, an instance of MCQ is created, and the test method is called with the argument 12.Method test:
test method takes an integer n as a parameter.1 to n (inclusive) using a for loop.n is divisible by x using the condition if (n % x == 0).x followed by a space.Output Calculation:
test method is called with n = 12.1 to 12 and checks for divisibility.12 without leaving a remainder (i.e., the factors of 12) are 1, 2, 3, 4, 6, and 12.| Iteration | x Value |
Condition x <= n |
Condition n % x == 0 |
Action |
|---|---|---|---|---|
| 1 | 1 | true | true | Print 1 |
| 2 | 2 | true | true | Print 2 |
| 3 | 3 | true | true | Print 3 |
| 4 | 4 | true | true | Print 4 |
| 5 | 5 | true | false | Do nothing |
| 6 | 6 | true | true | Print 6 |
| 7 | 7 | true | false | Do nothing |
| 8 | 8 | true | false | Do nothing |
| 9 | 9 | true | false | Do nothing |
| 10 | 10 | true | false | Do nothing |
| 11 | 11 | true | false | Do nothing |
| 12 | 12 | true | true | Print 12 |
| 13 | 13 | false | - | Loop ends |
nested loop
Certainly! Here is an example of a nested loop in Java:
public class NestedLoopExample { public static void main(String[] args) { // Outer loop for (int i = 1; i <= 3; i++) { System.out.println("Outer loop iteration: " + i); // Inner loop for (int j = 1; j <= 2; j++) { System.out.println(" Inner loop iteration: " + j); } } } }
Output:
Outer loop iteration: 1 Inner loop iteration: 1 Inner loop iteration: 2 Outer loop iteration: 2 Inner loop iteration: 1 Inner loop iteration: 2 Outer loop iteration: 3 Inner loop iteration: 1 Inner loop iteration: 2
In this example, the outer loop runs 3 times, and for each iteration of the outer loop, the inner loop runs 2 times. This demonstrates how nested loops work in Java.
The break statement is used to exit the current loop.
Here is an example in Java to demonstrate its usage:
public class BreakStatementExample { public static void main(String[] args) { // Loop from 1 to 5 for (int i = 1; i <= 5; i++) { if (i == 3) { break; // Exit the loop when i is 3 } System.out.println("Iteration: " + i); } System.out.println("Loop exited."); } }
Output:
Iteration: 1 Iteration: 2 Loop exited.
In this example, the break statement exits the loop when i equals 3, so the loop stops executing and the program continues with the next statement after the loop.