Questions and Answers

Java Features?

Java is one the most famous and most used language in the real world, there are many features in Java that makes it better than any other language some of them are mentioned below:

Features-of-Java-768

  • Simple: Java is quite simple to understand and the syntax
  • Platform Independent: Java is platform independent means we can run the same program in any software and hardware and will get the same result.
  • Interpreted: Java is interpreted as well as a compiler-based language. 
  • Robust: features like Garbage collection, exception handling, etc that make the language robust.
  • Object-Oriented: Java is an object-oriented language that supports the concepts of class,  objects, four pillars of OOPS, etc. 
  • Secured: As we can directly share an application with the user without sharing the actual program makes Java a secure language. 
  • Dynamic: supports dynamic loading of classes and interfaces.
  • Distributed: feature of Java makes us able to access files by calling the methods from any machine connected.
  • Multithreaded: deal with multiple tasks at once by defining multiple threads
  • Architecture Neutral: it is not dependent on the architecture.

What are Classes in Java? 

In Java, Classes are the collection of objects sharing similar characteristics and attributes. Classes represent the blueprint or template from which objects are created.  Classes are not real-world entities but help us to create objects which are real-world entities.

Explain public static void main(String args[]) in Java.

Main_function

Unlike any other programming language like C, C++, etc. In Java, we declared the main function as a public static void main (String args[]). The meanings of the terms are mentioned below:

  1. public: the public is the access modifier responsible for mentioning who can access the element or the method and what is the limit.  It is responsible for making the main function globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.
  2. static: static is a keyword used so that we can use the element without initiating the class so to avoid the unnecessary allocation of the memory. 
  3. void: void is a keyword and is used to specify that a method doesn’t return anything. As the main function doesn’t return anything we use void.
  4. main: main represents that the function declared is the main function. It helps JVM to identify that the declared function is the main function.
  5. String args[]: It stores Java command-line arguments and is an array of type java.lang.String class.

What is Java String Pool?

A Java String Pool is a place in heap memory where all the strings defined in the program are stored. A separate place in a stack is there where the variable storing the string is stored.

Java-String-Pool-768

How many types of packages are there in Java?

There are two types of packages in Java

  • User-defined packages
  • Build In packages

Differentiate between instance and local variables.

Instance Variable

Local Variable

Declared outside the method, directly invoked by the method.

Declared within the method.                                                              

Has a default value.

No default value

It can be used throughout the class.

The scope is limited to the method.

What is the difference between static (class) method and instance method?

Static(Class) method

Instance method

Static method is associated with a class rather than an object.

The instance method is associated with an object rather than a class.

Static methods can be called using the class name only without creating an instance of a class.

The instance methods can be called on a specific instance of a class using the object reference.

Static methods do not have access to this keyword.

Instance methods have access to this keyword.

Static methods can access only static members of the class.

Instance methods can access both static and non-static methods of the class.

What is a Class Variable?

In Java, a class variable (also known as a static variable) is a variable that is declared within a class but outside of any method, constructor, or block. Class variables are declared with the static keyword, and they are shared by all instances (objects) of the class as well as by the class itself.

What is a static variable?

The static keyword is used to share the same variable or method of a given class. Static variables are the variables that once declared then a single copy of the variable is created and shared among all objects at the class level.

What are the differences between String and StringBuffer?

String

StringBuffer

Store of a sequence of characters.            Provides functionality to work with the strings.
It is immutable.It is mutable (can be modified and other string operations could be performed on them.)
No thread operations in a string.                                                           It is thread-safe (two threads can’t call the methods of StringBuffer simultaneously) 

What is “this”keyword in Java?

‘this’ is a keyword used to reference a variable that refers to the current object.

What is the constructor?

Constructor is a special method that is used to initialize objects. Constructor is called when a object is created. The name of constructor is same as of the class.

How many types of constructors are used in Java?

There are two types of constructors in Java as mentioned below:

  1. Default Constructor
  2. Parameterized Constructor
Default Constructor:

It is the type that does not accept any parameter value. It is used to set initial values for object attributes.

class_Name();
// Default constructor called
Parameterized Constructor:

It is the type of constructor that accepts parameters as arguments. These are used to assign values to instance variables during the initialization of objects.

class_Name(parameter1, parameter2......);
// All the values passed as parameter will be
// allocated accordingly

What is the purpose of a default constructor?

Constructors help to create instances of a class or can be said to create objects of a class. Constructor is called during the initialization of objects. A default constructor is a type of constructor which do not accept any parameter, So whatever value is assigned to properties of the objects are considered default values.

Define Polymorphism in Java.

The word polymorphism means having many forms.  Java Polymorphism is the ability of a message to be displayed in more than one form

Advantages of Polymorphism in Java

  1. Increases code reusability by allowing objects of different classes to be treated as objects of a common class.
  2. Improves readability and maintainability of code by reducing the amount of code that needs to be written and maintained.
  3. Supports dynamic binding, enabling the correct method to be called at runtime, based on the actual class of the object.
  4. Enables objects to be treated as a single type, making it easier to write generic code that can handle objects of different types.

Disadvantages of Polymorphism in Java

  1. Can make it more difficult to understand the behavior of an object, especially if the code is complex.
  2. This may lead to performance issues, as polymorphic behavior may require additional computations at runtime.

Define Overriding in Java

When a method in a subclass has the same name, the same parameters or signature, and the same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

Define Inheritance in Java

It is a mechanism in which one object acquires all the properties and behaviors of a parent object.

syntax of Java Inheritance

 

 

class Subclass-name extends Superclass-name  
{   
  //methods and fields  
}  

Types of inheritance in java

Define Java Package.

A java package is a group of similar types of classes, interfaces and sub-packages.

It can be categorized in two form,
1) Built-in package
2) User-defined package.

Example for built-in packages – as     java,    lang,    awt,   javax,    swing,   net,   io,   util,   sql etc.

Advantage of Java Package

1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

PHP Code Snippets Powered By : XYZScripts.com