Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Java Interview Question 2

Q.  Is Empty .java file name a valid source file name?

Ans:- Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname Let's take a simple example:
1. //save by .java only
2. class A{
3. public static void main(String args[]){
4. System.out.println("Hello java");
5. }
6. }
7. //compile by javac .java
8. //run by     java A
compile it by javac .java
run it by java A

Q. What is constructor? And the purpose of default constructor. 

Ans:- Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.
The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

Q. Does constructor return any value?

Ans:- yes, that is current instance (You cannot use return type yet it returns a value).

Q. Is constructor can inherited?

Ans:- No, constructor is not inherited.

Q. Can we make a constructor final?

Ans:- No, constructor can't be final.

Q. What is static block?
Ans:- Static block is used to initialize the static data member. and It is executed before main method at the time of class loading.

Q.Why main method is static?

Ans:- Because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation.

Q. What is static method?

Ans:- A static method belongs to the class rather than object of a class. it can be invoked without the need for creating an instance of a class. A static method can access static data member and can change the value of it.

Q. Define the static variable.
Ans:- A static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,institute name of students etc.
A static variable gets memory only once in class area at the time of class loading.

Q. Is it possible that we execute a program without using main() method?

Ans) Yes, one of the way is static block.

Q. What is 'this' keyword in java?

Ans:- 'this' is a keyword that that refers to the current object.

Q. If I don't provide any arguments on the command line, then the String array of Main method will be empty or null?

Ans:- It is empty. But not null.

Q. Create the difference between object oriented programming language and object based programming language?

Ans:- Object based programming languages follow all the features of OOPs except Inheritance. e.g.  JavaScript, VBScript etc.

Q. What will be the initial value of an object reference which is defined as an instance variable?

Ans:- The object references are all initialized to null in Java.

Q. What will happened if the static modifier is removed from the signature of the main method?

Ans:- Program compiles, but at runtime system throws an error "NoSuchMethodError".

Q. What are the difference between static (class) method and instance method?

Ans:-  1. A method i.e. declared as static is known as static method.
              A method i.e. not declared as static is known as instance method.
          2. Object is not required to call static method.
              Object is required to call instance methods.
          3.Non-static (instance) members cannot be accessed in static context (static method, static     
             block and static nested class) directly.
             static and non-static variables both can be accessed in instance methods.
         4. E.g. public static int cube(int n){ return n*n*n;}
             E.g. public void msg(){...}.

No comments:

Post a Comment