Q) What are differences between Java and C++?
Ans)
- Java does not support pointers. Pointers are inherently insecure and troublesome. Since pointers do not exist in Java.
- Java does not support operator overloading.
- Java does not perform any automatic type conversions that result in a loss of precision
- All the code in a Java program is encapsulated within one or more classes. Therefore, Java does not have global variables or global functions.
- Java does not support multiple inheritance.
- Java does not support destructors, but rather, add the finalize() function.
- Java does not have the delete operator.
- The <<>> are not overloaded for I/O operations.
Q) Define Access Specifiers & Access modifiers?
Ans)
Access Specifiers -> A.S gives access privileges to outside of application (or) others, they are Public, Protected, Private, Defaults.
Access Modifiers -> A.M which gives additional meaning to data, methods and classes, final cannot be modified at any point of time.
Q) Define Transient and Volatile
Ans)
Transient -> The transient modifier applies to variables only, the object or variable will not persist. Transient variables are not serialized.
Volatile -> It tells the compiler a variable may change asynchronously due to threads.
Q) What is the use of Wrapper classes?
Ans) Primitive data types can be converted into objects by using wrapper classes. These are part of java.lang.package.
Q) Define Arguments and Parameters?
Ans) While defining method, variable passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.
Q) Can an application have multiple classes having main() method??
Ans) Yes, it is possible.
While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.
Q) Can we have multiple main methods in the same class?
Ans) No, the program fails to compile.
The compiler says that the main method is already defined in the class.
Q) Static methods cannot access instance variables why?
Ans)
Static methods can be invoked before the object is created;
Instance variables are created only when the new object is created.
So there is no possibility to the static method to access the instance variables.Q) What are differences between Constructor and Method?
Ans)
Constructor:
- Used for instantiating a class
- No return type
- Same name as class
- "this" keyword is used refer another constructor in the same class
- "super" keyword is used to refer Super class constructor
- Will not be inherited to the sub calss
- Can Overload but cannot be Overriden
- Automatically called when an object is created
Method:
- Group of java statements
- Should have a return type. Void is used when nothing to return
- Has a name except the class method name, begin with lower case
- "this" refers by instance of the class
- "super" refers to execute an overridden method in the super class
- Can be inherited to subclass
- needs to be called explicitly
Q) Define Final, Finally, Finalize.
Final -> When we declare a sub class a final the compiler will give error as “cannot subclass final class” Final to prevent inheritance and method overriding. Once to declare a variable as final it cannot occupy memory per instance basis.
- Final class cannot have static methods
- Final class cannot have abstract methods (Because of final class never allows any class to inherit it)
- Final class can have a final method.
Finally -> Finally create a block of code that will be executed after try catch block has completed. Finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement match the exception. Any time a method is about to return to the caller from inside try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also execute.
Using System.exit() in try block will not allow finally code to execute
Finalize -> Some times an object need to perform some actions when it is going to destroy, if an object holding some non-java resource such as file handle (or) window character font, these resources are freed before the object is going to destroy.