- AIt allows the method to be called without creating an instance of the class.
- BIt makes the method execute faster.
- CStatic keyword prevents the method from being overridden.
- DAll of these.
Time Taken:
Correct Answer:
Wrong Answer:
Percentage: %
It allows the method to be called without creating an instance of the class.
In Java, the static keyword is used for:
Static Methods and Variables:
Methods: A static method belongs to the class rather than instances of the class. This means you can call the method directly on the class itself without needing to create an instance.
class Example { static void staticMethod() { System.out.println("Static method"); } } // Calling the static method without creating an instance Example.staticMethod();
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.
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
The method used to extract a single character from a String object in Java is charAt().
charAt(int index) is a method of the String class that returns the character at the specified index in the string. The index is zero-based, meaning the first character is at index 0.c) charAt()
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
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 |
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.
If a 'for loop' is used within a 'for loop', it is called a nested for loop.
If a 'while loop' is used within a 'while loop', it is called a nested while loop.