Java Interview Questions

Hi There! So I have drafted some Java, and RESTAPI questions I was asked on an interview and their answers. Sources are also included.

  1. How do you read a file in java?

Step1:  Use java io.file library, Files and Path classes.

Step 2: make sure your method throws IO Exception.

Step 3: Make a byte array for the file to be read into, this allows a ready state for the file to be converted into various different types of java objects ig. String

Import java.nio.file.Files;
Import java.nio.file.Path;

Path path = Paths.get(directory, fileName);

Try {
Byte[] data = Files.readAllBytes(path);
 System.out.println(new string(data));
} catch (IOException e) {
}

writing-files-in-java/

https://stackabuse.com/reading-and-

2.What type of a java object does the read method return?

The read method reads the next byte of the data from the input stream and returns int in the range of 0 to 255.

https://www.tutorialspoint.com/java/io/inputstream_read.htm

3.How do you replace all the occurrences of string “hello”?

 The Java String replaceAll() returns a string after it replaces each substring of that matches the given regular expression with the given replacement.

public String replaceAll(String regex, String replacement) {
    return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

https://howtodoinjava.com/java/string/java-string-replaceall-example

4.What is the difference between hashmap and array list?

Hashmap is the implementation of the hash table 

ArrayList implements List Interface.

HashMap does not preserve the insertion order 

Arraylist does preserve the order

ArrayList only stores one object 

HashMap stores two objects key and value. 

ArrayList allows duplicate elements 

HashMap only allow duplicate values.

http://bit.ly/31pBImT

5.How do you return a key?

. getkey() method

6.how do you return all the keys in hashmap?

for ( String key : team1.keySet() ) { System.out.println( key );   }

http://bit.ly/2oSflJn

7.How do you set up a RestAPI?

  • Create your Spring Boot project (one of three ways)
  • Create your Java Database Connectivity object
  • Label class with @Entity annotation for Framework to read
  • Create your Repository class and Service class for it to read from
  • Create your REST styled Controller class
  • Perform Unit Testing
  • Build and Run your Spring Boot app
  • Package as standalone .JAR (Java Archive) file

8.What is RestAPI?

A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data.

A RESTful API — also referred to as a RESTful web service — is based on representational state transfer (REST) technology, an architectural style and approach to communications often used in web services development.


https://searchapparchitecture.techtarget.com/definition/RESTful-API

9.Tell me about the conditions of restful webservice?

The six constraints are 

Client–server – By separating the user interface concerns from the data storage concerns, we improve the portability of the user interface across multiple platforms and improve scalability by simplifying the server components.

Stateless – Each request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server. Session state is therefore kept entirely on the client.

Cacheable – Cache constraints require that the data within a response to a request be implicitly or explicitly labeled as cacheable or non-cacheable. If a response is cacheable, then a client cache is given the right to reuse that response data for later, equivalent requests.

Uniform interface – By applying the software engineering principle of generality to the component interface, the overall system architecture is simplified and the visibility of interactions is improved. In order to obtain a uniform interface, multiple architectural

constraints are needed to guide the behavior of components. REST is defined by four interface constraints: identification of resources; manipulation of resources through representations; self-descriptive messages; and, hypermedia as the engine of application state.

Layered system – The layered system style allows an architecture to be composed of hierarchical layers by constraining component behavior such that each component cannot “see” beyond the immediate layer with which they are interacting.

Code on demand (optional) – REST allows client functionality to be extended by downloading and executing code in the form of applets or scripts. This simplifies clients by reducing the number of features required to be pre-implemented.
https://restfulapi.net/

10.What is common in the uniformity constraint?

The HTTP protocol and the http verbs

11.How do you conduct content negotiation in REST API?

Content negotiation allows a user to determine which media types they prefer to receive from the server

One way to pass content type information to server, client may use specific extension in resource URIs. For example, a client can ask for details using:

http://rest.api.com/v1/employees/20423.xml

http://rest.api.com/v1/employees/20423.json

Another way, at server side, an incoming request may have an entity attached to it. To determine it’s type, server uses the HTTP request header Content-Type. Some common examples of content types are “text/plain”, “application/xml”, “text/html”, “application/json”, “image/gif”, and “image/jpeg”.

https://restfulapi.net/content-negotiation/

Leave a comment

Design a site like this with WordPress.com
Get started