Interview Questions : OOPs and Core Java
OOPS and CORE JAVA
1. What is JVM (Java Virtual Machine)?
a. JVM is software implementation which stands on top of the hardware platform and OS. It provides abstraction compiled java program and the hardware and OS. The Java virtual machine executes instructions that a Java compiler generates. All the Java programs are compiled into bytecodes(.class files). JVM interpret Java bytecodes into instructions understandable by the particular processor.
2. What is OOPs?
a. Object-Oriented Programming (OOP) is a programming language model orgnized around the “Objects” rather than the “actions” and data rather than the logic. Programming techniques may include data abstraction, encapsulation, inheritance, information hiding, modularity and polymorphism.
3. What is Class?
a. Class is a template that describes the kinds of state and behavior that object of its type supports.
4. What is Object?
a. Object is run time entity and will have its own state and access to all of its behaviors defined by its class. At run time when JVM encounters the “new” keyword , it will use appropriate class create a object which is instance its class.
5. What is Interface?
a. Interface is 100% abstract super class that defines the methods a subclass must support, but not how they supported. A powerful companion to inheritance is the use of Interfaces.
6. What is abstract class?
a. Abstract class can never be instantiated, its sole purpose is to be extended(to subclassed). It is incomplete class.
7. What is the purpose of abstract class?
a. Consider a abstract class represent Person, it has three methods “numOfEyes”, “numOfLegs” and “sex”. Now conceptually all humans have to two legs and two eyes, but sex is something which can vary from person to person. So write implementaton for methods “numOfEyes” and “numOfLegs” and leave the method “sex” implemetation to subclass. Now class Male and class Female extends Person, implementation for method “sex” of Female is implemented in class Female and of Male is implemented in class Male.
8. What are the abstract methods?
a. Abstract methods do not have implementation. Abstrct methods should be implemeted in the subclass which inherits them. The class inherits abstract class should implement all the abstract methods or else java compiler will throw an error. Abstract class can have abstract methods. Abstract methods defined using “abstract” keyword.
9. What is state (instance variables) in Java?
a. State (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object’s instance variables make up the object’s state.
10. What is Behavior (methods) in Java?
a. Behavior (methods) When a programmer creates a class, she creates methods for that class. Methods are where the class’ logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.
11. What is relation between Classes and Objects?
a. They look very much same but are not same. Class is a definition, while object is instance of the class created. Class is blue print or prototype while objects are actual existing in real world. For example CAR is class and Mustang is object of the class.
12. What is the difference between “Abstract” classes and “Interfaces”?
a. Abstract classes can only be extended by any class while Interface can not be, it has to be, implemented. Interfaces can not implement methods, whereas an abstract class can have implementation. Class can implement any number of interfaces but only one super class.
13. What is difference between Static and Non-Static fields of class?
a. Static fields are fields per class whereas Non-Static fields are fields per object of the class. Non-Static values are called as instance variables. Each object of the class has its own copy of Non-Static instance variables. So when a new object is created of the same class it will have completely its own copy of instance variable. Where in Static values have only one copy of instance variables and will be shared among all the objects of the class.
14. What is use of “instanceof” keyword?
a. “instanceof” keyword is used to check what is the type of the object.
15. How do refer to a current instance of object?
a. You can refer the current instance of object using “this” keyword.