By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Java OOPS Interview Questions
Q 1. What is class in Java? In the real world, you often have many objects of the same kind. For example, your bicycle is just one of many bicycles in the world. Using object-oriented terminology, we say that your bicycle object is an instance (in the glossary) of the class of objects known as bicycles. Bicycles have some state (current gear, current cadence, two wheels) and behaviour (change gears, brake) in common. However, each bicycle's state is independent of and can be different from that of other bicycles. When building bicycles, manufacturers take advantage of the fact that bicycles share characteristics, building many bicycles from the same blueprint. It would be very inefficient to produce a new blueprint for every individual bicycle manufactured.
In object-oriented software, it's also possible to have many objects of the same kind that share characteristics: rectangles, employee records, video clips, and so on. Like the bicycle manufacturers, you can take advantage of the fact that objects of the same kind are similar and you can create a blueprint for those objects. A software blueprint for objects is called a class (in the glossary).
Q 2. What is a constructor in java? "A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes:
Example: Create a constructor:
// Create a MyClass class public class MyClass { int x; // Create a class attribute
// Create a class constructor for the MyClass class public MyClass() { x = 5; // Set the initial value for the class attribute x }
public static void main(String[] args) { MyClass myObj = new MyClass(); // Create an object of class MyClass (This will call the constructor) System.out.println(myObj.x); // Print the value of x } }
// Outputs 5
Q 3. What is object in java? An object is a software bundle of variables and related methods. You can represent real-world objects using software objects. You might want to represent real-world dogs as software objects in an animation program or a real-world bicycle as a software object within an electronic exercise bike. However, you can also use software objects to model abstract concepts. For example, an event is a common object used in GUI window systems to represent the action of a user pressing a mouse button or a key on the keyboard.
Q 4. How to create object in java? Declaration: The code set in bold are all variable declarations that associate a variable name with an object type. Instantiation: The new keyword is a Java operator that creates the object. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
Q 5. What is an object in java? Software objects are conceptually similar to real-world objects: they too consist of state and related behaviour. An object stores its state in fields (variables in some programming languages) and exposes its behaviour through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation - a fundamental principle of object-oriented programming.
Q 6. What is oops in java? Object-oriented programming System(OOPs) is a programming paradigm based on the concept of 'objects' that contain data and methods. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs. Object-oriented programming brings together data and its behaviour(methods) in a single location(object) , thus making it easier to understand how a program works. We will cover each and every feature of OOPs in detail so that you won't face any difficultly understanding OOPs Concepts.
Q 7. Who executes the byte code in java? Bytecode is the compiled format for Java programs. Once a Java program has been converted to bytecode, it can be transferred across a network and executed by Java Virtual Machine (JVM).
Q 8. Why is Java secure? Java has no concept of pointers. Hence java is secure. Java does not provide access to actual memory locations.
Q 9. Why java does not support multiple inheritance? Java supports multiple inheritance through interfaces only. A class can implement any number of interfaces but can extend only one class.
Multiple inheritance is not supported because it leads to a deadly diamond problem. However, it can be solved but it leads to a complex system so multiple inheritance has been dropped by Java founders.
Q 10. Why java doesn't support multiple inheritance? Java supports multiple inheritance through interfaces only. A class can implement any number of interfaces but can extend only one class. Multiple inheritance is not supported because it leads to a deadly diamond problem.
Q 11. Why we can't create the object of abstract class in java? Because an abstract class is an incomplete class (incomplete in the sense it contains abstract methods without body and output) we cannot create an instance or object; the same way we say for an interface.
Q 12. What is Constructor Overloading? When a class has multiple constructors with different function definitions or different parameters it is called constructor overloading.
import java.io.*; import java.lang.*; public class constructor_overloading { double sum; constructor_overloading(){ sum=0; } constructor_overloading(int x,int y){ sum=x+y; } constructor_overloading(double x,double y){ sum=x+y; } void print_sum(){ System.out.println(sum); } public static void main(String args[]){ constructor_overloading c=new constructor_overloading(); c.print_sum(); constructor_overloading c1=new constructor_overloading(10,20); c1.print_sum(); constructor_overloading c2=new constructor_overloading(10.11,20.11); c2.print_sum(); } }
Q 13. How many types of Inheritance are possible in Java? Single, multiple, multilevel, hybrid and hierarchical inheritance are possible in java. Hybrid inheritance and hierarchical inheritance are only possible through interfaces.
Q 14. How many types of constructor does Java support? Java supports the following types of constructors: - Non-Parameterized or Default Constructors - Parameterized Constructors - Copy constructor
Q 15. What is a singleton class in Java? What's the benefit of making a class singleton? A singleton class is a class in Java that can at most have one instance of it in an application. If new instances are created for the same class they point to the first instance created and thus have the same values for all attributes and properties. Singleton classes are created to create global points of access to objects. Singleton classes find their primary usages in caching, logging, device drivers which are all entities for universal access.
Q 16. What is the role of finalize()? Finalize() is used for garbage collection. It's called by the Java run environment by default to clear out unused objects. This is done for memory management and clearing out the heap.
Q 17. Explain encapsulation in Java. Encapsulation is the process of wrapping variables and functions together into a single unit in order to hide the unnecessary details. The wrapped up entities are called classes in java. Encapsulation is also known as data hiding because it hides the underlying intricacies.
Q 18. Explain abstraction in Java. Abstraction is the process of revealing the essential information and hiding the trivial details across units in java. Java has abstract classes and methods through which it does data abstraction.
Q 19. If a child class inherits base class then are the constructor of the base class also inherited by the child class? Constructors are not properties of a class. Hence they cannot be inherited. If one can inherit constructors then it would also mean that a child class can be created with the constructor of a parent class which can later cause referencing error when the child class is instantiated. Hence in order to avoid such complications, constructors cannot be inherited. The child class can invoke the parent class constructor by using the super keyword.
Q 20. What is the use of super? super() is used to invoke the superclass constructor by the subclass constructor. In this way, we do not have to create different objects for super and subclasses.
Q 21. How is encapsulation achieved in Java? Encapsulation is achieved by wrapping up data and code into simple wrappers called classes. Objects instantiate the class to get a copy of the class data.
Q 22. What is an abstract class in Java? An abstract class is a class that can only be inherited and it cannot be used for object creation. It's a type of restricted class with limited functionality.
Q 23. How is polymorphism achieved in Java? An example of polymorphism is the == operator which can be used to compare both numerics and strings.
Q 24. Can the main method be declared as private in Java? Yes, the main method can be declared as private.
Q 25. What is an object in Java? An object is an instance of a class in java. It shares all attributes and properties of a class.
Q 26. What happens if we make the constructor final? If we make the constructors final then the class variables initialized inside the constructor will become unusable. Their state cannot be changed.
Q 27. What is constructor chaining? constructor chaining is the process of invoking constructors of the same class or different classes inside a constructor. In this way, multiple objects are not required for constructor invocation with constructors having different parameters.
Java Multithreading Interview Questions
Q 1. What is multithreading in java? Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such a program is called a thread. So, threads are light-weight processes within a process.
Q 2. What is thread-safe in java? Thread-safety or thread-safe code in Java refers to code which can safely be used or shared in concurrent or multi-threading environment and they will behave as expected. any code, class, or object which can behave differently from its contract on the concurrent environment is not thread-safe.
Q 3. What is a thread in java? A thread is a lightweight program that allows multiple processes to run concurrently. Every java program has at least one thread called the main thread, the main thread is created by JVM. The user can define their own threads by extending the Thread class (or) by implementing the Runnable interface. Threads are executed concurrently.
public static void main(String[] args){//main thread starts here }
Q 4. What is volatile in java? Volatile keyword is used to modify the value of a variable by different threads. It is also used to make classes thread-safe. It means that multiple threads can use a method and instance of the classes at the same time without any problem.
Q 5. How to generate random numbers in java within range? import java.util.concurrent.ThreadLocalRandom;
// nextInt is normally exclusive of the top value, // so add 1 to make it inclusive int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
Q 6. If we clone objects using the assignment operator does the references differ? When objects are cloned using the assignment operator, both objects share the same reference. Changes made to the data by one object would also be reflected in the other object.
Q 7. Can we start a thread twice in java? Once a thread is started, it can never be started again. Doing so will throw an IllegalThreadStateException
Q 8. How can java threads be created? Threads can be created by implementing the runnable interface. Threads can also be created by extending the thread class
Also See: All The Useful Java Interview Questions & Answers - Part 1 All The Useful Java Interview Questions & Answers - Part 2
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.