java interview questions geeks for geeks

This article contains 100 most frequently asked questions on JAVA Programming language. These questions are extracted from various interview experiences, held at various locations and companies.

Q. What do you know about Java Programming Language ? A. Java is a general purpose, object oriented programming language, designed by James Gosling in 1991. First version of Java was launched on May 23, 1995. Earlier, Java was named as Oak.

Q. What are Identifiers in Java ? A. Identifiers are the name given to a class, method, or a variable. Identifiers can start from alphabets, $ sign, but cannot start with numbers or any other sign, except underscore.

Q. Explain the procedure of compilation of a Java Code ? A. For compiling a source code, Java uses both compiler as well as interpreter. Java program takes place at two stages : Compiler & Interpreter. Compiler changes a source code to byte code (.class file) and Interpreter changes byte code to machine code.Following is the procedure of compiling a Java Code :

Q. What are access modifiers in Java ? A. In Java, Access modifiers, specifies the accessibility or scope of a field, method, constructor, or a class.

Q. Name some access modifiers in Java ? A. There are four access modifiers in Java Programming Language :

Q. Name some non access modifiers ? A. ” final, abstract, strictfp, synchronized, volatile ” are non access modifiers.

Q. Explain me the role of JVM. A. JVM stands for Java Virtual Machine. JVM is a software that provides run time environment in which java byte code can be executed.

Q. Explain JRE ? A. JRE is Java Runtime Environment. It is the implementation of JVM.

Q. What is JDK ? A. JDK is Java Development kit. It is the collection of programming tools, JRE, JVM.

Q. What is Applet ? A. It is a small application written in Java or any other language, that compiles in Byte code. It is basically a small internet based program embedded inside a pattern.

Q. What is a Class ? A. A Class is a specification or a template of an object. Class contains variables and methods.

Q. What is an Object ? A. Object is an instance (dynamic memory allocation) of a class. The process of creating objects out of class is known as instantiation. Object has a state, behaviour and an identity.

Q. Explain OOPS methodology ? A. The entire OOPS methodology has been derived from a single root called object. In OOPS, all programs involve creation of classes and object. Four important features of OOPS are :

Q. What is Procedural oriented programming ? A. It is the methodology where programming is done using procedures & functions.

Q. Is Java an object oriented or object based programming language ? A. Object Based programming language does not support all features of OOPs like Polymorphism and Inheritance. Java is an object oriented programming language. Some of object based programming languages are JavaScript, Visual Basics.

Q. Explain Encapsulation with example ? A. Encapsulation is a mechanism where the data and the code that act on the data will bind together. Example : Class is an example of encapsulation. As class is the collection of data members and member functions, hence class binds them together.

Q. Explain Abstraction with example ? A. Abstraction refers to the act of hiding the background details and providing only necessary details to the user. Example : Let us the example of a car. While driving car, driver(user) only knows basic operations like gearing system, brakes, but is totally unaware about the internal happenings and processes. This is abstraction.

Q. Explain Polymorphism with example ? A. Polymorphism represents the ability to assume several different forms of a program. Example : Overloading of methods is an example of Polymorphism. Other examples include, abstract classes and interfaces.

Q. How we achieve the concept of polymorphism in Java? A. Inheritance, Overloading and Overriding are used to achieve Polymorphism in java.

Q. Explain Inheritance with example ? A. Inheritance refers to the process in which child class inherits the properties of its parent class. A class that is inherited is called a superclass. The class that does the inheriting is called a subclass. Inheritance is done by using the keyword extends. Example : Suppose we are having two classes A and B, class B extends A -> Child class B, is inheriting the property of its parent class.

Q. What are the types of Inheritance shown by Java ? A. There are four types of Inheritance in Java :

Q. Explain different types of Inheritance ? A. Single Inheritance : It is a type of inheritance, where a single child class inherits the property of its parent class.

Multilevel Inheritance : When a class extends a class, which extends another class, then this is called multilevel inheritance.

Hybrid Inheritance : A hybrid inheritance is a combination of more than one types of inheritance.

Hierarchical Interitance : In this type of Inheritance, more than one classes are derived from a single base class or parent class.

Q. Why Java Doesn’t Support multiple Inheritance ? A. This is due to the ambiguity problem.

Q. What happens if super class and sub class having same field name ? A. Super class fields will be hidden, meanwhile fields of Subclass will be visible. However, you can also access super class fields with super keyword.

Q. What is Method Overloading ? A. Method Overloading means to have two or more methods with same name in the same class with different arguments. A method can be overloaded in the same class or in a subclass.

Q. What is Method Overriding ? A. Method overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its superclass.

Q. Can we override a method marked as final ? A. No, we can not override a method marked with final.

Q. Can we override a method marked as static ? A. No, we can not override a method marked with static.

Q. Is it possible to override the main method ? A. No, main is a static method. A static method can’t be overridden in Java.

Q. Explain the role of implements keyword ? A. implements keyword is used to developed inheritance between interface and class.

Q. Is String a keyword in java? A. No. String is not a keyword in java. String is a final class in java.lang package which is used to represent the set of characters in java.

Q. How we can create object without using new operator ? A. Using factory methods: NumberFormat obj = NumberFormat. getNumberInstance( ); Here, we are creating NumberFormat object using the factory method getNumberInstance().

Q. What are reference variables ? A. Reference variable usually points to object. In other words, a reference variable is associated with an object. For example : A ob = new A(); Here, ob is a reference variable for a class A. Reference variable are like pointers in C.

Q. Explain Mutable and Immutable object in Java ? A. Mutable objects are the objects which can be modified. For example : StringBuffer. Immutable objects are the objects which can not be modified. For example : String.

Q. What is memory leak ? A. A Memory Leak is a situation when there are objects present in the heap that are no longer used, still, garbage collector is unable to remove them from memory and, thus they are unnecessarily maintained.

Q. Explain the difference between an object and an instance ? A. An Object may or may not have a class definition. Example – int arr[] where arr is an array. An Instance should have a class definition. Example – Solution sol = new Solution(); Here, sol is an instance.

Q. Explain the concept of Singleton Class ? A. In object-oriented programming, a singleton class is a class that can have only one object at a time.

Q. What is the role of super keyword ? A. Super keyword is used to access the method or member variables from the superclass. If a method overrides one of the methods in its superclass, the method can invoke the overridden method through the use of the super keyword.

Q. What is an Interface ? A. An Interface in the Java programming language is an abstract type that is used to specify a behavior that classes must implement. Defining an Interface :

access interface name { return-type method-name1(); return-type method-name2(); type final var1 = value; } An Interface cannot extend anything but another interfaces. Interfaces can not be final.

Q. Can we instantiate an interface ? A. No, You can’t instantiate an interface directly, but you can instantiate a class that implements an interface.

Q. What is an abstract class ? A. Abstract classes are the classes that usually contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Whenever abstract keyword is used in front of a class, then it becomes an abstract class. If even a single method is abstract, the whole class must be declared abstract.

Q. When you declare a method as abstract, can other non abstract methods access it? A. Yes.

Q. Can there be an abstract class with no abstract methods in it ? A. Yes.

Q. Explain Constructor ? A. Constructor is defined as a special method that is used for initializing the objects of a class. Constructor has same name as its class. Constructor does not have any return type.

Q. How are this() and super() used with constructors ? A. Constructors use this to refer to another constructor in the same class with different parameters. Constructors use super keyword to invoke the superclass’s constructor.

Q. What is final modifier? A. Whenever a variable is marked with final, it means that its value can not be changed.

Q. What is static block? A. Static block is a block of code, which is executed exactly once i.e when the class is first loaded into JVM. Before main method the static block executes. Syntax : static { System.out.println(” This is a static block.”); }

Q. What are Static variables ? A. It is a variable which belongs to the class and initialized only once at the start of the execution.

Q. A static method can only call other static methods. True or False ? A. True.

Q. A static method can reference to the current object using keywords super or this. True or False ? A. False.

Q. What are instance variables ? A. Instance variables are the variables used within a class but outside a method. In this, each instance of class, contains its own copies of these variables.

Q. What if we include a return type to a constructor ? A. No error, as it will be considered as a method.

Q. What is Constructor Overloading ? A. Constructor overloading is the process of defining more than one constructors, each one having different parameters.

Q. Do we have destructors in java ? A. No. Same job is done through Garbage Collection.

Q. Explain the role of garbage collector ? A. When no reference to an object exists, that object is needed to be no longer needed, and the memory occupied by the object can be reclaimed. There is no need to explicitly destroying the objects. This is the whole process of garbage collection in java.

Q. What are packages in Java ? A. Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. It is achieved through package keyword.

Q. What is S.C.P ? A. String Constant Pool is the memory space in heap memory specially allocated to store the string objects created using string literals. In String Constant Pool, there will be no two string objects having the same content.

Q. Which is the final class in these three classes – String, StringBuffer and StringBuilder? A. All three are final.

Q. Which one will you prefer among == and equals() method to compare two string objects? A. equals() method because it compares two string objects based on their content. If you use == operator, it checks only references of two objects are equal or not.

Q. Which class will you recommend among String, StringBuffer and StringBuilder classes if I want mutable and thread safe objects? A. StringBuffer.

Q. What is the similarity and difference between String and StringBuffer class? A. The main similarity between String and StringBuffer class is that both are thread safe. The main difference between them is that String objects are immutable where as StringBuffer objects are mutable.

Q. What is the similarity and difference between StringBuffer and StringBuilder class ? A. The main similarity between StringBuffer and StringBuilder class is that both produces mutable string objects. The main difference between them is that StringBuffer class is thread safe where as StringBuilder class is not thread safe.

Q. What is an exception ? A. An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

Q. What is an error ? A. An Error indicates that a non-recoverable condition has occurred that should not be caught.

Q. Which is superclass of Exception ? A. Throwable Class, is the parent class of all exception related classes.

Q. Explain the role of finally block ? A. It creates a block of code, that will be executed after a try/catch block or before. It will be executed everytime, no matter whether an exception is thrown or not.

Q. Can we have the try block without catch block ? A. Yes, we can have the try block without catch block, but finally block should follow the try block.

Q. What is a Thread ? A. A Thread refers to the smallest unit of processing.

Q. How can you gain the concept of multithreading in Java ? A. In either of two ways :

Q. Explain Thread Synchronization ? A. When tow or more threads needs access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. This is Synchronization. It is achieved through synchronized keyword in Java.

Q. What is the difference between yield() and sleep()? A. yield() allows the current thread to release its lock from the object and scheduler gives the lock of the object to the other thread with same priority. sleep() allows the thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t release the lock.

Q. What is the difference between wait() and sleep()? A. wait() is a method of Object class. sleep() is a method of Object class. sleep() allows the thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t release the lock. wait() allows thread to release the lock and goes to suspended state. The thread is only active when a notify() or notifyAll() method is called for the same object.

Q. What is difference between notify() and notifyAll()? A. notify( ) wakes up the first thread that called wait( ) on the same object. notifyAll( ) wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first.

Q. What is volatile keyword ? A. Volatile keyword is used to modify the value of a variable by different threads. It is also used to make classes thread safe.

Q. What are the daemon threads? A. Daemon threads are special threads that run in the background, however, these not used to run the application code generally.

Q. What are Collections in Java ? A. The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects. Java Collections can achieve all the operations like searching, sorting, insertion, manipulation, and deletion.

Q. Explain checked & unchecked exceptions ? A. Checked Exception : Exceptions are checked by the compiler at the time of compilation, are checked exceptions. Example – ClassNotFound Exception

Unchecked Exception : These exceptions are not checked during the compile time by the compiler. Example – ArrayIndexOutOfBounds Exception

Q. What is the role of finalize() ? A. finalize() method is a protected and non-static method of java.lang.Object class. This method is used to perform some final operations or clean up operations on an object before it is removed from the memory.

So, these are most frequently asked interview questions on JAVA. We, at CODE OF GEEKS, wish you all the best for your upcoming future. For any assistance, feel free to mail us at [email protected]

Live Mock Interview | Technical Round – Coding Fundamentals

To access static variables, we need not create an object of that class, we can simply access the variable as:

Q12. Why static methods cannot access non-static variables or methods? Ans) A static method cannot access non-static variables or methods because static methods can be accessed without instantiating the class, so if the class is not instantiated the variables are not initialized and thus cannot be accessed from a static method.Q13.What is a static class? A class can be said to be static class if all the variables and methods of the class are static and the constructor is private. Making the constructor private will prevent the class to be instantiated. So the only possibility to access is using the Class name only. Q14. How many types of Variable? Explain. There are three types of variables in Java:

Q10. What are the different ways to create objects in Java? There are many different ways to create objects in Java. Please see 5 Different ways to create objects in JavaQ11. What’s the purpose of Static methods and static variables? When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects. Static variable: Static variables are also known as Class variables. These variables are declared similarly as instance variables, the difference is that static variables are declared using the static keyword within a class outside any method constructor or block. Unlike instance variables, we can only have one copy of a static variable per class irrespective of how many objects we create. Static variables are created at the start of program execution and destroyed automatically when execution ends. To access static variables, we need not create an object of that class. Static methods: A static method can be accessed without creating objects. Just by using the Class name the method can be accessed. The static method can only access static variables and not local or global non-static variables. For Example:

Q1. Explain JVM, JRE and JDK? JVM (Java Virtual Machine): JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. JVM is the one that actually calls the main method present in a Java code. JVM is a part of JRE(Java Runtime Environment).JRE (Java Runtime Environment): JRE refers to a runtime environment in which Java bytecode can be executed. It implements the JVM (Java Virtual Machine) and provides all the class libraries and other support files that JVM uses at runtime. So JRE is a software package that contains what is required to run a Java program. Basically, it’s an implementation of the JVM which physically exists. JDK(Java Development Kit): It is the tool necessary to compile, document and package Java programs. The JDK completely includes JRE which contains tools for Java programmers. The Java Development Kit is provided free of charge. Along with JRE, it includes an interpreter/loader, a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in Java development. In short, it contains JRE + development tools.Q2. Explain public static void main(String args[]). Public: Public is an access modifier. Public means that this Method will be accessible by any Class. static : It is a keyword in java which identifies it is class-based i.e it can be accessed without creating the instance of a Class. Since we want the main method to be executed without any instance also, we use static. Void: It is the return type of the method. Void defines the method which will not return any value. main: This is the first method executed by JVM. The signature of the method must be the same.Q3. Why Java is platform independent? Platform independent practically means “write once run anywhere”. Java is called so because of its byte codes which can run on any system irrespective of its underlying operating system.Q4. Why is Java not pure Object-oriented? Java is not considered pure Object-oriented because it supports primitive data-types such as boolean, byte, char, int, float, double, long, short.Q5. Define class and object. Explain them with an example using java. Class: A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.Interfaces: A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.Object: It is a basic unit of Object Oriented Programming and represents the real-life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of :State : It is represented by attributes of an object. It also reflects the properties of an object.Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.Identity : It gives a unique name to an object and enables one object to interact with other objects. For Example: Employee is an example of a class A specific employee with unique identification is an example of an object.

In the above program the variable localVar is local variable to the function getLocalVarValue(). If we use the variable localVar outside getLocalVarValue() function, the compiler will produce an error as “Cannot find the symbol localVar”.Instance Variables: Instance variables are non-static variables and are declared in a class outside any method, constructor or block. As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed. Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.

Top level classes in java can’t be private or protected, but inner classes in java can. The reason for not making a top level class as private is very obvious, because nobody can see a private class and thus they can not use it. Declaring a class as protected also doesn’t make any sense. The only difference between default visibility and protected visibility is that we can use it in any package by inheriting it. Since in java there is no such concept of package inheritance, defining a class as protected is no different from default.

What will happen if you put System.exit(0) on try or catch block? Will finally block execute?

Java supports multiple inheritance but not through classes, it supports only through its interfaces. The reason for not supporting multiple inheritance is to avoid the conflict and complexity arises due to it and keep Java a Simple Object Oriented Language. If we recall this in C++, there is a special case of multiple inheritance (diamond problem) where you have a multiple inheritance with two classes which have methods in conflicts. So, Java developers decided to avoid such conflicts and didn’t allow multiple inheritance through classes at all.

E.g. throw throw new Exception(“You have some exception”) throw new IOException(“Connection failed!!”) throws throws IOException, NullPointerException, ArithmeticException

Set and List both are child interface of Collection interface. There are following two main differences between them

SDE I @ Indeed.com | Ex-SDE I @ GeeksforGeeks | ACM ICPC Regionalist (2017, 2019) | Competitive Programmer

Then he asked me a lot of problems. For most of them, I have to tell the approach, for a few of them have to write the code on coderpad.io. I don’t remember all problems.

Applied through LinkedIn, HR posted about openings in mid of June 2020. Received a mail for being shortlisted.

I finished this round within 35 minutes for the last 10-minute interviewer told me to dry run the code for the second problem. He was not convinced at first if my code is right. After Dry run, he was convinced.

This Round was of 1 hour based on Data Structures & Algorithms and projects listed on the resume. The interviewer was very cool.

What are the concepts of OOP?

Object Oriented Programming (OOP) includes:

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance
  • Predefined types must be objects
  • User defined types must be objects
  • Operations must be performed by sending messages to objects
  • FAQ

    Which is the best site for java interview questions?

    Most Popular Java Interview Questions and Answers
    • Q #1) What is JAVA? …
    • Q #2) What are the features of JAVA? …
    • Q #3) How does Java enable high performance? …
    • Q #4) Name the Java IDE’s? …
    • Q #5) What do you mean by Constructor? …
    • Q #6) What is meant by the Local variable and the Instance variable? …
    • Q #7) What is a Class?

    How do I prepare for Java interview for 2 years experience?

    Which is the best site for Java interview questions?
    • Tutorialspoint. www.Tutorialspoint.com.
    • StackOverflow. www.stackoverflow.com.
    • DZone. www.dzone.com.
    • Wikipedia. www.wikipedia.org.
    • IBM Developer Works. www.ibm.com/developerworks/java/

    What are the interview questions for 2 years experience in Java?

    Java interview questions for 2 years experience
    • Can we override static method in java?
    • Can you overload main method in java?
    • Can we override private methods in java?
    • What is the base class for all the classes?
    • Can you list down some of important method from object class?

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *