Java Short Question and Answers – 1

1 )Explain the reason that the main method in a Java application must be   declared static.

Ans: )The keyword static indicates that the method is a class method which can be called without the requirement to instantiate an object of the class. This is used by the Java interpreter to launch the program by invoking the main method of the class identified in the command to start the program.


2 ) Describe the purpose of the keyword void when used as the return type for the main method.

Ans ) The void keyword when used as the return type for any Java methods indicates that the method does not return anything.


3) What is OOPS?
Answer: OOP is the common abbreviation for Object-Oriented Programming.


4) Describe the principles of OOPS.
Answer: There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.

5) What is Collection API?
Answer: The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.
Example of classes:
HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces:
Collection, Set, List and Map.


6 ) What’s the difference between constructors and other methods?

Ans: ) Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.


7 ) When using command-line arguments in Java, if the name of the string array is args, the args.length variable can be used by the code in the program to determine the number of arguments actually entered: True or False?

Ans: ) True


8 ) As in C++, the first string in the array of command-line arguments contains the name of the Java application: True or False?

Ans: ) False. Unlike C++, the first string in the array of command-line arguments in a Java application does not contain the name of the application.


9 ) The controlling class of every Java application must contain a main method. Can other classes in the same application also have a main method? If not, why not? If so, why might you want to do this?

Ans: ) Yes. It is often desirable to provide a main method for a class that will not ultimately be the controlling class in an application to allow the class to be tested in a stand-alone mode, independent of any other classes. The program actually makes use of the argument.


10) Explain JVM

Ans: ) A Java Virtual Machine is a runtime environment required for execution of a Java application.Every Java application runs inside a runtime instance of some concrete implementation of abstract specifications of JVM.It is JVM which is crux of ‘platform independent’ nature of the language


11) What is the signature of a method?

Ans: )A method signature is a collection of information about the method, as in a C prototype or a forward function declaration in other languages. It includes the method’s name, type, and visibility, as well as its arguments and return type.”


12 ) what is mean by String and StringBuffer?

Ans) String is a final class which represents a character string, string are constant they value cannot be changed after they r created its is final class Stringbuffer, Stringbuffer implemetsmuttable String character, its is like a string but can be modified, the length and content can be changed during method call.


13 ) Explain the Encapsulation principle.
Ans ) Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.


14 ) Explain the Inheritance principle.
Ans: ) Inheritance is the process by which one object acquires the properties of another object.


15) Explain the Polymorphism principle .
Ans) The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as “one interface, multiple methods”.


16 ) Explain the different forms of Polymorphism .
Ans: ) From a practical programming viewpoint, polymorphism exists in three distinct forms in Java:

  • Method overloading
  • Method overriding through inheritance
  • Method overriding through the Java interface

17) What are Access Specifiers available in Java?
Ans: ) Access specifiers are keywords that determines the type of access to the member of a class. These are:

  • Public
  • Protected
  • Private
  • Defaults

18 ) What is a Vector?

Ans: ) Vector is a grow able array of objects.


19 ) What’s the main difference between a Vector and an ArrayList

Ans: )  Java Vector class is internally synchronized and ArrayList is not.


20 ) What are different Java declarations and their associated rules?
Ans: ) All variables in Java are introduced/declared with some basic datatypes with some basic values, e.g. every decimal value by default is a
double.The names of variables must avoid reserved Java keywords.The local variables are explicitly initialized.

21 ) Can protected or friendly features be accessed from different  packages?
Ans: ) No,when features are friendly or protected they can be accessed from all the classes in that package but not from classes in another package.


22) How to define an Abstract class?
Ans: ) A class containing abstract method is called Abstract class. An Abstract class can’t be instantiated.
Example of Abstract class:
abstract class testAbstractClass {
protected String myString;
public String getMyString(){
return myString;
}
public abstract string anyAbstractFunction();
}


23 ) How to define an Interface?
Ans: ) In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.

Emaple of Interface:

public interface sampleInterface {
public void functionOne();

public long CONSTANT_ONE = 1000;
}

24) Explain the user defined Exceptions?
Ans: ) User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.

Example:
class myCustomException extends Exception {
// The class simply has to exist to be an exception
}


25) How you can force the garbage collection?
Ans: )Garbage collection is automatic process and can’t be forced.

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.