Project Round Java QuestionAnswer
Project Round Java QuestionAnswer
dependency from local repository if available else download from central repository
in
pom.xml.
Add spring dependency along with JAX-WS implementation class dependency in pom.xml
Q.#What are all the critical situations you come across in your project?
Q.#Why wait () placed in object class? Why not it is placed in Thread class?
Answer:
Very simple reason behind it actually wait () method is used to wait the thread of
execution and it used for inter thread communication to avoid data inconsistency,
so first
reason is wait () and notify () method are not specific for single object it can be
apply, second
reason is if it will present in thread class then one thread have to know the
status of another
thread
Example: Suppose I have 4 thread th1 ,th2 , th3, th4 and th4 so in inter thread
communication
lock will be applied on Object by thread so which thread apply lock that only known
to object
.simply locking mechanism no way related to thread its related to Object.
@ExceptionHandler so in this method we have to write the logic for map the
exception. And
return the same view which is return by controller class at the time of exception
raise. And wehave to return some user understandable message by view page.
Note: Both return logical view should be same
l1Value.add(40.000);
List<Double> l2Value = new ArrayList<>();
l2Value.add(90.000);
l2Value.add(60.000);
l2Value.add(30.000);
dataMap.put(l1Key, l1Value);
dataMap.put(l2Key, l2Value);
return dataMap;
}
Q.#How can we take Map into List?
Example:
public List<Map<String, Integer>> setData() {
List<Map<String, Integer>> listMap = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();
map.put("A", 34);
map.put("D", 90);
map.put("B", 12);
map.put("C", 91);
listMap.add(map);
return listMap;
}
Q.I have a table in remote database, how to update the data in that table using
rest?
Simple write the update logic in DAO and call this from controller class but in
controller class u have to
mention the annotation @PUT for update through network. Code try as per above 2
example only logic
will be changed but not approach.
Q.How to set timeout for the browser? (Clue: restful client API)?
Client client = ClientBuilder.newClient();
client.property(ClientProperties.CONNECT_TIMEOUT, 1000);
client.property(ClientProperties.READ_TIMEOUT, 1000);
Q. In written test they are asking sorting programs (bubble sort, quicksort)
Bubble-Sort:
package com.core.all.interview.programmes;
public class SortArrayByBubbleSort {
public static void sort(int input[]) {
int temp = 0;
for (int i = 0; i <= input.length - 1; i++) {
for (int j = i + 1; j <= input.length - 1; j++) {
if (input[i] > input[j]) {
temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
System.out.println(input[i]);
}
}
public static void main(String[] args) {
int[] i = new int[] { 12, 44, 23, 43, 21, 8, 0, 6, 45, 44, 58, 17
};
sort(i);
}
}
Quicksort:
package com.core.all.interview.programmes;
import java.util.Arrays;
public class SortArrayByQuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (arr == null || arr.length == 0)
return;
if (low >= high)
return;
int middle = low + (high - low) / 2;
int pivot = arr[middle];
// make left < pivot and right > pivot
int i = low, j = high;
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
// recursively sort two sub parts
if (low < j)
quickSort(arr, low, j);
if (high > i)
quickSort(arr, i, high);
}
public static void main(String[] args) {
int[] i = new int[] { 12, 44, 23, 43, 21, 8, 0, 6, 45, 44, 58, 17
};
quickSort(i, 0, i.length - 1);
System.out.println(Arrays.toString(i));
}
}
Folder Structure
The main method is the heart of the Spring boot application to run the application.
@SpringBootApplication:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
package com.gkatzioura.security.securityendpoints.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigu
rerAdapter;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/welcome").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.permitAll()
.and()
.logout()
.permitAll();
}
}
package com.gkatzioura.security.securityendpoints.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigu
rerAdapter;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/welcome").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.permitAll()
.and()
.logout()
.permitAll();
}
}