Home | Next-> |
What is java?
Java is a programming language expressly designed for use in the distributed environment of the Internet. It was designed to have the "look and feel" of the C++ language, but it is simpler to use than C++ and enforces an object-oriented programming model. Java can be used to create complete applications that may run on a single computer or be distributed among servers and clients in a network .
What are the important features of Java?
Java is object oriented, platform independent, robust, secure, simple,etc. Most important is Platform independence.
What do you mean by platform independence?
Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).
How does Java acheive platform independence? *IMP*
A Java source file on compilation produces an intermediary .class rather than a executable file. This .class file is interpreted by the JVM. Since JVM acts as an intermediary layer.
What is byte code?
Byte code is a set of instructions generated by the compiler. JVM executes the byte code.
What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
Are JVM's platform independent?
JVM's are not platform independent. JVM's are platform specific run time implementation provided by the vendor. A Windows JVM cannot be installed in LinuX
Who provides the JVM?
Any software vendor can provide a JVM but it should comply to the Java langauge specification.
What is the difference between a JDK and a JVM? *IMP*
JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.
What is a pointer and does Java support pointers? *IMP*
Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn't support the usage of pointers.
How is an object reference different from a pointer?
Both object reference and pointer point to the memory location of the object. You can manipulate pointers but not object references.
Does Java support multiple inheritance?
Java doesn't support multiple inheritance.
Why Java doesn't support multiple inheritance? *IMP*
When a class inherits from more than class, it will lead to the diamond problem - say A is the super class of B and C & D is a subclass of both B and C. D inherits properties of A from two different inheritance paths ie via both B & C. This leads to ambiguity and related problems, so multiple inheritance is not allowed in Java.
Is Java a pure object oriented language?
Java is a pure object oriented language. Except for the primitives everything else are objects in Java.
What is the difference between Path and Classpath?
Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.
Why does Java not support operator overloading?
Operator overloading makes the code very difficult to read and maintain. To maintain code simplicity, Java doesn't support operator overloading.
What are keywords?
Keywords cannot be used as identifiers.
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.
What is 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 test;
double price;
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 = 100;
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.
Data Type-> Deafult Value
byte -> 0
short-> 0
int -> 0
long -> 0
float -> 0.0f
double -> 0.0d
char -> '\u0000'
String(or any object)-> null
boolean -> false
What are the default values of primitive data types. *IMP*
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 check[]; // Here the check 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.
What are wrapped classes?
Wrapped classes are classes that allow primitive types to be accessed as objects.
Can you explain keyword , identifier and literal with an example?
Consider the below statement:
int a = 10;
Here int is the keyword which has special meaning attached Java programming langauge ie whatever is declared is an integer value.
a 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 a = 123 // this is the usual decimal representation.
int a = 0123 // this is octal representation. Octal values start with a zero.
int a = 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 abc = 'A'; // represented using single quotes.
char abc = '\u0041'; // represented using unicode representation.
char abc = 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? *IMP*
Yes. The below is an example.
char ch = 'j';
System.out.println(ch++);
The above statement will print h. ++ is a numeral operand and since char is internally represented as integer, ++ operand can be applied on char value.
What are the frequent Exception's encountered because of improper coding?
java.lang:
ArithmeticException, ClassCastException, IllegalAccessException, IndexOutOfBoundsException, NullPointerException, IllegalStateException.
java.util:
NoSuchElementException
java.io:
FileNotFoundException, IOException, StreamCorruptedException, InvalidClassException,
InvalidObjectException
java.net:
SocketException, SocketTimeoutException, BindException
java.rmi:
ConnectException, NoSuchObjectException, UnexpectedException
java.text:
ParseException
What are order of precedence and associativity, and how are they used?
Order of precedence determines the order in which operators are evaluated in expressions. Associatity determines whether an expression is evaluated left-to-right or right-to-left
How will the below literal value be internally represented?
float f = 12.24;
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 a = 100/0;
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;
This will compile and execute fine. The result will be ...
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.
eg:
int test(){
int a=100;
string abc="start";
//other code
}
for(i=10;i<=10;i++){
int j=100;
//other code
}
above variabe 'a' and 'j' are local variable and only accessible inside the method and loop only.
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 test = str[0];
for (int a=0; a<=10; a++){
System.out.println(test + a); }
} }
In the above the class, the variable 'a' 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?
String abc[]; // most frequently used form of array declaration.
String[] abc;
String abc[] ;
int i[][]; // most frequently form multi-dimensional array
int[] i[];
int[] i[],j;
Imp : i is double dimensional whereas j is single dimensional. ***** Need to be verified.
How vector is different fron array?
Size of array can't be changed once it is define.The size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
What's the main difference between a Vector and an ArrayList?
Java Vector class is internally synchronized and ArrayList is not.
What are the 3 steps in defining arrays?
declaration,initialization and assignment
int i[]; // array declaration.
i[] = new int[5]; // array initialization.
i[0] = 5; i[1] = 10; 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 a [] = {10, 20, 30 40};
What is wrong with the below code segment?
int i[5] = new int[]
System.out.println(i.length)
>length of an array should be given when it is initialized. Here it is given during declaration.Itwillresult in compilation error.What will be the output of the below code segment?
int i[] = new int[7]
System.out.println(i.length)
It will print 8. Remember array indexes start with 0.
Next |
*****Please feel free to give feedback at shrawan.iitg@gmail.com and comments below *****
No comments:
Post a Comment