|
Java Interview Questions & Answers
Part 2- Identifiers, Keywords, Variables, Data Types and Arrays
What are keywords?
Keywords cannot be used as identifiers.
Which of the below are not keywords?
default
Ans :
What are reserved keywords? And can you name few reserved keywords?
Few of the words are reserved as keywords for future usage. Compiler forces the developer not to use a reserved keyword. const and
What is the overhead of introducing a new keyword?
When a new keyword is used in a new version of the JDK, there is high chances that has been used by developers as identifiers. This make tougher for the code base to migrate to new version since it requires code change, recompilation,testing and release cycle.
Have you come across difficulties due to introduction of a new keyword?
Yes. enum keyword was used extensively as identifier in one of our project - we had to change the code in a lot of places to migrate it to newer v ersion.
What are identifiers?
Identifiers are names given to a variable, method, class and interface. Identifiers must conform to the following rules:
a. The identifiers can contain a to z, A to Z,0 to 9,_ and $.
b. Special characters other than _ and $ cannot be used in identifiers.
c. Identifiers cannot start with numbers.
d. keywords cannot be used as identifiers.
What is meant by naming conventions? And what are the naming conventions followed in Java?
Naming conventions are part of coding standards which are prescribed for better readability and maintenance. The following are simple conventions followed in Java:
1. Instance and local Variables should start with a lowercase and subsequent word should start with a capital letter. Examples:
int quantity;
double unitPrice;
2. Class level variables ie constants should be in capital letters and _ is used word seprator
final static double PI = 3.14;
final static int MAX_PRIORITY = 10;
3. Method names should start with small case and subsequent word should be capital letter.
public double getAvailableBalance(String accountNo) throws InvalidAccountException{}
4. Classes and Interfaces should start with a capital letter and subsequent words should also be capital letters.
public class HelloWorld{}
If you dont follow coding standards, will it result in compilation error?
No. These are standards. Each company or fot that matter each software unit might have its own coding standards. These are not enforced by the compiler.
How to make sure that all programmers are following the coding standards?
The best and simple way is to do peer reviews and code walkthroughs. You can use external plugins to your IDE (integrated development environment) to enforce during coding itself.
What are literals?
Literals are source code representation of primitive data types.
Name the eight data types which are available in Java?
boolean, byte, short, int, long, float, double and char.
Are primitive data types objects in Java?
Primitive data types are not objects.
What are all the number data types? And are they signed or unsigned?
Excpet boolean and char, others are number data types and they are all signed which means that they can hold both positive and negative values.
What are the possible values that a boolean data type can hold?
The boolean is the simplest data type which can hold true or false.
What are default values?
Values which are defaulted during object initialisation are called default values. Each data type has a default value.
What are the default values of primitive data types?
For boolean data type, it is false. For byte,short,int and long, it is 0. For float and double, it is 0.0. For char, the default value is '\u000'.
Are object references defaulted?
Yes. Object references are defaulted to null.
Are arrays defaulted?
If arrays is just declared but not initialised then the array reference will be defaulted to null. This is because arrays are objects in Java.
int arr[]; // Here the arr reference is defaulted to null.
If array values are not assigned, then will be defaulted to their respective default values.
double priceRange[] = new double[3]; // Here all the elements in the array will be defaulted to 0.0 - the default value of double.
String str[] = new String[3]; // Here all the elements will be defaulted to null - the default value for object references.
Can you explain keyword , identifier and literal with an example?
Consider the below statement:
int i = 10;
Here int is the keyword which has special meaning attached Java programming langauge ie whatever is declared is an integer value.
i is the identifier or the variable name.
10 is the literal or the actual value.
What are the 3 ways to represent an integer value?
The following are the 3 different ways to represent an integer value:
int i = 123 // this is the usual decimal representation.
int i = 0123 // this is octal representation. Octal values start with a zero.
int i = 0XCAAD // this is hexadecimal representation. Hexadecimal values start with 0X.
In how many ways a char value be represented?
Char value can be represented in 3 ways. They are as follows:
char ch = 'A'; // represented using single quotes.
char ch = '\u0041'; // represented using unicode representation.
char ch = 41; // represented using integer value.
How is it possible to represent char using a integer value?
char is internally represented as a unsigned 16 bit integer value ie it will accept integer values from 0 to 65536.
Can char be used when an integer value is expected?
Yes. A fine example is switch statement will accept char value for multiway condition checking.
Can char be manipulated like integers?
Yes possible. The below is an example.
char ch = 'A';
System.out.println(ch++);
The above statement will print B. ++ is a numeral operand and since char is internally represented as integer, ++ operand can be applied on char value.
What is Unicode?
How many languages are supported by the unicode?
What should i have to do if i have to print my name in Hindi?
How will the below literal value be internally represented?
float f = 21.22;
Ans: It will be represented as a double value. Floating point literals are always double by default. If you want a float, you must append an F or f to the literal.
Give your observation on the below statement.
int i = 10/0;
Ans: The statement will result in RuntimeException (DivideByZeroException). Integer values cannot be divided by zero.
Give your observation on the below statement.
double d = 10.12/0;
Ans : This will compile and execute fine. The result will be ...
What is a Variable?
What are the 3 types of variables?
There are 3 types of variables in Java. They are :
1. Local variables.
2. Instance variables.
3. Class variables.
What are Local variables?
Local varaiables are those which are declared within a block of code like methods, loops, exception blocks, etc. Local variables should be initialised before accessing them. Local variables are stored on the stack, hence they are sometimes called stack variables. They are also called as method variables or block variables.
When are local variables eligible for garbage collection?
As soon as the block is completed the variables are eligible for GC. Block could be a condition, a loop, a exception block or a method
Consider the below class:
public class Test {
public static void main (String str[]){
String name = str[0];
for (int i=0; i<=10; i++){
System.out.println(name + i);
}
}
}
In the above the class, the variable i is eligible for garbage collection immediately after the completion of for loop.
name string variable and str[] argument are eligible for GC after the completion of main method.
What are Instance variables?
Instance variables are those which are defined at the class level. As we know, object have identity and behaviour - the identity is provided by the instance variables. These are also called as object variables. Instance variables need not be initialized before using them. Instance variables will be initialized to their default values automatically when not initialized.
Are arrays primitive data types?
No. Arrays are not primitives - they are objects.
What are different ways to declare an array?
Object obj[]; // most frequently used form of array declaration.
Object []obj; // nothing wrong with this.
Object[] obj; // nothing wrong with this.
int i[][]; // most frequently form multi-dimensional array
int[] i[]; // nothing wrong with this.
int[] i[],j; // nothing wrong with this. Important : i is double dimensional whereas j is single dimensional. ***** Need to be verified.
How many array dimensions does Java support?
What are the 3 steps in defining arrays?
declaration,initialization and assignment
int i[]; // array declaration.
i[] = new int[2]; // array initialization.
i[0] = 10; i[1] = 7; i[2] = 9; // element value assignment.
What is the simplest way to defining an primitive array?
The below statement merges declaration,initialization and assignment into a single step:
int i [] = {10, 20, 30 40};
What is wrong with the below code segment?
int i[] = new int[5]
System.out.println(i.length())
Ans: length is an instance variable of array object - here it is given a method.
What is wrong with the below code segment?
int i[5] = new int[]
System.out.println(i.length)
Ans: length of an array should be given when it is initialized. Here it is given during declaration. It will result in compilation error.
What will be the output of the below code segment?
int i[] = new int[5]
System.out.println(i.length)
Ans: It will print 6. Remember array indexes start with 0.
Is there any limit for a length of an array?
What are the frequent RuntimeException's encountered because of improper coding?
ArrayIndexOutOfBoundsException and NullPointerException.
Pls send your questions, suggestions and feedback to j2eeguide@yahoo.com
© Copyright JGuide 2006, All Rights Reserved.
|