Core Java Interview Questions Flashcards
Name some OOPS concepts in Java.
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
What does platform independence mean?
Platform independence means that you can run the same Java program on any Operating System. For example, you can write a Java program on Windows and run it on Mac OS.
What is JVM and is it platform independent?
The JVM (Java Virtual Machine) is the heart of Java programming language. It is responsible for converting byte code into machine-readable code. It is not platform independent. There are different JVMs for different OS. It’s called virtual because it provides an interface that does not depend on the underlying OS.
What is the difference between JDK and JVM?
JDK (java Development Kit) is for developmental purposes and JVM is a part of it to execute the java programs.
JDK provides all the tools, executables, and binaries required to compile, debug, and execute a Java program. The execution part is handled by JVM to provide machine independence.
What is the difference between JVM and JRE?
Java Runtime Environment (JRE) is the implementation of JVM. JRE consists of JVM and java binaries and other classes to execute any program successfully. JRE does not contain any development tools.
Which class is the superclass of all classes?
java.lang.Object is the root class for all the java classes and it doesn’t need to be extended.
Can Java support multiple inheritance?
No. However, multiple inheritances are supported in interfaces. An interface can extend multiple interfaces b/c they just declare the methods and implementation will be present in the implementing class.
What is the importance of the main method in Java?
The main method is the entry point of any standalone java application. It is public and static so that Java runtime can access it without initializing the class.. The input parameter is an array of String through which we can pass runtime arguments to the java program.
What is overloading and overriding in Java?
Method overloading is when we have more than one method with the same name in a single class, but the arguments are different.
Method overriding comes into play with inheritance when we have two methods with the same signature, one in the parent, and one in the child class. We can use @Override annotation in the child class’s overridden method to denote that if the parent class method is changed, so is the child class.
Can we overload the main method?
Yes! We can have multiple methods with the name “main”. However, if we run the class, the JRE will look for the main method with the syntax:
public static void main(String args[]).
Can we have multiple public classes in a java source file?
We can’t have more than one public class in a single jave source file. A single source file can have multiple classes that are not public.
What is a Java Package and which package is imported by default?
A Java package is the mechanism to organize the java classes by grouping them. The grouping logic can be based on functionality or modules based.
The java.lang package is imported by default and we don’t need to import any class from this package explicitly.
What are access modifiers in Java?
Java provides access control through public, private, and protected access modifier keywords. When none of these are used, it’s called default access modifier. A java class can only have public or default access modifier.
What is a final keyword?
In a class, the final keyword is used to make sure that no other class can extend it.
In a method, it is used to make sure child classes cannot override it.
In a variable, it is used to make sure that it can be assigned only once.
- Java interface variables are by default final and static.
What is a static keyword?
The static keyword can be used with class-level variables to make it “global”. All the objects will share the same variable.
A static method can access only static variables of class and invoke only static methods of the class.
What is finally and finalize in Java?
The finally block is used with try-catch to out the code that you want to get executed always, even if an exception is thrown by the try-catch block. It’s mostly used to release resources created in the try block.
The finalize method is a special method in the Object class that we can override in our classes. It gets called by the garbage collector when the object is getting garbage collected. It is usually overridden to release system resources when the object is garbage collected.
Can we declare a class as static?
We cannot declare a top-level class as static, however an inner class can be declared as static. If the inner class is declared as static, it's called a static nested class. The static nested class is the same as any other top-level class and is nested for only packaging convenience.
What is try-with-resources?
It is used for automatic resource management. We can create resources inside the try block and use it. Java takes care of closing it as soon as try-catch block gets finished.
What is a multi-catch block in java?
We can catch multiple exceptions in a single catch block. This makes our code shorter and cleaner when every catch block has a similar code.
If the catch block handles multiple exceptions, you can separate them using a pipe and in this case, the exception parameter is final, so you can’t change it.