Sunday, 10 August 2014

"C" Interview Questions


                                    C LANGUAGE INTERVIEW QUESTIONS



1. What does static variable mean?
Ans: Static variables are the variables which retain their values between the function calls. They are initialized only once their scope is within the function in which they are defined.


2. What is a pointer?
Ans: Pointers are variables which stores the address of another variable. That variable may be a scalar (including another pointer), or an aggregate (array or structure). The pointed-to object may be part of a larger object, such as a field of a structure or an element in an array.



3. What are the uses of a pointer?
Ans: Pointer is used in the following cases
i) It is used to access array elements
ii) It is used for dynamic memory allocation.
iii) It is used in Call by reference
iv) It is used in data structures like trees, graph, linked list etc.



4. What is a structure?
Ans: Structure constitutes a super data type which represents several different data types in a single unit. A structure can be initialized if it is static or global.



5. What is a union?
Ans: Union is a collection of heterogeneous data type but it uses efficient memory utilization technique by allocating enough memory to hold the largest member. Here a single area of memory contains values of different types at different time. A union can never be initialized.



6. What are the differences between structures and union?
Ans: A structure variable contains each of the named members, and its size is large enough to hold all the members. Structure elements are of same size.
A union contains one of the named members at a given time and is large enough to hold the largest member. Union element can be of different sizes.



7. What are the differences between structures and arrays?
Ans: Structure is a collection of heterogeneous data type but array is a collection of homogeneous data types.
Array 
1-It is a collection of data items of same data type.
2-It has declaration only
3-.There is no keyword.
4- array name represent the address of the starting element.


Structure
1-It is a collection of data items of different data type.
2- It has declaration and definition
3- keyword struct is used
4-Structure name is known as tag it is the short hand notation of the declaration.



8. In header files whether functions are declared or defined?
Ans: Functions are declared within header file. That is function prototypes exist in a header file,not function bodies. They are defined in library (lib).



9. What are the differences between malloc () and calloc ()?
Ans: Malloc Calloc 1-Malloc takes one argument Malloc(a);where a number of bytes 2-memory allocated contains garbage values
1-Calloc takes two arguments Calloc(b,c) where b no of object and c size of object
2-It initializes the contains of block of memory to zerosMalloc takes one argument, memory allocated contains garbage values.
It allocates contiguous memory locations. Calloc takes two arguments, memory allocated contains all zeros, and the memory allocated is not contiguous.



10. What are macros? What are its advantages and disadvantages?
Ans: Macros are abbreviations for lengthy and frequently used statements. When a macro is called the entire code is substituted by a single line though the macro definition is of several lines.
The advantage of macro is that it reduces the time taken for control transfer as in case of
function.
The disadvantage of it is here the entire code is substituted so the program becomes
lengthy if a macro is called several times.




11. Difference between pass by reference and pass by value?
Ans: Pass by reference passes a pointer to the value. This allows the callee to modify the variable directly.Pass by value gives a copy of the value to the callee. This allows the callee to modify the value without modifying the variable. (In other words, the callee simply cannot modify the variable, since it lacks a reference to it.)

12. What is static identifier?
Ans: A file-scope variable that is declared static is visible only to functions within that file. A
function-scope or block-scope variable that is declared as static is visible only within that scope. Furthermore, static variables only have a single instance. In the case of function- or block-scope variables, this means that the variable is not “automatic” and thus retains its value across function invocations.



13. Where is the auto variables stored?
Ans: Auto variables can be stored anywhere, so long as recursion works. Practically, they’re stored on
the stack. It is not necessary that always a stack exist. You could theoretically allocate function invocation records from the heap.



14. Where does global, static, and local, register variables, free memory and C Program instructions get stored?
Ans: Global: Wherever the linker puts them. Typically the “BSS segment” on many platforms.
Static: Again, wherever the linker puts them. Often, they’re intermixed with the globals. The only difference between globals and statics is whether the linker will resolve the symbols across compilation units.Local: Typically on the stack, unless the variable gets register allocated and never spills.Register: Nowadays, these are equivalent to “Local” variables. They live on the stack unless they get register-allocated.



15. Difference between arrays and linked list?
Ans: An array is a repeated pattern of variables in contiguous storage. A linked list is a set of
structures scattered through memory, held together by pointers in each element that point to the next element. With an array, we can (on most architectures) move from one element to the next by adding a fixed constant to the integer value of the pointer. With a linked list, there is a “next” pointer in each structure which says what element comes next.



16. What are enumerations?
Ans: They are a list of named integer-valued constants. Example:enum color { black , orange=4,
yellow, green, blue, violet };This declaration defines the symbols “black”, “orange”, “yellow”, etc. to have the values “1,” “4,” “5,” … etc. The difference between an enumeration and a macro is that the enum actually declares a type, and therefore can be type checked.



17. Describe about storage allocation and scope of global, extern, static, local and register variables?
Ans:
Globals have application-scope. They’re available in any compilation unit that includes an
appropriate declaration (usually brought from a header file). They’re stored wherever the linker puts them, usually a place called the “BSS segment.”
Extern? This is essentially “global.”
Static: Stored the same place as globals, typically, but only available to the compilation unit that contains them. If they are block-scope global, only available within that block and its subblocks.
Local: Stored on the stack, typically. Only available in that block and its subblocks.
(Although pointers to locals can be passed to functions invoked from within a scope where that local is valid.)
Register: See tirade above on “local” vs. “register.” The only difference is that
the C compiler will not let you take the address of something you’ve declared as “register.”



18. What are register variables? What are the advantages of using register variables?
Ans: If a variable is declared with a register storage class,it is known as register variable.The
register variable is stored in the cpu register instead of main memory.Frequently used variables
are declared as register variable as it’s access time is faster.



19. What is the use of typedef?
Ans: The typedef help in easier modification when the programs are ported to another machine.
A descriptive new name given to the existing data type may be easier to understand the code.



20. Can we specify variable field width in a scanf() format string? If possible how?
Ans: All field widths are variable with scanf(). You can specify a maximum field width for a given
field by placing an integer value between the ‘%’ and the field type specifier. (e.g. %64s). Such a specifier will still accept a narrower field width.
The one exception is %#c (where # is an integer). This reads EXACTLY # characters, and it is the
only way to specify a fixed field width with scanf().





23. What is recursion?
Ans: A recursion function is one which calls itself either directly or indirectly it must halt at a definite point to avoid infinite recursion.


24. Differentiate between for loop and a while loop? What are it uses?
Ans: For executing a set of statements fixed number of times we use for loop while when the number of
iterations to be performed is not known in advance we use while loop.


25. What is storage class? What are the different storage classes in C?
Ans: Storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage. The storage classes in c are auto, register, and extern, static, typedef.


26. What the advantages of using Unions?
Ans: When the C compiler is allocating memory for unions it will always reserve enough room for the
largest member.


27. What is the difference between Strings and Arrays?
Ans: String is a sequence of characters ending with NULL .it can be treated as a one dimensional array
of characters terminated by a NULL character.


28. What is a far pointer? Where we use it?
Ans: In large data model (compact, large, huge) the address B0008000 is acceptable because in these
model all pointers to data are 32bits long. If we use small data model(tiny, small, medium) the above address won’t work since in these model each pointer is 16bits long. If we are working in a small data model and want to access the address B0008000 then we use far pointer. Far pointer is always treated as a 32bit pointer and contains a segment address and offset address both of 16bits each. Thus the address is represented using segment : offset format B000h:8000h. For any
given memory address there are many possible far address segment : offset pair. The segment register contains the address where the segment begins and offset register contains the offset of data/code from where segment begins.


29. What is a huge pointer?
Ans: Huge pointer is 32bit long containing segment address and offset address. Huge pointers are
normalized pointers so for any given memory address there is only one possible huge address segment: offset pair. Huge pointer arithmetic is doe with calls to special subroutines so its arithmetic slower than any other pointers.


30. What is a normalized pointer, how do we normalize a pointer?
Ans: It is a 32bit pointer, which has as much of its value in the segment register as possible. Since
a segment can start every 16bytes so the offset will have a value from 0 to F. for normalization convert the address into 20bit address then use the 16bit for segment address and 4bit for the offset address. Given a pointer 500D: 9407,we convert it to a 20bitabsolute address 549D7,Which then normalized to 549D:0007.


31. What is near pointer?
Ans: A near pointer is 16 bits long. It uses the current content of the CS (code segment) register (if
the pointer is pointing to code) or current contents of DS (data segment) register (if the pointer is pointing to data) for the segment part, the offset part is stored in a 16 bit near pointer. Using near pointer limits the data/code to 64kb segment.

32. In C, why is the void pointer useful? When would you use it?
Ans: The void pointer is useful because it is a generic pointer that any pointer can be cast into and
back again without loss of information.


33. What is a NULL Pointer? Whether it is same as an uninitialized pointer?
Ans: Null pointer is a pointer which points to nothing but uninitialized pointer may point to anywhere.


34. Are pointers integer?
Ans: No, pointers are not integers. A pointer is an address. It is a positive number.


35. What does the error ‘Null Pointer Assignment’ means and what causes this error?
Ans: As null pointer points to nothing so accessing a uninitialized pointer or invalid location may cause an error.


36. What is generic pointer in C?
Ans: In C void* acts as a generic pointer. When other pointer types are assigned to generic pointer,
conversions are applied automatically (implicit conversion).


37. Are the expressions arr and &arr same for an array of integers?
Ans: Yes for array of integers they are same.


38. IMP>How pointer variables are initialized?
Ans: Pointer variables are initialized by one of the following ways.
I. Static memory allocation
II. Dynamic memory allocation


39. What is static memory allocation?
Ans: Compiler allocates memory space for a declared variable. By using the address of operator, the
reserved address is obtained and this address is assigned to a pointer variable. This way of assigning pointer value to a pointer variable at compilation time is known as static memory allocation.


40. What is dynamic memory allocation?
Ans: A dynamic memory allocation uses functions such as malloc() or calloc() to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these function are assigned to pointer variables, such a way of allocating memory at run time is known as dynamic memory allocation.



41. What is the purpose of realloc?
Ans: It increases or decreases the size of dynamically allocated array. The function realloc (ptr,n) uses two arguments. The first argument ptr is a pointer to a block of memory for which the size is to be altered. The second argument specifies the new size. The size may be increased or decreased. If sufficient space is not available to the old region the function may create a new region.

42. What is pointer to a pointer?
Ans: If a pointer variable points another pointer value. Such a situation is known as a pointer to a pointer.
Example:
int *p1,**p2,v=10;
P1=&v; p2=&p1;
Here p2 is a pointer to a pointer.


43. What is an array of pointers?
Ans: if the elements of an array are addresses, such an array is called an array of pointers.


44. Difference between linker and linkage?
Ans: Linker converts an object code into an executable code by linking together the necessary built in
functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.


45. Is it possible to have negative index in an array?
Ans: Yes it is possible to index with negative value provided there are data stored in this location. Even if it is illegal to refer to the elements that are out of array bounds, the compiler will not produce error because C has no check on the bounds of an array.


46. Why is it necessary to give the size of an array in an array declaration?
Ans: When an array is declared, the compiler allocates a base address and reserves enough space in
memory for all the elements of the array. The size is required to allocate the required space and hence size must be mentioned.


47. What modular programming?
Ans: If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming.


48. What is a function?
Ans: A large program is subdivided into a number of smaller programs or subprograms. Each subprogram
specifies one or more actions to be performed for the larger program. Such sub programs are called functions.



49. What is an argument?
Ans: An argument is an entity used to pass data from the calling to a called function.



50. What are built in functions?
Ans: The functions that are predefined and supplied along with the compiler are known as built-in functions. They are also known as library functions.



61. What do the ‘c’ and ‘v’ in argc and argv stand for?
Ans: The c in argc(argument count) stands for the number of command line argument the program is
invoked with and v in argv(argument vector) is a pointer to an array of character string that contain the arguments.

62. IMP>what are C tokens?
Ans: There are six classes of tokens: identifier, keywords, constants, string literals, operators and other separators.


63. What are C identifiers?
Ans: These are names given to various programming element such as variables, function, arrays.It is a combination of letter, digit and underscore.It should begin with letter. Backspace is not allowed.


64. Difference between syntax vs logical error?
Ans:
Syntax Error 
1-These involves validation of syntax of language.
2-compiler prints diagnostic message.


Logical Error
1-logical error are caused by an incorrect algorithm or by a statement mistyped in such a way
that it doesn’t violet syntax of language.
2-difficult to find.



65. What is preincrement and post increment?
Ans: ++n (pre increment) increments n before its value is used in an assignment operation or any
expression containing it. n++ (post increment) does increment after the value of n is used.



66. Write a program to interchange 2 variables without using the third one.
Ans:
a ^= b; ie a=a^b
b ^= a; ie b=b^a;
a ^= b ie a=a^b;
here the numbers are converted into binary and then xor operation is performed.
You know, you’re just asking “have you seen this overly clever trick that’s not worth applying on
modern architectures and only really applies to integer variables?”



67. What is the maximum combined length of command line arguments including the space between adjacent arguments?
Ans: It depends on the operating system.



68. What are bit fields? What is the use of bit fields in a Structure declaration?
Ans: A bit field is a set of adjacent bits within a single implementation based storage unit that we
will call a “word”.
The syntax of field definition and access is based on structure.
Struct {
unsigned int k :1;
unsigned int l :1;
unsigned int m :1;
}flags;
the number following the colon represents the field width in bits.Flag is a variable that contains three bit fields.



69. What is a preprocessor, what are the advantages of preprocessor?
Ans: A preprocessor processes the source code program before it passes through the compiler.
1- a preprocessor involves the readability of program
2- It facilitates easier modification
3- It helps in writing portable programs
4- It enables easier debugging
5- It enables testing a part of program
6- It helps in developing generalized program



70. What are the facilities provided by preprocessor?
Ans:
1-file inclusion
2-substitution facility
3-conditional compilation



71. What are the two forms of #include directive?
Ans:
1.#include”filename”
2.#include
the first form is used to search the directory that contains the source file.If the search fails in the home directory it searches the implementation defined locations.In the second form ,the preprocessor searches the file only in the implementation defined locations.

72. How would you use the functions randomize() and random()?
Ans:
Randomize() initiates random number generation with a random value.
Random() generates random number between 0 and n-1;



73. What do the functions atoi(), itoa() and gcvt() do?
Ans:
atoi() is a macro that converts integer to character.
itoa() It converts an integer to string
gcvt() It converts a floating point number to string



74. How would you use the functions fseek(), freed(), fwrite() and ftell()?
Ans:
fseek(f,1,i) Move the pointer for file f a distance 1 byte from location i.
fread(s,i1,i2,f) Enter i2 dataitems,each of size i1 bytes,from file f to string s.
fwrite(s,i1,i2,f) send i2 data items,each of size i1 bytes from string s to file f.
ftell(f) Return the current pointer position within file f.


The data type returned for functions fread,fseek and fwrite is int and ftell is long int.


75. What is the difference between the functions memmove() and memcpy()?
Ans: The arguments of memmove() can overlap in memory. The arguments of memcpy() cannot.



76. What is a file?
Ans: A file is a region of storage in hard disks or in auxiliary storage devices.It contains bytes of
information .It is not a data type.



77. IMP>what are the types of file?
Ans: Files are of two types
1-high level files (stream oriented files) :These files are accessed using library functions
2-low level files(system oriented files) :These files are accessed using system calls



78. IMP>what is a stream?
Ans: A stream is a source of data or destination of data that may be associated with a disk or other
I/O device. The source stream provides data to a program and it is known as input stream. The destination stream eceives the output from the program and is known as output stream.



79. What is meant by file opening?
Ans: The action of connecting a program to a file is called opening of a file. This requires creating
an I/O stream before reading or writing the data.



80. What is FILE?
Ans: FILE is a predefined data type. It is defined in stdio.h file.



81. What is a file pointer?
Ans: The pointer to a FILE data type is called as a stream pointer or a file pointer. A file pointer points to the block of information of the stream that had just been opened.

82. How is fopen()used ?
Ans: The function fopen() returns a file pointer. Hence a file pointer is declared and it is assigned
as
FILE *fp;
fp= fopen(filename,mode);
filename is a string representing the name of the file and the mode represents:
“r” for read operation
“w” for write operation
“a” for append operation
“r+”,”w+”,”a+” for update operation



83How is a file closed ?
Ans: A file is closed using fclose() function
Eg. fclose(fp);
Where fp is a file pointer.



84. What is a random access file?
Ans: 
A file can be accessed at random using fseek() function
fseek(fp,position,origin);
fp file pointer
position number of bytes offset from origin
origin 0,1 or 2 denote the beginning ,current position or end of file respectively.



85. What is the purpose of ftell ?
Ans: The function ftell() is used to get the current file represented by the file pointer.
ftell(fp);
returns a long integer value representing the current file position of the file pointed by the
file pointer fp.If an error occurs ,-1 is returned.



86. What is the purpose of rewind() ?
Ans: The function rewind is used to bring the file pointer to the beginning of the file.
Rewind(fp);
Where fp is a file pointer.Also we can get the same effect by
feek(fp,0,0);



87. Difference between a array name and a pointer variable?
Ans: A pointer variable is a variable where as an array name is a fixed address and is not a
variable. A
pointer variable must be initialized but an array name cannot be initialized. An array name being a constant value , ++ and — operators cannot be applied to it.



88. Represent a two-dimensional array using pointer?
Ans:
Address of a[I][j] Value of a[I][j]
&a[I][j]
or
a[I] + j
or
*(a+I) + j
*&a[I][j] or a[I][j]
or
*(a[I] + j )
or
*( * ( a+I) +j )



89. Difference between an array of pointers and a pointer to an array?
Ans:
Array of pointers 
1- Declaration is: data_type *array_name[size];
2-Size represents the row size.
3- The space for columns may be dynamically


Pointers to an array
1-Declaration is data_type ( *array_name)[size];
2-Size represents the column size.



90. Can we use any name in place of argv and argc as command line arguments ?
Ans: yes we can use any user defined name in place of argc and argv;



91. What are the pointer declarations used in C?
Ans:
1- Array of pointers, e.g , int *a[10]; Array of pointers to integer
2-Pointers to an array,e.g , int (*a)[10]; Pointer to an array of into
3-Function returning a pointer,e.g, float *f( ) ; Function returning a pointer to float
4-Pointer to a pointer ,e.g, int **x; Pointer to apointer to int
5-pointer to a data type ,e.g, char *p; pointer to char

92. Differentiate between a constant pointer and pointer to a constant?
Ans:
const char *p; //pointer to a const character.
char const *p; //pointer to a const character.
char * const p; //const pointer to a char variable.
const char * const p; // const pointer to a const character.



93. Is the allocated space within a function automatically deallocated when the function returns?
Ans: No pointer is different from what it points to .Local variables including local pointers
variables in a function are deallocated automatically when function returns.,But in case of a
local pointer variable ,deallocation means that the pointer is deallocated and not the block of
memory allocated to it. Memory dynamically allocated always persists until the allocation is freed
or the program terminates.



94. Discuss on pointer arithmetic?
Ans:
1- Assignment of pointers to the same type of pointers.
2- Adding or subtracting a pointer and an integer.
3-subtracting or comparing two pointer.
4-incrementing or decrementing the pointers pointing to the elements of an array. When a pointer
to an integer is incremented by one , the address is incremented by two. It is done automatically
by the compiler.
5-Assigning the value 0 to the pointer variable and comparing 0 with the pointer. The pointer
having address 0 points to nowhere at all.



95. What is the invalid pointer arithmetic?
Ans:
i) adding ,multiplying and dividing two pointers.
ii) Shifting or masking pointer.
iii) Addition of float or double to pointer.
iv) Assignment of a pointer of one type to a pointer of another type ?



96. What are the advantages of using array of pointers to string instead of an array of strings?
Ans:
i) Efficient use of memory.
ii) Easier to exchange the strings by moving their pointers while sorting.



97. Are the expressions *ptr ++ and ++ *ptr same?
Ans: No,*ptr ++ increments pointer and not the value pointed by it. Whereas ++ *ptr
increments the value being pointed to by ptr.



98. What would be the equivalent pointer expression foe referring the same element as
a[p][q][r][s] ?

Ans : *( * ( * ( * (a+p) + q ) + r ) + s)



99. Are the variables argc and argv are always local to main?
Ans: Yes they are local to main.



100. Can main () be called recursively?
Ans: Yes any function including main () can be called recursively
.





Saturday, 9 August 2014

Java Interview Questions



1. What is meant by Object Oriented Programming?
OOP is a method of programming in which programs are organised as cooperative collections of objects. Each object is an instance of a class and each class belong to a hierarchy.

2.What is a Class?
Class is a template for a set of objects that share a common structure and a common behaviour.

3.What is an Object?
Object is an instance of a class. It has state,behaviour and identity. It is also called as an instance of a class.

4.What is an Instance?
An instance has state, behaviour and identity. The structure and behaviour of similar classes are defined in their common class. An instance is also called as an object.

5. What are the core OOP's concepts?
Abstraction, Encapsulation,Inheritance and Polymorphism are the core OOP's concepts.

6.What is meant by abstraction?
Abstraction defines the essential characteristics of an object that distinguish it from all other kinds of objects. Abstraction provides crisply-defined conceptual boundaries relative to the perspective of the viewer. Its the process of focussing on the essential characteristics of an object. Abstraction is one of the fundamental elements of the object model.

7.What is meant by Encapsulation?
Encapsulation is the process of compartmentalising the elements of an abtraction that defines the structure and behaviour. Encapsulation helps to separate the contractual interface of an abstraction and implementation.

8.What is meant by Inheritance?
Inheritance is a relationship among classes, wherein one class shares the structure or behaviour defined in another class. This is called Single Inheritance. If a class shares the structure or behaviour from multiple classes, then it is called Multiple Inheritance. Inheritance defines "is-a" hierarchy among classes in which one subclass inherits from one or more generalised superclasses.

9.What is meant by Polymorphism?
Polymorphism literally means taking more than one form. Polymorphism is a characteristic of being able to assign a different behavior or value in a subclass, to something that was declared in a parent class. 
10.What is an Abstract Class?
Abstract class is a class that has no instances. An abstract class is written with the expectation that its concrete subclasses will add to its structure and behaviour, typically by implementing its abstract operations.

11.What is an Interface?
Interface is an outside view of a class or object which emphaizes its abstraction while hiding its structure and secrets of its behaviour.

12.What is a base class?
Base class is the most generalised class in a class structure. Most applications have such root classes. In Java, Object is the base class for all classes.

13.What is a subclass?
Subclass is a class that inherits from one or more classes.

14.What is a superclass?
Superclass is a class from which another class inherits.

15.What is a constructor?
Constructor is an operation that creates an object and/or initialises its state.

16.What is a destructor?
Destructor is an operation that frees the state of an object and/or destroys the object itself. In Java, there is no concept of destructors. Its taken care by the JVM.

17.What is meant by Binding?
Binding denotes association of a name with a class

18.What is meant by static binding?
Static binding is a binding in which the class association is made during compile time. This is also called as Early binding.

19.What is meant by Dynamic binding?
Dynamic binding is a binding in which the class association is not made until the object is created at execution time. It is also called as Late binding.

20.Define Modularity?
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.

21.What is meant by Persistence?
Persistence is the property of an object by which its existence transcends space and time.

22.What is colloboration?
Colloboration is a process whereby several objects cooperate to provide some higher level behaviour.

23.In Java, How to make an object completely encapsulated?
All the instance variables should be declared as private and public getter and setter methods should be provided for accessing the instance variables.

24.How is polymorphism acheived in java?
Inheritance, Overloading and Overriding are used to acheive Polymorphism in java.

 25.What are the most important features of Java?
Java is object oriented, platform independent,secure,robust,simple,etc

26.Which one of them do you consider the best feature of Java?
Platform independence.

27.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).

28.What is byte code?
Byte code is a set of instructions generated by the compiler. JVM executes the byte code. 
29.How does Java acheive platform independence?
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.

30.What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.

31.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.

32.Who provides the JVM?
Any software vendor can provide a JVM but it should comply to the Java langauge specification.

33.What is the difference between a JDK and a JVM?
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.

34.What is a pointer and does Java support pointers?
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. 
35.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.

36.Does Java support multiple inheritance?
Java doesn't support multiple inheritance.

37.Why Java doesn't support multiple inheritance?
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.

38.Is Java a pure object oriented language?
Java is a pure object oriented language. Except for the primitives everything else are objects in Java.

39.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.

40.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.

41.What are keywords?
Keywords cannot be used as identifiers.

42.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.

43.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.

44.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.

45.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.

46.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{}

47.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.

48.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.

49.What are literals?
Literals are source code representation of primitive data types.

50.Name the eight data types which are available in Java?
boolean, byte, short, int, long, float, double and char.

51.Are primitive data types objects in Java?
Primitive data types are not objects.

52.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.

53.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.

54.What are default values?
Values which are defaulted during object initialisation are called default values. Each data type has a default value.

55.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'.

56.Are object references defaulted?
Yes. Object references are defaulted to null.

57.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.

58.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.

59.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.

60.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.

61.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.

62.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.

63.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.

64.What is Unicode?
A universal, 16-bit, standard coded character set for the representation of all human scripts.

65.What should i have to do if i have to print my name in Spanish?
Provide code...

66.How will the below literal value be internally represented?
float f = 21.22;
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.

67.Give your observation on the below statement.
int i = 10/0;
The statement will result in RuntimeException (DivideByZeroException). Integer values cannot be divided by zero.

68.Give your observation on the below statement.
double d = 10.12/0;
This will compile and execute fine. The result will be Infinity

69.What is a Variable?
a variable is a facility for storing data. The current value of the variable is the data actually stored in the variable.

70.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.

71.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.

72.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 methodConsider 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.

73.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.

74.Are arrays primitive data types?
No. Arrays are not primitives - they are objects.

75.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 arrayint[] i[]; // nothing wrong with this. int[] i[],j; // nothing wrong with this. Important : i is double dimensional whereas j is single dimensional.

76.What are the 3 steps in defining arrays?
declaration,initialization and assignmentint 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()) length is an instance variable of array object - here it is given a method.

77.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. It will result in compilation error.

78.What will be the output of the below code segment?
int i[] = new int[5]System.out.println(i.length)
It will print 6. Remember array indexes start with 0.

79.What are the frequent RuntimeException's encountered because of improper coding with respect to arrays?
ArrayIndexOutOfBoundsException and NullPointerException.

80.Should a main method be compulsorily declared in all java classes?
No not required. main method should be defined only if the source class is a java application.

81.What is the return type of the main method?
Main method doesn't return anything hence declared void.

82.Why is the main method declared static?
main method is the entry point for a Java application and main method is called by the JVM even before the instantiation of the class hence it is declared as static. static methods can be called even before the creation of objects.

83.What is the argument of main method?
main method accepts an array of String objects as argument.

84.How to pass an argument to main method?
You should pass the argument as a command line argument. Command line arguments are seprated by a space. The following is an example:java Hello Tom JerryIn the above command line Hello is the class, Tom is the first argument and Jerry is the second argument.

85.What will happen if no argument is passed to the main method?
If you dont access the argument, the main method will execute without any problem. If try to access the argument, NullPointerException will be thrown.

86.Can a main method be overloaded?
Yes. You can have any number of main methods with different method signature and implementation in the class.

87.Can a main method be declared final?
Yes. Any inheriting class will not be able to have it's own default main method.

88.Does the order of public and static declaration matter in main method?
No it doesn't matter but void should always come before main().

89.Can a source file contain more than one Class declaration?
Yes. A single source file can contain any number of Class declarations but only one of the class can be declared as public.

90.If a source file has 2 class declaration in it, on compilation how many class files will be created?
2 class files will be generated by the compiler.

91.Can the first line of the source code be a comment?
Yes. Comments can appear anywhere in the code. It is just a "skip this line" instruction to the conpiler.

92.Can the source file name and the class name in the file be different?
If the class in the source is not of public access, then the name can be anything but should confirm to identifier rules.

93.Explain Inheritance?
Inheritance is a concept where the properties and behaviour of a class is reused in another class. The class which is being reused or inherited is called the super class or the parent class. The class which is inheriting is called the subclass or the child class of the inherited class.

94.Does Java support multiple inheritance?
No. Java Supports only single inheritance.

95.Why Java doesn’t support muliple inheritance?
When a class inherits from more than class, it will lead to inheritance path amibiguity (this is normally calledthe 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.

96.Which keyword is used for inheriting from another class?
extends keyword is used for inheritance.

97.Can a subclass be referenced for a super class object?
No. If Vehicle is super class and Car is the subclass then the following reference would be wrong – Car c = new Vehicle();

98.Can a parent class be referenced for a subclass object?
Yes. The below is an example :Vehicle v = new Car();

99.Can an interface be referenced for any object?
Yes. We can create a object reference for an interface. The object should have provided the implementation for the object.Runnable r = new Test(); Test class should have implemented the runnable interface and overridded the run method.

100.What are constructors?
Constructors are used to initialise an object. Constructors are invoked when the new operator on the class are called.

101.What are the decalaration of a constructor?
Constructor name should be the same as class name.Constructors doesn’t return anything – not even void.

102.Does constructors throw exceptions?
Yes. Like methods constructors can also throw exceptions.

103.Can constructors be overloaded?
Yes. Constructors can be overloaded.

104.Is it compulsory to define a constructor for a class?
No. If you don’t define a constructor, the compiler will provide a default constructor.

105.What is a default constructor?
Default constructor is a no argument constructor which initialises the instance variables to their default values. This will be provided by the compiler at the time of compilation. Default constructor will be provided only when you don’t have any constructor defined.

106.Explain about “this” operator?
“this” is used to refer the currently executing object and it’s state. “this” is also used for chaining constructors and methods.

107.Explain about “super” operator?
“super” is used to refer to the parent object of the currently running object. “super” is also to invoke super class methods and classes.

108.What is the sequence of constructor invocation?
When you instantiate a subclass, the object will begin with an invocation of the constructor in the base class and initialize downwards through constructors in each subclass till it reaches the final subclass constructor. Note: This top down imagery for class inheritance, rather than a upward tree-like approach, is standard in OOP but is sometimes confusing to newcomers.

109.Can a constructor be defined for an interface?
No

110.Explain Polymorphism?
The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class. Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. Overloading and overriding are two types of polymorphism.

111.What is Overloading?
In a class, if two methods have the same name and a different signature,it is known as overloading in Object oriented concepts.

112.When to use overloading?
Overloading is a powerful feature, but you should use it only as needed. Use it when you actually do need multiple methods with different parameters, but the methods do the same thing. That is, don't use overloading if the multiple methods perform different tasks. This approach will only confuse other developers who get a peek at your code.

113.Explain Overriding?
Overriding means, to give a specific definition by the derived class for a method implemented in the base class.

114.What is a package?
Package is a collection of related classes and interfaces. package declaration should be first statement in a java class.

115.Which package is imported by default?
java.lang package is imported by default even without a package declaration.

116.Is package statement mandatory in a Java source file?
It's not mandatory.

117.What will happen if there is no package for Java source file?
The classes will packaged into a no name default package. In practice, we always put classes into a meaningful package.

118.What is the impact using a * during importing(for example import java.io.*;?
When a * is used in a import statement, it indicates that the classes used in the current source can be available in the imported package. Using slightly increases the compilation time but has no impact on the execution time.

119.Can a class declared as private be accessed outside it's package?
A class can't be declared as private. It will result in compile time error.

120.Can a class be declared as protected?
A class can't be declared as protected. only methods can be declared as protected.

121.What is the access scope of a protected method?
A protected method can be accessed by the classes within the same package or by the subclasses of the class in any package.

122.What is the impact of marking a constructor as private?
Nobody can instantiate the class from outside that class.

123.What is meant by default access?
default access means the class,method,construtor or the variable can be accessed only within the package.

124.Is default a keyword?
Yes. default is a keyword but is associated with switch statement not with access specifiers.

125.Then how to give default access to a class?
If you dont specify any access, then it means the class is of default access.

126.Can i give two access specifiers to a method at the same time?
No its not possible. It will result in compile time error.

127.What is the purpose of declaring a variable as final?
A final variable's value can't be changed. final variables should be initialized before using them.

128.Can a final variable be declared inside a method?
No. Local variables cannot be declared as final.

129.What is the impact of declaring a method as final?
A method declared as final can't be overridden. A sub-class doesn't have the independence to provide different implementation to the method.

130.I don't want my class to be inherited by any other class. What should i do?
You should declared your class as final. A class declared as final can't be inherited by any other class.

131.When will you declare a class as final?
When a class is independent and completely concrete in nature, then the class has to be marked as final.

132.Can you give few examples of final classes defined in Java API?
java.lang.String,java.lang.Math are final classes.

133.How to define a constant variable in Java?
The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can't be changed also.static final int PI = 3.14; is an example for constant.

134.Can a class be declared as static?
No a class cannot be defined as static. Only a method,a variable or a block of code can be declared as static.

135.When will you define a method as static?
When a method needs to be accessed even before the creation of the object of the class then we should declare the method as static.

136.I want to print "Hello" even before main is executed. How will you acheive that?
Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main method.

137.What is the use of a static code block?
static code blocks could be used for one time initialisation activities.

138.Cant you use the constructor for initialisation rather than static block?
Constructors are used for object level initialisation whereas the static block are used for class level initialisation ie to initialise constants.

139.Will the static block be executed for each object?
No. It will be executed only once for each class ie at the time of loading a class.

140.What are the restriction imposed on a static method or a static block of code?
A static method should not refer to instance variables without creating an instance.It cannot use "this" or "super". A static method can acces only static variables or static methods.

141.When overriding a static method, can it be converted to a non-static method?
No. It should be static only.

142.What is the importance of static variable?
static variables are class level variables where all objects of the class refer to the same variable. If one object changes the value then the change gets reflected in all the objects.

143.Can we declare a static variable inside a method?
Static varaibles are class level variables and they can't be declared inside a method. If declared, the class will not compile.

144.What is an Abstract Class and what is it's purpose?
A Class which doesn't provide complete implementation is defined as an abstract class. Abstract classes enforce abstraction.

145.What is the use of a abstract variable?
Variables can't be declared as abstract. only classes and methods can be declared as abstract.

146.Can a abstract class be declared final?
Not possible. An abstract class without being inherited is of no use and a final class cannot be inherited. So if a abstract class is declared as final, it will result in compile time error.

147.What is an abstract method?
An abstract method is a method whose implementation is deferred to a subclass.

148.Can a abstract class be defined without any abstract methods?
Yes it's possible. This is basically to avoid instance creation of the class.

149.What happens if a subclass has inherited a abstract class but has not provided implementation for all the abstract methods of the super class?
Then the subclass also becomes abstract. This will be enforced by the compiler.

150.What happens if a class has implemented an interface but has not provided implementation for a method in a interface?
Its the same as the earlier answer. The class has to be marked as abstract. This will be enforced by the compiler.

151.Can you create an object of an abstract class?
Not possible. Abstract classes are not concrete and hence can't be instantiated. If you try instantiating, you will get compilation error.

152.Can I create a reference for a an abstract class?
Yes. You can create a reference for an abstract class only when the object being has provided the implementation for the abstract class - it means that the object should be of a concrete subclass of the abstract class. This applies to interfaces also. Below is an example for interface referencing an object:java.sql.Connection con = java.sql.DriverManager.getConnection("");

153.Can a class be marked as native?
No. Only methods can be marked as native.

154.What is the use of native methods?
When a java method accesses native library written in someother programming language then the method has to be marked as native.

155.What is the disadvantage of native methods?
By using native methods, the java program loses platform independence - the accessed platform might be tightly coupled with a operating system hence java program also loses OS independence.

156.What is the purpose of transient modifier?
Only variables can be marked as transient. Variables marked as transient will not be persisted during object persistence.

157.What is the purpose of volatile modifier?
Only variables can be marked as volatile. Volatile variables might be modified asynchronously.

158.What is an Interface?
Interfaces say what a class must do but does not say how a class must do it. Interfaces are 100% abstract.

159.Class C implements Interface I containing method m1 and m2 declarations. Class C has provided implementation for method m2. Can i create an object of Class C?
No not possible. Class C should provide implementation for all the methods in the Interface I. Since Class C didn't provide implementation for m1 method, it has to be declared as abstract. Abstract classes can't be instantiated.

160.Can a method inside a Interface be declared as final?
No not possible. Doing so will result in compilation error. public and abstract are the only applicable modifiers for method declaration in an interface.

161.Can an Interface implement another Interface?
Intefaces doesn't provide implementation hence a interface cannot implement another interface.

162.Can an Interface extend another Interface?
Yes an Interface can inherit another Interface, for that matter an Interface can extend more than one Interface.

163.Can a Class extend more than one Class?
Not possible. A Class can extend only one class but can implement any number of Interfaces.

164.Why is an Interface be able to extend more than one Interface but a Class can't extend more than one Class?
Basically Java doesn't allow multiple inheritance, so a Class is restricted to extend only one Class. But an Interface is a pure abstraction model and doesn't have inheritance hierarchy like classes(do remember that the base class of all classes is Object). So an Interface is allowed to extend more than one Interface.

165.Can an Interface be final?
Not possible. Doing so so will result in compilation error.

166.Can a class be defined inside an Interface?
Yes it's possible.

167.Can an Interface be defined inside a class?
Yes it's possible.

168.What is a Marker Interface?
An Interface which doesn't have any declaration inside but still enforces a mechanism.

169.Can we define private and protected modifiers for variables in interfaces?
No. Always all variables declared inside a interface are of public access.

170.What modifiers are allowed for methods in an Interface?
Only public and abstract modifiers are allowed for methods in interfaces.

171.When can an object reference be cast to an interface reference?
An object reference can be cast to an interface reference when the object implements the referenced interface.

172.What are Inner Classes?
Inner classes are classes which are defined inside another class.

173.What are Nested Classes?
Static Inner classes are called sometimes referred as nested classes because these classes can exist without any relationship with the containing class.

174.What is the super class of all Inner Classes?
Inner class is just a concept and can be applied to any class, hence there is no common super class for inner classes.

175.What are the disadvantages of Inner classes?
1. Inner classes are not reusable hence defeats one of the fundamental feature of Java.2. Highly confusing and difficult syntax which leads poor code maintainability.

176.Name the different types of Inner Classes?
The following are the different types of Inner classes:Regular Inner ClassMethod Local Inner ClassStatic Inner ClassAnonymous Inner Class

177.What are Regular Inner Classes?
A Regular inner class is declared inside the curly braces of another class outside any method or other code block. This is the simplest form of inner classes.

178.Can a regular inner class access a private member of the enclosing class?
Yes. Since inner classes are treated as a member of the outer class they can access private members of the outer class.

179.How will you instantiate a regular inner class from outside the enclosing class?
Outer out=new Outer();Outer.Inner in=out.new Inner();

180.What are Local Inner Classes or Method Local Inner Classes?
A method-local inner class is defined within a method of the enclosing class.

181.What are the constraints on Method Local Inner Classes?
The following are the restrictions for Method Inner Classes:Method Local Inner classes cannot acccess local variables but can access final variables.Only abstract and final modifiers can be applied to Method Local Inner classesMethod Local Inner classes can be instantiated only within the method in which it is contained that too after the class definition.

182.What are Anonymous Inner Classes? Name the various forms of Anonymous Inner Classes.
Anonymous Inner Classes have no name, and their type must be either a subclass of the named type or an implementer of the named interface. The following are the different forms of inner classes:Anonymous subclass(i.e. extends a class)Anonymous implementer (i.e. implements an interface)Argument-Defined Anonymous Inner Classes

183.How many classes can an Anonymous Inner classes inherit from?One.

184.How many Interfaces can an Anonymous Inner classes implement?
One. Normal classes and other inner classes can implement more than one interface whereas anonymous inner classes can either implement a single interface or extend a single class.

185.What are Static Inner Classes?
Static Inner Classes are inner classes which marked with a static modifier. These classes need not have any relationship with the outer class. These can be instantiated even without the existence of the outer class object.

186.Can you instantiate the static Inner Class without the existence of the outer class object?
If Yes, Write a sample statement. Yes. It can be instantiated as follows by referencing the Outer class.Outer.Inner in = new Outer.Inner();

187.What are the constraints on Static Inner Classes?
It cannot access non-static members of the outer class.It cannot use this reference to the outer class.


188.How many class files are produced for source file having one Outer class and one Inner class?
Two class files will be produced as follows:Outer.classOuter$Inner.class