Friday, 16 January 2015

java interview questions


 Is null a keyword?
The null value is not a keyword.

Can java support global variables?
no. State variables lessen the cohesion of a program

What does it mean that a method or class is abstract?
An abstract class cannot be instantiated. Only its subclasses can be instantiated.

What is the Java API?
The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets.

What is the Vector class?
The Vector class provides the capability to implement a growable array of objects

Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier

What is the difference between the >> and >>> operators?
The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out 

How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16- bit and larger bit patterns

Is sizeof a keyword?
The sizeof operator is not a keyword

What are wrapped classes?
Wrapped classes are classes that allow primitive types to be accessed as objects

What is a native method?
A native method is a method that is implemented in a language other than Java.

Can a for statement loop indefinitely?
Yes, a for statement can loop indefinitely. For example, consider the following:
for(;;) ;

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

What is the difference between the Boolean & operator and the && operator?
             If an expression involving the Boolean & operator is evaluated, both operands are evaluated. Then the & operator is applied to the operand.
          When an expression involving the && operator is evaluated,the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. The && operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.

What is the GregorianCalendar class?
The GregorianCalendar provides support for traditional Western calendars
   
Are constructors inherited?
no.
Does java have destructors
no.Garbage colector does the job working in its background

name 4 methods java class have?

public StringtoString()
public Object clone()
public boolean equals();
public int hashCode()


what does java doesn’t support when compared to C++?

pointer
operator overload
typedef, define
preprocessors
structures and unions
enums
functions (within the class)
goto statements
global variables

 
 What is the package?
The package is a Java namespace or part of Java libraries. The Java API is grouped into libraries of related classes and interfaces; these libraries are known as packages.

Describe the principles of OOPS.
There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation

Explain the Encapsulation principle.
Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse

Explain the Inheritance principle.
Inheritance is the process by which one object acquires the properties of another object.

Explain the Polymorphism principle.
The meaning of Polymorphism is something like one name many forms.

Explain the different forms of Polymorphism.
From a practical programming viewpoint, polymorphism exists in three distinct forms in Java:
Method overloading
Method overriding through inheritance
Method overriding through the Java interface

What do you understand by Synchronization?
Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time.

public synchronized void Method1 () {
// Appropriate method-related code.
}
E.g. Synchronizing a block of code inside a function:
public myFunction (){
synchronized (this) {
// Synchronized code here.
}
}

why synchronization is important?
. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often causes dirty data and leads to significant errors.


Describe the wrapper classes in Java.
Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.

Following table lists the primitive types and the corresponding wrapper classes:

Primitive Wrapper
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void

What are new language features in J2SE 5.0?
Generally:
1. generics
2. static imports
3. annotations
4. typesafe enums
5. enhanced for loop
6. autoboxing/unboxing
7. varargs
8. covariant return types

how synchromization is done?
1st it acquires lock on methods object or class and only then the thread executes a synchronized menthod

 Can a lock be acquired on a class?
Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object.

Can Java object be locked down for exclusive use by a given thread?
Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it

Why are the interfaces more flexible than abstract classes?
--An interface-defined type can be implemented by any class in a class hierarchy and can be extended by another interface. In contrast, an abstract-class-defined type can be implemented only by classes that subclass the abstract class.

--An interface-defined type can be used well in polymorphism. The so-called interface type vs. implementation types.

--Abstract classes evolve more easily than interfaces. If you add a new concrete method to an abstract class, the hierarchy system is still working. If you add a method to an interface, the classes that rely on the interface will break when recompiled.
--Generally, use interfaces for flexibility; use abstract classes for ease of evolution (like expanding class functionality).

How many times may an object's finalize() method be invoked by the garbage collector?
An object's finalize() method may only be invoked once by the garbage collector.

What is the purpose of the finally clause of a try-catch-finally statement? garbage collector?
The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught.

What is the argument type of a program's main() method?
A program's main() method takes an argument of the String[] type.

Which Java operator is right associative?
The = operator is right associative.


What is the Locale class?
The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region.

Can a double value be cast to a byte?
Yes, a double value can be cast to a byte.

If an object is garbage collected, can it become reachable again?
Once an object is garbage collected, it ceases to exist. It can no longer become reachable again

What is a Java package and how is it used?
A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces

Can try statements be nested?
Try statements may be tested.

What modifiers may be used with a top-level class?
A top-level class may be public, abstract, or final.

What is a marker interface ?
An interface that contains no methods. E.g.: Serializable, Cloneable, SingleThreadModel etc. It is used to just mark java classes that support certain capability

.
What are tag interfaces?
Tag interface is an alternate name for marker interface.


What is JVM?
JVM stands for Java Virtual Machine. It is the run time for java programs. All are java programs are running inside this JVM only. It converts java byte code to OS specific commands.

Name few Garbage collection algorithms?
Here they go:
Mark and Sweep
Reference counting
Tracing collectors
Copying collectors
Heap compaction
Mark-compact collectors


What if the main method is declared as private?
The program compiles properly but at runtime it will give "Main method not public." message

What if the static modifier is removed from the signature of the main method?
Program compiles. But at runtime throws an error "NoSuchMethodError".

What if  static public void instead of public static void is written?
Program compiles and runs properly.

What if I do not provide the String array as the argument to the method?
Program compiles but throws a runtime error "NoSuchMethodError".

What is the first argument of the String array in main method?
The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.

Can an application have multiple classes having main method?
Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.

Can I have multiple main methods in the same class?
No the program fails to compile. The compiler says that the main method is already defined in the class.

What is JIT?
JIT stands for Just In Time compiler. It compiles java byte code to native code.

Do I need to import java.lang package any time? Why ?
No. It is by default loaded internally by the JVM.

Are the imports checked for validity at compile time? e.g. will the code containing an import such as java.lang.ABCD compile?
Yes the imports are checked for the semantic validity at compile time. The code containing above line of import will not compile. It will throw an error saying,can not resolve symbol
symbol : class ABCD
location: package io
import java.io.ABCD

Does importing a package imports the subpackages as well?
No

What is the difference between declaring a variable and defining a variable?
In declaration we just mention the type of the variable and it's name. We do not initialize it. But defining means declaration + initialization.
e.g String s; is just a declaration while String s = new String ("abcd"); Or String s = "abcd"; are both definitions.

What is the difference between error and an exception?
An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime. While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.).

Is it necessary that each try block must be followed by a catch block?
It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

If I write return at the end of the try block, will the finally block still execute?
Yes even if you write return as the last statement in the try block and no exception occurs, the finally block will execute. The finally block will execute and then the control return.

If I write System.exit (0); at the end of the try block, will the finally block still execute?
No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.

What is the Locale class?
The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region.

Is Empty .java file a valid source file?
Yes, an empty .java file is a perfectly valid source file.

Can a .java file contain more than one java classes?
Yes, a .java file contain more than one java classes, provided at the most one of them is a public class.

Is String a primitive data type in Java?
No String is not a primitive data type in Java, even though it is one of the most extensively used object. Strings in Java are instances of String class defined in java.lang package.

Is main a keyword in Java?
No, main is not a keyword in Java.

Is next a keyword in Java?
No, next is not a keyword.

Is delete a keyword in Java?
No, delete is not a keyword in Java. Java does not make use of explicit destructors the way C++ does.

Is exit a keyword in Java?
No. To exit a program explicitly you use exit method in System object.

What happens if you don't initialize an instance variable of any of the primitive types in Java?
Java by default initializes it to the default value for that primitive type. Thus an int will be initialized to 0, a Boolean will be initialized to false.

What will be the initial value of an object reference which is defined as an instance variable?
The object references are all initialized to null in Java. However in order to do anything useful with these references, you must set them to a valid object, else you will get NullPointerException everywhere you try to use such default initialized references.

What are the different scopes for Java variables?
The scope of a Java variable is determined by the context in which the variable is declared. Thus a java variable can have one of the three scopes at any given point in time.
1. Instance : - These are typical object level variables, they are initialized to default values at the time of creation of object, and remain accessible as long as the object accessible.
2. Local : - These are the variables that are defined within a method. They remain accessible only during the course of method execution. When the method finishes execution, these variables fall out of scope.
3. Static: - These are the class level variables. They are initialized when the class is loaded in JVM for the first time and remain there as long as the class remains loaded. They are not tied to any particular object instance.

What is the default value of the local variables?
The local variables are not initialized to any default value, neither primitives nor object references. If you try to use these variables without initializing them explicitly, the java compiler will not compile the code. It will complain about the local variable not being initialized..

Can a public class MyClass be defined in a source file named YourClass.java?
No the source file name, if it contains a public class, must be the same as the public class name itself with a .java extension.

Can main method be declared final?
Yes, the main method can be declared final, in addition to being public static.

What will be the output of the following statement?
System.out.println ("1" + 3);
It will print 13.

What will be the default values of all the elements of an array defined as an instance variable?
If the array is an array of primitive types, then all the elements of the array will be initialized to the default value corresponding to that primitive type. e.g. All the elements of an array of int will be initialized to 0, while that of Boolean type will be initialized to false. Whereas if the array is an array of references (of any type), all the elements will be initialized to null.







No comments:

Post a Comment