Java Related Flashcards
Why is Java not truly OOP?
Java language is not a Pure Object Oriented Language as it contain these properties:
- Java is not fully object oriented because it supports primitive data type like it,byte,long etc.,which are not objects.
- The static keyword: When we declares a class as static then it can be used without the use of an object in Java. If we are using static function or static variable then we can’t call that function or variable by using dot(.) or class object defying object oriented feature.
How do you create an immutable class?
- The class must be declared as final so that child classes can’t be created.
- Data members in the class must be declared private so that direct access is not allowed.
- Data members in the class must be declared as final so that we can’t change the value of it after object creation. (-Make all fields final and private.)
- Don’t provide “setter” methods — methods that modify fields or objects referred to by fields.
What is Synchronization in java?
Hashset adn hasmap difference
Overloading or Overriding which one is runtime polymorphism.
Overloading is used in compile-time polymorphism. Overriding is implemented in runtime polymorphism.
What is Functional interface
A functional interface is an interface that contains only one abstract method. A functional interface can have any number of default methods.
What is a marker interface? give examples
A marker interface is an interface that has no methods or constants inside it. It provides run-time type information about objects.
E.g. Serializable, Cloneable, Remote etc
What is ADT
class of objects whose logical behavior is defined by a set of values and a set of operations. ADT only mentions what operations are to be performed but not how these operations will be implemented.
What are the different types of memory areas allocated by JVM?
In java, JVM allocates memory to different processes, methods and
objects. Some of the memory areas allocated by JVM are:
1. ClassLoader: It is a component of JVM used to load class
files.
2. Class (Method) Area: It stores per-class structures such as
the runtime constant pool, field and method data, and the
code for methods.
3. Heap: Heap is created a runtime and it contains the runtime
data area in which objects are allocated.
4. Stack: Stack stores local variables and partial results at
runtime. It also helps in method invocation and return
value. Each thread creates a private JVM stack at the time
of thread creation.
5. Program Counter Register: This memory area contains the
address of the Java virtual machine instruction that is
currently being executed.
6. Native Method Stack: This area is reserved for all the
native methods used in the application.
What is JIT compiler?
591
A JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it’s called) into a form that’s usually faster, typically the host CPU’s native instruction set. A JIT has access to dynamic runtime information whereas a standard compiler doesn’t and can make better optimizations like inlining functions that are used frequently.
Who changes java code to bytecode
Javac
What is classLoader in java
ClassLoader has a memory space allocated by jVM. Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime. They’re also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn’t need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.
What is the difference between
byte and char data types in Java?
Both byte and char are numeric data types in Java. They are used to
represent numbers in a specific range.
Major difference between them is that a byte can store raw binary
data where as a char stores characters or text data.
Usage of char is E.g. char ch = ‘x’;
Byte values range from -128 to 127.
A byte is made of 8 bits. But a char is made of 16 bits. So it is
equivalent to 2 bytes.
What are the main principles of
Object Oriented Programming?
Main principles of Object Oriented Programming (OOPS) are:
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
What is the difference between Object Oriented Programming
language and Object Based Programming language?
Object Oriented Programming languages like Java and C++ follow
concepts of OOPS like- Encapsulation, Abstraction, Polymorphism
and Inheritance etc.
Object Based Programming languages follow some features of
OOPS but they do not provide support for Polymorphism and
Inheritance. Egg. JavaScript, VBScript etc
Can we inherit a Constructor?
No, Java does not support inheritance of constructor.
Why constructors cannot be final
If we set a method as final it means we do not want any class to
override it. But the constructor (as per Java Language
Specification) cannot be overridden. So there is no use of marking it
final.
If there are no pointers in Java, then why do we get NullPointerException?
In Java, the pointer equivalent is Object reference. When we use a .
it points to object reference. So JVM uses pointers but
programmers only see object references.
In case an object reference points to null object, and we try to
access a method or member variable on it, then we get
NullPointerException.
Is it possible to use this() and super() both in same constructor?
No, Java does not allow using both super() and this() in same
constructor. As per Java specification, super() or this() must be the
first statement in a constructor.
What is the meaning of object
cloning in Java
Object.clone() method is used for creating an exact copy of the
object in Java. It acts like a copy constructor. It creates and returns
a copy of the object, with the same class and with all the fields
having same values as of the original object.
One disadvantage of cloning is that the return type is an Object. It
has to be explicitly cast to actual type.
CloneNotSupportedException
Thrown to indicate that the clone method in class Object has been called to clone an object, but that the object’s class does not implement the Cloneable