top of page
Anchor 1
Frequently Asked C Interview Question & Answers

What are wrapper classes in Java?

Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class. Refer to the below image which displays different primitive type, wrapper class and constructor argument.

Close

What are constructors in Java?

In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.

There are two types of constructors:

  1. Default Constructor: In Java, a default constructor is the one which does not take any inputs. In other words, default constructors are the no-argument constructors which will be created by default in case you no other constructor is defined by the user. Its main purpose is to initialize the instance variables with the default values. Also, it is majorly used for object creation. 

  2. Parameterized Constructor: The parameterized constructor in Java, is the constructor which is capable of initializing the instance variables with the provided values. In other words, the constructors which take the arguments are called parameterized constructors.

Close

What is singleton class in Java and how can we make a class singleton?

Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

Close

What is the difference between equals() and == in Java?

Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.

“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example, a method can be overridden like String class. equals() method is used to compare the values of two objects.

Close

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

The major difference between Heap and Stack memory are:

Stack:

  • Stack memory is used only by one thread of execution.

  • Stack memory can’t be accessed by other threads.

  • Follows LIFO manner to free memory.

  • Exists until the end of execution of the thread.

  • Stack memory only contains local primitive and reference variables to objects in heap space.

 

Heap:

  • Heap memory is used by all the parts of the application.

  • Objects stored in the heap are globally accessible.

  • Memory management is based on the generation associated with each object.

  • Heap memory lives from the start till the end of application execution.

  • Whenever an object is created, it’s always stored in the Heap space.

Close

What is the difference between this() and super() in Java?

In Java, super() and this(), both are special keywords that are used to call the constructor. 

this():

  • this() represents the current instance of a class.

  • Used to call the default constructor of the same class.

  • Used to access methods of the current class.

  • Used for pointing the current class instance.

  • Must be the first line of a block.

 

super():​

  • super() represents the current instance of a parent/base class.

  • Used to call the default constructor of the parent/base class.

  • Used to access methods of the base class.

  • Used for pointing the superclass instance.

  • Must be the first line of a block.

Close

What is the difference between break and continue statements?

break:

  • Can be used in switch and loop (for, while, do while) statements.

  • It causes the switch or loop statements to terminate the moment it is executed.

  • It terminates the innermost enclosing loop or switch immediately.

Example:

for (int i = 0; i < 5; i++)

{

if (i == 3)

{

break;

}

System.out.println(i);

}

continue:

  • Can be only used with loop statements.

  • It doesn’t terminate the loop but causes the loop to jump to the next iteration.

  • A continue within a loop nested with a switch will cause the next loop iteration to execute.

Example:

for (int i = 0; i < 5; i++)

{

if(i == 2)

{

continue;

}

System.out.println(i);

}

Close

What is final keyword in Java?

final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as:

  • final variableWhen the final keyword is used with a variable then its value can’t be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.

  • final method: When a method is declared final then it can’t be overridden by the inheriting class.

  • final class: When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend other class.

Close

Differentiate between the constructors and methods in Java?

Methods:

  • Used to represent the behaviour of an object.

  • Must have a return type.

  • Needs to be invoked explicitly.

  • No default method is provided by the compiler.

  • Method name may or may not be same as the class name.

Constructors:

  • Used to initialize the state of an object.

  • Do not have any return type.

  • Is invoked implicitly.

  • A default constructor is provided by the compiler if the class has none.

  • Constructor name must always be the same as the class name.

Close

What is the difference between a local variable and an instance variable?

In Java, a local variable is typically used inside a method, constructor, or a block and has only local scope. Thus, this variable can be used only within the scope of a block. The best benefit of having a local variable is that other methods in the class won’t be even aware of that variable.

Example

 

if(x > 100)

{

String test = "CWC";

}

 

Whereas, an instance variable in Java, is a variable which is bounded to its object itself. These variables are declared within a class, but outside a method. Every object of that class will create it’s own copy of the variable while using it. Thus, any changes made to the variable won’t reflect in any other instances of that class and will be bound to that particular instance only.

class Test

{

public String EmpName;

public int empAge;

}

Close
bottom of page