0% found this document useful (0 votes)
4 views14 pages

Java String Swap Without Third Variable

The document provides a Java program demonstrating how to swap two strings without using a third variable. It also explains key concepts in React, including useState(), JSX, props, and React Hooks, along with comparisons between functional components and class-based components. Additionally, it covers various Java interview questions related to method overloading, class loaders, inheritance, and other core Java concepts.

Uploaded by

rohit01ggn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Java String Swap Without Third Variable

The document provides a Java program demonstrating how to swap two strings without using a third variable. It also explains key concepts in React, including useState(), JSX, props, and React Hooks, along with comparisons between functional components and class-based components. Additionally, it covers various Java interview questions related to method overloading, class loaders, inheritance, and other core Java concepts.

Uploaded by

rohit01ggn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Program to Swap

Two Strings Without


Using Third Variable
package [Link];

public class SwapTwoStrings {

public static void main(String[] args) {

String s1 = "java";
String s2 = "guides";

[Link]("Before swapping two


strings:");
[Link]("s1 => " + s1);
[Link]("s2 => " + s2);

// Using a third variable to swap the strings


String temp;
temp = s1; // store s1 in temp
s1 = s2; // assign s2 to s1
s2 = temp; // assign temp (original s1) to s2

[Link]("After swapping two


strings:");
[Link]("s1 => " + s1);
[Link]("s2 => " + s2);
}
}

React

What is useState() in React?


The useState() is a built-in React Hook that allows you
for having state variables in functional components. It
should be used when the DOM has something that is
dynamically manipulating/controlling.

In the below-given example code, The useState(0) will


return a tuple where the count is the first parameter that
represents the counter’s current state and the second
parameter setCounter method will allow us to update
the state of the counter.

...
const [count, setCounter] = useState(0);
const [otherStuffs, setOtherStuffs] = useState(...);
...
const setCount = () => {
setCounter(count + 1);
setOtherStuffs(...);
...
};
We can make use of setCounter() method for updating
the state of count anywhere. In this example, we are
using setCounter() inside the setCount function where
various other things can also be done. The idea with
the usage of hooks is that we will be able to keep our
code more functional and avoid class-based
components if they are not required.
What is JSX?
JSX stands for JavaScript XML. It allows us to write
HTML inside JavaScript and place them in the DOM
without using functions like appendChild( ) or
createElement( ).

As stated in the official docs of React, JSX provides


syntactic sugar for [Link]( ) function.

Note- We can create react applications without using


JSX as well.

Let’s understand how JSX works:

Without using JSX, we would have to create an


element by the following process:

const text = [Link]('p', {}, 'This is a text');


const container = [Link]('div','{}',text );
[Link](container,rootElement);
Using JSX, the above code can be simplified:

const container = (
<div>
<p>This is a text</p>
</div>
);
[Link](container,rootElement);
As one can see in the code above, we are directly
using HTML inside JavaScript.

What are props in React?


The props in React are the inputs to a component of
React. They can be single-valued or objects having a
set of values that will be passed to components of
React during creation by using a naming convention
that almost looks similar to HTML-tag attributes. We
can say that props are the data passed from a parent
component into a child component.

The main purpose of props is to provide different


component functionalities such as:

• Passing custom data to the React component.


• Using through [Link] inside render()
method of the component.
• Triggering state changes.
For example, consider we are creating an element with
reactProp property as given below: <Element reactProp =
"1" />This reactProp name will be considered as a
property attached to the native props object of React
which already exists on each component created with
the help of React library: [Link];.
What is React Hooks?
React Hooks are the built-in functions that permit
developers for using the state and lifecycle methods
within React components. These are newly added
features made available in React 16.8 version. Each
lifecycle of a component is having 3 phases which
include mount, unmount, and update. Along with that,
components have properties and states. Hooks will
allow using these methods by developers for improving
the reuse of code with higher flexibility navigating the
component tree.

Using Hook, all features of React can be used without


writing class components. For example, before React
version 16.8, it required a class component for
managing the state of a component. But now using the
useState hook, we can keep the state in a functional
component.

Differentiate React Hooks vs Classes.

React Hooks Classes

It is used in functional It is used in class-based compone


components of React. of React.
It will not require a It is necessary to declare the
declaration of any kind of constructor inside the class
constructor. component.

It does not require the use


Keyword this will be used in state
of this keyword in state
declaration ([Link]) and in
declaration or
modification ([Link]()).
modification.

It is easier to use because No specific function is available fo


of helping us to access the state and
the useState functionality. corresponding setState variable.

React Hooks can be Because of the long setup of state


helpful in implementing declarations, class states are
Redux and context API. generally not preferred.

Java

1. Tell me the difference between Method


Overloading and Method Overriding in Java.
This is one of the core Java interview questions for mid-
experienced [Link] Overloading is used
to increase the program’s readability. It has the same
method name but different [Link]
Overriding provides the specific implementation of the
method. It has the same method and same parameters.

2. What do you mean by the class loader in


Java?
In the Java Virtual Machine (JVM), the class loader is a
type of subsystem. It is used for loading class files. When
you run the program, it will be first loaded by the class
loader. The built-in class loaders are of three types in
Java-

•The Bootstrap Classloader is the first-class loader in


JVM. It is the superclass of the Extension class loader.
It loads the [Link] file that constitutes all the class
files of Java Standard Edition. The files being
[Link] package classes, [Link] package classes,
[Link] packages classes, [Link] packages classes,
[Link] packages classes, etc.
•The Extension Classloader is the class loader of
Bootstrap and the parent class loader of the system
classloader. It generally loads the jar files located
inside the $JAVA_HOME_/jre/lib/ext directory.
•The System Application Classloader is the type child
class loader of the Extension class loader. From the
cross path, the System Application Classloader loads
the class files. It is by default; the classpath is set to
the current directory. Therefore, it is also called the
Application class loader.
3. What do you mean by inheritance in
Java?
To this Java interview question, you can answer it by
saying; In Java, inheritance is a mechanism through which
an object acquires all the properties and behavior of
another class. It is usually used for Method Overriding
and Code [Link] concept of inheritance in Java is
based on the fact that it creates new classes that are
built upon the existing classes. Therefore, these methods
and fields of the parent class can be reused if we inherit
from an existing class. And also, we can add new
methods and fields to our current class.
In Java, the inheritance is of five types-

•Hybrid Inheritance
•Hierarchical Inheritance
•Single-level Inheritance
•Multi-level Inheritance
•Multiple Inheritance
4. Why can we not override the static
method in Java?
This is one of the core Java programming interview
questions for experienced professionals in the Java
interview.

The reasons why we cannot override the static method in


Java are :
(a) The static method does not belong to the object level.
Instead, it belongs to the class level. The object decides
which method can be called in the method overriding.

(b) In the static method, which is the class-level method,


the type reference decides which method to call without
referring to the object. It concludes that the called
method is determined at the compile time.

If any child class defines the static method with the same
signature as the parent class, then the method in the
child class hides the method in the parent class.

5. Can you tell us what the Dynamic Method


Dispatch is in Java?
The Dynamic method dispatch is a process through which
a call towards an overridden method is solved at a run
time. It is the object that is being referred to, not the type
reference variable. It decides which version of the
overridden method needs to be executed.

6. What is a Java ClassPath?


A Java ClassPath is an environment variable that the Java
Virtual Machine (JVM) uses to collect all classes used by
the program.

7. What can be stated as a volatile keyword


in Java?
If a variable is marked as volatile, it can be read from the
main memory instead of the cache memory.

8. Where does the final block not execute in


Java?
To this Java interview question, you can answer it by
saying; There is only one case where the final block does
not execute in Java. The final block does not execute
when you run [Link](0) in the try or catch block in
the Java programs.

9. What are the major points of distinction


between StringBuffer and StringBuilder in
Java?
A StringBuffer is thread-safe. Therefore, simultaneously
two threads cannot call the methods of StringBuffer. On
the other hand, in comparison to StringBuffer, a
StringBuilder is not known to be thread-safe. Hence, it
means that two threads can call the methods of
StringBuilder at the same time.

On the basis of performance StringBuffer’s performance


is less efficient as it is thread-safe. Whereas
StringBuilder’s performance is more efficient as it is not
thread-safe.

10. Can you tell the difference between the


Vector and ArrayList in Java?
You can answer this Java interview question by listing the
differences between the vector and the ArrayList.

•(a) The common difference between the vector and the


ArrayList in Java is that the vector is synchronized
and thread-safe while the ArrayList is not.
•(b) Since vector is synchronized, it is slow, but since the
ArrayList is not synchronized, it is comparatively
faster
11. Why is String immutable in Java?
You can answer this Java interview question by saying
there are several reasons why String is immutable or
unchangeable in Java.

String pool– if you assign a value to String using the


double quotes (” “), it gets stored in the string literal pool
area, and a single String can be referenced by several
reference variables, which will make it affect all reference
variables if the String becomes mutable.

Classloading- String is used for the mechanism of class


loading. If String becomes mutable, then it will become a
security threat because anyone can hack it.

Cache hash value– when you use String as a key in


HashMap or any other collection, you can cache its hash
value. You do not need to calculate each time as it will
always be constant because the string is immutable.
12. Can you tell the difference between the
HashMap and HashSet in Java?
This is one of the advanced Java Interview questions for 2
to 3 years of experience candidates. The interviewer will
expect you to know the answer to this Java interview
[Link] answer this Java interview question, HashMap
implements the Map interface, which maps keys to value.
It is not synchronized, and it is not thread safe. Null keys
and values are allowed, whereas duplicate keys are not
allowed.

HashMap<Integer,String> studentHashMap=new
HashMap<Integer,String>();

[Link](1, “Anushka”);

[Link](2. “Akansha”);

In the case of HashSet, it implements a Set interface that


does not allow duplicate values. As a result, it is neither
synchronized nor thread-safe.

HashSet studentSet=new HashSet();

[Link](“Anushka”);

[Link](“Akansha”);

[Link](“Anjana”);

You might also like