Q1. What is Java?
ANS: Java is a high-level, object-oriented, robust, secure, platform-independent, high-performance, Multithreaded, and portable programming language developed by James Gosling in June 1991. It can also be known as a platform as it provides its own JRE and API.
Q2. What are the features of Java?
ANS: 1. Object-Oriented: Everything is an object.
2 . Platform-Independent: Java code is converted into bytecode, which is platform-independent.
3 . Robust: Strong memory management, garbage collection, and exception handling.
4 . Multithreaded: Built-in support for multithreading.
5 . Secure: Java applications run in a secure environment using the Java Runtime Environment (JRE).
Q3.Explain the Difference Between JDK,JRE, and JVM.
ANS: JDK (Java Development Kit): Includes tools for developing and debugging Java applications.
JRE (Java Runtime Environment): Provides libraries and JVM for running Java applications.
JVM (Java Virtual Machine): Converts bytecode to machine code and executes it.
Q4. What do you understand by Java virtual machine?
ANS: Java Virtual Machine (JVM) is a virtual machine that enables the computer to run the Java program. JVM acts like a run-time engine, calling the main method present in the Java code. JVM is the specification that must be implemented in the computer system. The Java code is compiled by JVM to be Bytecode, which is machine-independent and close to the native code.
Q5. What gives Java its 'write once and run anywhere' nature?
ANS: The bytecode. The 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.
Q6. Is Empty .java file name a valid source file name?
ANS: Yes, Java allows to save our java file by .java only, we need to compile it by javac .java and run by java classnam.
Q7. What if I write static public void instead of public static void?
ANS: The program compiles and runs correctly because the order of specifiers doesn't matter in Java.
Q8. What is a ClassLoader in Java?
ANS: A ClassLoader is a part of the JVM that loads classes into memory.
Bootstrap ClassLoader: Loads core Java classes.
Extension ClassLoader: Loads classes from the lib/ext directory.
Application ClassLoader: Loads classes from the classpath.
Q9. What are Java’s access modifiers?
ANS: 1.Public: Accessible everywhere.
2.Protected: Accessible within the package and by subclasses.
3.Default (Package-private): Accessible only within the package.
4.Private: Accessible only within the class.
Q10. What are the advantages of Packages in Java?
ANS: There are various advantages of defining packages in Java.
Packages avoid the name clashes.
The Package provides easier access control.
We can also have the hidden classes that are not visible outside and used by the package.
It is easier to locate the related classes.
Q11. What are Java’s memory areas?
ANS:Heap: Stores objects and instance variables.
Stack: Stores method calls and local variables.
Method Area: Stores metadata and static data.
PC Registers: Stores address of currently executing JVM instruction.
Native Method Stack: For native method calls.
Q12. What is an object?
ANS: The Object is the real-time entity having some state and behavior. In Java, an Object is an instance of the class having the instance variables as the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword.
Q13. What is the constructor?
ANS: The constructor can be defined as the special type of method that is used to initialize the state of an object. It is invoked when the class is instantiated, and the memory is allocated for the object. Every time, an object is created using the new keyword, the default constructor of the class is called. The name of the constructor must be similar to the class name. The constructor must not have an explicit return type.
Q14. What is the difference between an object-oriented programming language and an object-based programming language?
ANS: 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.
Object-oriented languages do not have the inbuilt objects whereas Object-based languages have inbuilt objects, for example, JavaScript has a window object.
Examples of object-oriented programming are Java, C#, etc. whereas the examples of object-based languages are JavaScript, VBScript, etc.
Q15. How many types of constructors are used in Java?
ANS: Based on the parameters passed in the constructors, there are two types of constructors in Java.
1.Default Constructor: default constructor is the one which does not accept any value. The default constructor is mainly used to initialize the instance variable with the default values. It can also be used for performing some useful task on object creation. A default constructor is invoked implicitly by the compiler if there is no constructor defined in the class.
2.Parameterized Constructor: The parameterized constructor is the one which can initialize the instance variables with the given values. In other words, we can say that the constructors which can accept the arguments are called parameterized constructors.
Q16. Does constructor return any value?
ANS: yes, The constructor implicitly returns the current instance of the class (You can't use an explicit return type with the constructor).
Q17. Is constructor inherited?
ANS: No, The constructor is not inherited.
Q18. Can you make a constructor final?
ANS: No, the constructor can't be final.
Q20. What is the purpose of the final keyword?
ANS: final variable: Its value cannot be changed.
final method: Cannot be overridden by subclasses.
final class: Cannot be subclassed.
Browse our course links: Java Training in Pune
To Join our FREE DEMO Session: Click Here
Q21. What is Garbage Collection in Java?
ANS: Garbage Collection automatically deallocates unused objects in memory.
Mark and Sweep: Identifies unused objects and clears them.
Finalization: Cleanup before deallocation.
Q22. What is the static variable?
ANS: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.
Q23. What is the static method?
ANS: A static method belongs to the class rather than the object.
There is no need to create the object to call the static methods.
A static method can access and change the value of the static variable.
Q24. Why is the main method static?
ANS: Because the object is not required to call the static method. If we make the main method non-static, JVM will have to create its object first and then call main() method which will lead to the extra memory allocation.
Q25. Can we override the static methods?
ANS: No, we can't override static methods.
Q26. What is the static block?
ANS: Static block is used to initialize the static data member. It is executed before the main method, at the time of classloading.
Q27. 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.
Q28. Can we make the abstract methods static in Java?
ANS: In Java, if we make the abstract methods static, It will become the part of the class, and we can directly call it which is unnecessary. Calling an undefined method is completely useless therefore it is not allowed.
Q29. Can we declare the static variables and methods in an abstract class?
ANS: Yes, we can declare static variables and methods in an abstract method. As we know there is no requirement to make the object to access the static context, therefore, we can access the static context declared inside the abstract class by using the name of the abstract class.
Q30. What is this keyword in Java?
ANS: This keyword is a reference variable that refers to the current object. There are various uses of this keyword in Java. It can be used to refer to current class properties such as instance methods, variables, constructors, etc. It can also be passed as an argument into the methods or constructors. It can also be returned from the method as the current class instance.
Q31. Can we assign the reference to this variable?
ANS: No, this cannot be assigned to any value because it always points to the current class object and this is the final reference in Java. However, if we try to do so, the compiler error will be shown.
.
Q32. Can this keyword be used to refer static members?
ANS: Yes, It is possible to use this keyword to refer static members because this is just a reference variable which refers to the current class object. However, as we know that, it is unnecessary to access static variables through objects, therefore, it is not the best practice to use this to refer static members
Browse our course links: Java Training in Pune
To Join our FREE DEMO Session: Click Here
Q33. What are the main uses of this keyword?
ANS: There are the following uses of this keyword.
1.this can be used to refer to the current class instance variable.
2.this can be used to invoke current class method (implicitly)
3.this() can be used to invoke the current class constructor.
4.this can be passed as an argument in the method call.
5.this can be passed as an argument in the constructor call.
6.this can be used to return the current class instance from the method.
Q34. What is the Inheritance?
ANS: Inheritance is a mechanism by which one object acquires all the properties and behavior of another object of another class. It is used for Code Reusability and Method Overriding. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
There are five types of inheritance in Java.
1.Single-level inheritance
2.Multi-level inheritance
3.Hierarchical Inheritance
4.Hybrid Inheritance
Q35. Which class is the superclass for all the classes?
ANS: The object class is the superclass of all other classes in Java.
Q36. What is super in java?
ANS:The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever you create the instance of the subclass, an instance of the parent class is created implicitly which is referred by a super reference variable. The super() is called in the class constructor implicitly by the compiler if there is no super or this.
Q37. What are the main uses of the super keyword?
ANS: super can be used to refer to the immediate parent class instance variable.
super can be used to invoke the immediate parent class method.
super() can be used to invoke the immediate parent class constructor.
Q38. What are the differences between this and the super keyword?
ANS: The super keyword always points to the parent class contexts whereas this keyword always points to the current class context.
The super keyword is primarily used for initializing the base class variables within the derived class constructor whereas this keyword is primarily used to differentiate between local and instance variables when passed in the class constructor.
The super and this must be the first statement inside the constructor otherwise the compiler will throw an error.
Q39. What is object cloning?
ANS: Object cloning is used to create the exact copy of an object. The clone() method of the Object class is used to clone an object. The java.lang.A cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement the Cloneable interface, the clone() method generates CloneNotSupportedException.
Q40. What is method overloading?
ANS: Method overloading is the polymorphism technique that allows us to create multiple methods with the same name but different signatures. We can achieve method overloading in two ways.
1.By Changing the number of arguments
2.By Changing the data type of arguments
Q41. What is method overriding?
ANS: If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to implement the interface methods.
Rules for Method overriding:
1.The method must have the same name as in the parent class.
2.The method must have the same signature as in the parent class.
3.Two classes must have an IS-A relationship between them.
Q42. Can we override the static method?
ANS: No, you can't override the static method because they are the part of the class, not the object.
Q43. What is the final method?
ANS: If we change any method to a final method, we can't override it.
Q44. What is the final class?
ANS:If we make any class final, we can't inherit it into any of the subclasses.
Q45. Can you declare the main method as final?
ANS: Yes, We can declare the main method as public static final void main(String[] args){}.
Browse our course links: Java Training in Pune
To Join our FREE DEMO Session: Click Here
Q46. What is the difference between throw and throws in Java?
ANS: throw: Used to explicitly throw an exception in the method body.
Example: throw new ArithmeticException("Division by zero");
throws: Declares the exceptions that a method can throw.
Example: public void method() throws IOException { ... }
Q47. Explain the Singleton Design Pattern.
ANS: The Singleton pattern ensures that a class has only one instance and provides a global point of access to it.
Q48. What is the purpose of the enum keyword in Java?
ANS: An enum defines a fixed set of constants.
Q49. What are Wrapper Classes in Java?
ANS: Wrapper classes convert primitive data types into objects.
Examples: Integer, Double, Boolean, etc.
Q50. Explain the difference between deep copy and shallow copy.
Shallow Copy: Copies references; changes in one object affect the other.
Deep Copy: Copies the actual data; changes in one object don’t affect the other.
Browse our course links: Java Training in Pune
To Join our FREE DEMO Session: Click Here
Get More Information