Gadgets

Thursday, 18 February 2016

Java language fundamentals

Every java programer must know the java language fundamentals. Here I have given java language fundamentals frequent asking questions. Java Language fundamentals contain basic topics of java.
1) What is java Identifier?
 A name in java program is called identifier, which can use for identification purpose. It can be class name, method name, variable name or label name.
class Test {
            public static void main(String arg[])  {
                int number=10;
            }
} 
Identifiers are Test, main, String, arg, number.

2) What are the rules to define java identifier?
Ø  Java identifier allowed a-z, A-Z, 0-9, _ and $.
Ø  Identifiers are case sensitive.
Ø  Identifiers should not start with number 0-9.
Ø  No length limit for identifiers.
Ø  We can’t use reserved words as identifiers.
Ø  We can use java predefined class names and interface to identifiers.

3) What are the various reserved words in java?
Java contains total 53 reserved words, in that 3 reserved literals (true, false, null) and remain 50 keywords.
Data type keywords-8, flow-control keywords-11, modifier keywords-11, exception handling keywords-6, class related keywords-6, object related keywords-4 and enum, void, goto and const.

4) What are the various modifiers in java?
Java contains total 11 modifiers.
Public, private, protected, final, abstract, synchronized, native, static, strictfp, transient and volatile.

5) What is use of assert keyword?
Assert keyword introduced in JDK-1.4 version, assert keyword is used to debug the code.

6) What is the use of enum keyword?
We can define a group of named constants by using enum keyword.

7) What are the various primitive data types in java?
There are two types of primitive data types in java
Numeric data types (integral, floating types, Boolean and char) and non-numeric types (String, Array).

8) Why java char is 2bytes where as C & C++ char size 1byte?
Java is Unicode based, Unicode characters are >256 to <=65536. If we want represent Unicode character then we should use 16bits (2 bytes).
C & C++ are ASCII code based, ASCII characters are <=256 so we can define ASCII in 8bits (1 byte).

9) What is literal?
Literal is value, which can be assigned to the variables.
            int number=10; // Here 10 is literal
10) How many types of variables in java?
There are 3 types variables in java
Local Variables is declared inside method.
Static Variables is declared as static and it can’t be local.
Instance Variables is declared inside class and outside method.

11) What is the var-arg method?
We can declare any number of arguments by using var-arg method. We can declare var-arg method as int... x, int...x and int ...x
public Test {
static void display(int... x) {
for(int s:x){
System.out.println(s);
}
}
public static void main(String arg[])
 {
display(10);
display(10,20,30);
display(10,20);

}

12) Which method will get high priority normal method or var-arg method?
If we declare normal method and var-arg method first priority will get normal method.

13) What is the main() method signature?
            public static void main(String arg[]) 
public keyword is an access modifier, that mean other classes can access.
static is a keyword. If we declare a method as static, no need to create object for that method.
void is return type. It does not return any value.
“main” is a method name. It represents startup of the program.

14) If a class doesn’t contain main() method then what will happen?
If a class doesn’t contain main() method then runtime error will occur.
// Error: Main method not found in class Test, please define the main method as: public static void main(String[] args)
15) What is the System.out.println()?
System.out.println() is used to display the output to the user on the console.
System is a class in the java.lang package.
“out” is a static member of the System class
“println” is a method of the java.io.PrintStream

16) Is it possible to overload main() method?
We can overload the main() method but JVM will always call main(String arg[]) onyl.
public static void main(String arg[]) // JVM always call String arg[] method
 {
 }
public static void main(Integer arg[])
 {
}

17) Is it possible to override main() method?
We can override main() method. Here we call it is Method hiding, not override.
public class Test {
public static void main(String arg[]) {
    System.out.println("parent");
   }
}
class Exam extends Test {
public static void main(String arg[]) {
    System.out.println("child");
   }
}

18) What are the java coding standards?
// first write our package given format
package com.vkinst.branch;
// import required package
import java.io.*; 
// class name start with capital latter and 2nd word start with capital
public class StudentList   {
// variable name start with small latter and 2nd word start with capital and name should be meaningful
            int studentId;       
            String studentName;
            public void diplayList() {
// method name start with small latter and 2nd word start with capital and name should be meaningful
            }
 }
19) What are the java bean coding standards?
Java bean is simple java class, it contain private properties and public getter, setter methods.
20) What is command line argument?
The arguments which are passing by the command prompt is called command line argument.

No comments:

Post a Comment