Thursday , November 21 2024

Java questions

1. What is JAVA?
Ans. Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

2.How many types of memory areas are allocated by JVM?
Ans. These are the types.
1) Class(Method) Area
2)Heap
3)Stack
4)Program Counter Register
5)Native Method Stack

3. What is the difference between a JDK and a JVM?
Ans. 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 you will not be able to compile your source files using a JVM.

4. What is JIT compiler?
Ans. Just-In-Time(JIT) compiler:It is used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

5. List any five features of Java?
Ans. Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded

6. What is the main difference between Java platform and other platforms?
Ans. The Java platform differs from most other platforms in the sense that it’s a software-based platform that runs on top of other hardware-based platforms.It has two components:

1)Runtime Environment
2)API(Application Programming Interface)

7. What is a pointer and does Java support pointers?
Ans. Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn’t support the usage of pointers.

8. What gives Java its ‘write once and run anywhere’ nature?
Ans. The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.

9. Why Java is considered dynamic?
Ans. It is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

10. List some Java keywords(unlike C, C++ keywords)?
Ans. keywords are import, super, finally, etc.

11. What is classloader?
Ans. The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.

12. Does Java support multiple inheritance?
Ans. Java doesn’t support multiple inheritance.

13. Are arrays primitive data types?
Ans. In Java, Arrays are objects.

14. Define class?
Ans. A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.

15. What is difference between Path and Classpath?
Ans. Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.

16.What do you mean by synchronized Non Access Modifier?
Ans.Java provides these modifiers for providing functionalities other than Access Modifiers, synchronized used to indicate that a method can be accessed by only one thread at a time.

17.According to Java Operator precedence, which operator is considered to be with highest precedence?
Ans.Postfix operators i.e () [] . is at the highest precedence.

18.Why is String class considered immutable?
Ans.The String class is immutable, so that once it is created a String object cannot be changed. Since String is immutable it can safely be shared between many threads ,which is considered very important for multithreaded programming.

19.What is inheritance in java?
Ans.Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.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 parent class, and you can add new methods and fields also.

20.What is difference between object oriented programming language and object based programming language?
Ans.Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.

21.What is the difference between StringBuffer and StringBuilder class?
Ans.Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects.

22.What is super keyword in java?
Ans.The super keyword in java is a reference variable that is used to refer immediate parent class object.Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.

23.Is constructor inherited?
Ans.No, constructor is not inherited.

24.Usage of java super Keyword.
Ans.

  1. super is used to refer immediate parent class instance variable.
  2. super() is used to invoke immediate parent class constructor.
  3. super is used to invoke immediate parent class method.

25.Which package is used for pattern matching with regular expressions?
Ans.java.util.regex package is used for this purpose.

26.What is static block?
Ans.

  • Is used to initialize the static data member.
  • It is excuted before main method at the time of classloading.

27.java.util.regex consists of which classes?
Ans.java.util.regex consists of three classes − Pattern class, Matcher class and PatternSyntaxException class.

28.What is method overloading?
Ans.If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program.

29.Can we overload main() method?
Ans.Yes, You can have many main() methods in a class by overloading the main method.

30.What is finalize() method?
Ans.It is possible to define a method that will be called just before an object’s final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.

31.What do you mean by Checked Exceptions?
0Ans.It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

32.Which class is the superclass for every class.
Ans.Object class.

33.What is immutable object? Can you write immutable object?
Ans.Immutable classes are Java classes whose objects can not be modified once created. Any modification in Immutable object result in new object. For example is String is immutable in Java. Mostly Immutable are also final in Java, in order to prevent sub class from overriding methods in Java which can compromise Immutability. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.

34.Explain Runtime Exceptions?
Ans.It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compliation.

35.What is composition?
Ans.Holding the reference of the other class within some other class is known as composition.

36.When throws keyword is used?
Ans.If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method’s signature.

37.Can you use this() and super() both in a constructor?
Ans.No. Because super() or this() must be the first statement.

38.When throw keyword is used?
Ans.An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.

39.Why we cannot override static method?
Ans.It is because the static method is the part of class and it is bound with class whereas instance method is bound with object and static gets memory in class area and instance gets memory in heap.

40.What is Polymorphism?
Ans.Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

41.Can you have virtual functions in Java?
Ans.Yes, all functions in Java are virtual by default.

42.What is Abstraction?
Ans.It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.

43.What is covariant return type?
Ans.Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type.

44.What is Encapsulation?
Ans.It is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. Therefore encapsulation is also referred to as data hiding.

45.What is blank final variable?
Ans.A final variable, not initalized at the time of declaration, is known as blank final variable.

Leave a Reply