Query OverFlow
Query OverFlow
QOF Series
Documented By,
Sakkthivel T
2021PECCB223
CODERS FORUM
QUERY:
Interviewer : Correct!!
This, I learned last week only..
Thanks
Interviewer : So, final keyword can be used for variable,
method & class (ALL 3).
final int a;
final int m(String s) {}
final class Z {}
So, if someone asks it's purpose, say all 3... not just about
final variables...
---------------------------------------------------------------------------
QUERY:
Interviewer : What is AtomicInteger in Java ?
Interviewer : yes.
---------------------------------------------------------------------------
QUERY:
--------------------------------------------------------------
QUERY:
Interviewer : Okay...
You're right.
O(1) is faster than Speed Of Light !!
Then why would anyone need TreeSet.... ? When do the order
of elements matter the most?
Because, anyways Set doesn't offer indexes to access the
elements... then why care about Order Of Elements ?
Candidate : TreeSet is more suitable than a HashSet for
problems like this one, where we need to maintain a sorted
order of unique elements while also considering their
frequency.
This problem is "Remove Duplicates" which is asked in
codeblitz.
Given an integer array in any order, remove some duplicates
in-place such that each unique element appears at most
twice.which means if element appears more than 2 times,
remove all extra element and make them pair.
Input Format
First line contains integer n(length of Array) Second line
contains n integer
Constraints : 1 <=num.length<=10
Sample Input 0:
10
4555667641
Sample Output 0:
14455667
Solution using TreeSet
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Map<Integer,Integer> m=new HashMap<>();
Set<Integer> s3=new TreeSet<>();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String s=sc.nextLine();
String s1[]=s.split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++){
s3.add(Integer.parseInt(s1[i]));
a[i]=Integer.parseInt(s1[i]);
if(m.containsKey(a[i])){
m.put(a[i],m.get(a[i])+1);
}
else{
m.put(a[i],1);
}
}
List<Integer> l=new ArrayList<>();
for(int i:s3){
if(m.get(i)>1){
l.add(i);
l.add(i);
}
else{
l.add(i);
}
}
for(int i:l){
System.out.print(i+" ");
}
}
}
Interviewer: Okay 🫡
In this, the order of Set elements is important.
Eventhough Set doesn't support Indexes, when iterating
through FOR loop, it's fetched as per the order.
This is an Excellent Point, Candidate.
But, hold on
I need more than "hashset uses hashing"........🫡
It does: 40 / 10 = 4
Is there any element at index 4 ? => True . So, O(1)
---------------------------------------------------------------------------
QUERY:
---------------------------------------------------------------------------
QUERY:
Interviewer : Correct
No more counter questions.
Everything well-explained. What about this ? 🫡
---------------------------------------------------------------------------
---------------------------------------------------------------------------
QUERY:
Interviewer: What's Bean in Java?
(Note: It's NOT coffee bean)
Interviewer: CLUE: To answer this, you will need some idea
about Serializability (yesterday's Query).
Candidate: It is a reusable software in Java. In Java, beans
are classes that encapsulate multiple classes into a single
object.
Interviewer: Hmm... not straight to the point...
50% correct
50% wrong
Interviewer: You have slightly mixed Applet definition with
Bean.
Candidate: It's any Java class that follows certain
conventions, implementing serializable, having private fields,
and public getter setters for 'em with a no-argument
constructor. It's a conceptual concept that standardizes
components so they are reusable and maintainable.
Interviewer: The "definition" is correct.
But assume I am just a layman. I wouldn't understand a single
word...
I mean, what's the purpose of a Bean?
Interviewer: In which part of the Java application is this
'Bean' most used?
Candidate: Spring Framework, Spring Boot, Java Persistence
API (JPA), Java EE mostly use beans.
Interviewer: Wow... I didn't expect the direct answer...
But yes, you're right!
Beans come into play in Database Code, especially when it
comes to Persistence.
Interviewer: But these are all huge things...
Persistence... Spring... JPA...
Can you please simplify and say how these Beans are exactly
used in simple Database Terms?
Candidate: Data Encapsulation:
A JavaBean can represent a table row from a database. Each
field in the JavaBean corresponds to a column in the database.
Getter and Setter Methods:
The bean has getter and setter methods to get and set the
values of these fields, just like how you'd read and write data
in the database.
Candidate: This is how JavaBean is used in terms of the
database.
Interviewer: Okay.
Interviewer: I can understand the concepts.
Interviewer: But let's just check if others do too.
Interviewer: We can check the Poll results exactly at 7:00
PM today.
If they are still confused, someone might have to give a more
simpler/shorter explanation.
Interviewer: Meanwhile, anybody's free to answer Sunday's
Question (which was a carry-forward from Saturday...)
Candidate: Serializable is a marker interface in Java, i.e., it
has no methods. It serves to "mark" Java objects that can be
serialized, i.e., converted into a byte stream that can then be
saved to a file, sent over a network, or stored in a database.
Candidate: Just asking out of curiosity...
Is Entity Class and Java Bean the same?
Candidate: Even Entity Class represents a table row... am I
right?
Candidate: No, it seems they are not the same... An entity
class maps a Java object to a database table for persistence,
but a JavaBean is a general-purpose object often used for UI
and data transfer.
Candidate: All the classes come under Serializable.
Candidate: For UI, ah?
Candidate: Any project examples? Where should I use the
entity class, and where should I use Java Bean?
Interviewer: Exactly correct
No more cross-questions for this Query.
Interviewer: Oh...
Now I am confused...
Candidate: For transferring and storing data between UI
components to backend logic.
Candidate: Usually, I transfer data by HTTP request-
response... Where is JavaBean used there?
Candidate: Me too 🫡
Candidate: Entity class:
In a registration form where the user wants to sign in using
their data such as email ID, password, name, etc., here the
details are stored in the database, and the UI shows validation
and success messages.
JavaBean:
JavaBean can be used to represent the form data. It holds the
user input temporarily and interacts with the form in the UI
layer.
Interviewer: Difference between JSON and Java Bean then?
Candidate: Yeah... what's the difference? 🫡
Candidate: In simple words, JavaBeans are used for handling
temporary data, and entity classes are used for persistent data
in the database.
Candidate: JSON is a data format used for data interchange.
JavaBeans are Java-specific components used for
encapsulating data and behavior in an object-oriented way.
Interviewer: Actually, I will give this info for free (before
this becomes a war zone).
JSON AND JAVABEAN ARE THE SAME.
JSON is for JavaScript.
Java Bean is for Java.
Candidate: JavaBeans are used in the frontend, and entity
classes are used in the backend.
Interviewer: End Result...
Interviewer: ?
Candidate: Is my friend correct?
Interviewer: Candidate, dude...
This is the first answer given.
Interviewer: Read existing chat first...
Candidate: Ohh
Candidate: Anyhow, I got clarity. Thank you, Candidate .
Interviewer: Then answer this Poll, Candidate.
Candidate: This explained clearly... Adding an example
would make it perfect.
Interviewer: It's 7 PM...
Apparently, more got confused than cleared...
I will type & send an answer in my words (maybe it can help).
Interviewer: NOTE: Candidate's ANSWERS ARE
ABSOLUTELY GREAT.
I am just sending for those 7 members who are still confused.
Interviewer: Explanation for Today's Query.
Interviewer: Serializability is the ability of an object to be
able to convert to Binary (byte[]).
If converted, it's easy to store in the database (Persistence).
Interviewer: Explanation for Yesterday's Query.
Interviewer: Once you have read the explanation, update in
the Poll.
If still confused, maybe we can discuss this in a MeetUp
Interviewer: Yesterday, we have been using this buzzword
quite a lot of times...
So, today's Query is on that:
---------------------------------------------------------------------------
Query :
---------------------------------------------------------------------------
Query :
---------------------------------------------------------------------------
Query :
Interviewer: What’s the use of the pipeline operator (|) in
Java?
Candidate: The pipeline operator (|) is primarily used for:
1. Bitwise OR operations in Java.
2. Handling multiple exceptions in a single catch block.
Interviewer: How does the pipeline operator help in handling
multiple exceptions?
Candidate: If a try block throws multiple exceptions, the
pipeline operator allows grouping those exceptions in a single
catch block. This simplifies the code and avoids redundancy.
Interviewer: Can you provide a sample code for this?
Candidate: Here’s an example:
public class Example {
public static void main(String[] args) {
try {
int[] num = {1, 2, 3};
System.out.println(num[4]); // Throws
ArrayIndexOutOfBoundsException
int result = 10 / 0; // Throws ArithmeticException
} catch (ArrayIndexOutOfBoundsException |
ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
This reduces repetitive code and makes exception handling
concise and readable.
---------------------------------------------------------------------------
Query :
Interviewer: What are inner classes and anonymous classes?
Can classes be declared as "static"?
Candidate: Inner Classes:
Inner classes are classes defined within another class. They
have access to the members (both static and non-static) of
their enclosing class. They are categorized as:
1. Member Inner Classes – Non-static inner classes
defined inside a class but outside any method.
2. Static Nested Classes – Declared using the static
keyword.
3. Local Inner Classes – Defined inside a method or block.
4. Anonymous Inner Classes – Classes without a name,
usually used for one-time usage or when implementing
interfaces.
Static Classes: A class can be declared as static if and only if
it is a nested class. Static nested classes can access only the
static members of the outer class and do not require an
instance of the outer class.
class InnerClass {
void display() {
System.out.println("Accessing from InnerClass: " +
outerField);
}
}
void createInnerInstance() {
InnerClass inner = new InnerClass();
inner.display();
}
}
public class Main {
public static void main(String[] args) {
OuterClass outer = new OuterClass();
outer.createInnerInstance();
}
}
Output:
Accessing from InnerClass: Outer field
Query :
Interviewer: What is an anonymous class? Can you provide
an example?
Candidate: An anonymous class is a nested class without a
name. It is declared and instantiated in a single expression and
is used when a class is needed for immediate use, such as
implementing an interface or extending a class.
Example of an Anonymous Class:
public class Main {
public static void main(String[] args) {
// Anonymous class implementing Runnable interface
Runnable task = new Runnable() {
@Override
public void run() {
System.out.println("Anonymous class implementing
Runnable running!");
}
};
new Thread(task).start();
}
}
Output:
Anonymous class implementing Runnable running!
These classes are particularly useful for concise and specific
implementations of functionality.
---------------------------------------------------------------------------