Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Q2. Explain Public Static Void Main (String Args ) in Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Q1. Explain JDK, JRE and JVM?

JDK: It stands for Java Development Kit. It is the tool necessary to compile, document and JRE +
development tools.
JRE: It stands for Java Runtime Environment JRE refers to a runtime environment in which Java
bytecode can be executed. It’s an implementation of the JVM which physically exists.
JVM: It stands for Java Virtual Machine. It is an abstract machine. It is a specification that provides a
run-time environment in which Java bytecode can be executed. JVM follows three notations:
Specification, Implementation, and Runtime Instance.

Q2. Explain public static void main(String args[]) in Java.

main() in Java is the entry point for any Java program. It is always written as public static void
main(String[] args).

 public: Public is an access modifier, which is used to specify who can access this method.
Public means that this Method will be accessible by any Class.
 static: It is a keyword in java which identifies it is class-based. main() is made static in Java
so that it can be accessed without creating the instance of a Class. In case, main is not made
static then the compiler will throw an error as main() is called by the JVM before any objects
are made and only static methods can be directly invoked via the class. 
 void: It is the return type of the method. Void defines the method which will not return any
value.
 main: It is the name of the method which is searched by JVM as a starting point for an
application with a particular signature only. It is the method where the main execution occurs.
 String args[]: It is the parameter passed to the main method.

Q3. Why Java is platform independent?

Java is called platform independent because of its byte codes which can run on any system
irrespective of its underlying operating system.

Q4. Why Java is not 100% Object-oriented?


Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean,
byte, char, int, float, double, long, short which are not objects.

Q5. 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.

Q6. 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.

Q7. 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:
method can be overridden like String class. equals() method is used to compare the values of two
objects.

Q8. What is a package in Java? List down various advantages of packages.

Packages in Java, are the collection of related classes and interfaces which are bundled together. By
using packages, developers can easily modularize the code and optimize its reuse. Also, the code
within the packages can be imported by other classes and reused. Below I have listed down a few of
its advantages:

 Packages help in avoiding name clashes


 They provide easier access control on the code
 Packages can also contain hidden classes which are not visible to the outer classes and only
used within the package
 Creates a proper hierarchical structure which makes it easier to locate the related classes

Q9. Why pointers are not used in Java?

Java doesn’t use pointers because they are unsafe and increases the complexity of the program. Since,
Java is known for its simplicity of code, adding the concept of pointers will be contradicting.
Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct
access to memory by the user,  pointers are discouraged in Java.

Q10. What is JIT compiler in Java?

JIT stands for Just-In-Time compiler in Java. It is a program that helps in converting the Java
bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is
enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles
the bytecode of the invoked method into native machine code, compiling it “just in time” to execute.
Once the method has been compiled, the JVM summons the compiled code of that method directly
rather than interpreting it. This is why it is often responsible for the performance optimization of Java
applications at the run time.

Q11. What are access modifiers in Java?

In Java, access modifiers are special keywords which are used to restrict the access of a class,
constructor, data member and method in another class. Java supports four types of access modifiers:
1. Default
2. Private
3. Protected
4. Public

Q12. Define a Java Class.

A class in Java is a blueprint which includes all your data.  A class contains fields (variables) and
methods to describe the behavior of an object. Let’s have a look at the syntax of a class.

1class Abc {
2member variables // class body
3methods}

Q13. What is an object in Java and how is it created?

An object is a real-world entity that has a state and behavior. An object has three characteristics:

1. State
2. Behavior
3. Identity

An object is created using the ‘new’ keyword. For example:

ClassName obj = new ClassName();

Q14. What is Object Oriented Programming?

Object-oriented programming or popularly known as OOPs is a programming model or approach


where the programs are organized around objects rather than logic and functions. In other words, OOP
mainly focuses on the objects that are required to be manipulated instead of logic. This approach is
ideal for the programs large and complex codes and needs to be actively updated or maintained.

Q15. What are the main concepts of OOPs in Java?

Object-Oriented Programming or OOPs is a programming style that is associated with concepts like:

1. Inheritance: Inheritance is a process where one class acquires the properties of another.
2. Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code
together as a single unit.
3. Abstraction: Abstraction is the methodology of hiding the implementation details from the
user and only providing the functionality to the users. 
4. Polymorphism: Polymorphism is the ability of a variable, function or object to take multiple
forms.

Q16. 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
1if(x > 100)
2{
3String test = "AMS";
4}

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.

1
class Test{
2
public String EmpName;
3
public int empAge;
4

Q17. 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 variable

When 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.

Q18. What is the difference between break and continue statements?

break continue

1. Can be used in switch and loop (for, while,


1. Can be only used with loop statements
do while) statements

2. It causes the switch or loop statements to 2. It doesn’t terminate the loop but causes the loop
terminate the moment it is executed to jump to the next iteration

3. It terminates the innermost enclosing loop 3. A continue within a loop nested with a switch will
or switch immediately cause the next loop iteration to execute

Example break:

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


2{

3if (i == 3)

4{

5break;

6}

7System.out.println(i);

8}
Example continue:

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

2{

3if(i == 2)

4{

5continue;

6}

7System.out.println(i);

8}

Q19.What is an infinite loop in Java? Explain with an example.

An infinite loop is an instruction sequence in Java that loops endlessly when a functional exit isn’t
met. This type of loop can be the result of a programming error or may also be a deliberate action
based on the application behavior. An infinite loop will terminate automatically once the application
exits.

For example:

1public class InfiniteForLoopDemo

2{

3public static void main(String[] arg) {

4for(;;)

5System.out.println("Welcome to Edureka!");

6// To terminate this program press ctrl + c in the console.

7}

8}

Can we Overload or Override static methods in java ?


 Overriding : Overriding is related to run-time polymorphism. A subclass (or derived class)
provides a specific implementation of a method in superclass (or base class) at runtime.
 Overloading: Overloading is related to compile time (or static) polymorphism. This feature
allows different methods to have same name, but different signatures, especially number of
input parameters and type of input paramaters.
 Can we overload static methods?   The answer is ‘Yes’. We can have two ore more static
methods with same name, but differences in input parameters
 Can we Override static methods in java?  We can declare static methods with same signature
in subclass, but it is not considered overriding as there won’t be any run-time polymorphism.
Hence the answer is ‘No’. Static methods cannot be overridden because method overriding
only occurs in the context of dynamic (i.e. runtime) lookup of methods. Static methods (by
their name) are looked up statically (i.e. at compile-time).

Why the main method is static in java?


The method is static because otherwise there would be ambiguity: which constructor should be
called? Especially if your class looks like this:

public class JavaClass


{
protected JavaClass(int x)
{ }
public void main(String[] args)
{

}
}

Should the JVM call new JavaClass(int)? What should it pass for x? If not, should the JVM instantiate
JavaClass without running any constructor method? because that will special-case your entire class –
sometimes you have an instance that hasn’t been initialized, and you have to check for it in every
method that could be called. There are just too many edge cases and ambiguities for it to make sense
for the JVM to have to instantiate a class before the entry point is called. That’s why main is static.

What happens if you remove static modifier from the main method?
Program compiles successfully . But at runtime throws an error “NoSuchMethodError”.

What is the scope of variables in Java in following cases? 

 Member Variables (Class Level Scope) : The member variables must be declared inside class
(outside any function). They can be directly accessed anywhere in class
 Local Variables (Method Level Scope) : Variables declared inside a method have method
level scope and can’t be accessed outside the method.
 Loop Variables (Block Scope) : A variable declared inside pair of brackets “{” and “}” in a
method has scope withing the brackets only.

What is an abstract class? How abstract classes are similar or different in Java from C++?
Abstract classes are classes that contain one or more abstract methods. An abstract method is a
method that is declared, but contains no implementation. Abstract classes may not be instantiated, and
require subclasses to provide implementations for the abstract methods.

 Like C++, in Java, an instance of an abstract class cannot be created, we can have references
of abstract class type though.
 Like C++, an abstract class can contain constructors in Java. And a constructor of abstract
class is called when an instance of a inherited class is created
 In Java, we can have an abstract class without any abstract method. This allows us to create
classes that cannot be instantiated, but can only be inherited.
 Abstract classes can also have final methods (methods that cannot be overridden). For
example, the following program compiles and runs fine.

Which class is the superclass for every class ?


Object class

Can we overload main() method?


The main method in Java is no extra-terrestrial method. Apart from the fact that main() is just like any
other method & can be overloaded in a similar manner, JVM always looks for the method signature to
launch the program.

 The normal main method acts as an entry point for the JVM to start the execution of program.
 We can overload the main method in Java. But the program doesn’t execute the overloaded
main method when we run your program, we need to call the overloaded main method from
the actual main method only.

How is inheritance in C++  different from Java?

1. In Java, all classes inherit from the Object class directly or indirectly. Therefore, there
is always a single inheritance tree of classes in Java, and Object class is root of the
tree.
2. In Java, members of the grandparent class are not directly accessible. See this G-Fact
for more details.
3. The meaning of protected member access specifier is somewhat different in Java. In
Java, protected members of a class “A” are accessible in other class “B” of same
package, even if B doesn’t inherit from A (they both have to be in the same package).
4. Java uses extends keyword for inheritance. Unlike C++, Java doesn’t provide an
inheritance specifier like public, protected or private. Therefore, we cannot change the
protection level of members of base class in Java, if some data member is public or
protected in base class then it remains public or protected in derived class. Like C++,
private members of base class are not accessible in derived class.
Unlike C++, in Java, we don’t have to remember those rules of inheritance which are
combination of base class access specifier and inheritance specifier.
5. In Java, methods are virtual by default. In C++, we explicitly use virtual keyword. See
this G-Fact for more details.
6. Java uses a separate keyword interface for interfaces, and abstract keyword for
abstract classes and abstract functions.
7. Unlike C++, Java doesn’t support multiple inheritance. A class cannot inherit from
more than one class. A class can implement multiple interfaces though.
8. In C++, default constructor of parent class is automatically called, but if we want to
call parametrized constructor of a parent class, we must use Initializer list. Like C++,
default constructor of the parent class is automatically called in Java, but if we want to
call parameterized constructor then we must use super to call the parent constructor.

What is “super” keyword in java?


The super keyword in java is a reference variable that is used to refer parent class objects.
The keyword “super” came into the picture with the concept of Inheritance. Whenever you
create the instance of subclass, an instance of parent class is created implicitly i.e. referred by
super reference variable.
Various scenarios of using java super Keyword:

 super is used to refer immediate parent instance variable


 super is used to call parent class method
 super() is used to call immediate parent constructor

What is static variable in Java?


The static keyword in java is used for memory management mainly. We can apply java static
keyword with variables, methods, blocks and nested class. The static keyword belongs to the
class than instance of the class.

The static can be:

 variable (also known as class variable)


 method (also known as class method)
 block
 nested class

Differences between HashMap and HashTable in Java.


1. HashMap is non synchronized. It is not-thread safe and can’t be shared between many
threads without proper synchronization code whereas Hashtable is synchronized. It is thread-
safe and can be shared with many threads.
2. HashMap allows one null key and multiple null values whereas Hashtable doesn’t allow
any null key or value.
3. HashMap is generally preferred over HashTable if thread synchronization is not needed

How are Java objects stored in memory?


In Java, all objects are dynamically allocated on Heap. This is different from C++ where
objects can be allocated memory either on Stack or on Heap. In C++, when we allocate abject
using new(), the object is allocated on Heap, otherwise on Stack if not global or static.
In Java, when we only declare a variable of a class type, only a reference is created (memory
is not allocated for the object). To allocate memory to an object, we must use new(). So the
object is always allocated memory on heap.  

You might also like