Core Java Interview Questions and Answers | OOPS Interview Questions and Answers

Core Java  Interview Questions

1) What is the most important feature of java?



A)Java is a platform independent language.

2) What do you mean by platform independent?

A)platform independent means that we can write and compile the java code in one platform (example windows) and can execute the class in any other supported platform example (Linux, Solaris).

3) What is a JVM?

A) JVM is Java Virtual Machine which is a run time environment for the compiled java class files.

4) JVM is platform independent?

A) JVM is not platform independent. JVM is platform specific run time implementation provided by the vendor.

5) What is the difference between a JDK and a JVM?

A) JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.

6) What is the latest version of Java?

A) Java SE 10.

7) What is a JIT compiler?

A) JIT compiler stands for a just-in-time compiler and is a part of JVM which increases the speed of execution of a java program.

8) What is the base class of all java classes?

A) java.lang.Object is the base class of all java classes.

9) Does Java support multiple inheritance?

A) Java doesn’t support multiple inheritances with classes.

10) Are arrays primitive data types?

A)   In Java, arrays are referenced types.

11) Is Java pure object-oriented?

A)   99.99% java is purely object-oriented.

12) What is the difference between Path and Classpath?

A)   Path and Classpath are operating system level environment variables. The path is used to define where the system can find the executable (.exe) files and Classpath is used to specify the location .class files.

13) What are the local variables?

A)   If we are defining any variable inside a method (or) block (or) constructor() then that variable is called as local variables. Before using the local variable must provide the initialization.

14) What are Instance variables?

A)    Instance variables are those which are defined at the class level. Instance variables need not to be initialized before using them are automatically initialized with their default values. JVM will provide the default initialization for Instance variables.

15) What is Bytecode?

A) Byte code is the compiled code of a Java program. Byte means the name of the format and codes means the group of statements.

                                     (or)

A) The group of statements representing a Java application in the middle level using byte format is known as Byte code.

16) Should a main() method be compulsory declared in all java classes?

A)    No, not required. The main() method should be defined only if the source class is a java application.

17) What is the return type of main() method?

  A)  The main () method doesn’t return anything hence declared as void.

18) Why is the main() method declared as static?

  A) Main() method is called by the JVM even before the instantiation of the class hence it is declared as static.

19) What is the argument of the main() method?

  A) main() method accepts an array of String Object as command line arguments.

20) Can a main() method be overloaded?

  A) Yes. We can define any number of main() methods with different method signature and implementation in the class.

21) What is an object?
A) An object is the instance of a class. An object represents data of a class.
                (or)
A) The memory space allocated for the member of the class (where all the members of a class would have been loaded) dynamically at run time is known as an object.
22) What is Reference variable?
A) Reference is a variable representing the address of the object.

23) Can a source file contain more than one class declaration?

A)    Yes, a single source file can contain any number of class declaration but only one of the classes can be declared as public.

24) What is “.class”?

A) “.class” would be the extension given to a file which contains byte code.

25)  Which package is imported by default?

A)  java.lang package is imported by default even an import statement.


26) Can a class declare as private be accessed outside of its package?

A) Not possible. But outer classes are not possible to declare with private, only inner classes are allowed to declare with private.


27) What are the methods in Object?

A) clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString,wait(),wait(long timeout),wait(long timeout,intnanos).


28) What is the default import statement in Java?

A) java.lang.*; is automatically imported into all Java programs.

29) What is the default of an object reference declared as an instance variable?

A) Null unless we define it explicitly.

30) Can a top level class be private or protected?

A) No, a top-level class is not private or protected. It can have either “public” or no modifier. If it does not have a modifier it is supposed to have default access. If a top-level class is declared as private the compilers will that “the modifier private is not allowed here”. This means that a top-level class cannot be private. Same is the case with protected.

31) What Type of parameter passing does Java support?

A) In Java, the arguments are always passed by value.


32) Primitive data types are passed by reference or by value?

A) Primitive data types are passed by value.


33) Objects are passed by value or by reference?

A) Java supports only pass by value. With Objects, the object reference itself is passed by value and so both the original reference and parameter copy refer to the same object.


34) Is Java a pure object oriented language?

A) Java is a pure object-oriented language. Except for the primitives, everything else objects in Java.


35) Is default a keyword?

A) Yes. The default is a keyword but it is associated with a switch statement and not with access specifiers.

36) What is the default value of the local variables?


 A) The local variables are not initialized to any default value. It is initialized to either primitives or object references.


37) What is the static variable?

 A) A static variable is used to refer to the common property of all objects. Static variable gets memory only once in the class area at the time of class loading.

38) What is the final variable?

A) If you make any variable as final, you cannot change the value of that variable.

39) What is a transient keyword?


A) If you define any data member as transient it will not be serialized.

40) What is the modifier not allowed for a method?

A) Except transient and volatile all other 9 modifiers are allowed.


41) What is static loading?

A) The concept of allocating the memory (in the Ram) and loading the functionalities & data to the RAM before the execution of the program starts or before the function call is made is known as static loading.

42) What is Dynamic loading?

A) The concept of allocating the memory and loading the functionalities to the RAM at the run time, dynamically when the function call is made (or) when the program is under execution is known as Dynamic loading.

43) What if I write static public void instead of public static void?

A) The program compiles and runs properly.


44) What if I do not provide the String array as the argument to the method?
 A) The program compiles but throws a runtime error “NoSuchMethodError”.

45) What is the first argument of the String array in the main method?

 A) The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.


46) Can I have multiple main methods in the same class?

 A) No, the program fails to compile. The compiler says that the main method is already defined in the class.


47) Do I need to import java.lang package any time?

A) No. It is by default loaded internally by the JVM.


48) What is the difference between Static loading and Dynamic loading?

A)   
Static loading: 
1) Static loading increases the overhead on the operating system.
2) Structured programming language programs work on the concept of static loading.

Dynamic loading:
1) Dynamic loading reduces the overhead on the operating system.
2) Object-oriented language like java work on the concept of Dynamic loading.

49) What is the access scope of a protected method?

A) A protected method can be accessed by the classes with in the same package or by the subclasses of the class in any package.

50) What is the impact of declaring a method as final?

A) A method declared as final can’t be overridden. A sub-class can’t have the same method signature with a different implementation.


51) Can there be an abstract class with no abstract methods in it?

A) Yes.


52) Can we execute a program without a main() method?
A) Yes, one of the ways is static block but in the previous version of JDK not in jdk1.7 onwards
Class A


{


static
 {
System.out.println(“static block invoked”);
System.exit(0);
  }

53) Explain the processes performed by the Java virtual machine?

A) The javap is a standard .class file for printer or disassemble. This file converts the description of .class into the original source code.


54) What is the main difference between the Java platform and other platforms?

A) The Java platform differs from most other platforms by its software-based platform that runs on top of other hardware-based platforms. It has two components.
1. Runtime Environment
2. API (Application Program Interface)

55) What gives java it's written once and run anywhere nature?


A) The byte code .java is compiled as a byte code which acts as an intermediate language between source code and machine code. This byte code is not platform specific and hence can run in any platform.

56) What happens if I write static public void instead of public static void?

A) Programs compile and run properly.

57) What is the default value of the local variables?

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


58) How to define a constant variable in Java?

A) The variable should be declared as static and final so only once the copy of the variables exists for all instances of the class and the value can’t be changed. Static final int a = 2; is an example for constant.


59)  What is a package?

A)  A package is a collection of related classes and interfaces. Package declaration should be the first statement in a java class.


60) What if the main method is declared as private?

A) The program compiles properly but at runtime, it will give a message “Main method not public”.


61) What if the static modifier is removed from the signature of the main method?

A) Program compiles. But at runtime throws an error “NoSuchMethodError”.


62)  What is a native method?

 A) A native method is a method that is implemented in a language other than Java.


63) Why we cannot see the executable code in java once the execution of the Java application is completed?

A) The executable code of any Java function would be persistent as long as the program is under execution. Once the execution of the program is completed automatically the execution of that functionalities would be destroyed. So at any given point of time, we can’t see the executable code in java.

64) Why Java is platform independent while JVM is platform independent?

A) Since compiled code of a Java application executed on any machine irrespective of the operating system under which the machine is working, we can say Java is platform Independent.

65) What is “rt.jar” file?

A) “rt.jar” file contains all the basic and fundamentals java libraries (like java.lang, java.io, java.utiletc) which are required for developing any Java application in general.

66)Can a main() method be overloaded?

A) Yes. We can define any number of main() methods with different method signature and implementation in the class.

67) Can a main() method be declared as final?


A)  Yes. Any inheriting class cannot have it’s own default main() method.

68) What is Reference variable?

A) Reference is a variable representing the address of the object.
69) What is the difference between Reference and Object?
A) Reference represents the only address of the object but object represents the data of a class.
70) What is the default initialization?
A) The concept of JVM automatically initializing uninitialized variables with default values is known as default initialization. Default initialization is applicable only to the instance and static variables.
71) When do we get a compilation error because of variables?
A) Using uninitialized variables leads for a compilation error in Java.
72) What is Typecasting?
A) The concept of converting the value of one data type into its equivalent & corresponding value of another data type is known as Typecasting.
73) What are different types of Typecasting?
A) There are "2" types of Typecasting:
1) Implicit Type Casting 2) Explicit Type Casting
74) What is Implicit Typecasting?
A) The concept of JVM automatically converting a value of one data type into its equivalent & corresponding value of another data type by itself that is without user concern is known as Implicit type casting.
75) What is Explicit Typecasting?
A) The concept of JVM converting a value of one data type into its equivalent & corresponding value of another data type explicitly according to user specifications is known as Explicit typecasting.
76) What is Hash code?
A) Hash code is nothing but the Index of the address of the object.
77) What is available to the static method?
A) To the static method, only static members of the same class are directly available and non-static members are not available directly. In order to access non-static members from the static functions, we need object without object we cannot access non-static members from the static functions.
78) What is available to the non-static method?
A) To the non-static method, both static and non-static members of the same class are directly available (because to the non-static method, the object is directly available and context would be available to the object). Thus from the non-static method, we can access both static & non-static members of the same class directly.
79) What is polymorphism?
A) The concept of defining multiple functionalities with the same name associated with the same object could be called as polymorphism.

80) What are two types of polymorphism?
A) The two types of polymorphism are:
1) Static polymorphism / Compile time polymorphism.
2) Dynamic polymorphism / Runtime polymorphism. 

81) What is Static polymorphism?
A) The concept of defining multiple functionalities with the same name within the same class by changing the data types of the parameters is known as static polymorphism.
82) What happens in static polymorphism at the time compilation?
A) In static polymorphism, out of multiple methods with the same name which method has to get executed would be decided at the time of compilation itself. Thus we call that has static (or) compile time polymorphism.
83) What happens in dynamic polymorphism?
A) In dynamic polymorphism, out of multiple methods with the same name & with same data types of the parameters which function (or) method has to get executed would be decided dynamically at the run-time based on the run-time conditions. Thus we call that kind of polymorphism has dynamic (or) run-time polymorphism.
84) What is a pointer and does Java support pointers?

A) The pointer is a reference to handle a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn’t support the usage of pointers.


Also Read: Java strings interview questions Java collections interview questions Java exception handling interview questions Java multithreading interview questions Java io streams interview questions oops Java interview questions Factorial program in Java Java constructor interview questions
Share:

Search This Blog

  • ()

Our Services

Powered by Blogger.

Netflix codes - Find all secret Netflix codes that unlock tons of hidden movies.

Netflix codes - Find all secret Netflix codes that unlock tons of hidden movies. As all Netflix clients know, the gushing admin...