Questions in MNC Company
MNC Question – Part 1
- What is classloader? Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java program, it is loaded first by the classloader. There are three built-in classloaders in Java.
Bootstrap ClassLoader: This is the first classloader which is the superclass of Extension classloader. It loads the rt.jar file which contains all class files of Java Standard Edition like java.lang package classes, java.net package classes, java.util package classes, java.io package classes, java.sql package classes, etc.
Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of System classloader. It loads the jar files located inside $JAVA_HOME/jre/lib/ext directory.
System/Application ClassLoader: This is the child classloader of Extension classloader. It loads the class files from the classpath. By default, the classpath is set to the current directory. You can change the classpath using “-cp” or “-classpath” switch. It is also known as Application classloader. - Is Empty .java file name a valid source file name?
Yes, Java allows to save our java file by .java only, without name.
we need to compile it (:) compile it by javac .java (then) run it by java ClassName - Is select, update, start, class or null keyword in java?
NO. - If I don’t provide any arguments on the command line, then what will the value stored in the String array passed into the main() method, empty or NULL?
It is empty, but not null - What if I write static public void instead of public static void?
The program compiles and runs correctly because the order of specifiers doesn’t matter in Java. - What is the output of the following Java program?
class Test
{
public static void main (String args[])
{
System.out.println(10 + 20 + “Javatpoint”);
System.out.println(“Javatpoint” + 10 + 20);
}
} - Is constructor inherited?
No, The constructor is not inherited. - What are the restrictions that are applied to the Java static methods?
a)The static method can not use non-static data member or call the non-static method directly.
b)this and super cannot be used in static context as they are non-static. - Can we override the static methods?
No, we can’t override static methods. - What is the static block?
Static block is used to initialize the static data member. It is executed before the main method, at the time of classloading.class Astatic {
static{ System.out.println(“static block is invoked”); }
public static void main(String args[]){
System.out.println(“Hello main”);
}
}
MNC Question – Part 2
- Can we make the abstract methods static in Java?
In Java, if we make the abstract methods static, It will become the part of the class, and we can directly call it which is unnecessary. Calling an undefined method is completely useless therefore it is not allowed. - What are the main uses of this keyword?
There are the following uses of this keyword.this can be used to refer to the current class instance variable.
this can be used to invoke current class method (implicitly)
this() can be used to invoke the current class constructor.
this can be passed as an argument in the method call.
this can be passed as an argument in the constructor call.
this can be used to return the current class instance from the method. - Why does Java not support pointers?
The pointer is a variable that refers to the memory address. They are not used in Java because they are unsafe(unsecured) and complex to understand. - Can we override the overloaded method?
Yes. - Can we override the private methods?
No, we cannot override the private methods because the scope of private methods is limited to the class and we cannot access them outside of the class. - Can we change the scope of the overridden method in the subclass?
Yes, we can change the scope of the overridden method in the subclass. However, we must notice that we cannot decrease the accessibility of the method. The following point must be taken care of while changing the accessibility of the method.The private can be changed to protected, public, or default.
The protected can be changed to public or default.
The default can be changed to public.
The public will always remain public. - Can we modify the throws clause of the superclass method while overriding it in the subclass?
Yes, we can modify the throws clause of the superclass method while overriding it in the subclass. However, there are some rules which are to be followed while overriding in case of exception handling.If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception, but it can declare the unchecked exception.
If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception. - What is covariant return type?
Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type. The covariant return type specifies that the return type may vary in the same direction as the subclass. - What is the final variable?
In Java, the final variable is used to restrict the user from updating it. If we initialize the final variable, we can’t change its value. In other words, we can say that the final variable once assigned to a value, can never be changed after that. The final variable which is not assigned to any value can only be assigned through the class constructor. - Can we declare a constructor as final?
The constructor can never be declared as final because it is never inherited. Constructors are not ordinary methods; therefore, there is no sense to declare constructors as final. However, if you try to do so, The compiler will throw an error.
<Previous> | <Next> |