Frequently Asked Questions in Core Java - Part 1

This page will contain some of the frequently asked questions. 

1. Is Java platform independent? Explain.

Yes. Refer to the article features-of-java.

2. Is Java a compiled language or interpreted Language?

Java is a compiled interpreted language. Refer to the article java-is-a-compiled-and-interpreted-language

3. What is the difference between path and classpath?

Please refer to the article path-vs-classpath.

4. Explain the object oriented principles with examples?

Object oriented principles are Encapsulation, Inheritance, Polymorphism and Abstraction. Refer to the article object-oriented-programming-oop-concepts-with-examples.

5. Which all access modifiers can be applied to a top level class in java?

A regular top level class can be public or default (without any modifiers). A top level class is one that is directly under the package (including default package) and does not come inside any class. A class that comes inside another class is called an inner class and can have any access modifier a class member can have.

6. Why regular top level class cannot be private or protected?

 A regular top level class cannot be private or protected mainly because there is no need to do so.

  • Protected is for making a class member available to sub classes. Since we cannot  sub package there is no need for making a top level class protected.
  • Similarly private is for making a class member hidden from other classes. If a top level class is completely hidden, no one will be able to access it and hence we won't be able to use it in any way.

7. Can we have more than one main method in a class?

Yes,  we have more than one main method in our class through overloading just like any other method. The main method is just like any other method, but the signature public static void main(String args[]) is the only one known to the JVM and hence it looks for a method with that signature only. We should call the other main methods ourselves just like any other method.

8. What is the significance of public, static, void and String args[] in main method signature?

The signature of main method, which is the entry point to a java application, is public static void main(String args[])

  • public - public is the least restricted access modifier. Because jvm, which is outside our program will have to invoke main, we need to give public.
  • static - static methods and variables belong to class and be called without creating object using just the class name. Making main method as static allowes JVM to invoke main without creating an object of your class.
  • void - as we are not returning anything back to JVM, we specify the return type as void.
  • String args[] - This String array parameter is for accepting any command line arguments if passed. (Note: The name doesn't matter here.)

9. What are Command Line Arguments?

Command Line Arguments are Arguments passed through command line. Command line is the line in which you type a command in DOS or a shell prompt and is not specific to java. In Java we can pass command line arguments to a program as:

java MyClass arg1 arg2 arg3

The String array args[] (name doesn’t matter) in the main method will have these populated.

10. Are static methods inherited?

Yes, static methods are inherited, but they are NOT polymorphic, or in simple terms, they are NOT overriden, unlike instance methods which are polymorphic and overriden. 

Through polymorphism, we mean overriding. In java, only instance methods are overridden. Static methods, static variables and instance variables are just re-declared (hiding parent method) in the child class, but not overridden.

11. Are default variables and methods inherited?

Default variables and methods are inherited only if both parent and child are in same package.

12. How can you use annotations to make sure you are doing a valid override?

In java 5 and above, we can confirm whether we are doing valid override by using the @Override annotation above the subclass's method. If not valid override, compiler will throw error.

For example, compiler will always throw error when @override is given over static methods in subclasses even though the name and signature matches exactly with the parent method. This is because static methods are just re-declared (hiding parent method) in the child class, but not overridden.

13. What is RTTI in Java?

RTTI stands for runtime type identification. RTTI lets you find the exact type of an object at runtime, for example when you have a base type reference referring to a subclass object.

Two common forms of RTTI are:

  • JVM’s cast verification when a cast operation is used and
  • During the use of instanceOf operator.

However these two requires that you have all the types available at compile-time and run-time.

Reflection is another form of runtime type identification (RTTI) which allows you to discover class information solely at run-time.

14. Differentiate between JDK and JRE?

JDK stands for Java Development Kit, contains javac.exe and is used for compiling java source. JRE stands for Java Runtime Environment, contains java.exe and is used for executing java programs. Refer to jdk-jre-jvm-and-your-first-java-program

15. Explain initialization blocks in java?

Initialization blocks are blocks called automatically by java for initializing a class similar to constructors. Refer to blocks-and-methods-in-java.

16. How can you set and get system properties?

We can use java -D to set a system property. System properties consist of name=value pairs and must be appended directly behind the -D, for example,

java -Dmyproperty=myvalue.

Now you can get it from within the code as

System.getProperty("myproperty"); or

System.getProperties().getProperty("myproperty");

Comments

Heartin,

Could you please explain reflection with one example? When it will be used and its advantages

 

Thanks in advance

Chinnu

Was it useful?

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (53) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (8) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (12) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)