61-70 Flashcards
A person says that he compiled a java class successfully without even having a main method in it? Is it possible?
main method is an entry point of Java class and is required for execution of the program however; a class gets compiled successfully even if it doesn’t have a main method. It can’t be run though.
Can we call a non-static method from inside a static method?
Non-Static methods are owned by objects of a class and have object level scope and in order to call the non-Static methods from a static block (like from a static main method), an object of the class needs to be created first. Then using object reference, these methods can be invoked.
What are the two environment variables that must be set in order to run any Java programs?
Java programs can be executed in a machine only once following two environment variables have been properly set:
PATH variable
CLASSPATH variable
Can variables be used in Java without initialization?
In Java, if a variable is used in a code without prior initialization by a valid value, program doesn’t compile and gives an error as no default value is assigned to variables in Java.
Can a class in Java be inherited from more than one class?
In Java, a class can be derived from only one class and not from multiple classes. Multiple inheritances is not supported by Java.
Can a constructor have different name than a Class name in Java?
Constructor in Java must have same name as the class name and if the name is different, it doesn’t act as a constructor and compiler thinks of it as a normal method.
Can a dead thread be started again?
In java, a thread which is in dead state can’t be started again. There is no way to restart a dead thread.