Core Java Flashcards

1
Q

What is the difference between an interface and an abstract class?

A

An abstract class can have both concrete and abstract methods whereas an interface must have only abstract methods if any.

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

Can you instantiate an abstract class? an interface?

A

No and No. Abstact classes are made to be extended only.

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

What is the difference between Treeset and HashSet?

A

The two general purpose Set implementations are HashSet and TreeSet. HashSet is much faster (constant time versus log time for most operations) but offers no ordering guarantees.

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

What is the difference between ArrayList and Vector?

A

Vector is synchronized whereas ArrayList is not.

Vector is considered as a legacy class in Java now; use ArrayList instead

Also Vector is not thread safe. Use CopyOnWriteArrayList type or synchronize ArrayList externally using the synchronizedList()

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

What type of Collection would not allow duplicate elements?

A

Set

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

What is the difference between Hashtable and Hashmap?

A

“a. Hashtable is synchronized whereas Hashmap is not.

b. Hashmap permits null values and the null key.”

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

How do you serialize (persist) an object in Java?

A

a. Step 1: An object is marked serializable by implementing the java.io.Serializable interface, which signifies to the underlying API that the object can be flattened into bytes and subsequently inflated in the future.
b. Step 2: The next step is to actually persist the object. That is done with the java.io.ObjectOutputStream class. That class is a filter stream–it is wrapped around a lower-level byte stream (called a node stream) to handle the serialization protocol for us. Node streams can be used to write to file systems or even across sockets. That means we could easily transfer a flattened object across a network wire and have it be rebuilt on the other side!
c. To restore the object back, you use ObjectInputStream.readObject() method call. The method call reads in the raw bytes that we previously persisted and creates a live object that is an exact replica of the original. Because readObject () can read any serializable object, a cast to the correct type is required. With that in mind, the class file must be accessible from the system in which the restoration occurs. In other words, the object’s class file and methods are not saved; only the object’s state is saved.

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

What is a stored procedure and how would you call it in Java?

A

An executable block ofPL/SQL code& stored in Oracle db.

Is called from a Java class using a CallableStatement object. When called, its name & any relevantparametersare sent over theJDBCconnection toDBMS, which executes the procedure & returns the results (if applicable) via connection.

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

What is the difference between Statement and PreparedStatement?

A

PreparedStatements are pre-compiled by the JVM. The database doesn’t have to compile the SQL each and every time it is executed. PreparedStatement can be parameterized, which can make the SQL more readable. Furthermore, PreparedStatement will properly escape reserved characters to prevent SQL injection attacks.

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

How can you force Garbage Collection?

A

Garbage collection cannot be forced but only requested using System.gc().

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

What is final, finalize() and finally?

A

“a. final: final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be overridden. A final variable can’t change from its initialized value.

b. finalize(): finalize method is used just before an object is destroyed and called just prior to garbage collection.”
c. finally: finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address this contingency.

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

What is a Marker Interface?

A

A marker interface is an interface which has no methods at all. Example: Serializable, Remote, Cloneable. Generally, they are used to give additional information about the behavior of a class.

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

How would you clone an object?

A

First, tag the class with the Cloneable marker interface. Next, invoke clone (). The clone method is declared in java.lang.Object and does a shallow copy.

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

Why are strings immutable in java?

A

Identical String literals are collected in the “String pool” in an effort to conserve memory. Reference variables will then point to the same String object instance. Changing the object’s state in the String pool will make changes to all references to that String object. Instead, when a change to a String is made, the JVM makes a new String object, and the reference variable points to the new String in the String pool.

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

What is the difference between String, StringBuilder, and StringBuffer?

A

Strings are immutable. Both StringBuilder and StringBuffer are mutable. Furthermore, StringBuffer is sychronized while StringBuilder is not.

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

What is the difference between static and final variables?

A

Static variable is a global variable shared by all the instances of objects and it has only single copy. A final variable is a constant variable and it cannot be changed.

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

What are the implicit modifiers required for interface variables?

A

public static final

17
Q

What are transient variables?

A

Transient variables are those variables which cannot be serialized.
public – can be accessed from any package.

private – only members of the same class can access.

protected – can be accessed by classes inside the package and subclasses anywhere.

default - no access by classes or subclasses outside the package

18
Q

What is a wrapper class?

A

Wrapper class is a wrapper around a primitive data type. It represents primitive data types in their corresponding class instances e.g. a boolean data type can be represented as a Boolean class instance. All of the primitive wrapper classes in Java are immutable i.e. once assigned a value to a wrapper class instance cannot be changed further.

19
Q

What is Reflection API?

A

The first component of the Reflection API is the mechanism used to fetch information about a class. This mechanism is built into the class named Class. The special class Class is the universal type for the meta information that describes objects within the Java system. Class loaders in the Java system return objects of type Class. The three most interesting methods in this class were:
“forName, which would load a class of a given name, using the current class loader
getName, which would return the name of the class as a String object,which was useful for identifying object references by their class name
newInstance, which would invoke the null constructor on the class (if it exists) and return you an object instance of that class of object “
“To these three useful methods the Reflection API adds some additional methods to class Class. These are as follows:
getConstructor, getConstructors, getDeclaredConstructor
getMethod, getMethods, getDeclaredMethods
getField, getFields, getDeclaredFields
getSuperclass
getInterfaces
getDeclaredClasses”

20
Q

Is Java pass by value or pass by reference?

A

Java is strictly pass by value.

21
Q

In what ways can you create a thread?

A

By extending the Thread Class or by implementing the Runnable Interface

22
Q

What is encapsulation?

A

Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.

23
Q

What is polymorphism?

A

Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class. An object can also be polymorphically referenced by its supertype “parent” class, for example ParentClass obj = new SubClass( );

24
What is inheritance?
A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
25
What is the difference between method overriding and method overloading?
Method overriding - In a subclass when one declares an identical method from the superclass, this method overrides the one in the superclass. Method overloading - Within the same class when one declares more than method with the same name but different signature (parameters).
26
Describe the singleton pattern
A singleton pattern should be used when only a single instance of a class should exist. Singletons are often used to manage internal or external resources (windows manager, print spooler, etc)
27
Do I need to initialize an object to call a static method
No. All static methods belong to the class and are not bound to any one object
28
Difference between array and array list?
"1. Array is static in size and arraylist is dynamic in size. 2. Add method is used to insert values into arraylist where as for array assignment operator is needed 3. Arrays are multi dimensional and array list is always single dimensional Both can store null values, duplicate values"
29
What is singleton?
Singleton class control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.
30
What memory areas are allocated by JVM?
Class Loader, Class Area, Heap, Stack, PC Register, Native method stack, Execution Engine
31
How do you operator over loading in Java
Can't. Because java doesn't support operator over loading
32
Difference between linkedlist and arraylist
1. Searching is faster in ArrayList than LinkedList 2. LinkedList element deletion is faster compared to ArrayList. 3. Insertions are easy and fast in LinkedList as compared to ArrayList because there is no risk of resizing array
33
ArrayList, LinkedList, and Vector are all implementations of the List interface. Which of them is most efficient for adding and removing elements from the list?
Most Efficient For Frequent Add/Remove at the Beginning or Middle: Use LinkedList because it provides O(1) time complexity for adding/removing elements at the beginning or end and O(1) for actual insertion/removal once the position is found. For Frequent Add/Remove at the End: Use ArrayList or Vector because they provide O(1) amortized time complexity for adding/removing elements at the end. For Thread-Safe Operations: Use Vector if thread safety is required, but note that it has synchronization overhead. For Random Access: Use ArrayList or Vector because they provide O(1) random access.
34
Recommended use cases based on efficiency of each: ArrayList, LinkedList & Vector
Use LinkedList if your primary use case involves frequent additions and removals at the beginning or middle of the list. Use ArrayList if your primary use case involves frequent additions and removals at the end of the list or if you need fast random access. Avoid Vector unless you specifically need a synchronized (thread-safe) list, as it has performance overhead compared to ArrayList.
35
ArrayList Characteristics
Internal Structure: Backed by a dynamic array. Add/Remove at the End: Adding or removing elements at the end of the list is efficient (O(1) on average). However, if the internal array needs to resize, the operation can take O(n) time (though this is amortized to O(1) over many operations). Add/Remove in the Middle: Adding or removing elements in the middle of the list requires shifting all subsequent elements, which is inefficient (O(n)). Random Access: Very efficient (O(1)) due to array indexing.
36
LinkedList
Internal Structure: Backed by a doubly linked list. Add/Remove at the Beginning or End: Adding or removing elements at the beginning or end of the list is efficient (O(1)). Add/Remove in the Middle: Adding or removing elements in the middle requires traversing the list to find the correct position, which is inefficient (O(n)). However, once the position is found, the actual insertion or removal is O(1). Random Access: Inefficient (O(n)) because the list must be traversed to reach the desired index.
37
Vector
Internal Structure: Similar to ArrayList (backed by a dynamic array), but synchronized (thread-safe). Add/Remove at the End: Similar to ArrayList, adding or removing elements at the end is efficient (O(1) on average, with occasional O(n) for resizing). Add/Remove in the Middle: Similar to ArrayList, adding or removing elements in the middle is inefficient (O(n) due to element shifting). Random Access: Very efficient (O(1)) due to array indexing. Overhead: The synchronization overhead makes Vector slower than ArrayList for single-threaded use cases. Vector is considered as a legacy class in Java now; use ArrayList instead Also Vector is not thread safe. Use CopyOnWriteArrayList type or synchronize ArrayList externally using the synchronizedList()
39
40