0% found this document useful (0 votes)
27 views3 pages

Java

Uploaded by

masterofscp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

Java

Uploaded by

masterofscp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Why is Java called the ‘Platform Independent Programming Language’?

Execution of your program


does not dependent on type of operating system. So compile code only once and run it on any
System.

Explain Final keyword in java? Final keyword in java is used to restrict usage of variable, class and
method:
• Variable: Value of Final variable is constant, you can not change it;
• Method: you can’t override a Final method;
• Class: you can’t inherit from Final class.

When is the super keyword used? super keyword is used to refer:


• immediate parent class constructor;
• immediate parent class variable;
• immediate parent class method.

What is the difference between StringBuffer and String?


• String is an Immutable class, i.e. you can not modify its content once created. Whenever we
alter content of String object, it creates a new string and refer to that,it does not modify the
existing one.
• StringBuffer is a mutable class, means you can change its content later. This is the reason
that the performance with StringBuffer is better than with String.

Why multiple inheritance is not supported in java? Java supports multiple inheritance but not
through classes, it supports only through its interfaces. To avoid the conflict and complexity arises
due to it and keep Java a Simple Object Oriented Language.

Can a top level class be private or protected? Top level classes in java can’t be private or protected,
but inner classes in java can.

Throw vs Throws: throw keyword is used in the method body to throw an exception, while throws
is used in method signature to declare the exceptions that can occur in the statements present in the
method.

finalize() method? Sometimes an object can hold non-java resources such as file handle or database
connection. You can override this method in your class and do the required tasks. Right before an
object is freed, the java run time calls the finalize() method on that object.

Difference in Set and List interface?


• Set and List both are child interface of Collection interface;
• List can hold duplicate values but Set doesn’t allow this.
• In List interface data is present in the order you inserted but in the case of Set insertion order
is not preserved.

• 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? 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? sometimes you have an instance that hasn’t been
initialized

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 “this” keyword in java? Within an instance method or a constructor, this is a reference to
the current object — the object whose method or constructor is being called. You can refer to any
member of the current object from within an instance method or a constructor by using this.

What is an abstract class? 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? 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.

What is object cloning? Object cloning means to create an exact copy of the original object. If a
class needs to support cloning, it must implement java.lang.Cloneable interface and override clone()
method from Object class.

Why method overloading is not possible by changing the return type in java? # of arguments, Type
of arguments & Sequence of arguments are the parameters which are used to generate the unique
mangled name for each function.

Can we override private methods in Java? No, a private method cannot be overridden since it is not
visible from any other class. Read more
What is blank final variable? A final variable in Java can be assigned a value only once, we can
assign a value either in declaration or later. A blank final variable in Java is a final variable that is
not initialized during declaration. Below is a simple example of blank final.
// A simple blank final example
final int i;
i = 30;

What is “super” keyword in java? 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:
• 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.
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.


• 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.
• HashMap allows one null key and multiple null values
• Hashtable doesn’t allow any null key or value.
• 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.

You might also like