Sunday, 22 January 2017

SET - II

What is the base class of all classes ?
java.lang.Object

Does java support multiple inheritance ?
Java doesn't support multiple inheritance with classes.

Is java a pure object oriented ?
99.999999% java is purely object oriented.

Is arrays primitive data types ?
In java, arrays are referenced types.

What is the difference between Path and Classpath ?
Path and ClassPath are operating system level environment variables. Path is used to define where the system can find the executable(.exe) files and classpath is used to specify the location .class files.

What are local variables ?
If we are defining any variable inside a method (or) block (or) constructor then that variable is called as local variables. Before using the local variable must be provided the initialization.

What are instance variables ?
Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized with their default values i.e. JVM will provide the default initialization for instance variables.

How to define a constant variable in java ?
The variable should be declared as static and final, so only one copy of the variable exist for all instances of the class and the value can't be changes. static final int PI=2.14; is an example for constant.

Should a main() method be compulsory declared in all java classes ?
No, not required. The main() method should be defined only if the source class is a java application.

What is the return type of the main() method ?
main() method doesn't return anything hence declared as void.

Why is the main() method declared static ?
main() method is called by the JVM even before the instantiation of the class, hence it is declared as static.

What is the argument of main() method ?
main() method accepts an array of String object as command line arguments.

Can a main() method be overloaded ?
Yes, we can define any number of main() methods with different method signature and implementation in the class.

Can a main() method be declared as final ?
Yes, any inheriting class cannot have it's own default main() method.

Does the order of public and static declaration matter in main() method ?
No, it doesn't matter but void always come before main().

Can a source file contain more than one class declaration ?
Yes a single source file can contain any number of class declarations but only one othe classes can be declared as public.

What is a package ?
package is a collection of related classes and interfaces, package declaration should be the first statement in a java class.

What package is imported by default ?
java.lang package is imported by default even without an import statement.

Can a class declared as private be accessed outside it's package ?
Not possible. But outer classes are not possible to declare with private, only inner classes are allowed to declare with private.

Can a class be declared as protected ?
As outer class can't be declared as protected. But inner class is allowed to declare with protected. Note : private and protected modifiers are applicable only for members(i.e. for Inner classes, for variables, for methods and for constructors).

If a class does not have main method, how can we execute user defined methods of that class ?
We should use another class main method and call this method with its class name. In projects, we do not write main method in every class, instead we write main method in one class and will call all other classes methods from this class's main method for testing. Basically main method is given to start class logic execution but not for developing logic directly.

Is it possible to create user defined method in all classes with same name ?
Yes, we can define user defined methods with same name in multiple classes. To call those methods we must use that method's class name.

In a single java file, can we define main method in all classes ?
Yes, we can define. There is no Compile Error, Runtime Error because compiler stores each class byte code in separate .class file there is no confusion for JVM in execution of main method.

Can we call main method explicitly ?
Yes, even the main method is a method, so we can call Syntax to call main method. main(new String[0]);

What happens when we pass negative number as size to array object ?
For example - main(new String[-3]);
No CE, but it leads exception "java.lang.NegativeArraySizeException".

No comments:

Post a Comment