Core Java

Core Java

Share

01/08/2023

important with solution.

Hello, guys! 👋 Welcome to my page! 🎉

I'm excited to have you here on this platform where we'll explore the fascinating world of coding and programming together! 💻🌐

I have writing here some logical program using java 8 feature Stream API.
-----------------------------------------------------------------------
Q.1 Write a program to sort number using stream.

List list= Arrays.asList(1,4,2,5,6,3,10);

List sortedlist=list.stream().sorted().collect(Collectors.toList());
System.out.println(sortedlist);//[1, 2, 3, 4, 5, 6, 10]
-----------------------------------------------------------------------
Q.2 write a program to print even number from list

List evenNumber=list.stream().filter(i->i%2==0).collect(Collectors.toList());
System.out.println(evenNumber); //[4, 2, 6,10]

-----------------------------------------------------------------------
Q.3 write a program to print even number from list and multiply with 5

List evenNumber=list.stream().filter(i->i%2==0).map(m->m*5).collect(Collectors.toList());
System.out.println(evenNumber); //[20, 10, 30, 50]
-----------------------------------------------------------------------
Q.3 Write a program to count number in stream

System.out.println(list.stream().count()); //7
-----------------------------------------------------------------------
Q.4 write a program to print number start with 1

List startwithone= list.stream().filter(f->f.toString().startsWith("1")).collect(Collectors.toList());
System.out.println(startwithone); //[1, 10]
-----------------------------------------------------------------------
Q.5 write a program to find duplicate element from list using stream api.

List list = Arrays.asList(1, 2, 3, 2, 4,2, 5, 6, 4, 7, 8, 8, 9);

Set set= new HashSet();
list.stream().filter(i>!set.add(i)).distinct().forEach(System.out::println);

-----------------------------------------------------------------------
Q.6 write a program to sort employee thier name

List employees = Arrays.asList(new Employee(1,"John", 4500),new Employee(2,"Alice", 6000),
new Employee(3,"Bob", 3500),new Employee(4,"Jane", 8000));
employees.sort(Comparator.comparing(Employee::getName));

for(Employee e1:employees) {
System.out.println("empName==>"+e1.getName());
}
-----------------------------------------------------------------------
Q.7 write a program to get employee where sal greater than 5000

List filteredEmployees = employees.stream().filter(e -> e.getSalary() > 5000).collect(Collectors.toList());

for (Employee e1: filteredEmployees) {
System.out.println("Name: " + e1.getName() + ", Salary: " + e1.getSalary());
}
-----------------------------------------------------------------------
Q.8 write a program to count the occurrences of each character and print the character along with its frequency.

String inputstr="programming";

Map map = inputstr.chars().mapToObj(c->(char)c)
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
System.out.println(map);
}

----------------------------------------------------------------------
Q,9 write a program to print asc order in java
List list=Arrays.asList("pritam","rakesh","rajput","deepak");

Collections.sort(list,Comparator.naturalOrder());
System.out.println(list);
-----------------------------------------------------------------------
Q,10 . write a program to print desc order

List list= Arrays.asList(1,2,3,4,5,71,7,0);

Collections.sort(list,Comparator.reverseOrder());
System.out.println(list);
----------------------------------------------------------------------

16/06/2023

important Question
with answer.

Hello All, below question are asked to me in my interview as 3+ experienced in

===========================================
Q.1 Is it possible to change the port of the embedded tomcat sever in spring boot

ans. yes it is possible by using server.port in the application.properties file
server.port=9090

===========================================
Q.2 can we override or replace the Embedded tomcat server in spring boot.

ans. yes we can replace the embedded tomcat server with any server by using the Starter dependency in the pom.xml file.

1. Exclude the default embedded Tomcat dependency from your pom.xml file if you are using Maven.



org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-tomcat




2.Include the dependency for the desired embedded server. For example, if you want to use Jetty,
include the following dependencies:



org.springframework.boot
spring-boot-starter-jetty


===========================================
3.what is auto-configuration in spring boot ? how does it help ? why spring boot is called opinionated.
ans. auto configuration is spring boot magic features. it automatically configures a lot of things based upon what is
present in the classpath.
for example spring boot starter web,

===========================================
4. what is the difference between and annotation.
ans. :

It is a convenience annotation that combines three commonly used annotations: , , and .

It is typically used to annotate the main class of a Spring Boot application.
The annotation indicates that the class declares one or more Spring beans.
The annotation triggers the auto-configuration process in Spring Boot.
The annotation scans the specified packages for Spring components, such as controllers, services, and repositories.

:
==================

It is a standalone annotation used to enable auto-configuration in a Spring application.
It triggers the automatic configuration of Spring beans based on the classpath and environment.
When is present, Spring Boot uses its algorithm to scan the classpath and configure beans based on the libraries and frameworks.

===========================================
5. how to disable a specific auto configuration class.
ans. we can use exclude attribute of annotation to exclude specific class.

(exclude={className})

===========================================
6. what is the different between and in spring boot.
ans. Map of the model object to view or template and make it human readable.
simply return the object and object data is directly written in HTTP response as JSON or XML.
= +

===========================================
7. what is different between an
ans. can be used with GET ,POST, PUT and many other request methods using the method attribution the annotation.

where as is only an extensions of with GET method which helps you to improve on clarity on request.

(value="/user/{userid}", method=RequestMethod.GET)
(value="/user/{userid}")

===========================================
8.what is different b/w an embedded container and a war ?

The main different between an embedded container and a war file that you can spring boot application as a jar from the command
prompt without setting up a web server

but to run a war file , you need to first set up a web server like tomcat which has servlet container and then you need to
deployee war file.
===========================================

9. what is the use of in Spring Boot.
ans. Spring has the provision of Profiles to keep the daparate configuation of environments.
Profiles provide a way to customize the behavior of an application based on different environments, such as development, testing, production, or specific deployment scenarios.

1. The annotation can be applied to classes, methods, or individual beans to associate them with specific profiles.
2. Profiles can be defined using the spring.profiles.active property in the application.properties or application.yml file,
or by setting the SPRING_PROFILES_ACTIVE environment variable.
3.("profileName") - ("Developement"),("production") ,("Testing")

===========================================
10. what is spring acutator what are its advantages.
ans. Provides special feature to monitor and manage your application when you push it to production.

for example - check health beans, information, performation ,shutdown application.



org.springframework.boot
spring-boot-starter-actuator


management.endpoints.web.exposure.include=*

======================================
11. what is use of annotation in spring framework.
Qualifier =The annotation is used along with to resolve ambiguity when multiple beans of
the same type are present. It allows you to specify a specific bean to be autowired when multiple beans of
the same type are available.


public PizzaController(("vegPizza") Pizza pizza) {
super();
this.pizza = pizza;
}

===========================================

"

Want your business to be the top-listed Computer & Electronics Service in Pune?
Click here to claim your Sponsored Listing.

Website

Address


Pune