Java Interview Questions Part – 1

.1. What is JVM?
Java Virtual Machine, which provides JRE in which byte codes can be executed.

2. What is JRE?
Java run time environment, which is implemented by JVM. This has minimum requirements for executing java byte code. Like supporting files, core classes etc.

3. What is JDK?
It is Java development kit, Which has JRE and developing tools.

4. What is JIT?
JIT is just in time compiler, which compiles the code at run time. Compiler is the one which translates programming code to object code. Machine understandable code

5. What is an object?
Object is the one which has state and behaviour

6. What is a class?
Class is a blueprint that describes the behaviour that object of its type supports

7. What is a method?
Collection of statements grouped together to perform an operation

8. Inheritance: When one class acquires the properties of another class, it is known as inheritance. It uses extends keyword. In java multiple inheritance is not possible. Once class and extend only one class

9. Encapsulation: Process of hiding irrelevant information to other part of the program in known as encapsulation

10. Abstraction: Process of proving relevant information to other part of the program is known as Abstraction

11. Polymorphism: Object taking different forms based on substances.
Method Overloading: When same method name is used more than one time in same class but with different argument it is knows as method Overloading
Method Overriding: Same method name with same arguments in base and subclass is known as method overriding

12. Can Main() be overloaded?
Yes

13. Static Method and Variables

  1. Static variables value is same for all instances of same class
  2. Static method belongs to the class and can be invoked without creating instance of the class
  3. Static method cannot have non-static data member.
  4. This and super cannot be used with static context

14. Class Modifiers

  1. Default:
    1. A class with no modifier is known as Default Modifier
    2. Package level access, can be seen only by classes within same package
  2. Public:
    1. Gives access to all classes from all packages.
    2. But if we need to import public class from another package, we still need to import it to use it.
  3. Private:
    1. Class level access, Can be used in a class where private member is declared.
    2. When extended we wont be able to inherit private member variable
    3. We can see private method/private member in subclass with the same name as Super class. But that don’t mean it is been overridden. That is just a method declared in sub-class.
  4. Protected:
    1. Protected member can be accessed by subclass even if the subclass is in different package (Through Inheritance)
    2. The difference between default and protected is, Default can be accessed in subclass but only within same package. But protected can be accessed by subclass even in different package.
  5. Final:
    1. Final key word can be applied for variables, methods and classes
    2. Variable marked as final cannot change its value.
    3. Final methods cannot be overridden
    4. Final class cannot be extended.
    5. When a final variable is not initialized a value at run time is known as blank final variable
    6. We can declare final method in subclass with same name but with different arguments
    7. We can initialize blank final variable using constructor
  6. Abstract:
    1. Abstract class can never be instantiated, it can only be extended
    2. Abstract class can have both abstract and non-abstract methods
    3. If one method is declared as abstract, the class should be declared as abstract
    4. One abstract class can extend another abstract class but doesn’t necessarily implement all methods
    5. A class cant be marked both as abstract and final. Why?
      1. Final classes cannot be extended, But abstract class can only be Inherited
      2. Methods cannot be both Final and private in abstract class, as both cannot be subclassed.

15. Interface:

  1. Interfaces are similar to class.
  2. Can have abstract methods, default methods and static methods.
  3. One interface can extend another interface and a class can implement interface not extend it
  4. Interface doesn’t have constructors
  5. All the methods in interfaces are abstract

16. Difference between Abstract class and Interface.

Abstract Class Interface
Can have constructor Cannot have constructor
Can have fields that are non-static, final All the fields are public static final
Can have methods with access specifiers like public, protected and private All methods are public

 As from Java 8, Interfaces can also have static and default methods which is not mandatory to be implemented by implementing class.

17. Stack Memory vs Heap Memory:

Stack Memory Heap Memory
Stack memory is static memory, allocated during compile time Heap Memory is dynamic memory allocated during Run time
Directly stored in memory so access to this memory is faster Accessing this memory is slower, and heap size  is limited by size of virtual memory
Local variables are stored in Stack Objects are stored in Heap

 18. Enum:

  1. Enum is a datatype that has contant values.
  2. We should use Enum when a variable needs to use one value from set of values
  3. Increases compile time checking and will help us to parse only valid constants
  4. Enum can have constructors but cannot invoke constructor directly. It is invoked automatically
  5. RULES:
    1. Enum can be declared as separate class or class member
    2. They should not be declared within a method

 

19. Casting

  1. Assigning one object to another or one datatype to another is known as casting
  2. With datatypes, Byte, Short, int, long,  float and double.
    1. Byte can be converted to short, int, long, float, double
    2. Short can be converted to int, long, float, double
    3. Int can be converted to long, float, double
    4. Long can be converted to float and double
    5. Float to double
  1. Upcasting:
    1. When a subclass object is assigned to superClass object is it known as Upcasting
    2. This is done automatically
    3. Example

Subclass s1 = new Subclass();
Superclass super1 = (Superclass) s1;
another way: Superclass super1 = new Subclass();

  1. Downcasting:
    1. When Superclass object is assigned to subclass it is known as Downcasting
    2. This has to be done manually
    3. Advantage is, it is used to get derived type using a reference to a base type
    4. Example:

Superclass s1 = new Subclass();
Subclass sub1 = (Subclass) s1;

  1. String and String.valueOf(): (Casting)
    1. String:
      1. This works when the object really is a string
      2. Object o = “String”
        String str = (String) o;
    2.  valueof():
      1.  Irrespective of object, this converts to string.
      2. Object o = 20;
        String s1 = String.valueof(o)
  2. Can a constructor be private?
    Yes.
  3. Can a Constructor be static or final or abstract. Why?
    1. Static – Belongs to a class and not a object. When we create an object, Constructor gets initialized
    2. Abstract –means method cannot have a body and can be used in subclass. But constructor are called when creating object (new keyword) so it needs to have body
    3. Final– A constructor internally is final, so we cannot declare constructor final again. Also if we make constructor final, we wont be able to create object of the class
    4. A constructor can be private and can be overloaded|
  4. Why is string non-primitive type?
    1. String is an object, array of characters
    2. Int, short, long, char, float and double all have fixed length memory. But string does not have different length
  5. Post Increment and Pre Increment:
    1. i++:
      1. Will increment the value of i in the first iteration but returns the original value of I.
    2. ++i:
      1. Will increment the value of I and returns the incremented value
  6. Bitwise Operators: (Example here)
    1. >>
      1. Division Operation
      2. 3 >> 6 : Output is: 0
      3. ‘-’ or ‘+’ sign is noted
    2. <<
      1. Multiplication Operation
      2. 2 << 5: this works as 2^5 –> Output is 64 (starts from 2^0+2^1 etc)
    3. >>>
      1. This is unsigned right shift
      2. Irrespective of sign it will always returns as positive
      3. Shifts 0 to the left most position irrespective of sign

Leave a Reply

Your email address will not be published. Required fields are marked *