Core Java Interview Questions and answers
Question.1 What is the Java API?
Answer: The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets.
Question.2 Describe the principles of OOPS?
Answer: There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.
Question.3 Explain the Inheritance principle?
Answer: Inheritance is the process by which one object acquires the properties of another object. Inheritance allows well-tested procedures to be reused and enables changes to make once and have effect in all relevant places
Question.4 Explain the Polymorphism principle. Explain the different forms of Polymorphism?
Answer: Polymorphism in simple terms means one name many forms. Polymorphism enables one entity to be used as a general category for different types of actions. The specific action is determined by the exact nature of the situation.
Polymorphism exists in three distinct forms in Java:
* Method overloading
* Method overriding through inheritance
* Method overriding through the Java interface
Question.5 What type of parameter passing does Java support?
Answer: In Java the arguments (primitives and objects) are always passed by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object.
Question.6 Explain the Encapsulation principle?
Answer: Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. Objects allow procedures to be encapsulated with their data to reduce potential interference. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
Question.7 What do you understand by a variable?
Answer: Variable is a named memory location that can be easily referred in the program. The variable is used to hold the data and it can be changed during the course of the execution of the program.
Question.8 What is data encapsulation?
Answer: Encapsulation may be used by creating ‘get’ and ‘set’ methods in a class (JAVABEAN) which are used to access the fields of the object. Typically the fields are made private while the get and set methods are public. Encapsulation can be used to validate the data that is to be stored, to do calculations on data that is stored in a field or fields, or for use in introspection (often the case when using javabeans in Struts, for instance). Wrapping of data and function into a single unit is called as data encapsulation. Encapsulation is nothing but wrapping up the data and associated methods into a single unit in such a way that data can be accessed with the help of associated methods. Encapsulation provides data security. It is nothing but data hiding.
Question.9 Name primitive Java types?
Answer: The 8 primitive types are byte, char, short, int, long, float, double, and boolean.
Question.10 State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.
Answer: public : Public class is visible in other packages, field is visible everywhere (class must be public too)
private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.
protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.
default: What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.
Question.11 What if the main method is declared as private?
Answer: The program compiles properly but at runtime it will give “Main method not public.” message.
Question.12 What if I write static public void instead of public static void?
Answer: Program compiles and runs properly.
Question.13 What environment variables do I need to set on my machine in order to be able to run Java programs?
Answer: CLASSPATH and PATH are the two variables.
Question.14 How can one prove that the array is not null but empty using one line of code?
Answer: Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.
Question.15 If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?
Answer: It is empty. But not null.
Question.16 Can I have multiple main methods in the same class?
Answer: No the program fails to compile. The compiler says that the main method is already defined in the class.
Question.17 Do I need to import java.lang package any time?
Answer: No. It is by default loaded internally by the JVM.
Question.18 Can a top level class be private or protected?
Answer: No. A top level class can not be private or protected. It can have either “public” or no modifier. If it does not have a modifier it is supposed to have a default access.If a top level class is declared as private the compiler will complain that the “modifier private is not allowed here”. This means that a top level class can not be private. Same is the case with protected.
Question.19 What is the difference between a while statement and a do statement?
Answer: A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.
Check for Learn Core Java in-depth