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 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
7
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
8
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
9
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
10
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
11
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
12
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you establish a connection for JDBC?

A

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

test is a database schema

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

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

17
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
18
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
19
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
20
Q

What are the implicit modifiers require for interface variables?

A

Public
Static
Final

21
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.

22
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

23
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.

24
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.

25
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.

26
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.

27
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.

28
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().

29
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.

30
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.

31
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

32
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.