Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Tell us something about JIT compiler.

 JIT stands for Just-In-Time and it is used for improving the performance during run
time. It does the task of compiling parts of byte code having similar functionality at
the same time thereby reducing the amount of compilation time for the code to run.
 The compiler is nothing but a translator of source code to machine-executable code.
But what is special about the JIT compiler? Let us see how it works:
o First, the Java source code (.java) conversion to byte code (.class) occurs with
the help of the javac compiler.
o Then, the .class files are loaded at run time by JVM and with the help of an
interpreter, these are converted to machine understandable code.
o JIT compiler is a part of JVM. When the JIT compiler is enabled, the JVM
analyzes the method calls in the .class files and compiles them to get more
efficient and native code. It also ensures that the prioritized method calls are
optimized.
o Once the above step is done, the JVM executes the optimized code directly
instead of interpreting the code again. This increases the performance and
speed of the execution.

What are the main differences between the Java


platform and other platforms?
There are the following differences between the Java platform and other platforms.

o Java is the software-based platform whereas other platforms may be the


hardware platforms or software-based platforms.
o Java is executed on the top of other hardware platforms whereas other
platforms can only have the hardware components

What gives Java its 'write once and run anywhere'


nature?
The bytecode. Java compiler converts the Java programs into the class file (Byte
Code) which is the intermediate language between source code and machine code.
This bytecode is not platform specific and can be executed on any computer.

What if I write static public void instead of public


static void?
The program compiles and runs correctly because the order of specifiers doesn't
matter in Java.

What is the default value of the local variables?


The local variables are not initialized to any default value, neither primitives nor
object references.

What are the advantages of Packages in Java?


There are various advantages of defining packages in Java.

o Packages avoid the name clashes.


o The Package provides easier access control.
o We can also have the hidden classes that are not visible outside and used by
the package.
o It is easier to locate the related classes.

What is the difference between an object-oriented


programming language and object-based
programming language?
There are the following basic differences between the object-oriented language and
object-based language.

o Object-oriented languages follow all the concepts of OOPs whereas, the


object-based language doesn't follow all the concepts of OOPs like
inheritance and polymorphism.
o Object-oriented languages do not have the inbuilt objects whereas Object-
based languages have the inbuilt objects, for example, JavaScript has window
object.
o Examples of object-oriented programming are Java, C#, Smalltalk, etc.
whereas the examples of object-based languages are JavaScript, VBScript, etc.

What is the purpose of a default constructor?


The purpose of the default constructor is to assign the default value to the objects.
The java compiler creates a default constructor implicitly if there is no constructor in
the class.

class Student3{
int id;
String name;

void display(){System.out.println(id+" "+name);}

public static void main(String args[]){


Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
}
}

Output:

0 null
0 null

Is constructor inherited?
No, The constructor is not inherited.

Can you make a constructor final?


No, the constructor can't be final.

What is the static variable?


The static variable is used to refer to the common property of all objects (that is not
unique for each object), e.g., The company name of employees, college name of
students, etc. Static variable gets memory only once in the class area at the time of
class loading. Using a static variable makes your program more memory efficient (it
saves memory). Static variable belongs to the class rather than the object.

What is the static method?


o A static method belongs to the class rather than the object.
o There is no need to create the object to call the static methods.
o A static method can access and change the value of the static variable.

What are the restrictions that are applied to the


Java static methods?
Two main restrictions are applied to the static methods.

o The static method can not use non-static data member or call the non-static
method directly.
o this and super cannot be used in static context as they are non-static.

Can we override the static methods?


No, we can't override static methods.

Can we execute a program without main() method?


Ans) No, It was possible before JDK 1.7 using the static block. Since JDK 1.7, it is not
possible. More Details.

What do you mean by data encapsulation?

 Data Encapsulation is an Object-Oriented Programming concept of hiding the data


attributes and their behaviours in a single unit.
 It helps developers to follow modularity while developing software by ensuring that
each object is independent of other objects by having its own methods, attributes, and
functionalities.
 It is used for the security of the private properties of an object and hence serves the
purpose of data hiding.

What is aggregation?
Aggregation can be defined as the relationship between two classes where the
aggregate class contains a reference to the class it owns. Aggregation is best
described as a has-a relationship.

Can you use this() and super() both in a


constructor?
No, because this() and super() must be the first statement in the class constructor.

Can we overload the methods by making them


static?
No, We cannot overload the methods by just applying the static keyword to
them(number of parameters and types are the same)
What is the final variable?
In Java, the final variable is used to restrict the user from updating it. If we initialize
the final variable, we can't change its value. In other words, we can say that the final
variable once assigned to a value, can never be changed after that. The final variable
which is not assigned to any value can only be assigned through the class
constructor.

Explain the difference between instance variable and a class variable.

Instance Variable: A class variable without a static modifier known as an instance


variable is typically shared by all instances of the class. These variables can have
distinct values among several objects. The contents of an instance variable are
completely independent of one object instance from another because they are related
to a specific object instance of the class.

Class Variable: Class Variable variable can be declared anywhere at the class level
using the keyword static. These variables can only have one value when applied to
various objects. These variables can be shared by all class members since they are
not connected to any specific object of the class.

1.Program to reverse Strring:


public class StringPrograms {

public static void main(String[] args) {


String str = "123";

System.out.println(reverse(str));
}

public static String reverse(String in) {


if (in == null)
throw new IllegalArgumentException("Null is not
valid input");

StringBuilder out = new StringBuilder();

char[] chars = in.toCharArray();

for (int i = chars.length - 1; i >= 0; i--)


out.append(chars[i]);

return out.toString();
}
Write a Java program to print a Fibonacci sequence using recursion.
public class PrintFibonacci {

public static void printFibonacciSequence(int count) {


int a = 0;
int b = 1;
int c = 1;

for (int i = 1; i <= count; i++) {


System.out.print(a + ", ");

a = b;
b = c;
c = a + b;
}
}

public static void main(String[] args) {


printFibonacciSequence(10);
}
}

How do you remove spaces from a string in Java?

The following example code shows one way to remove spaces from a string using
with the Character.isWhitespace() method:

String removeWhiteSpaces(String input) {


StringBuilder output = new StringBuilder();

char[] charArray = input.toCharArray();

for (char c : charArray) {


if (!Character.isWhitespace(c))
output.append(c);
}

return output.toString();
}

You might also like