Posts

Quiz

It is possible to employ multiple computer cores to filter a list. Which statement will filter with increased performance? books.parallelStream().filter(book - > { return book.getTitle().startsWith("E"); }).forEach(System.out::println); Correct books.pStream().filter(book - > { return book.getTitle().startsWith("E"); }).forEach(System.out::println); books.streamParallel().filter(book - > { return book.getTitle().startsWith("E"); }).forEach(System.out::println); books.streamp().filter(book - > { return book.getTitle().startsWith("E"); }).forEach(System.out::println); ****************************************************************************************************** Which statement is equivalent to the following code? GreetingMessage gm = new GreetingMessage() { @Override public void greet(String name) { System.out.println("Hello " + name); ...

Java Quize Questions

Q.1 A Queue implements First-In-First-Out(FIFO) data structrure.Which method removes the first element of a list? 1. queue.remove 2. queue.get 3. queue.poll 4. queue.dequeue Ans :- queue.poll Q.2 Collections in Java are an extension of what class? 1.Util 2.Iterable 3.Correct 4.List 5.Interface Ans:- Iterable Q.3 What is a characteristic of an ArrayList? 1.It is faster at removing elements from the middle of a list. 2.It takes up less memory than a linked list. 3.It is harder to implement than a linked list. 4.It is slower at retrieving elements at a particular location. Ans:- It takes up less memory than a linked list. Q.4 What prints to the console after this code executes? LinkedList myList = new LinkedList(); myList.add("A"); myList.add("C"); myList.add(2, "B"); myList.remove("B"); System.out.println(myList); 1. [A, C] 2.[C, A] 3.[A] 4.[C] Ans :- 1. [A, C] Q.5 You create a HashMap data structure using the following stateme...

How to create React project using command prompt?

Image
  How to create React project using command prompt? Ans :- firstly you have to install node js and npm in your system after after that follow these steps:- Step 1:- Open command prompt and for create project write  npx create-next-app Step 2:- Now it will say to write your Project name. Step 3 :- Now write your project name as per your choice. Step 4 :- Now it will ask what is your needs to create a project   Step 5 :- Now you can see dependencies are Installed as shown in below image Step 6 :- Now use this command for change the directory Step 7 :- Now you can see below directory is changed Step 8 :- Now for open the project in Vs code use this command  code . Step 9 :- Now you can see your project is directly opened in Visual Studio Code

Q.we have two tables one is Employee table and another is city table in Employee table we have empId,empName and cityId and in City table we have cityId,cityName and stateName JOIN two tables WHERE empId =100 please write a Query for this.

 Solution :-  SELECT e.empId,           e.empName,           e.cityId,           c.cityName,           c.stateName    FROM Employee e    JOIN City c ON e.cityId = c.cityId    WHERE e.empId =100;

how to create schema and table in mysql workbench ?

Write a simple program to swap the numbers.

 Solution :-  package mv; public class SwapNumber { public static void main(String[] args) { int a=20; int b=30; System.out.println("Before swapping:a:"+a+",b:"+b); int temp=a; a=b; b=temp; System.out.println("After swapping: a:"+a+",b:"+b); } }

What is Intermediate and Terminal Operations ?

 Q. What is Intermediate and Terminal Operations ? Ans:- stream<T> will perform the following  operations:- 1.Intermediate operations 2.Terminal operations 1.Intermediate operations:-  the operations which are performed in b/w streams of objects are known as Intermediate operations. a. filter() b.map() c.sorted() d.distinct() e.peek()  2.Terminal operations:-   the operations which are     performed at the end of stream are known as Terminal operations => The following are some important Terminal operations. a. forEach() b.collect() c.reduce() d.count() e.anyMatch(),allMatch(),noneMatch() f.findAny(),findFirst() ****************************************************************

What is stream API, why is it needed?

 What is stream API, why is it needed? Ans:-   stream is an interface from java.util.stream package which supports stream of objects,which means continuous flow of objects. Stream<T> will perform the following operations:- a. Intermediate operations b.Terminal operations => The stream API is needed for several reasons:- 1.Functional programming Paradigm:- The Stream API supports functional programming paradigm, allowing developers to express operations on data in a concise and expressive manner. 2.Parallelism:- The stream API supports parallel processing, making it easier to take advantage of multi-core processors .Parallel stream can be created to process elements concurrently. 3.Code Readability:- Using the Stream API often leads to more readable and maintainable code.  4.Efficient Operations:- Stream operations, such as map, filter, and reduce, are designed to be efficient.

Can I write Lambda expressions for any Interface ?

                        Accenture Interview Question for 3+ Q. Can I write Lambda Expression for any Interface? Ans :- Yes, you can use  lambda expressions in java for any interface that has only one abstract method. Such interfaces are known as functional interfaces, and lambda expressions provide a concise way to implement the abstract method of a function Interface And we use “@FunctionalInterface” annotation for functional Interface.

Write a program to return list of all even numbers that are greater than 5 in list.

Q. Write a program to return list of all even numbers that are greater than 5 in list. SOLUTION  :-  package mv; import java.util.Arrays; import java.util.stream.Collectors; import java.util.*; public class GreaterFive { public static void main(String[] args) { List<Integer> list=Arrays.asList(1,2,3,4,5,6,7,8,9,10); List<Integer> greterThanFive=list.stream().filter(i->i%2==0 && i >5).collect(Collectors.toList()); System.out.println(greterThanFive); } } *******************************************************************

How to find sum of Integer using For Loop?

 Q. How to find sum of Integer using For Loop? SOLUTION:- package v1; import java.util.*; import java.util.Arrays; public class SumOfIntegerUsingForLoop { public static void main(String[] args) { List<Integer> list=Arrays.asList(10,20,30,40); Integer sum=0; for(Integer a:list) { sum=sum+a; }         System.out.println("Sum Of Integer:"+sum); } } ************************************************************************

How to get Count of Users Starts with “A” where user have two fields Id and name

 Q. How to get Count of Users Starts with “A” where user have two fields Id and name SOLUTION:-  User.java package v1; public class User { int id; String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User(int id, String name) { super(); this.id = id; this.name = name; } } *********************************************************** Test.java package v1; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { List<User> list=new ArrayList<>(); list.add(new User(1, "Bharat")); list.add(new User(2, "Aditya")); list.add(new User(3, "Mohan")); list.add(new User(4, "Apurva")); list.add(new User(5, "Aditi")); list.add(new User(6, "Neha")); Long count...

How to find Sum of Odd and even number using stream.

Q. How to find Sum of Odd and even number using stream. Ans :- SOLUTION:-  package v1; import java.util.Arrays; import java.util.List; public class SomOfOddAndEven { public static void main(String[] args) { Integer[] number= {1,2,3,4,5,6,7,8,9,10}; List<Integer> list=Arrays.asList(number); int sumOfEven=list.stream().filter(i->i%2==0).mapToInt(i->i).sum(); System.out.println("Sum of Even numbers:"+sumOfEven); int sumOfOdd=list.stream().filter(i->i%2!=0).mapToInt(i->i).sum(); System.out.println("Sum of Odd numbers:"+sumOfOdd); } } ****************************************************************

What is the difference between List and Set ?

Q. What is the difference between List and Set.   Ans:- s.No List Set 1. List allow duplicates Set does’t allow duplicates 2. The List is an ordered collection which maintains the insertion order. Whereas set is an unordered collection which does not preserve the insertion order. 3. The List interface contains a single legacy class which is vector class Where as set interface does not have any legacy class 4. The List Interface can allow n number of null values 5.set Interface only allows a Single null value

What is flatMap() ? Write code for flatMap.

 Interview Question Q. What is flatMap() ? Write code for flatMap. Ans:-  flatMap()  :- flatMap()  transform each element and flattern the resulting collection into a single combined collection. Example of flatMap():- package v1; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class FlatMapExample { public static void main(String[] args) { List<List<Integer>> list=new ArrayList<>(); list.add(Arrays.asList(1,2,3)); list.add(Arrays.asList(4,5,6)); list.add(Arrays.asList(7,8,9)); System.out.println(list); List<Integer> flatList=list.stream().flatMap(i->i.stream().map(j->j*2)).collect(Collectors.toList()); System.out.println("FlatList:"+flatList); } } ************************************************************

what is the default server in spring boot?

 what is the default server in spring boot? Ans :- tomcat server is default server in spring boot  and it runs on port 8080.

How to Change server port in Spring Boot?

How to Change server port in Spring Boot? Ans :- open sts-> open springBoot Project-> open package src/main/resources-> application.properties-> server.port=8090

I want to remove duplicate elements on the list how can you do that.

 TCS Interview Question Q. I want to remove duplicate elements on the list how can you do that. SOLUTION:- package i1; import java.util.HashSet; import java.util.Set; public class RemoveDuplicateUsingSet { public static void main(String[] args) { Set<Integer> s=new HashSet<>(); s.add(1); s.add(2); s.add(2); s.add(3); s.add(4); s.add(5); s.add(7); s.add(7);                 System.out.println("Set :"+s); }                 }

How to achieve abstraction?

 Q. How to achieve abstraction ? Ans :- we can achieve abstraction by using abstract classes and Interfaces. Abstraction in java can be achieved in two ways: Using abstract classes:- Abstract classes achieve partial abstraction as concrete methods can also be defined in them. Using Interfaces :- Interfaces achieve complete abstraction as only abstract methods can be defined in them.

How to Create springBoot Project using sts ide ?

 How to Create SpringBoot Project using sts ide ? Ans :- Open sts ->File -> new ->Spring boot starter project->Add dependency -> Finish