Which keyword is used to declare a method that cannot be overridden in Java?
A. final
B. static
C. abstract
D. private
Select an option to see the answer and solution.
What is method hiding in Java?
A. Defining a new method with the same name in a subclass
B. Making a method private in a subclass
C. Redefining a method with a different return type
D. Defining a method with the same name in the superclass
Select an option to see the answer and solution.
What is the purpose of the "return" statement in a method in Java?
A. To specify the method's access modifier
B. To indicate that the method should return a value
C. To stop the execution of the program
D. To indicate the method is static
Select an option to see the answer and solution.
In Java, can a method have the same name as the class?
A. Yes, it is a constructor
B. Yes, it is a static method
C. Yes, it is a public method
D. No, it is not allowed
Select an option to see the answer and solution.
What is method signature in Java?
A. The method name and its parameter types
B. The method's return type
C. The method's access modifier
D. The method's implementation
Select an option to see the answer and solution.
In Java, which access modifier allows a method to be accessed from any class in any package?
A. public
B. private
C. protected
D. default (package-private)
Select an option to see the answer and solution.
What is a non-static (instance) method in Java?
A. A method that belongs to an instance of a class
B. A method that cannot be called on objects
C. A method that is marked as "static"
D. A method that returns a static value
Select an option to see the answer and solution.
In Java, what does the "final" keyword indicate when used with a method?
A. The method cannot be overridden in subclasses
B. The method is a static method
C. The method is a constructor
D. The method cannot be accessed from other classes
Select an option to see the answer and solution.
What is the difference between a method declaration and a method definition in Java?
A. Declaration specifies method name, return type, and parameters; definition provides the implementation
B. Declaration is used for constructors, and definition is used for regular methods
C. Declaration is optional, and definition is mandatory
D. Declaration and definition are the same in Java
Select an option to see the answer and solution.
In Java, what is the purpose of method overloading?
A. Defining multiple methods with the same name but different parameters
B. Hiding methods in a superclass
C. Redefining methods in a subclass
D. Calling methods from other classes
Select an option to see the answer and solution.
What is the expected output?
public class Profile {
private Profile(int w) { // line 1
System.out.print(w);
}
public static Profile() { // line 5
System.out.print (10);
}
public static void main(String args[]) {
Profile obj = new Profile(50);
}
}A. Won't compile because of line (1), constructor can't be private
B. 10 50
C. 50
D. Won't compile because of line (5), constructor can't be static
Select an option to see the answer and solution.
The following code contains one compilation error, find it?
public class Test {
Test() { } // line 1
static void Test() { this(); } // line 2
public static void main(String[] args) { // line 3
Test(); // line 4
}
}A. At line 1, constructor Tester must be marked public like its class
B. At line 2, constructor call
C. At line 3, compilation error, ambiguity problem, compiler can't determine whether a constructor
D. At line 4
Select an option to see the answer and solution.
What will be the return type of a method that not returns any value?
A. void
B. int
C. double
D. None of the above
Select an option to see the answer and solution.
Which of the following options is the best for generating random integer 0 or 1?
A. (int)Math.random()
B. (int)Math.random() + 1
C. (int)(Math.random() + 0.5)
D. (int)(Math.random() + 0.2)
Select an option to see the answer and solution.
What is Math.floor(3.6)?
Select an option to see the answer and solution.
In which area of memory, the system stores parameters and local variables whenever a method is invoked?
A. Heap
B. Storage Area
C. Stack
D. Array
Select an option to see the answer and solution.
What is the expected output?
public class Profile {
private Profile(int w) { // line 1
System.out.print(w);
}
public final Profile() { // line 5
System.out.print(10);
}
public static void main(String args[]) {
Profile obj = new Profile(50);
}
}A. Won't compile because of line (1); constructor can't be private
B. Won't compile because of line (5); constructor can't be final
C. 50
D. 10 50
Select an option to see the answer and solution.
What is the expected output?
class Animal {
Animal() {
System.out.println("Animal");
}
}
class Wild extends Animal{
Wild() {
System.out.println("Wild");
super();
}
}
public class Test {
public static void main(String args[]) {
Wild wild = new Wild();
}
}A. Animal Wild
B. Wild Animal
C. Runtime Exception
D. Compilation Error
Select an option to see the answer and solution.
Which of the modifier can't be used for constructors?
A. public
B. private
C. static
D. protected
Select an option to see the answer and solution.
The variables declared in a class for the use of all methods of the class are called
A. reference variables
B. objects
C. instance variables
D. None of these
Select an option to see the answer and solution.