Java Flashcards

1
Q

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

A

Interface

  1. Interface can have only abstract, default and static methods
  2. The interface keyword is used to declare an interface
  3. The implement keyword is used to make a class abide by the methods defined by the interface
  4. Interface supports multiple inheritance
  5. Interface can’t provide the implementation of abstract class
  6. Interface has only static and final variables
  7. All method declarations are implicitly public
  8. You can implement any number of interfaces

Abstract

  1. Abstract class can have abstract and non-abstract methods
  2. Abstract class doesn’t support multiple inheritance
  3. You can declare fields that have final, non-final, static and non-static variables
  4. Abstract class can provide the implementation of methods, have concrete methods.
  5. The abstract keyword is used to declare abstract class
  6. All fields are automatically public, static, and final
  7. All methods that you declare or define are public
  8. You can extend only one class, whether or not it is abstract

Both

  1. Can not be instantiated
  2. Can be subclassed
  3. If the subclass does not provide the implementation for the parent class abstract methods, the subclass must be declared abstract.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are abstract methods?

A

An abstract method is a method that is declared without an implementation.

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

What are default methods?

A

Default methods enable you to add new functionality to the interfaces by specifying the method definition and using the default keyword as the method signature

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

What is an interface class?

A

Interface is a reference type and is a contract that binds specific method signatures and fields to the class that implements it

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

What is an abstract class?

A

An abstract class is a class that is declared abstract. It may or may not include abstract methods.

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

What is the diamond problem?

A

There is a class A. Both class B and C derives from class A. Thus both class B and class C implements method from class A. Now we have class D. Class D derives from class B and C. The diamond problem comes from when class D makes a call to method definitions in class A. This call is ambiguous because it is not sure whether to call the version of the method derived from class B or class C.

Java does not have the diamond problem since it does not support multiple inheritance. Instead, Java uses interfaces.

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

What is the static keyword?

A

Variables that are declared static is called a class variable that is initialized one and all instances share the same copy of the variable. It can be accessed directly without creating an instance of the class. It can be accessed directly by the class name and doesn’t need any object.

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

What is an instance variable

A

A variable without the static keyword and each instance of the class has its own copy of the variable.

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

What is the difference between an ArrayList and a Vector?

A

Vector is synchronized, which means only one thread is working and no other thread can perform an operation at a time.

Arraylist is not synchronized, so multiple threads can work on ArrayList at the same time.

Both ArrayList and a Vector can grow and shrink dynamically, but the way they resize is different. Vector double itself by ArrayList grow by half of its size.

ArrayList is faster since it is non-synchronized and Vectors are slower because it is thread-safe since the Vector gets locked until the work is complete by the thread.

An ArrayList is fail-fast since it is not thread safe unlike a Vector

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

What is fail-fast?

A

Fail-fast is an exception that gets thrown when a collection is being iterated and it detects some changes being made to the collection.

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

What is the difference between a tree set and a hashset?

A

HashSet

  1. Better performance than Tree Set, offers constant time
  2. Does not maintain any order of elements
  3. Store contents according to the key, or the hash code. The hash code is used as an index

Tree Set

  1. Takes log(n) time
  2. Sort in ascending order by default

Both

  1. Extends AbstractSet and implements the Set interface
  2. Does not hold duplicate elements
  3. It is more optimal to add elements to HashSet and convert it into Tree Set for sorted elements.
  4. Both are non-synchronized, so they are not thread-safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a hashcode?

A

A number generated from an object. It is used to store and retrieve objects quickly in a hashtable.

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

What type of Collections that does not allow duplicates?

A

Set

A set is a Collection that cannot contain duplicate elements.

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

What is the difference between HashTable and HashMap?

A

HashMap

  1. It is not synchronized, so it is not thread safe
  2. HashMap allows one null key and any number of null values
  3. HashMap object values are iterated by using iterator
  4. Iterator is fail-fast
  5. HashMap is much faster and uses less memory than HashTable since it is not synchronized

HashTable

  1. It is synchronized, so it is thread safe
  2. HashTable do not allow null keys and null values
  3. HashTable is the only class other than Vector which uses enumerator to iterate the object values
  4. Not fail-fast
  5. Slower performance since it is synchronized
  6. Legacy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

Java has the Serialization API

Serialization is the process of saving an object’s state to a persistence store and also rebuilding the object from the save information when needed in the future.

To serialize an object

  1. Implement the Serializable interface. This is consider a marker interface and have no default implementation. The interface is simply an indication to the Java runtime that the object is valid for persistence
  2. Use the writeObject(someObject object) method from the ObjectOutputStream class.
  3. To restore the object, you use the readObject() method from the ObjectInputStream class and you will need to cast the object to the specific class.
  4. Only the object state is saved
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

A stored procedure is an executable block of code that is written in PL/SQL and stored in the Oracle database. A stored procedure is called from a Java class using CallableStatement object. When the procedure is called, its name and any relevant parameters are sent over the JDBC connection to the DBMS which executes the procedure and returns the result (if applicable) via the connection.

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

What is PL/SQL

A

PL/SQL is a procedural language extension to Structured Query Language. The purpose is to combine database language and procedural programming language. The basic unit in PL/SQL is called a block, which is part up of three parts: a declarative part, an executable part, and an exception-building part.

18
Q

What are the difference between stored procedures and functions?

A

Stored Procedure

  1. Is used to perform business logic
  2. Must not have the return type
  3. May return 0 or more values
  4. We can call functions from the procedure
  5. Procedure supports input and output parameters
  6. Exception handling using try/catch block can be used in stored procedures

Function

  1. Is used to perform calculation
  2. Must have the return type
  3. May return only one value
  4. Procedure cannot be called from function
  5. Function supports only input parameter
  6. Exception handling using try/catch can’t be used in user defined functions
19
Q

What is JDBC?

A

JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and the database.

  1. Can make connection to a database
  2. Can create SQL statements
  3. Can execute SQL queries in the database
  4. Can view and modify records
  5. Can write Java Applications
  6. Can write Java Servlets
  7. Can write Java ServerPages (JSP)
20
Q

How do you establish a connection for JDBC?

A

DriverManager
.getConnection(“jdbc:mysql://localhost:3306/test”, “username”,”password”)

test is a database schema

21
Q

What is JSP?

A

JavaServer Pages

It is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web-based applications.

22
Q

What is the difference between Statement, PreparedStatement, CallableStatement?

A

Statement

  1. It is used to execute normal SQL queries
  2. It is preferred when a particular SQL query is to be executed only once
  3. You cannot pass the parameters to SQL query using this interface
  4. This interface is mainly used for DDL statements like CREATE, ALTER, DROP
  5. The performance of this interface is very low

PreparedStatement

  1. It is used to execute parameterized or dynamic SQL queries
  2. It is preferred when a particular query is to be executed multiple times
  3. You can pass the parameters to SQL query at run time using this interface, making it more readable
  4. It is used for any kind of SQL queries which are to be executed multiple times
  5. The performance of this interface is better than the Statement interface (when used for multiple execution of same query)
  6. It is precompiled by the JVM so the database doesn’t have to compile the SQL each and every time it is executed
  7. It will properly escape reserved characters to prevent SQL injection attacks

CallableStatement

  1. It is used to call the stored procedures
  2. It is preferred when the stored procedures are to be executed
  3. You can pass 3 types of parameters using this interface (In, Out, In Out)
  4. It is used to execute stored procedures and functions
  5. The performance of this interface is high
23
Q

What is Java Garbage Collection?

A

Garbage are unreferenced objects. Garbage Collection is process of reclaiming the runtime unused memory automatically, so Java provides better memory management. It only collections objects that are created by the new keyword. The finalize method is used to perform cleanup processing (destroying remaining objects). You can use the gc() method to invoke garbage collector . Neither finalization nor garbage collection is guaranteed.

24
Q

What is the final, finalize(), and finally

A

Final

  1. Final is used to apply restrictions on class, method, and variable
  2. Final class can’t be inherited
  3. Final method can’t be overridden
  4. Final variable value can’t be changed
  5. Final is a keyword

Finally

  1. Finally is used to place important code
  2. It will be executed whether exception is handled or not
  3. Finally is a block

Finalize

  1. Finalize is used to perform clean up processing just before object is garbage collected
  2. Finalize is a method
25
Q

What are some Marker interfaces?

A

Marker interfaces in Java are interfaces with no members declared in them. They are just an empty interface used to mark or identify a special operation. Marker interfaces give instructions to JVM that classes implementing them will have special behavior

  1. Cloneable
  2. Serializable
  3. Remote
  4. EventListener
26
Q

How do you clone an object?

A

A clone is an exact copy of the original. Cloning an object in Java means the ability to create an object with similar state.

To clone an object, we need to tag the class with the Cloneable marker interface. Next we invoke the clone() method. The clone method belongs to the Object class and does a shallow copy.

27
Q

Why are Strings immutable?

A

String is an immutable class in Java. An immutable class simply means that the instance can’t be modified.

When a string is created and if the string does not exist in the STRING POOL, a new object will be created and stored in the pool. Otherwise, the reference of the existing string in the STRING POOL will be returned. Since the String is immutable, the hashcode for it will always be the same and is also thread safe and does not need synchronization. This make String efficient and safe.

28
Q

What is the difference between String, StringBuilder and StringBuffer?

A

String

  1. Stored in a String Pool
  2. It is Immutable
  3. It is thread safe (immutable)
  4. It has fast performance

StringBuffer

  1. It is stored in the Heap
  2. It is mutable
  3. It is thread safe (Synchronized)
  4. It has slow performance

StringBuilder

  1. It is stored in the Heap
  2. It is mutable
  3. It is not thread safe (Not synchronized)
  4. It has fast performance
29
Q

What is the difference between static and final variables?

A

Static

  1. You can have static variables, methods, blocks of code, nested class
  2. Static variables are class variables and belong to the class. They are initialized only once and will be initialized first before the initialization of any instance variables. A single copy can be shared by all instance of the class. It can also be accessed directly by the class name and doesn’t need any object
  3. Static class is just one which doesn’t implicitly have a reference to an instance of the outer class. The main method is static since it must be accessible for an application to run before any instantiation takes place.

Final

  1. Final keyword is used in several different context to define an entity which cannot later be changed
  2. A final class cannot be subclassed. This is for security and efficiency. All methods in a final class are implicitly final
  3. A final method can’t be overridden by subclasses. This is to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class
  4. A final variable can only be initialized once
30
Q

What are the implicit modifiers require for interface variables?

A

Public
Static
Final

31
Q

What are transient variables?

A

The transient keyword in Java is used to indicate that a field should not be serialized. Variables marked transient to indicate that they are not part of the persistent state of an object.

32
Q

What are access modifiers?

A

Access modifiers in Java specifies the scope (accessibility) of a data member, method,constructor or class.

Private

  1. Access is only within the class
  2. Private constructor means that you cannot create the instance of that class from outside the class

Default

  1. Default is when you don’t use any modifier
  2. It is accessible only within the package

Protected

  1. Access within the package and outside the package but only through inheritance
  2. It can’t be applied on the class

Public
1. Accessible everywhere

33
Q

What is a wrapper class?

A

Wrapper class (Integer, Long, Byte, Double, FLoat, Short) are subclasses of the abstract class Number. It converts primitive types into objects called boxing and converted back to primitive type called unboxing.

34
Q

What is Reflection API?

A

Java Reflection is a process of examining or modifying the run time behavior of a class at run time. The Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class. It is mainly used in IDE, Debugger, Test Tools.

35
Q

Is Java pass by value or pass by reference?

A

Java manipulate objects by references and all object variables are references, but it passes them by value.

36
Q

What is the difference between pass by value and pass by reference?

A

When passing an argument to a method, Java will create a copy of the values inside the original variables and pass that to the method as arguments and that is why it is called pass by value. The key with pass by value is that the method will not receive the actual variable that is being passed but just a copy of the value being stored inside the variable.

37
Q

What is encapsulation?

A

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only though the methods of their current class. This is also known as data hiding.

38
Q

How do you start a thread?

A

There are two ways to create a thread in Java.

The first way is to extend the Thread class, override the run() method with the code you want to execute, then create a new object from your class and call start()

The second method is to pass an implementation of the Runnable interface to the constructor of Thread, then call start().

39
Q

What is the difference between == and equals()?

A

== operator is used to compare 2 objects and check if the objects refer to the same place in memory.

equals() method is defined in the Object class. The method checks to see if both objects reference the same place in memory by default unless overridden. It should instead compares the values inside the object.

40
Q

What is polymorphism?

A

Polymorphism is the ability of an object to take on many forms. Any Java object that can pass more than one IS-A test is considered to be polymorphic.

41
Q

What is inheritance?

A

Inheritance can be defined as the process where one class acquires the properties (fields and methods) of another. The class which inherits the properties using the extends keyword of other is known as subclass

42
Q

What is the difference between method overriding and method overloading?

A

Overloading occurs when two or more methods in one class have the same method but different parameters

Overriding means having two methods with the same method name and parameters (method signature). One of the methods is in the parent class and the other is in the child class. overriding allows a child class to provide a specific implementation of a method that is already provided in the parent class.