In this article, you will find interview questions and answers on Java. These interview questions and answers are asked by the employers in the technical round of an interview. These questions are useful for the candidate who is attending an interview in the software industry on the Java platform. The interview questions presented in this article are collected from various Java interviews conducted by the multinational companies. These interview questions on Java are consolidated in one place to make it easy and convenient for you. This article helps in the preparation process of Java technical interview.
1. What is the vital feature in Java?
The main feature in Java language is platform independent.
2. What is platform independent?
The Java code which is written and compiled in one platform such as windows can execute class in the other platforms such as Linux, UNIX, etc is known as platform independent.
3. Define JVM?
JVM- java Virtual Machine
For compiled java class files, JVM is considered as a runtime environment.
4. Differentiate JDK and JVM?
JDK – Java Development Kit
JVM – Java Virtual Machine
It is used for development which has an execution environment whereas JVM is runtime environment purely and thus, the source files cannot be compiled using JVM.
5. Define a pointer and does Java language support pointers?
In general, pointer is a reference to memory location. Reliability issues and memory leaks are possible by handling pointers improperly. Thus, usage of Java is not supported by Java.
6. Differentiate abstract class and interface?
Instance methods which are used for building default behavior are present in an abstract class. In an interface, you can declare only instance methods and constants but the default behavior cannot be built and implicitly all the methods are abstract. All the public methods without implementation are present in an interface. In an abstract class, you can declare the class members as protected, private, etc but, it has few abstract methods.
7. Define static in java?
The meaning of static is one for each class and not for every object and the existence of instance of a class is not considered. So, you can use without creating instance for that particular class. The methods which are declared as static are final implicitly as overriding is done depending on type of object. In general, methods declared as static can be attached to a class but not an object. Static method in a super class is shadowed by other static method in subclass until the actual method is not declared as final. But, it is not possible to override a static method using a non-static method. Simply in a subclass, a static method cannot be changed to an instance method.
8. In Java, what is the use of garbage collector?
Garbage collection is used to spot and remove objects that are not used by the program from long time and the allocate resources for that object is reclaimed and reused. The memory allocated by the Java object is deleted if it is unreachable within the same program.
9. Explain synchronization with respect to multi-threading?
The ability to control access to shared resources of multiple threads with respect to multithreading is known as synchronization. One thread can modify shared variable whereas other thread is in the process of updating or using the same shared variable, without synchronization. Usually, this may lead to different errors.
10. Describe the ways of using thread?
Runnable interface is used to implement a thread or you can also inherit from a Thread class. Formal is beneficial as if you are going for multiple inheritance then only an interface is helpful.
11. Differentiate method and a constructor?
A member function of a class which is used for creating objects for that particular class is known as a constructor. The constructor name and class name is same. A constructor does not have a return type and new operator is used to invoke a constructor. An ordinary member function of a class is known as a method. A method has a return type and own name but dot operator is used to invoke it.
12. Define an Iterator?
Some collection classes offer their contents traversal through an interface called java.util.Iterator. You can walk through the collection of objects using this interface, in turn operates on every object. While traversing an iterator, it is not suggested to modify collection.
13. Define pass by reference?
In pass by reference, address itself is passed instead of passing the value.
14. Define pass by value?
In pass by value, a copy of value is passed.
15. Define Map and HashMap?
A Map is an interface whereas Hash Map is class implementing that.
16. Define Vector and ArrayList?
Vector is synchronized but Array List is not synchronized.
17. Define Swing and Awt?
Swings are light weight components whereas AWT are heavy weight components. Thus, Swings works fast when compared to AWT.
18. Define final?
If a class is declared using final keyword then it cannot be extended. i.e., final class will not have a subclass. A method declared with final keyword cannot be overridden if its class is inherited. It is not possible to change the value of a final variable.
19. Define checked and unchecked exceptions?
According to checked exception is an exception of a subclass other than subclasses and class Runtime Exception. The method which is used to throw IOException is read () of java.io.FileInputStream is an example of checked exception. In general, unchecked exceptions are RuntimeExceptions and its subclasses. Class error and its subclasses are the unchecked exceptions. The client programmers are not forced to declare in throws class or to catch the exception by the compiler with unchecked exception. Client programmer is not aware that the exception can be thrown. String’s charAt () method throws StringIndexOutOfBoundsException is an example. At the compile time, these checked exceptions are caught. Often, errors cannot be caught.
20. Define overriding?
If a method is declared by a class with the same return type, name and arguments as a method in the superclass then method class overrides the same which is present in superclass. If a method is invoked for object of a particular class then it is new definition of called method and not definition of method from superclass. Methods are overridden to be more public but not private.
21. Name the different types of inner classes?
• Member classes
• Anonymous classes
• Local classes and
• Nested top-level classes
Clearing the Java technical round is not an easy task and the candidate who is attending an interview on the Java platform must have a good knowledge on all the concepts. Interview is an opportunity had given to the student to explore his skills and talent by answering all the questions asked by the employer correctly. The candidate who is facing an interview can recollect all the concepts quickly by going through the interview questions and answers presented in this article.
The interview questions and answers presented in this article are asked in the technical round of an interview by the employers. The student who is attending an interview in the software industry on C++ platform makes use of these questions. These interview questions are given best answers so that the candidate must give right answer to the employer. The candidate must give the answer confidently, so it is necessary to prepare well before facing an interview. These interview questions help in the preparation process and the students can revise all the concepts by going through these questions. These interview questions are the frequently asked questions by the employers in most of the interviews conducted by software companies.
1. What is C++?
C++ language was developed by Bjarne Stroustrup in AT&T Bell Labs which are an extension of C language. It is an object oriented programming language which is used for developing commercial and enterprise applications. Visual C++ of Microsoft became the best and perfect choice for the programmers and developers.
2. Mention the fundamental concepts of object oriented programming language?
It is mandatory to understand vital concepts which are highly used in the object oriented programming languages. Includes:
• Encapsulation and data abstraction
• Message passing
• Dynamic binding
3. Define encapsulation?
Encapsulation is wrapping up of functions and data in a single unit. This single is nothing but a class. Information about an object is hidden such as code and the internal data structure in encapsulation.
4. What is an inheritance?
Inheritance is deriving a new class from the old class and the old class is known as base class whereas new class is known as derived class. The main usage of inheritance is reuse of the existing code than rewriting the code from scratch when it is required. In this process, object o one class obtains properties of other class.
5. Define polymorphism?
Polymorphism is one name with different forms. In the program, it permits you to have more than one function with same name. It helps in overloading so an operation is shown in different behaviors differently.
6. Define message passing?
A set of objects is present in an object oriented programs for communicating with each other. Specifying the name of object, information to be sent and the function name is included in message passing.
7. Define tokens in C++?
In a program, the smallest individual units are known as tokens. The following are the tokens of C++ programming language:
8. What is a constructor?
The member function which has a same name of its class is known as a constructor. When an object of a class is created then invoking of a constructor takes place. The name constructor is given because it constructs values of data members of class.
9. Where do you use enumerated data type?
Enumerated data type is a user defined data type which is used to attach names to numbers to increase unambiguousness of code. Lists of words are enumerated automatically by enum keyword by assigning then values 0, 1, 2, 3, etc.
10. Why the default constructor is used?
A default constructor is a constructor which does not accept any parameters is known as a default constructor.
11. Define destructor?
For an object class, a destructor is called when the object is out of the scope or it is deleted explicitly. As the name implies, the destructor is used to destroy objects that are created by the constructors. The destructor is also a member function like the constructor whose name is its class name but it is preceded by tilde.
12. Does variable declaration in C++ differ from C?
Yes. In the C language, variables are declared in the beginning of scope whereas in C++, anywhere in the scope variables can be declared. Understanding is made easy to the programmer as variables are declared in the context of usage.
13. Define a class?
A class is a template which contains data members and member functions.
14. Differentiate C and C++?
The main difference between C and C++ is C is a procedure oriented language whereas C++ is an objected oriented programming language. C language is considered as a super set of C++. Function overloading, inheritance and method overloading are not supported by C language whereas C++ supports them. In a C program, main function need not return a value whereas it must return a value in C++.
15. Define a copy constructor?
The constructor which has the same name of a class and which is used to copy the objects deeply is known as a copy constructor.
16. Define a scope resolution operator?
An identifier is referenced in the global scope which is hidden by other identifier with same name in local scope is allowed by the scope resolution operator.
17. Distinguish object and instance?
Object is an instance of user-defined type. From one class, multiple objects can be instantiated whereas an instance of a class is object.
18. Differentiate macro and inline?
• A strict parameter type checking is followed by inline but it is not followed by macro.
• Preprocessor is used to expand macros and the inline definitions may or may not be replaced by the compiler.
19. Define Multiple Inheritance?
The class which inherits or acquires properties from many classes then it is known as inheritance.
20. Define implicit conversion?
Automatic conversion is performed by C++ if the data type are mixed in the expression. Smaller type is changed to wider type. Integer converting to float type is an example.
21. In C++, give the usage of virtual destructor?
If the object is destroyed then the destructor is called automatically. In C++, virtual destructor is mainly used to avoid resource leaks by doing clean-up of an object.
22. In C++, what is a reference variable?
An alias is provided by a reference variable to a variable which is previously defined.
23. Describe virtual functions?
• The virtual functions must belong to particular class.
• They are not static members.
• Object pointers are used to gain access these virtual functions.
• A virtual function may be a friend of other class.
24. Mention inheritance advantages?
• Time is saved in developing a program.
• Code can be reused.
25. Define dynamic constructor?
While creating objects, memory is allocated using constructors. Objects are allocated memory at the construction time which is known as dynamic construction of objects. Here, allocation of memory can be done using an operator called new.
26. Why declaration is used?
Name is used from a namespace if it is declared.
27. Define conversion constructor?
The constructor which has a single parameter that is declared without using function specifier is known as a conversion constructor. The converting constructors are used to convert from first parameter type to converting constructor’s class type.
28. Define associative container?
Associative containers are specially designed to provide gain access to elements with keys. In general, the associative containers are of four types, they are:
29. Give some pure object oriented languages?
The candidate who is looking for the interview questions on C++ is at the right place as this article has the exact information to prepare for the interview. These interview questions are given appropriate and easy answers so that the candidate can remember them easily. Generally, most of the people find difficulty in clearing the technical round of an interview so they can face the interview easily after going through these