Get ReadyWithJava
Get ReadyWithJava
String name="Child";
}
class Parent{
String name="Parent";
}
4. fruits.add("banana");
5.sysout(fruits.size);
6.}
why variables must be effectively final/final in java lambda
List<Employee>
Employee-> name, salary
https://www.sitesbay.com/java/java-polymorphism
Parent
Types :
Local Variable Capture
Static Variable Capture
int countNoOfWords (Strint countNoOfWords (String string) { int count =0; char ch [] =new char(string
new obj()-- no obj found.(noclassdeffound)
New instance()-- no object.. Classnot found
I/P
List<String> fruits= new ArrayList<>();
fruits.add("mango");
fruits.add("apple"); fruits.add("banana");
O/P
String result="mango+apple+banana"
ng) { int count =0; char ch [] =new char(string.length); for(int i=0; i<string.length();i++){ch[i]=string.charAt(i
+){ch[i]=string.charAt(i); if((i>0) && (ch[i]=' ') || ((ch[0]!=' ' && i==0)) ) count++; } return count; }ing string) {
turn count; }ing string) { int count =0; char ch [] =new char(string.length); for(int i=0; i<string.length();i++){c
i<string.length();i++){ch[i]=string.charAt(i); if((i>0) && (ch[i]=' ') || ((ch[0]!=' ' && i==0)) ) count++; } return
=0)) ) count++; } return count; }
This is a simple java program java is cool
this 1
is 2
a 1
simple 1
java 2
cool 1
ques
system.arraycopy vs arrays.copyof
5. Follow up question, We have one object named e1 with property called Name. initalize the obj with
name “ABC”, 20. We have added that object into HashMap (map.put(e1,”value”)), now we change the
value of name from “ABC” to “MNO” by e1.setName() and try to get the value by map.get(e1). Will it
give “value” or null ?
e1.setName("IRIS");
Sysout(map.get(e1));
hashCode() equals ()is implemnted based on propety name
4. Suppose we have Student class with properties “name” & “age” and we have implemented only
hashCode() but not equals() methods. Now we created two objects s1, s2 with same property value
“RAM”, 20 respectively.
When we add them into HashMap then what will be the size of map?
Sysout(map.size());
hashCode() is implemnted based on propety name
Hash collision
24.When ConcurrentModificationException Occurred and how to avoid that?
class Employee {
int number;
String name;
int hashcode(){
return 1; }
}
public class Main
{
public static void main(String[] args) {
Employee emp = new Employee(1,"abc");
Employee emp2 = new Employee(1,"abc");
The difference is that Arrays.copyOf does not only copy elements, it also creates a new array. System.arraycopy
copies into an existing array.
it uses System.arraycopy internally to fill up the new array:
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
null as hashCode() is implemnted based on propety name so when externally objects name property is changed it
cannot retreive it example below:-package com.java.hashcodeimpl;
size will be 2 since based on equals comparison both objects will be different
Linked list will be created
4. fruits.add("banana");
5.sysout(fruits.size);
6.}
2
map.put(emp,"a");
map.put(emp2,"a");
map.put(emp2,"c");
2
TYPE
Arraylist
Arraylist
hashmap
hashmap
concurrentModificationException @ line no 3
6.ConcurrentHashMap ? internal implementation
No need to put any lock when getting the element from ConcurrentHashMap.
remove (Object key) going to remove an element from a
Segment, we need a lock on the that Segment.
7.Follow up question – we have 2 threads t1, t2, one is reading and other is updating
the same ConcurrentHashMap, now how you will make sure that reader thread must
get updated value?
8.what will happen when i call get bean when two beans with same type but different id
primary annotation
post and put
Singleton Bean will have one instance throughout application and Prototype bean will have
new instance in each request
Bean Factory
>Bean instantiation/wiring
Application Context
>Bean instantiation/wiring
>Automatic BeanPostProcessor registration
>Automatic BeanFactoryPostProcessor registration
>Convenient MessageSource access (for i18n)
>ApplicationEvent publication
> Internationalization using MessageSources
The ApplicationContext interface extends an interface called MessageSource,
no new instance of prototype bean is created. Every time the same instance which is
created at the start-up is returned.
Solution:
>application context: Instead of Autowired, we used BeanUtil class to load prototype bean
in API method.
>Lookup :@Lookup tells Spring to return an instance of the method’s return type when we
invoke it.
>By setting the proxy mode to ScopedProxyMode.TARGET_CLASS, Spring will create a new
instance of the prototype whenever you call its method.
Java considers something a singleton if it cannot create more than one instance of that
class within a given class loader, whereas Spring would consider something a singleton if it
cannot create more than one instance of a class within a given container/context.
9)You have two tables employee and organisation, in select e.count(*), o.departName from
employee you have orgId and employee detail and in Employee e inner join organisation on
organisation you have organisation detail e.deptID=o.deptID and o.deprtName = 'HR'
and department (Hr, Account, IT). Count the number group by deprtName
of employees in Hr department
transient keyword?
17. Write query for History Table(Comments id, time, SELECT * FROM History t1 WHERE Date
comments) Comments id can have duplicate id’s, time IN (SELECT Date FROM History AS T2
is incrementing, comments can be duplicate Find last WHERE T2.ID = t1.ID
10 rows for all the Comments id’s ORDER BY Date DESC LIMIT
10)
RAM 4
Ayesha 3
IRIS 1
pepsi--> for each country, year new licence--> get latest licence no of each c Pepsi Country licence no
in LNC001
r=(case when gender='F' then 'M' elseif 'M' then 'F') in LNC006
br LNC008
Sakshi M us LNC016
Shiva F us LNC017
Ayesha M
year country licence no year java streams
2019 in LNC006 2020
2020 br LNC011 2021
2018 us LNC017 2019
2020
2021
2018
2019
design pattern 32 type
send a visitor