Core Java Interview Questions Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Name some OOPS concepts in Java.

A
  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does platform independence mean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is JVM and is it platform independent?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between JDK and JVM?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between JVM and JRE?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which class is the superclass of all classes?

A

java.lang.Object is the root class for all the java classes and it doesn’t need to be extended.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can Java support multiple inheritance?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the importance of the main method in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is overloading and overriding in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can we overload the main method?

A

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[]).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can we have multiple public classes in a java source file?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Java Package and which package is imported by default?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are access modifiers in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a final keyword?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a static keyword?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is finally and finalize in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Can we declare a class as static?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is try-with-resources?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a multi-catch block in java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a static block?

A

Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader. It is used to initialize static variables of the class. Mostly it’s used to create static resources when class is loaded.

21
Q

What is an interface?

A

Interfaces are core concepts of java programming language and are used a lot not only in JDK, but also Java design patters, most of the frameworks and tools. interfaces provide a way to achieve abstraction in Java and used to define the contract for the subclasses to implement.

Interfaces are a good starting point to define Type and create top level hierarchy in our code. Since a Java class can implements multiple interfaces, it’s better to use interfaces as super class in most of the cases.

22
Q

What is an abstract class?

A

Abstract classes are used in Java to create a class with some default method implementation for subclasses. An abstract class can have an abstract method without a body and it can have methods with implementation also.

The abstract keyword is used to create an abstract class. Abstract classes can’t be instantiated and are mostly used to provide a base for subclasses to extend and implement the abstract methods, and override or use the implemented methods in abstract class.

23
Q

What is the difference between abstract class and interface?

A

> The abstract keyword is used to create abstract class whereas interface is the keyword for interfaces.

> Abstract classes can have method implementations whereas interfaces cannot. As of Java 8, interfaces can have default method implementations.

> A class can extend only one abstract class but it can implement multiple interfaces.

> We can run an abstract class if it has a main method, whereas we cannot run an interface.

24
Q

Can an interface implement or extend another interface?

A

Interfaces don’t implement other interfaces, they extend them. As of Java 8, interfaces can have default method implementations. So when a common default method id present in multiple interfaces, it’s mandatory to provide implementation of the method in the class implementing them.

25
Q

What is a Marker interface?

A

A marker interface is an empty interface without any method but used to force some functionality in implementing classes by Java. Some of the well known marker interfaces are Serializable and Cloneable.

26
Q

What are Wrapper classes?

A

Java Wrapper classes are the Object representation of 8 primitive types in Java. All of the Wrapper classes in Java are immutable and final. Java 5 autoboxing and unboxing allows easy conversion between primitive types and their corresponding Wrapper classes.

byte -> Byte
short -> Short
int -> Integer
long -> Long
float -> Float
double -> Double
char -> Character
boolean -> Boolean
27
Q

What is enum in Java?

A

Enum was introduced in Java 1.5 as a new type whose fields consist of a fixed set of constants. For example, we can create Direction as an enum with fields as EAST, WEST, NORTH, SOUTH.

enum is the keyword to create an enum type and similar to the class. Enum constants are implicitly static and final.

28
Q

What are Java annotations?

A

Java Annotations provide information about the code and they have no direct effect on the code they annotate. Annotations are introduced in Java 5. Annotation is metadata about the program embedded in the program itself. It can be parsed by the annotation parsing tool or the compiler. We can also specify annotation availability to either compile-time only or till runtime. Java built in annotations are: @Override, @Deprecated, and @SuppressWarnings.

29
Q

What is Composition in Java?

A

Composition is the design technique to implement a “Has-A” relationship in classes. We can use Object composition for code reuse.

30
Q

What is the benefit of Composition over Inheritance?

A

> Any changes in the superclass might affect subclasses even though we might not be using the superclass methods. The composition will never face this issue because we are only using only what methods we need.

> Inheritance exposes all of the superclass methods and variables to the client and if we have no control in designing superclass, it can lead to security holes. Composition allows us to provide restricted access to the methods and hence more secure.

> We can get runtime binding in composition where inheritance binds the classes at compile time. So composition provides flexibility in the invocation of methods.

31
Q

How to sort a collection of custom Objects in Java?

A

We need to implement Comparable interface to support sorting of custom objects in a collection. The Comparable interface has a compareTo(T obj) method which is used by sorting methods and by providing this method implementation, we can provide a default way to sort custom objects collection.

However, if you want to sort based on different criteria, such as sorting an Employees collection based on salary or age, then we can create Comparator instances and pass it as a sorting methodology.

32
Q

What is inner class in Java?

A

We can define a class inside a class and they care called nested classes. Any non-static nested class is known as an inner class. Inner classes are associated with the object of the class and they can access all the variables and methods of the outer class. Since inner classes are associated with the instance, we can’t have any static variables in them.

We can have local inner classes or anonymous inner classes inside a class.

33
Q

What is an anonymous inner class?

A

A local inner class without a name is known as an anonymous inner class. An anonymous class is defined and instantiated in a single statement. Anonymous inner classes always extend a class or implement an interface.

Since an anonymous class has no name, it is not possible to define a constructor for one. They are accessible only at the point where it is defined.

34
Q

What is Classloader?

A

Java Classloader is the program that loads byte code program into memory when we want to access any class.

35
Q

What are different types of classloaders?

A

There are 3 types of built-in Classloaders in Java:

  1. Bootstrap Classloader
  2. Extensions Classloader
  3. System Classloader
36
Q

What is ternary operator?

A

Java ternary operator is the only conditional operator that takes 3 operands. It’s a one liner replacement for the if-then-else statement and it’s used a lot in Java programming. We can user ternary operator if-else conditions or even switch conditions using nested ternary operators.

37
Q

What does the super keyword do?

A

The super keyword can be used to access the superclass method when you have overridden the method in the child class.

We can use the super keyword to invoke superclass constructors in the child class constructor but in this case, it should be the first statement in the constructor method.

38
Q

What are the break and the continue statements?

A

We can use the break statement to terminate a for, while, do-while loop. We can use it in the swithc statement to exit the switch case. We can use a break with the label to terminate the nested loops.

The continue statement skips the current iteration of a for, while, or do-while loop. We can use the continue statement with the label to skip the current iteration of the outermost loop.

39
Q

What is this keyword?

A

this keyword provides the reference to the current object and it’s mostly used to make sure that the object’s variables are used, not the local variables having the same name.

We can also use this keyword to invoke other constructors from a constructor.

40
Q

What is default constructor?

A

When we don’t define any constructor for the class, Java compiler automatically creates the default no-args constructor for the class. If there are other constructors defined, then the compiler will not create the default constructor for us.

41
Q

Can we have try without catch block?

A

Yes! We can have try-finally statement and hence avoiding a catch block.

42
Q

What is Garbage Collection?

A

Garbage Collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. in Java, the process of deallocating memory is handled automatically by the garbage collector.

We can run the garbage collector with code Runtime.getRuntime().gc() or use the utility method Systme.gc().

43
Q

What is Serialization and Deserialization?

A

We can convert a Java object to a Stream that is called Serialization. Once an object is converted to Stream, it can be saved to file or dent over the network or used in socket connections.

They object should implement a Serializable interface and we can use java.io.ObjectOutputStream to write objects to file or to any OutputStream object.

The process of converting stream data created through serialization to Object is called deserialization.

44
Q

What is the use of System class?

A

Java System Class is one of the core classes. One of the easiest wats to log information for debugging is System.out.println() method.

System class is final. It does not provide any public constructors and all of its methods are static.

Some of the utility methods of System class are for array copy, getting the current time, reading environment variables, etc.

45
Q

What is instanceof keyword?

A

We can use the instanceof keyword to check if an object belongs to a class or not. We should avoid its usage as much as possible.

46
Q

Can we use String with switch case?

A

If you are using Java 7+, you can use String in switch case statements.

47
Q

Is Java Pass by value or pass by reference?

A

Java is pass by value. For example, object variables contain the reference to the Objects in heap space. When we invoke any method, a copy of these variables is passed and gets stored in the stack memory of the method.

48
Q

What is the difference between Heap and Stack Memory?

A

> Heap memory is used by all parts of the application, whereas stack memory is used only by one thread of execution.

> Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.

> Memory management in the stack is done in a LIFO manner, whereas it’s more complex in Heap memory because it’s used globally.

49
Q

Is the Java compiler stored in JDK, JRE, or JVM?

A

The task of the Java compiler is to convert a Java program into bytecode. We have javac executable for that. So it must be stored in JDK because we don’t need it in the JRE and JVM is just the specs.