Java Interview Questions -15

Java Interview Questions -15

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

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

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

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

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

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

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

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

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

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

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

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

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

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

15.What is Unicode?

A universal, 16-bit, standard coded character set for the representation of all human scripts.

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

17.What is the argument of main method?

main method accepts an array of String objects as argument.

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

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

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

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

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

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

24.Give your observation on the below statement.

double d = 10.12/0;
This will compile and execute fine. The result will be Infinity

25.Are arrays primitive data types?

No. Arrays are not primitives - they are objects.

26.What are the frequent RuntimeException's encountered because of improper coding with respect to arrays?

ArrayIndexOutOfBoundsException and NullPointerException.

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

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

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

30.What is the return type of the main method?

Main method doesn't return anything hence declared void.

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

32.Does the order of public and static declaration matter in main method?

No it doesn't matter but void should always come before main().

33.Can constructors be overloaded?

Yes. Constructors can be overloaded.

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

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

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

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

38.Which keyword is used for inheriting from another class?

extends keyword is used for inheritance.

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

40.What are constructors?

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

41.What are the decalaration of a constructor?

Constructor name should be the same as class name.Constructors doesn’t return anything – not even void.

42.Can a main method be overloaded?

Yes. You can have any number of main methods with different method signature and implementation in the class.

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

44.Can a main method be declared final?

Yes. Any inheriting class will not be able to have it's own default main method.

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

46.Does Java support multiple inheritance?

No. Java Supports only single inheritance.

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

48.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();

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

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

51.Can a parent class be referenced for a subclass object?

Yes. The below is an example :Vehicle v = new Car();

52.Does constructors throw exceptions?

Yes. Like methods constructors can also throw exceptions.

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

54.Is default a keyword?

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

55.Then how to give default access to a class?

If you dont specify any access, then it means the class is of default access.

56.Can a constructor be defined for an interface?

No

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

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

59.Explain Overriding?

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

60.What is a package?

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

61.What is meant by default access?

default access means the class,method,construtor or the variable can be accessed only within the package.

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

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

64.Can a class be declared as protected?

A class can't be declared as protected. only methods can be declared as protected.

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

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

67.What is the impact of marking a constructor as private?

Nobody can instantiate the class from outside that class.

68.Which package is imported by default?

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

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

70.Is package statement mandatory in a Java source file?

It's not mandatory.

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

72.Can a final variable be declared inside a method?

No. Local variables cannot be declared as final.

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

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

75.Can you give few examples of final classes defined in Java API?

java.lang.String,java.lang.Math are final classes.

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

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

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

79.What is the use of a static code block?

static code blocks could be used for one time initialisation activities.

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

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

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

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

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

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

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

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

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

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

90.What is the use of a abstract variable?

Variables can't be declared as abstract. only classes and methods can be declared as abstract.

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

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

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

94.Can a abstract class be defined without any abstract methods?

Yes it's possible. This is basically to avoid instance creation of the class.

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

96.Can i give two access specifiers to a method at the same time?

No its not possible. It will result in compile time error.

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

98.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("");

99.Can a class be marked as native?

No. Only methods can be marked as native.

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

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

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

103.When overriding a static method, can it be converted to a non-static method?

No. It should be static only.

104.Can an Interface extend another Interface?

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

105.Can a Class extend more than one Class?

Not possible. A Class can extend only one class but can implement any number of Interfaces.

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

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

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

109.What is the purpose of volatile modifier?

Only variables can be marked as volatile. Volatile variables might be modified asynchronously.

110.Can an Interface implement another Interface?

Intefaces doesn't provide implementation hence a interface cannot implement another interface.



-----------------
Also Read:

0 comments:

Post a Comment