Java Related Flashcards

1
Q

Why is Java not truly OOP?

A

Java language is not a Pure Object Oriented Language as it contain these properties:

  1. Java is not fully object oriented because it supports primitive data type like it,byte,long etc.,which are not objects.
  2. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create an immutable class?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Synchronization in java?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Hashset adn hasmap difference

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Overloading or Overriding which one is runtime polymorphism.

A

Overloading is used in compile-time polymorphism. Overriding is implemented in runtime polymorphism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Functional interface

A

A functional interface is an interface that contains only one abstract method. A functional interface can have any number of default methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a marker interface? give examples

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is ADT

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the different types of memory areas allocated by JVM?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is JIT compiler?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Who changes java code to bytecode

A

Javac

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is classLoader in java

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between

byte and char data types in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the main principles of

Object Oriented Programming?

A

Main principles of Object Oriented Programming (OOPS) are:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between Object Oriented Programming

language and Object Based Programming language?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can we inherit a Constructor?

A

No, Java does not support inheritance of constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Why constructors cannot be final

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

If there are no pointers in Java, then why do we get NullPointerException?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Is it possible to use this() and super() both in same constructor?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the meaning of object

cloning in Java

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

CloneNotSupportedException

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Why do we mark main method as

static in Java?

A

The main method in Java is marked as static, so that JVM can call it
to start the program. If main method is not static, then which
constructor will be called by Java process?

23
Q

In what scenario do we use a static

block?

A
At times, there is a class that has static member variables. These
variables need some complicated initialization. At this time static
block helps as a tool to initialize complex static member variable
initialization.
static {
    Map map = new HashMap()
    map.put("AEN", "Alfred E. Newman");
    // etc.
    initials = Collections.unmodifiableMap(map);
}
24
Q

How will you implement method

overloading in Java?

A
  1. Different number of parameters
  2. Different data type of parameters
  3. Different sequence of data type of parameters
25
Q

What kinds of argument
variations are allowed in Method
Overloading?

A

Number of parameters
Data type of parameters
Sequence of data type of paramet

26
Q

Is it allowed to overload main()

method in Java?

A

Yes, Java allows users to create many methods with same name
‘main’. But only public static void main(String[] args) method is
used for execution

27
Q

virtual function???

A

A virtual function is a member function in the base class that we expect to redefine in derived classes.

28
Q

covariant return type

A

Covariant return, means that when one overrides a method, the return type of the overriding method is allowed to be a subtype of the overridden method’s return type.

29
Q

Types of annotation creation in Java?

A
  1. Class level @Target(ElementType.Type)
  2. field level @Target(ElementType.Field)
    3, method level @Target(ElementType.Method)
30
Q

What is a blank final variable in
Java? How can we initialize a blank final
variable?

A

When we declare a final variable without giving any initial value,
then it is called blank final variable.

A blank final instance variable can be initialized in a constructor.
A blank final static variable can be initialized in the static block of
class

31
Q

What is the purpose of package in

Java?

A

A package is used to encapsulate a group of classes, interfaces and
sub-packages.
Often, it is a hierarchical structure of storing
information.
It is easier to organize the related classes and subpackages in this manner.
A Package also provides access protection for classes and
interfaces.
A package also helps in removing naming collision

32
Q

What is java.lang package?

A
In Java, java.lang package contains the classes that are fundamental
to the design of Java programming language. The most important
class in this package is Object class.
It also contains wrapper classes like- Integer, Boolean, Character
etc. It provides Math class for mathematical operations.
33
Q

Is it mandatory to import java.lang

package every

A

No. By default, JVM loads it internally

34
Q

What is Locale in Java?

A

A Locale object represents a specific geographical, political, or
cultural region. It is used to locale-sensitive operations in Java.

35
Q

What is the serialization?

A

Serialization is a process converting an object into a byte array.
This byte array represents the class, version and internal state of the
object. JVM can use this byte array to transmit/read the object over
a network.

36
Q

What is the purpose of

serialization?

A
  1. Communication: It is used for transmitting an object over
    network between two machines.
  2. Persistence: We can store the object’s state in a database
    and retrieve it from database later on.
  3. Caching: Serialization can be used for caching to improve
    performance. We may need 10 minutes to build an object,
    but it may take just 10 seconds to de-serialize the object.
  4. Cross JVM Synchronization: It can be used in same way
    across multiple JVM that follow different architecture.
37
Q

Externalizable interface ???

A

Externalizable is an interface that enables you to define custom rules and your own mechanism for serialization.

38
Q

What is the difference between Serializable and Externalizable
interface?

A

Serializable is a marker interface but Externalizable is not a marker
interface.

39
Q

What is Reflection in Java?

A
Reflection is Java language's ability to inspect and dynamically call
classes, methods, attributes etc. at Runtime. It helps in examining or
modifying the Runtime behavior of a class at Runtime.

Used for testing, IDE, spring

40
Q

How can we create an Object

dynamically at Runtime in Java?

A
We can use Reflection to create an Object dynamically at Runtime
in Java. We can use Class.newInstance() or
Constructor.newInstance() methods for creating such Objects.
41
Q

What is the purpose of gc() in

Java?

A

Java provides two methods System.gc() and Runtime.gc() to request
the JVM to run the garbage collection. By using these methods,
programmers can explicitly send request for Garbage Collection.
But JVM process can reject this request and wait for some time
before running the GC

42
Q

What are the different types of

References in Java?

A

In Java, there are four types of references:

  1. Strong Reference
  2. Soft Reference
  3. Weak Reference
  4. Phantom Reference
43
Q

What is the purpose of the

Runtime class?

A
The purpose of the Runtime class is to provide access to the Java
Runtime system.
  1. Runtime.freeMemory() – This method returns the value of
    free memory in JVM
  2. Runtime.maxMemory() - This method returns the value of
    maximum memory that JVM can use.
  3. Runtime.gc() – This method can invoke garbage collection.
44
Q

How many types of Nested classes

are in Java?

A

Java provides four types of Nested classes:

  1. Member inner class
  2. Local inner class
  3. Anonymous inner class
  4. Static nested class
45
Q
What is the string constant pool? Which one creates a string object in the pool? 
String s = "s";
String s = new String("s);
A

A string constant pool is a separate place in the heap memory where the values of all the literal strings which are defined in the program are stored.
String s = “s”; creates a object in the pool.

46
Q

String s = new String (“test”);
String x = new String (“test”);
Why is s == x false?

A

The comparison here is refering to an address of two different string Strings created with the new keyword.

47
Q

What is String interning?

A

String interning refers to the concept of using only one copy of a
distinct String value that is Immutable.
It provides the advantage of making String processing efficient in
Time as well as Space complexity. But it introduces extra time in
creation of String.

48
Q

What is the basic difference
between a String and StringBuffer
object?

A

String is an immutable object. Its value cannot change after creation.
StringBuffer is a mutable object. We can keep appending or
modifying the contents of a StringBuffer in Java.

49
Q

StringBuffer and StringBuilder, which is thread safe?

A
50
Q

In Java, what are the differences

between a Checked and Unchecked?

A

Checked Exceptions extend Throwable class, but they do not extend
RuntimeException or Error classes. UncheckedException extend
RuntimeException class.

Checked Exceptions are checked at compile time in Java.
Unchecked Exceptions happen at Runtime, so they are not checked
at compile time.

IOException, SQLException etc. are examples of Checked
Exceptions. NullPointerException, ArithmeticException etc. are
examples of Unchecked Exceptions.

51
Q
What is the base class for
Error and Exception classes in Java?
A
Error as well as Exception class is derived from Throwable class
in Java.
52
Q

In what scenarios, a finally block

will not be executed?

A

There are two main scenarios in which finally block is not
executed:
1. Program exits by calling system.exit() call.
2. A fatal error causes JVM to crash.

53
Q

What are the differences between Pre-emptive Scheduling

Scheduler and Time Slicing Scheduler?

A

In Pre-emptive scheduling, the highest priority task will keep getting
time to execute until it goes to waiting state or dead state or a task
with higher priority comes into queue for scheduling.

In Time slicing scheduling, every task gets a predefined slice of
time for execution, and then it goes to the pool of tasks ready for
execution. The scheduler picks up the next task for execution, based
on priority and various other factors.

54
Q

What does the statick {} does

A

Answer