DronaBlog

Tuesday, August 14, 2018

Java Interview Questions and Answers - Part I

 Are you looking for which questions are asked during a Java interview? If so, then refer to this question and answer article on Java and supporting technologies. This article explains all Java related concepts in detail. This is the first article of the Java Interview Questions and Answers series.


Q1: What are the differences between Java and C++?
Answer:

Sr. No.
Java
C++
1
Java does not support pointers. Pointers are inherently tricky to use and troublesome.
C++ supports pointers.
2
Java does not support multiple inheritances because it causes more problems than it solves.
C++ supports multiple inheritances.
3
Java does not support destructors but adds a finalize() method. Finalize methods are called by the garbage collector prior to reclaiming the memory occupied by the object.
C++ supports destructors which are automatically invoked when the object is destroyed.
4
Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework.
C++ includes structures.
5
Java includes automatic garbage collection.
C++ requires explicit memory management.
6
Java has built in support for threads. In Java, there is a Thread class that you inherit to create a new thread and override the run() method.
C++ has no built in support for threads. C++ relies on non-standard third-party libraries for thread support.
7
Pointers, references, and pass-by-value are supported for all types (primitive or user-defined).
All types (primitive types and reference types) are always passed by value.


Q2: Explain the Java Platform.
Answer:
  • It is a software-only platform and it runs on top of other hardware-based platforms like UNIX, NT etc.
  • Java has a set of classes written in the Java language. Such classes are called the Java Application Programming Interface (Java API). It runs on the Java Virtual Machine.
  • Java Virtual Machine (JVM) is a software that is installed on the hardware platforms. JVM uses Byte codes as the machine language.

Q3 : What are the uses of Java packages? 
Answer:
Java package is a namespace. It helps to group a set of related classes and interfaces together. e.g. java.lang package is used to  group classes to the design of the Java programming language. Packages play a significant role in resolving conflicts in class names. 

For example, in the real time world we keep documents in one folder, images are kept in a separate folder and scripts or code are kept in a different folder. Packages keep classes in different packages for better organization of source code and also to resolve conflicts if class names are the same.

In order to create a package for your class use the statement below as the first statement - 
package com.abc.pqr;

Here, package is the keyword in Java and com.abc.pqr is the package name.

If you are going to import any other class then import the package in your class as,
import java.io.*;


Q4: What is Classpath in Java?
Answer: Classpath is a parameter in the Java Virtual Machine. It specifies the location of user-defined classes and packages. It can be set either on the command-line or through an environment variable.

Have you noticed the error below while running the  Java program?
Exception in thread "main" java.lang.NoClassDefFoundError: com/abc/pqr/MyWorld

If so, then you have not set the classpath in your system. To resolve this issue you can use one of the approaches below -

1. Set your project in the CLASSPATH environment variable of your system. e.g. "c:/TestProject"
2. Set the jar file of your project in the CLASSPATH environment variable of your system. This jar file should contain your .class file.to have a jar file e.g. we need to set the "c:/TestProject/HelloWorld.jar"" jar file in CLASSPATH and this .jar file has the MyWorld.class file in it. 
3. Run it with –cp or –classpath commands as shown below:
c:\>java –cp c:/TestProject com.abc.pqr.MyWorld
OR
c:\>java -classpath c:/TestProject/HelloWorld.jar com.abc.pqr.MyWorld


Q5: What are the advantages of the Object Oriented Approach?
Answer: Java is the Object Oriented Language and it comes with the benefits mentioned below due to its Object Oriented approach:
  1. We can achieve code re-usability with help of implementation inheritance and object composition.
  2. Everything in Java is an object and it maps to the real world. E.g vehicles, customers
  3. It helps to create modular architecture with the help of objects, systems, frameworks etc which are the building blocks of the big application.
  4. An Object Oriented Program forces designers to go through an extensive planning phase, which makes for better designs with less flaws.
  5. An Object Oriented Program is much easier to modify and maintain than a non-Object Oriented Program. 



No comments:

Post a Comment

Please do not enter any spam link in the comment box.

What is CRM system?

  In the digital age, where customer-centricity reigns supreme, businesses are increasingly turning to advanced technologies to manage and n...