Java interview questions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What are the differences between Heap and Stack Memory in Java?

Stack is generally used to store the order of method execution and local variables. In contrast, Heap memory is used to store the objects. After storing, they use dynamic memory allocation and deallocation.

What is the main objective of garbage collection?

The main objective of this process is to free up the memory space occupied by the unnecessary and unreachable objects during the Java program execution by deleting those unreachable objects. This ensures that the memory resource is used efficiently, but it provides no guarantee that there would be sufficient memory for the program execution.

Define package in Java.

The package is a collective bundle of classes and interfaces and the necessary libraries and JAR files. The use of packages helps in code reusability.

When can you use super keyword?

The super keyword is used to access hidden fields and overridden methods or attributes of the parent class. Following are the cases when this keyword can be used: Accessing data members of parent class when the member names of the class and its child subclasses are same. To call the default and parameterized constructor of the parent class inside the child class. Accessing the parent class methods when the child classes have overridden them

Explain 'super' keyword in Java.

The term "super" is a particular keyword designated as a reference keyword. The "super" keyword refers to the immediate parent class object.

Explain 'this' keyword in Java.

The term "this" is a particular keyword designated as a reference keyword. The "this" keyword is used to refer to the current class properties like method, instance, variable, and constructors.

Can a constructor return a value?

Yes, A constructor can return a value. It replaces the class's current instance implicitly; you cannot make a constructor return a value explicitly.

A single try block and multiple catch blocks can co-exist in a Java Program. Explain.

Yes, multiple catch blocks can exist but specific approaches should come prior to the general approach because only the first catch block satisfying the catch condition is executed.

Can you call a constructor of a class inside the another constructor?

Yes, the concept can be termed as constructor chaining and can be achieved using this().

Define Copy Constructor in Java

A Copy Constructor in Java is a constructor that initializes an object through another object of the same class.

4 pillars of java?

Abstraction Encapsulation Inheritance Polymorphism

Abstraction?

Abstraction is a process of hiding implementation details and exposes only the functionality to the user. In abstraction, we deal with ideas and not events. This means the user will only know "what it does" rather than "how it does".

What are Brief Access Specifiers and Types of Access Specifiers?

Access Specifiers are predefined keywords used to help JVM understand the scope of a variable, method, and class. We have four access specifiers. Public Access Specifier Private Access Specifier Protected Access Specifier Default Access Specifier

What is an Exception?

An Exception in Java is considered an unexpected event that can disrupt the program's normal flow. These events can be fixed through the process of Exception Handling.

Using relevant properties highlight the differences between interfaces and abstract classes.

Availability of methods: Only abstract methods are available in interfaces, whereas non-abstract methods can be present along with abstract methods in abstract classes. Variable types: Static and final variables can only be declared in the case of interfaces, whereas abstract classes can also have non-static and non-final variables. Inheritance: Multiple inheritances are facilitated by interfaces, whereas abstract classes do not promote multiple inheritances. Data member accessibility: By default, the class data members of interfaces are of the public- type. Conversely, the class members for an abstract class can be protected or private also. Implementation: With the help of an abstract class, the implementation of an interface is easily possible. However, the converse is not true;

Briefly explain the concept of constructor overloading

Constructor overloading is the process of creating multiple constructors in the class consisting of the same name with a difference in the constructor parameters. Depending upon the number of parameters and their corresponding types, distinguishing of the different types of constructors is done by the compiler.

What are the differences between constructor and method of a class in Java?

Constructor: Constructor is used for initializing the object state. Constructor has no return type. Constructor gets invoked implicitly. If the constructor is not defined, then a default constructor is provided by the java compiler. The constructor name should be equal to the class name. A constructor cannot be marked as final because whenever a class is inherited, the constructors are not inherited. Hence, marking it final doesn't make sense. Java throws compilation error saying - modifier final not allowed here Final variable instantiations are possible inside a constructor and the scope of this applies to the whole class and its objects. Method: Method is used for exposing the object's behavior. Method should have a return type. Even if it does not return anything, return type is void. Method has to be invoked on the object explicitly. If a method is not defined, then the compiler does not provide it. The name of the method can have any name or have a class name too. A method can be defined as final but it cannot be overridden in its subclasses. A final variable if initialised inside a method ensures that the variable cant be changed only within the scope of that method.

What do you mean by data encapsulation?

Data Encapsulation is an Object-Oriented Programming concept of hiding the data attributes and their behaviors in a single unit. It helps developers to follow modularity while developing software by ensuring that each object is independent of other objects by having its own methods, attributes, and functionalities. It is used for the security of the private properties of an object and hence serves the purpose of data hiding.

Do final, finally and finalize keywords have the same function?

Final: If any restriction is required for classes, variables, or methods, the final keyword comes in handy. Inheritance of a final class and overriding of a final method is restricted by the use of the final keyword. The variable value becomes fixed after incorporating the final keyword. Example: final int a=100; a = 0; // error The second statement will throw an error. Finally: It is the block present in a program where all the codes written inside it get executed irrespective of handling of exceptions. Example: try { int variable = 5; } catch (Exception exception) { System.out.println("Exception occurred"); } finally { System.out.println("Execution of finally block"); } Finalize: Prior to the garbage collection of an object, the finalize method is called so that the clean-up activity is implemented. Example: public static void main(String[] args) { String example = new String("InterviewBit"); example = null; System.gc(); // Garbage collector called } public void finalize() { // Finalize called }

What part of memory - Stack or Heap - is cleaned in garbage collection process

Heap

Comment on method overloading and overriding

In Java, method overloading is made possible by introducing different methods in the same class consisting of the same name. Still, all the functions differ in the number or type of parameters. It takes place inside a class and enhances program readability.Both the functions have the same name but differ in the number of arguments. Method overriding is the concept in which two methods having the same method signature are present in two different classes in which an inheritance relationship is present.

How is an infinite loop declared in Java?

Infinite loops are those loops that run infinitely without any breaking conditions.

Inheritance?

Inheritance is the process of one class inheriting properties and methods from another class in Java. Inheritance is used when we have is-a relationship between objects. Inheritance in Java is implemented using extends keyword.

What do you understand by an instance variable and a local variable?

Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an object and remain bound to it at any cost. All the objects of the class will have their copy of the variables for utilization. If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected. Example: class Athlete { public String athleteName; public double athleteSpeed; public int athleteAge; } Local variables are those variables present within a block, function, or constructor and can be accessed only inside them. The utilization of the variable is restricted to the block scope. Whenever a local variable is declared inside a method, the other class methods don't have any knowledge about the local variable. Example: public void athlete() { String athleteName; double athleteSpeed; int athleteAge; }

What are the differences between JVM, JRE and JDK in Java?

JDK- Java development kit Def: JDK is a complete software development kit for developing Java applications. It comprises JRE, JavaDoc, compiler, debuggers, etc. Main purpose: JDK is mainly used for code development and execution. JRE: Java runtime environment Def:JRE is a software package providing Java class libraries, JVM and all the required components to run the Java applications. Main purpose: JRE is mainly used for environment creation to execute the code. JVM: Java virtual machine Def: JVM is a platform-dependent, abstract machine comprising of 3 specifications - document describing the JVM implementation requirements, computer program meeting the JVM requirements and instance object for executing the Java byte code and provide the runtime environment for execution. Main purpose: JVM provides specifications for all the implementations to JRE.

Tell us something about JIT compiler.

JIT stands for Just-In-Time and it is used for improving the performance during run time. It does the task of compiling parts of byte code having similar functionality at the same time thereby reducing the amount of compilation time for the code to run.

Why is Java not completely object-oriented?

Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object-oriented language

Why is Java not a pure object oriented language?

Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object-oriented language.

Can the static methods be overridden?

No! Declaration of static methods having the same signature can be done in the subclass but run time polymorphism can not take place in such cases.

Polymorphism?

Polymorphism is the ability to perform many things in many ways. the process by which code, data, methods, or object behaves differently under different circumstances or contexts.

Can the static methods be overloaded?

Yes! There can be two or more static methods in a class with the same name but differing input parameters

Can you tell the difference between equals() method and equality operator (==) in Java?

equals() This is a method defined in the Object class. This method is used for checking the equality of contents between two objects as per the specified business logic. == It is a binary operator in Java .This operator is used for comparing addresses (or references), i.e checks if both the objects are pointing to the same memory location.

Explain the use of final keyword in variable, method and class.

final variable: When a variable is declared as final in Java, the value can't be modified once it has been assigned. If any value has not been assigned to that variable, then it can be assigned only by the constructor of the class. final method: A method declared as final cannot be overridden by its children's classes. A constructor cannot be marked as final because whenever a class is inherited, the constructors are not inherited. Hence, marking it final doesn't make sense. Java throws compilation error saying - modifier final not allowed here final class: No classes can be inherited from the class declared as final. But that final class can extend other classes for its usage

Encapsulation?

is the process of wrapping code and data together into a single unit.


Set pelajaran terkait

Section 7: The Standard Fire Policy

View Set

Google Data Analytics Foundation Module 1

View Set

nsg 320 exam 3 infectious disease kahoot

View Set

FIN 3403 Bliss practice problems ch6-8

View Set

Abdominal Sonography, part 3 - urinary tract, adrenal glands, abdominal vasculature, gastrointestinal tract and abdominal wall

View Set

Practice Questions Midterm Section 5

View Set