Interview Wiki Java v1/2 Flashcards

1
Q

(Beginner) What is the MVC pattern?

A

The Model-View-Controller pattern.

Application logic is separated into three layers:

–a model, which knows about the data
–a view, which knows how to display things
–a controller, which implements application logic and knows how to map the raw data into the elements to be displayed.

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

(Beginner) Are you familiar with any other design patterns other than MVC?

A

Common answers: Singleton, Factory, Observer, Facade

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

(Beginner) What is the command line utility used to compile java source code into byte code?

A

javac

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

(Beginner) How do you handle exceptions in Java?

A

try, catch, finally

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

(Beginner) What is the difference between Final, finally and finalize()?

A

Final - declares a constant who’s value can’t be changed, or a method which can’t be overridden

finally
- is always called before leaving a try catch block.
- Used usually to close connections.
- Typically used for freeing resources.

finalize() - Called during garbage collection

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

(Beginner) What is the difference between == operator and equals () method?

A

== compares objects (memory locations — are they the same instance)

.equals compares the object content based on how the class defines equality for instances of the class.

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

(Intermediate) What is AJAX?

A

A) AJAX = Asynchronous Javascript and XML

A) AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes… allows parts of a web page to be updated, without reloading the whole page.

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

(Intermediate) What is the difference between overriding and overloading methods?

A

A) Overwriting (Overriding) means having two methods with the same arguments, but different implementations. For example, a parent class has a method doWork() that does work for the what is present in the parent class. A child class extends the parent class, and needs to do work differently. The child class would override the doWork() method from the parent class.

A) Overloading means that you have multiple methods with the same name but different method signatures. For example, in the String class, there are 4 different indexOf methods: indexOf(int ch), indexOf(int ch, int fromIndex), indexOf(String str), indexOf(String str, int fromIndex)

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

(Intermediate) What does protected mean?

A

protected - changes the scope of the class or method to only be accessible to other classes or sub classes in the same package

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

(Advanced) What is the difference between SOAP and REST Web Services? (http://keydifferences.com/difference-between-soap-and-rest-web-services.html)

A
  • SOAP is a protocol that is based on the HTTP protocol.
  • REST is an architecture that uses the different HTTP methods to act on a resource.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

(Advanced) Which HTTP methods are supported by RESTful web services ? (http://www.restapitutorial.com/lessons/httpmethods.html)

A

GET, POST, PUT, PATCH and DELETE

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

(Advanced) What do http methods do? What are they used for? (http://www.restapitutorial.com/lessons/httpmethods.html)

A

GET: The HTTP GET method is used to read (or retrieve) a representation of a resource. In the “happy” (or non-error) path, GET returns a representation in XML or JSON
POST: The POST verb is most-often utilized to create new resources. In particular, it’s used to create subordinate resources. That is, subordinate to some other (e.g. parent) resource. In other words, when creating a new resource, POST to the parent and the service takes care of associating the new resource with the parent, assigning an ID (new resource URI), etc.
PUT: PUT is most-often utilized for update capabilities, PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource
PATCH: PATCH is used for modify capabilities. The PATCH request only needs to contain the changes to the resource, not the complete resource.
DELETE: DELETE is pretty easy to understand. It is used to delete a resource identified by a URI.

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

(Advanced) What are the differences between a LinkedList and an ArrayList? When might you prefer a LinkedList over an ArrayList?

A

There are many differences.

—ArrayList provides efficient random access, but has expensive inserts/deletes in the middle of the list and semi-expensive size growth.

—LinkedList provides cheap insertions at any point in the list, but only supports sequential iteration starting from the ends, and also uses more memory.

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

(Advanced) What are autoboxing and unboxing?

A

A) Boxing is the process of converting a primitive type (such as an int) to an object type (such as Integer), and auto-unboxing is the reverse process. Prior to Java 5, this had to be done with manual casts, but Java 5 and higher will automatically perform the conversions where necessary. One common scenario where this is useful is when working with generic collections - only object types can be used with generics, so without autoboxing, storing e.g., primitive ints in an ArrayList would require a significant amount of manual casting between int and Integer. (More details)

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

(Advanced) What will unboxing do with null values?

A

A NullPointerException will be thrown because you can’t have null invoke a method.

Ex: To unbox an Integer object to int, Java calls the “Integer Object”.intValue() method.

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

(Advanced) What is Cross Site Scripting (XSS) and how would you protect against it?

A

It is a Security Vulnerability that allows the user to inject javascript into a webpage and can get the page to execute that script.

Hackers will use this to steal a users session cookies and compromise their account.

To protect against it you can use:
1. URL Encoding and Use Reg Expressions to Filter out non Alpha Numeric Characters.
2. black list or white list to validate inputs.

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

(Advanced) If you could change one thing about Java what would it be?

A

Come up with answer - no answer was provided

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

(Java EE Questions) What is a deployment descriptor?

A

Typically it is a file that holds configuration information about how an application should be deployed in a JavaEE environment. For instance, in a web application. the web.xml file defines what servlets receive which URL mappings, what the index files are, what the error conditions are, and what roles are required for certain HTTP methods.

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

(Java EE Questions) What is an EJB?

A

An EJB is an Enterprise Java Bean. It is a Java class that is defined to interact directly with the JavaEE environment.

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

(Java EE Questions) What benefits do we get from using EJBs?

A

We can put JavaEE extensions in an EJB for things like transactions, validations, restart/retry logic.

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

(Java EE Questions) What is a Web container?

A

A web container is a Java application that serves web applications that are packaged in the form of WARs (Web Application Resource).

Technically, these are different from web application servers

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

(Java EE Questions) What is a Web Application Server?

A

Implementation of a JavaEE server

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

(Java EE Questions) When to use web containers vs. web application servers?

A

—Web container used if you do not need a full stack JavaEE server.
—Most web applications probably only ever need a web container.
—If you need to do stuff with EJB’s, you will either need to add frameworks to the web container or just run a web application server.

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

(java v2 Core) Discuss some of the features of Java. Comparedd to other languages.

4 things

A

generics, autoboxing, annotations, varArgs

Generics - provides compile-time (static) type safety for collections and eliminates the need for most typecasts (type conversion).
AutoBoxing - Automatic conversions between primitive types (such as int) and primitive wrapper classes (such as Integer
Annotations - allows language constructs such as classes and methods to be tagged with additional data, which can then be processed by metadata-aware utilities.
Varargs - The last parameter of a method can now be declared using a type name followed by three dots (e.g. void drawtext(String… lines)). In the calling code any number of parameters of that type can be used and they are then placed in an array to be passed to the method, or alternatively the calling code can pass an array of that type.

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

(java v2 Core) What is an interface? Why would you use one?

A

1) An Interface defines what has to be implemented;
2) Insulation from implementation, development can occur separately

_ _ ___

  • Provides a standard implementation of some behaviors. You subclass and provide implementations for other methods.
  • Commonly used in frameworks to provide structure and consistent implementation.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

(java v2 Core) What is an abstract class? How is it different from an interface? Why would you use one?

A

Abstract class
—cannot be instantiated but allows implementation of methods

Implementation
—Provides a standard implementation of some behaviors. You subclass and provide implementations for other methods.
—Commonly used in frameworks to provide structure and consistent implementation.

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

(java v2 Core) Can you explain static variables vs. instance variables?

A

—Static variables are “shared” among instances. They exist within the class.

—The instance variables as named are specific to each instance and are not shared across instances.

// static var ex
public class Car {
private String name;
private String engine;

public static int numberOfCars;

public Car(String name, String engine) {
    this.name = name;
    this.engine = engine;
    numberOfCars++;
}

// getters and setters } @Test public void whenNumberOfCarObjectsInitialized_thenStaticCounterIncreases() {
new Car("Jaguar", "V8");
new Car("Bugatti", "W16");
 
assertEquals(2, Car.numberOfCars); }

–instance var example
public class Studentsrecords
{
/* declaration of instance variables. */
public String name; //public instance
String division; //default instance
private int age; //private instance
#######

28
Q

(java v2 Core) What does the term ‘final’ mean in Java, as applied to a class, a method, or a variable?

A

A) A class declared as final can not be subclassed. All methods in a final class are implicitly final. Declaring a class as final has no affect on the references to the objects of that class when it is used as a variable.
B) A ‘final’ method cannot be overridden
C) Declaring a variable as final means it cannot be modified.

29
Q

(java v2 Core) Does the final keyword have different effects between a primitive and an object?

A

Yes

For primitive) variables the value cannot be changed (ie int x=10; assigns x a value of 10 forever.

For an object variable), it simply means you cannot change the variable to point to another object, but you can still modify the values inside the object!

30
Q

(java v2 Core) Do you have experience with Java Collections classes?

A

HashMap, Hashtable, ArrayList, Map, List
If yes,

31
Q

(java v2 Core) What is the main difference in an ArrayList and a List?

A

a List is an interface….

An ArrayList is an implementation of the List interface.

32
Q

(java v2 Core) Is Java pass-by-reference or pass-by-value? What is the difference?

A

Java is pass-by-value but it passes the reference which makes it behave like pass-by-reference.

33
Q

(java v2 Core) What is encapsulation?

A

Hides the implementation behind code (an interface).

Two features: instance variables are protected, getter and setter methods provide access to instance variables.

Encapsulation is done to hide “sensitive” data from users. To achieve this, you must:
declare class variables/attributes as private
provide public get and set methods to access and update the value of a private variable.
public class Person {
private String name; // private = restricted access

// Getter
public String getName() {
return name;
}

// Setter
public void setName(String newName) {
this.name = newName;
}
}
public class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.setName(“John”); // Set the value of the name variable to “John”
System.out.println(myObj.getName());
}
}

// Outputs “John”

34
Q

(java v2 Core) Can you call one constructor from another if a class has multiple constructors? How?

A

Yes, using “this” keyword.

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes:

// Create a Main class
public class Main {
int x; // Create a class attribute

// Create a class constructor for the Main class
public Main(int y) { // you don’t need parameters, but you can write them if you want them.
x = 5; // Set the initial value for the class attribute x
z = y;
}

public static void main(String[] args) {
Main myObj = new Main(); // Create an object of class Main (This will call the constructor)
System.out.println(myObj.x); // Print the value of x
}
}

// Outputs 5

35
Q

(JEE Questions) Have you worked with web services? SOAP or REST?
[If yes for SOAP or REST] What are they, explain SOAP or REST?

A

A)
* Web services is a self contained, self modular application that is designed to support inter operable machine-to-machine interaction over a network.
* The interface is described by a WSDL and the communication is done with SOAP messages.
* SOAP is a protocol specification for exchanging structured information in the implementation of Web Services.
* JAX-RPC/JAX-WS
* RESTful Service (Representational State Transfer) – uses HTTP methods (put/get/delete/post) along with the URI, instead of SOAP/XML payload to define client requests.
* For example: http://example.com/resources/item17
o Get: Retrieve a representation of item 17.
o Delete: Delete item 17
o Put: Replace item 17 with the passed in representation or create it (if it doesn’t exist).

https://spring.io/guides/tutorials/rest

36
Q

(JEE Questions) Compare and contrast SOAP and REST

A

A) SOAP has a Web Services Description Language (WSDL)
B) SOAP is a very structured, formalized process for inter service communication. All XML.
—Where REST is guidelines and does not have to be XML.

37
Q

(JEE Questions) In a REST URI or URL, what is the difference between a query parameter and a path parameter?

A
  • Query parameters are the name values pairs passed with the http request. (Everything after the question mark)
  • Path parameter is part of the URI pattern and have the {} in path

Query Param:
@GetMapping(“/api/foos”)
@ResponseBody
public String getFoos(@RequestParam String id) { // id is the query parameter
return “ID: “ + id;
}
http://localhost:8080/spring-mvc-basics/api/foos?id=abc
—-
path param:
@GetMapping(“/api/employees/{id}”)
@ResponseBody
public String getEmployeesById(@PathVariable String id) {
return “ID: “ + id;
}
id is the path parameter, 111 in the example below.
http://localhost:8080/api/employees/111

38
Q

(JEE Questions) What are some of the ways that you would secure a rest endpoint?

A

A) HTTP basic auth
B) SPENGO, certificate authentication
C) HTTP as your protocol, Encryption

39
Q

(JEE Questions) Do you have experience with JEE web applications?
[If yes] What is a servlet?

A
  • Servlet is a special Java class intended to handle web requests.
  • Receives an Http Request and returns and Http Response
  • It implements methods such as ‘doPost’ and ‘doGet’
  • Instances of servlets are created and managed by the application server
40
Q

(Database/Persistence) What databases have you used?

A

personal answer: mssql, db2, snowflake, netezza, oracle

41
Q

(Database/Persistence) Why use stored procedures?

A

—Solid answer: “stored procedures provides several advantages including
- better performance,
- higher productivity,
- ease of use,
- increased scalability.”

—Good answer: https://docs.oracle.com/cd/F49540_01/DOC/java.815/a64686/01_intr3.htm

42
Q

(Database/Persistence) Explain the pros and cons of using indexes.

A

Pros: Speed up query access
Cons: Slows down inserts and possible updates

43
Q

(Database/Persistence) Inner join vs outer join

A

Inner join returns records that match in both tables
Outer join returns all the rows from one table and the matching ones from a second table.

44
Q

(Database/Persistence) What are some good practices for error handling?

A

—Checked vs. Unchecked Exceptions
—logging errors
—passing context across tiers
—translating system errors into friendly UI messages
—ensuring exceptions never escape to the client
—etc

Exception – checked
In general, checked exceptions represent errors outside the control of the program. For example, the constructor of FileInputStream throws FileNotFoundException if the input file does not exist. Java verifies checked exceptions at compile-time. Therefore, we should use the throws keyword to declare a checked exception:

private static void checkedExceptionWithThrows() throws FileNotFoundException {
File file = new File(“not_existing_file.txt”);
FileInputStream stream = new FileInputStream(file);
}

Exception - unchecked
If a program throws an unchecked exception, it reflects some error inside the program logic. For example, if we divide a number by 0, Java will throw ArithmeticException:

private static void divideByZero() {
int numerator = 1;
int denominator = 0;
int result = numerator / denominator;
}

45
Q

(Design) What design patterns are you familiar with?
[Ask this question so onsite can go more into this]

A

Façade, Command, Singleton, Factory, Adapter, Bridge, Proxy, Template

The most popular approach (for Singletons) is to implement a Singleton by creating a regular class and making sure it has:

a private constructor
a static field containing its only instance
a static factory method for obtaining the instance

We’ll also add an info property for later use only. So our implementation will look like this:

public final class ClassSingleton {

private static ClassSingleton INSTANCE;
private String info = "Initial info class";

private ClassSingleton() {        
}

public static ClassSingleton getInstance() {
    if(INSTANCE == null) {
        INSTANCE = new ClassSingleton();
    }
    
    return INSTANCE;
}

// getters and setters }
46
Q

(Design) What are some things you would consider if you wanted to make your code reusable?

A

Possibilities:

1) focus on encapsulation,
2) package in a jar,
3) javadoc,
4) test harness,
5) make clear distinction between public methods (contract with the consumer) and private (implementation-specific). documentation,

6) extra cost to design/develop/test
7) expose as a web service, ensure dependencies are well-known and isolated from the consumer,

47
Q

(Design) What techniques do you use to grow your skills and technical knowledge? What technologies are you excited about? Where should companies be focusing?

A

No answer provided

48
Q

(Behavioral) When you have encountered a problem that you knew would impact your project, how did you handle the situation? When would you decide to escalate an issue? What could you have done differently?

A

No answer provided

49
Q

(Behavioral) What is a transient variable?

A

A variable that can be serialized but will not be.

Serialization is the conversion of the state of an object into a byte stream; deserialization does the opposite. After serialization, we can then save to a database or transfer over a network.

transient is a variables modifier used in serialization. At the time of serialization, if we don’t want to save value of a particular variable in a file, then we use transient keyword. When JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type.

transient keyword plays an important role to meet security constraints. There are various real-life examples where we don’t want to save private data in file. Another use of transient keyword is not to serialize the variable whose value can be calculated/derived using other serialized objects or system such as age of a person, current date, etc.
Practically we serialized only those fields which represent a state of instance, after all serialization is all about to save state of an object to a file. It is good habit to use transient keyword with private confidential fields of a class during serialization.

// A sample class that uses transient keyword to
// skip their serialization.
class Test implements Serializable
{
// Making password transient for security
private transient String password;

// Making age transient as age is auto- 
// computable from DOB and current date. 
transient int age; 
  
// serialize other fields 
private String username, email; 
Date dob; 
  
// other code  }
50
Q

(Behavioral) Why make a class final?

(Behavioral) Why make a method final?

A

Class: Dependency on the implementations…
…provided in the class.

method: You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object. (ie the method blue should always return the constant BLUE.)

51
Q

(Behavioral) What principal of OO design does this break?

A

Inheritance

52
Q

(Behavioral) When would you override the hashcode() method?

A

When an object’s uniqueness is defined differently

than the standard hashcode() method;

53
Q

(Behavioral) What are the access control modifiers in JAVA (e.g. public, etc.)?

A

public - objects outside of the class have access
private - only the owning class has access
default(package) - only classes in the package have access
protected - only the owning class and subclasses have access

54
Q

(Agile Engineering) What is TDD? Why would you want to use it?

A

no answer

55
Q

(Agile Engineering) What is Continuous Integration?

A

DevOps best practice, allowing developers to frequently merge code changes into a central repository where builds and tests then run.

Automated tools are used to assert the new code’s correctness before integration

56
Q

(Agile Engineering) Explain why you should focus on simple design vs future proofing/goldplating

A

Future-proofing software can be defined as the practice of designing, developing, and implementing software solutions in a way that allows them to adapt and remain effective in the face of future technological advancements, changing user needs, and evolving industry trends.

Gold plating in project management refers to the unnecessary addition of extra features, functionality or any type of enhancements to a project that are beyond its agreed-upon requirements

57
Q

(Agile Engineering) Which command would you use to list the previous commands you entered or get more information about those executed commands?

A

Use the “history” command

58
Q

(Agile Engineering) How would you parse out from the “history” command only those commands which included the text “scp”?

A

history | grep “scp”

59
Q

(Agile Engineering) How would you redirect the standard output to a file?

A

Use the “>” character. Example, history | grep “scp” > /tmp/history_withscp.txt

60
Q

(Agile Engineering) Will the “>” character append the standard output to the end of the file?

A

No, it overwrites the content of the file and creates the file if necessary

61
Q

(Agile Engineering) What is used to append the standard output to a file?

A

“»” is used

62
Q

(Agile Engineering) Which command is used to change the permissions of the file to read only for all?

A

chmod 444 /tmp/history_withscp.txt

63
Q

What is the difference between web app server and web container?

A

application server implements a full JavaEE stack and the container does not.

A web container is missing EJB’s, JMS, built in security, etc.

64
Q

When would you override the equals() method?

A

You can override the equals method in our class to check whether two objects have same data or not.

65
Q
A