
What is java? And its features.
Java is one of the oops base programming languages and it is easy to run in any software like Windows, mac, or Linux syntax is familiar to c and c++ and it is easy to write and read there are some features of java.
(i) Easy: Java is a language that is considered easy to learn.
(ii) Secured Feature: That helps develop a virus-free and tamper-free system for the users.
(iii) OOP: This is one of the important features in java, oop stands for object-oriented programming language.
(iv) Independent platform: Java is not compiled into a platform-specific machine, instead, It is compiled into platform-independent bytecode.
How does java enable high-performance language?
Java uses a just-in-time compiler to enable high performance. A Just-in-time compiler is a program that turns java bytecode, which is a program that contains instructions that must be interpreted into instructions that can be sent directly to the processor. some small points tell why java is a high-performance language.
(i) object-oriented
(ii) platform independent
(iii) secure
(iv) compiled and interpreted
(v) robust
(vi) distributed
(vii) multi-threaded
(viii) dynamic
What is meant by the Constructor, Local variable, and Instance variable in Java? Explain with an example.
Constructor: A member function used to initialize an object while creating it.
Example:- constructor java = new constructor();
constructor.setname=(“Nishant Kumar”);
In order to write our own constructor, we define a method with the same name as the class name like.
public constructor( ){
Name = “your name”;
}
Local variable:- A variable that can be created in the method class. This variable can be accessed out of methods, the local variable allows the methods to store its temporary state like.
Example:- public class LocalVariable{
Public static void main (String[ ] args){
int a=10;
Int b=20;
Int c = a+b;
System.out.println(“sum of a and b is:-”+c);
}
}
Instance variable:- instance variable is a nonstatic variable used by objects to store their states. It sounds similar to a local variable but it has an access modifier.”ObjectReference.VariableName.
Example:- public class InstanceVariable{
int c;
public void addition(){
int a=10;
Int b=20;
Int c = a+b;
System.out.println(“sum of a and b is:-”+c);
}
Public void difference(){
int x=100;
int y=150;
c=x-y;
System.out.println(“difference between a and b is: “+c);
}
public static void main (String [] args ){
IntstanceVariable ins1 = new InstanceVariable();
ins1.addition();
ins1.difference();
}
}
What are Class, Object, Inheritance & OOPs concepts in java? Explain with an example.
Class: A class in java is a blueprint that includes all your data. It contains variables & methods to describe the behavior of an object.
Example:- Class Student{
String studentName;
public string getName(int ID){
………
return studentName;}
}
Object:- Object is an instance of a class. It is the real entity and occupies memory. All the objects created from a single class share the same behavior and state, the object being a physical entity. Objects are created at the run time.
Example:- public class ObjectExample{
public static void main (String[] args){
Student s1 = new Student();
s1(“nishant kumar”);}
}
Inheritance:- The process where one class acquires the properties of another, the class which inherits the properties of another class is known as a subclass. The class whose properties are inherited is known as a superclass.
Example:- class Subclass extends Superclass
{
//methods and fields
}
OOPS is mythology for programmers to program efficiently and easily by using objects and class, it is to work on DRY (Don’t Repeat Yourself).
What is Encapsulation in Java?
This concept is from oops. Encapsulation is the wrapping up of data under a single unit. There is an example in oops.
Example:- class Sample{
Public:
Int a,b;
Void func(){
….
}
};
Int main(){
Sample s1;
s1.func();
return 0;
}
What is Polymorphism in Java?
This is also from oops. Polymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning poly means many and morphism means types.
I.e:-
(a).print(‘nishant’+’rajput’)
Output – nishantrajput
(b).print(10+50)
Output – 60
What is meant by Method Overriding? Explain with an example.
Ans. Whenever we write in super and subclasses in such a way that name and parents must be same, it is called method overriding.
Syntax:-class A{
Void show()
{
}
}
Class B extends A{
Void show()
{
}
}
What is meant by Interface & Abstract class?
Interface:- interface has only abstract methods. since java 8, it can have default and static methods also. An interface can extend any number of the interface at a time. An interface can have only abstract methods. An interface cannot declare constructor destructors. The interface has only statics and final variables.
Abstract:- Abstracts class can have abstract and non-abstract methods. An abstract class can extend only one class or one abstract class at a time. An abstract class can have both abstract and concrete methods. An abstract class can declare constructors and destructors. An abstract class can have final, non final static, and non static variables.
Difference between Array and Array List in a tabular form.
Array:- Fixed length (cannot change the size of the array), i.e:- String strArray[] = new String[10], so here we initialize the length of the array so you can not add more than that can not contain primitive data types, as well as objects of a class, can be multi-dimensional, it is also the part of core java there is a method (Length).this is used when size is fixed.
Array List:- Re-sizeable arrays are dynamic. I.e:- ArrayList<String> list = new ArrayList<” ”>(); Elements can be inserted at or deleted from a particular position; it also has inbuilt methods. Supports only object entries; it is one-dimensional and part of the collection framework the method uses that method name is “Size”.
Difference between String, StringBuilder, and StringBuffer. Explain with an example.
Ans. String:- Immutable, if you try to alter their value, another object gets created. If your string is not going to change then use this.
StringBuffer:- Mutable and thread-safe [synchronized] 2 threads can call the methods of StringBuffer Simultaneously and are less efficient. If your string can change and can be accessed from multiple threads then use this.
StringBuilder:- Non-synchronised / not thread-safe 2 threads can’t call simultaneously and are more efficient/faster. If your string can change and will only be accessed from a single thread then use this.
Explain Public and Private access specifiers.
Ans. Private:- It provides high security for its own data member. it can’t be inherited.
public:- It doesn’t provide any kind of security for its data, it is inherited from any projects.
Q.12) Difference between Abstract class and Interface.
Ans. Abstract class :- (i).Abstracts class can have abstract and non-abstract methods
(ii). An abstract class can extend only one class or one abstract class at a time
(iii). An abstract class can have both abstract and concrete methods. An abstract.class can declare constructors and destructors
(iv). Abstract class can have final, non-final static, and non-static variables.
Interface:- (i).interface has only abstract methods.since java 8,it can have default and static methods also
(ii). An interface can extend any number of interfaces at a time. An interface can have only abstract methods
(iii). An interface cannot declare constructor destructors. The interface has only statics and final variables.
Please do not enter any spam link in the comment box.