Commonly Asked Java Programming Interview Questions | Set 2 – GeeksforGeeks

In this article, some of the most authoritative Java Interview Questions and Answers are discussed, to give you the cutting edge in your interviews. Java is one of the most democratic and widely used programming language and platform. Java is flying, authentic and impregnable. From desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet, Java is used in every nook and corner. This is why you should master these questions if you want to stand out. Let us start by taking a look at some of the most frequently asked Java consultation questions,

so let ’ s get started with the plant of Java Interview Questions. Q1. Can we Overload or Override static methods in java ?

  • Overriding : Overriding is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in superclass (or base class) at runtime.
  • Overloading: Overloading is related to compile time (or static) polymorphism. This feature allows different methods to have same name, but different signatures, especially number of input parameters and type of input parameters.
  • Can we overload static methods?   The answer is ‘Yes’. We can have two ore more static methods with same name, but differences in input parameters
  • Can we Override static methods in java?  We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’. Static methods cannot be overridden because method overriding only occurs in the context of dynamic (i.e. runtime) lookup of methods. Static methods (by their name) are looked up statically (i.e. at compile-time).

Read more Q2. Why the main method is static in java?
The method acting is inactive because otherwise there would be ambiguity : which builder should be called ? specially if your class looks like this :

public class JavaClass
  protected JavaClass(int x)
  {   }
   public void main(String[] args) 


Should the JVM call new JavaClass ( int ) ? What should it pass for ten ? If not, should the JVM instantiate JavaClass without running any builder method ? because that will special-case your integral course – sometimes you have an exemplify that hasn ’ thyroxine been initialized, and you have to check for it in every method acting that could be called. There are equitable excessively many edge-cases and ambiguities for it to make sense for the JVM to have to instantiate a class before the entrance point is called. That ’ sulfur why main is inactive. Q3. What happens if you remove static modifier from the main method?
Program compiles successfully. But at runtime throws an erroneousness “ NoSuchMethodError ”.
Q4. What is the scope of variables in Java in following cases? 

  • Member Variables (Class Level Scope) : The member variables must be declared inside class (outside any function). They can be directly accessed anywhere in class
  • Local Variables (Method Level Scope) : Variables declared inside a method have method level scope and can’t be accessed outside the method.
  • Loop Variables (Block Scope) : A variable declared inside pair of brackets “{” and “}” in a method has scope within the brackets only.

Read more Q5. What is “this” keyword in java ?
Within an case method acting or a builder, this is a reference to the current object — the object whose method acting or builder is being called. You can refer to any extremity of the stream object from within an exemplify method or a builder by using this.
custom of this keyword

  • Used to refer current class instance variable.
  • To invoke current class constructor.
  • It can be passed as an argument in the method call.
  • It can be passed as argument in the constructor call.
  • Used to return the current class instance.
  • Used to invoke current class method (implicitly)

Q6. What is an abstract class ? How abstract classes are similar or different in Java from C++?
Abstract classes are classes that contain one or more pilfer methods. An abstraction method acting is a method acting that is declared but contains no execution. outline classes may not be instantiated, and require subclasses to provide implementations for the pilfer methods .

  • Like C++, in Java, an instance of an abstract class cannot be created, we can have references of abstract class type though.
  • Like C++, an abstract class can contain constructors in Java. And a constructor of abstract class is called when an instance of a inherited class is created
  • In Java, we can have an abstract class without any abstract method. This allows us to create classes that cannot be instantiated, but can only be inherited.
  • Abstract classes can also have final methods (methods that cannot be overridden). For example, the following program compiles and runs fine.

Read more Q7. Which class is the superclass for every class?
Object class Q8. Can we overload main() method?
The main method in Java is no extra-terrestrial method. aside from the fact that main ( ) is equitable like any other method & can be overloaded in a similar manner, JVM always looks for the method key signature to launch the plan .

  • The normal main method acts as an entry point for the JVM to start the execution of program.
  • We can overload the main method in Java. But the program doesn’t execute the overloaded main method when we run your program, we need to call the overloaded main method from the actual main method only.

Read more Q9. What is object cloning ?
Object cloning means to create an exact copy of the original object. If a class needs to support cloning, it must implement java.lang.Cloneable interface and override clone ( ) method acting from Object classify. Syntax of the clone ( ) method is :

protected Object clone() throws CloneNotSupportedException

If the object ’ sulfur class doesn ’ metric ton implement Cloneable interface then it throws an exception ‘ CloneNotSupportedException ’. Read more Q10. How is inheritance in C++  different from Java?

  1. In Java, all classes inherit from the Object class directly or indirectly. Therefore, there is always a single inheritance tree of classes in Java, and Object class is the root of the tree.
  2. In Java, members of the grandparent class are not directly accessible. See this G-Fact for more details.
  3. The meaning of protected member access specifier is somewhat different in Java. In Java, protected members of a class “A” are accessible in other class “B” of the same package, even if B doesn’t inherit from A (they both have to be in the same package).
  4. Java uses extends keyword for inheritance. Unlike C++, Java doesn’t provide an inheritance specifier like public, protected or private. Therefore, we cannot change the protection level of members of the base class in Java, if some data member is public or protected in base class then it remains public or protected in the derived class. Like C++, private members of base class are not accessible in the derived class. 
    Unlike C++, in Java, we don’t have to remember those rules of inheritance which are the combination of base class access specifier and inheritance specifier.
  5. In Java, methods are virtual by default. In C++, we explicitly use virtual keyword. See this G-Fact for more details.
  6. Java uses a separate keyword interface for interfaces, and abstract keyword for abstract classes and abstract functions.
  7. Unlike C++, Java doesn’t support multiple inheritance. A class cannot inherit from more than one class. A class can implement multiple interfaces though.
  8. In C++, default constructor of parent class is automatically called, but if we want to call parameterized constructor of a parent class, we must use Initializer list. Like C++, default constructor of the parent class is automatically called in Java, but if we want to call parameterized constructor then we must use super to call the parent constructor.

See examples here Q11. Why method overloading is not possible by changing the return type in java?
In C++ and Java, functions can not be overloaded if they differ merely in the return character. The return type of functions is not a part of the mangled name which is generated by the compiler for uniquely identifying each function. The No of arguments, Type of arguments & Sequence of arguments are the parameters which are used to generate the unique mangled identify for each serve. It is on the basis of these singular mangled names that compiler can understand which function to call even if the names are same ( overloading ).
Q12. Can we override private methods in Java?
No, a secret method acting can not be overridden since it is not visible from any early classify. Read more
Q13. What is blank final variable ?
A final variable star in Java can be assigned a value entirely once, we can assign a value either in announcement or later .

    final int i = 10;
    i = 30; // Error because i is final.

A blank final varying in Java is a final variable that is not initialized during announcement. Below is a simple example of blank final .

    // A simple blank final example 
    final int i;
    i = 30;

Read more Q14. What is “super” keyword in java ?
The extremely keyword in java is a mention variable that is used to refer parent class objects. The keyword “ extremely ” came into the photograph with the concept of Inheritance. Whenever you create the example of a subclass, an case of parent class is created implicitly i.e. referred by super reference variable.
diverse scenarios of using java superintendent Keyword :

  • super is used to refer immediate parent instance variable
  • super is used to call parent class method
  • super() is used to call immediate parent constructor

Read more Q15. What is static variable in Java ?
The static keyword in java is used for memory management chiefly. We can apply java electrostatic keyword with variables, methods, blocks and nest course. The static keyword belongs to the class than the example of the class. The inactive can be :

  • variable (also known as class variable)
  • method (also known as class method)
  • block
  • nested class

Q16. Differences between HashMap and HashTable in Java .
1. HashMap is not synchronized. It is not-thread safe and can ’ triiodothyronine be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads.
2. HashMap allows one nothing key and multiple null values whereas Hashtable doesn ’ triiodothyronine allow any null winder or value.
3. HashMap is broadly preferred over HashTable if train of thought synchronism is not needed
Read more Q17. How are Java objects stored in memory ?
In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In C++, when we allocate object using new ( ), the object is allocated on Heap, otherwise on Stack if not global or static.
In Java, when we only declare a variable of a class type, alone a reference book is created ( memory is not allocated for the object ). To allocate memory to an object, we must use raw ( ). So the object is always allocated memory on the batch. Read more
Q18. What are C++ features missing in Java?
Try to answer this on your own ahead seeing the answer – hera. See besides : Please write comments if you find anything wrong, or you want to plowshare more information about the subject discussed above .

My Personal Notes


informant :
Category : interview

We will be happy to hear your thoughts

Leave a reply

GauDay Crypto news and market tracking in real time
Enable registration in settings - general