OOP's Interview Questions

21 - Why java does not support operator overloading?
No, java doesn't support operator overloading because it's too easy to misuse operator overloading or to get it simply wrong.
22 - Why main method static in java?
Main method is static in java because jvm can call it without creating any instance of class which contains main method.
23 - What is the difference between object oriented and object based programming languages?
  • Object-oriented language supports OOPs features whereas object-based language doesn't support all the features of OOPs like Polymorphism and Inheritance.
  • OOPs doesn't has in-built object but Object-based language has in-built object.
  • OOPs languages are C++, Java etc and Object-based languages are Javascript, VBscript etc.

24 - Is it possible to keep private constructor in a class?
Yes, you can have private constructor in a class, defining a private constructor implies that only the native class (class in which the private constructor is defined) is allowed to create an instance/object of the class, and no other caller class is permitted to do so.
25 - How to run java class without main method?
Use static block like
public class Demo
{
	static 
	{
		System.out.println("Inside Static Block");
		System.exit(0);
	}
}
The above example will run without main method as static block runs first and it terminate without any error or warning because System.exit(0) will execute, but from java 7 onwards the above code will compile but not run without main method.
26 - What if the main method is declared as private?
The program compiles properly but at runtime it will give "Main method not public." message.
27 - What are the differences between Class Methods and Instance Methods?
Class methods are those which are declared as static because these methods are call without creating instance of the class.
Instance methods are those which require a instance of a class. Instance method require a object to call.
28 - What are the differences between Final, finally, Finalize methods?
Final keyword is used along with variables, methods and class. To define constant variable final is used. Final with method means method will not override by sub classes. Final with class means other class will not extends final class.
Finally is used for exception handling along with try and catch. You atleast need catch or finally block. Finally block execute every time weather you write return or System.exit(0) etc. so finally block is use to write system cleanup code like code to close jdbc connection etc.
finalize() is called by Garbage collection thread just before collecting eligible Objects.
29 - Can we instantiate an abstract class?
No, you can't initialize an abstract class at all.
30 - What is type casting?
Type casting lets you convert primitive values from one type to another. Java use two types of type casting
  • Implicit type casting.
  • Explicit type casting.
Java do implicit type casting internally and explicit type is done by developers according to their need.
Implicit type casting example -
int a;      //take 4 bytes in memory
byte b=20;  //take 1 byte in memory
a=b; 	    //Implicit cast ( this cast conversion happens automatically , because bigger type can hold smaller type)
Explicit type casting example -
1 - int d=90; 
    byte a=(byte) d;
    System.out.println(a);  //print 90
    
2 - float a=90.10f;
    int b;
    b=(int) a; //explicit cast (convert bigger type into smaller type), and programmer has to do explicitly
	

31 - What is Byte Code?
Byte code is a file generated by a compiler after compiling source code. Byte code is a machine independent code store with .class extension that is executed by a jvm [java virtual machine].
32 - What is the difference between JDK and JRE?
JDK is bundle of software that you can use to develop Java based software. JDK contains of various development tools like the Java source compilers, bundling and deployment tools, debuggers, development libraries, etc.
JRE is an implementation of the Java Virtual Machine which actually executes Java programs.
33 - What is the difference between a break statement and a continue statement?
Both statements work when condition becomes true. Break statement break and come out from the loop when condition is satisfied/true.
Continue statement skips the current iteration of a loop. Continue skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.
34 - What is the difference between an if statement and a switch statement?
If statement is the simple and easy control flow statement that execute the block of code after if and only if all the conditions contained in the if clause are met.
The switch statement takes one argument that controls the path of execution it will follow. Switch is very useful when you would otherwise be using a list of if statements that all check the value of the same variable. Switch is faster than if statement.
35 - Can you create a class as static?
Yes, you can have inner class as static.
36 - Explain what Just-in-time compilation means?
JIT is the part of the Java Virtual Machine (JVM) that is used to speed up the execution. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduce compilation time.
37 - What is difference between static block and init block?
Static block only run when class is loaded but init block is called every time before object is created that is before constructor executes.
Example -
public class Demo 
{
    static
    {
        System.out.println("Inside static");
    }
    
    {
        System.out.println("Inside init");
    }
    public static void main(String args[]){
        new Demo();
        new Demo();
    }
}
	
Output:
Inside static
Inside init
Inside init	
	

38 - Why static methods cannot access non static variables or methods?
Static members initialized when class is loaded into JVM and instance variable has different value for each object and they get created when instance of an class is created. So if you try to access a non static variable without any instance compiler will give error "non-static variable cannot be referenced from a static context" because those variables are not yet created and they don't have any existence until an instance is created. So you can't call variables which has no existence.
39 - What modifiers are allowed for methods in an Interface?
Only public modifier is allowed for methods in an interface because usually interfaces are in other packages, so to use and override their methods, interface methods are always public.
40 - Why do we need the import statement?
Import statement in java allows users to access classes from different packages using the class name. You define a path for jvm using import statement , so that jvm can understand which class you are using.