You are on page 1of 12

1.

What is Java? A high-level programming language developed by Sun Microsystems. Java was originally called OAK, and was designed for handheld devices and set-top boxes. Oak was unsuccessful so in 1995 Sun changed the name to Java and modified the language to take advantage of the burgeoning World Wide Web.

2.

What are the main features of java? The main features of java are Compiled and Interpreted Object oriented Robust and secure Type safe High Performance. What are pass by reference and passby value? Pass By Reference means the passing the address itself rather than passing the value. PassbyValue means passing a copy of the value to be passed. 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.

3.

4.

5.

What is the Java Virtual Machine (JVM)? The Java Virtual Machine is software that can be ported onto various hardware-based platforms. 6. What is variables and then types? Variables is an identifier that denotes a storage location used to store a data values.unlike constants that remain unchanged during the execution of a program, a variable may takes different values at different times during the execution of the program. Instance variables Class variables Local variable Parameters what is dot operator? The dot operator(.) is used to access the instance variables and methods of class objects.It is also used to access classes and sub-packages from a package. Examples : Person1.age ---------> Reference to the variable age

7.

8.

Define strings? Strings represent a sequence of characters.The easiest way to represent a sequence of characters in java is by using a character array. What is serialization? Serialization is the process of converting a objects into a stream of bytes. What are different types of access modifiers? Access specifiers are keywords that determine the type of access to the member a class. Public Protected Private Default What is an abstract class? Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods. What are class variables ? Class variables are global to a class and belong to the entire set of objects that class creates. Only one memory location is created for each variable. What is the Collection interface? The Collection interface provides support for the implementation of a mathematical bag an unordered collection of objects that may contain duplicates. What must a class do to implement an interface? The class must provide all of the methods in the interface and identify the interface in its implements clause. What is the Collections API? The Collections API is a set of classes and interfaces that support operations on collections of objects. What is an array? Array is a group of related data items that share a common name.For instance, we can define an array name salary to represent a set of salaries of a group of employees.Examples : salary[10] What is a list iterator? The List and Set collections provide iterators, which are objects that allow going over all the elements of a collection in sequence. The java.util.Iterator interface provides for one-way traversal and java.util.ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction (i.e. forward and or backward) and modify the list during iteration.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18

What is the main difference between a String and a StringBuffer class? String is immutable : you cant modify a string object but can replace it by creating a new instance. Creating a new instance is rather expensive. StringBuffer is mutable : use StringBuffer or StringBuilder when you want to modify the contents. StringBuilder was added in Java 5 and it is identical in all respects to StringBuffer except that it is not synchronized,which makes it slightly faster at the cost of not being thread-safe. When to use serialization? A common use of serialization is to use it to send an object over the network or if the state of an object needs to be persisted to a flat file or a database. What is the main difference between shallow cloning and deep cloning of objects? Java supports shallow cloning of objects by default when a class implements the java.lang.Cloneable interface. Deep cloning through serialization is faster to develop and easier to maintain but carries a performance overhead. What are wrapper classes? Primitive data types may be converted into object types by using the wrapper classes contained in the java.lang package. Exampes : int, float, long, char, double What is the difference between an instance variable and a static variable? Class variables are called static variables. There is only one occurrence of a class variable per JVM per class loader.When a class is loaded the class variables are initialized. Instance variables are non-static and there is one occurrence of an instance variable in each class instance.Also known as a member variable or a field. Where and how can you use a private constructor? Private constructor is used if you do not want other classes to instantiate the object and to prevent subclassing.The instantiation is done by a public static method (i.e. a static factory method) within the same class. What is type casting? Type casting means treating a variable of one type as though it is another type. Examples : int m = 5; byte n =i; What is a user defined exception? User defined exceptions may be implemented by defining a new exception class by extending the Exception class.

19.

20.

21.

22.

23

24.

25.

26.

What is an instanceof operator? Instanceof is an object reference operator and returns true if the object on the left-hand side is an instance of the glass given to the right hand side.This operator allows to determine whether the object belongs to a particular class or not. What are runtime exceptions? Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time. What is the difference between an interface and an abstract class? An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class. What is a package? A package is a namespace that organizes a set of related classes and interfaces.The classes contained in the packages of other programs can be easily reused.Packages, classes can be unique compared with classes in other packages.That is, two classes in two different packages can have the same name. Why do threads block on I/O? Threads block on i/o (that is enters the waiting state) so that other threads may execute while the i/o Operation is performed. What is the List interface? The List interface provides support for ordered collections of objects. What is the Vector class? The Vector class provides the capability to implement a growable array of objects. What is the base class of all classes? java.lang.Object 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. What is the difference between a while statement and a do while statement? A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do while statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do whilestatement will always execute the body of a loop at least once.

27.

28

29.

30.

31.

32.

33

34

35.

36.

Describe life cycle of thread? A thread is similiar to a program that has a single flow of control.A thread is a execution in a program. The life cycle of threads are Newborn state Runnable state Running state Blocked state Dead state What is an Applets? Applets are small java programs that are primarily used in Internet computing. They can be transported over the internet from one computer to another and run using the Applet Viewer or any web browser that supports java. What are wrapped classes? Wrapped classes are classes that allow primitive types to be accessed as objects. What is the difference between an if statement and a switch statement? The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.s What modifiers are allowed for methods in an Interface? Only public and abstract modifiers are allowed for methods in interfaces What is the difference between method overriding and overloading? Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments. What do you mean by polymorphism? Polymorphism means the ability to take more than one form. fro example, an operation may exhibit behavior in different instances. The behavior depends upon the types of data used in the operation. What is the difference between an abstract class and an interface? Abstract class Interface - Have executable methods and abstract methods.Can only subclass one abstract class. Interface - Have no implementation code. All methods are abstract.A class can implement any number of interfaces. What is the purpose of finalization? The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

37.

38

39.

40.

41.

42.

43

44.

45.

What is the difference between a break statement and a continue statement? Break statement results in the termination of the statement to which it applies (switch, for, do, or while). A Continue statement is used to end the current loop iteration and return control to the loop statement. When is a method said to be overloaded and when is a method said to be overridden? Overloading deals with multiple methods in the same class with the same name but different method signatures. Overriding deals with two methods, one in the parent class and the other one in the child class and has the same name and signatures. How is final different from finally and finalize()? Final - constant declaration. The finally block always executes when the try block exits, except System.exit(0) call. finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity. What is Byte Code? All Java programs are compiled into class files that contain bytecodes. These byte codes can be run in any platform and hence java is said to be platform independent. What is the difference between error and an exception? Exception means When a method encounters an abnormal condition (an exception condition) that it cant handle itself, it may throw an exception. ssError mens system doesnt handle.For example:Overflow,Out of memory.

46.

47.

48.

49.

50.

What if the main method is declared as private? When a method is declared as private, the program compiles properly but it will give runtimeerror Main method not public. 51. What type of parameter passing does Java support? In Java the arguments are always passed by value. What is singleton? It is one of the design pattern. This falls in the creational pattern of the design pattern. There will be only one instance for that entire JVM. You can achieve this by having the private constructor in the class. What is Locale? A Locale object represents a specific geographical, political, or cultural region.

52.

53.

54.

What is the difference between constructors and normal methods? Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times and it can return a value or can be void. What is the difference between static and non-static variables? A static variable is associated with the class as a whole rather than with specific instances of a class.Non-static variables take on unique values with each object instance. What is the difference between java and c++? Java is a true object - oriented language while c++ is basically c with object-oriented extension. C++ supports multiple inheritence but Java provides interfaces in case of multiple inheritence. Java does not support operator overloading.Java does not have template classes as in c++. java does not use pointers. What is handle and why it is used in EJB? The handle mechanism allows a client application to maintain a reference to an EJB object. A handle object may be obtained by calling the getHandle() method on the reference to an EJB object. The main interest is that the handle class implements java.io.serializable interface, which means that a handle may be serialized. This allows the client to store the handle, or to pass it to another process. The handle may then be deserialized and used to obtain the reference to the EJB object, by calling the getEJBObject() method. What are pass by reference and passby value in Java? Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed. Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object. What are the modifiers in Java ? Public: Public class is visible in other packages, field is visible everywhere (class must be pblic 2o) Private: Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature. Protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature. Default : What you get by default ie, without any access modifier (ie, public private protected).It means that it is visible to all within a particular package

55.

56.

57.

58.

59.

60.

What is Overriding? When a class defines a method using the same name, return type, and arguments as a method in its superclass, the method in the class overrides the method in the superclass. When the method is invoked for an object of the class, it is the new definition of the method that is called, and not the method definition from superclass. Rules to folow : Methods may be overridden to be more public, not more private. Methods may be overridden to be thows the same exception or subclass of the exception thrown by the method of superclass. What are Checked and UnCheckedException? A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() method Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cant be What is wrapper class? Explain with example? Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes. They are e.g. Integer, Character, Double etc. All wrapper classes are final. You can't extend the class. 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.). What is the basic difference between the 2 approaches to exception handling. 1> try catch block and 2> specifying the candidate exceptions in the throws clause? When should you use which approach? In the first approach as a programmer of the method, you urself are dealing with the exception. This is fine if you are in a best position to decide should be done in case of an exception. Whereas if it is not the responsibility of the method to deal with it's own exceptions, then do

61.

62.

63.

64.

not use this approach. In this case use the second approach. In the second approach we are forcing the caller of the method to catch the exceptions, that the method is likely to throw. This is often the approach library creators use. They list the exception in the throws clause and we must catch them. 65. What are synchronized methods and synchronized statements? Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement What is daemon thread and which method is used to create the daemon thread? Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread. What is the purpose of finalization? The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected Can an unreachable object become reachable again? An unreachable object may become reachable again. This can happen when the object's finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable objects 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 accessbile during the course of method excecution. 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 wrapper class? Explain with example? Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes. They are e.g. Integer, Character, Double etc. All wrapper classes are final. You can't extend the class.

66.

67.

68.

69.

only

70.

71.

What is the difference between int and Interger? int is primitive type and Integer is Wrapper Class. For Example : int i=2; Integer i = new Integer (2); Types of Constructor There are two types of Constructor they are:

Default Constructor (no-arg constructor ) Parameterized Constructor

A constructor having no parameter is known as default constructor. It provides the default values to the objects like 0, null etc depending on the type. A constructor that have parameter is known as parameterized constructor. Parameterized constructor used to provide diff. values to the distinct objects 17.1 You can have question like why we need Wrapper Class(Integer )? Answer is , if you are using Collection objects like Hashtable etc. you can't use int as a key .you have to use object so you can use Integer class. Hashtable ht = new Hashtable (); ht.put(2, "USA") ;// You can do this ht.put(new Integer(2), "USA") ;// you have to do like this. Difference between HashMap and HashTable? a) The key difference between the two is that access to the Hashtable is synchronized while access to the HashMap is not synchronized. b) HashMap permits null values as Key, while Hashtable doesn't. c) iterator in a HashMap is fail-safe(Fail-safe is relevant from the context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the collection object "structurally", a concurrent modification exception will be thrown) while the enumerator of HashTable is not. 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.). What are different types of inner classes? Nested top-level classes, Member classes, Local classes, Anonymous classes:

72.

73.

74.

Nested top-level classes- If you declare a class within a class and specify the static modifier, the compiler treats the class just like any other top-level class. Any class outside the declaring class accesses the nested class with the declaring class name acting similarly to a package. eg, outer.inner. Top-level inner classes implicitly have access only to static variables.There can also be inner interfaces. All of these are of the nested top-level variety. Member classes - Member inner classes are just like other member methods and member variables and access to the member class is restricted, just like methods and variables. This means a public member class acts similarly to a nested top-level class. The primary difference between member classes and nested top-level classes is that member classes have access to the specific instance of the enclosing class. Local classes - Local classes are like local variables, specific to a block of code. Their visibility is only within the block of their declaration. In order for the class to be useful beyond the declaration block, it would need to implement a more publicly available interface.Because local classes are not members, the modifiers public, protected, private, and static are not usable. Anonymous classes - Anonymous inner classes extend local inner classes one level further. As anonymous classes have no name, you cannot provide a constructor. 75. Explain different types of exceptions in java? There are many type of exceptions some of them you may encounter are:

1. ArrayIndexOutOfBoundsException - Thrown when attempting to access an array with an invalid index value (either negative or beyond the length of the array). 2. ClassCastException - Thrown when attempting to cast a reference variable to a type that fails the IS-A test. 3. IllegalArgumentException - Thrown when a method receives an argument formatted differently than the method expects. 4. IllegalStateException - Thrown when the state of the environment doesn't match the operation being attempted, e.g., using a Scanner that's been closed. 5. NullPointerException - Thrown when attempting to access an object with a reference variable whose current value is null. 6. NumberFormatException - Thrown when a method that converts a String to a number receives a String that it cannot convert. 7. AssertionError - Thrown when a statement's boolean test returns false. 8. ExceptionInInitializerError - Thrown when attempting to initialize a static variable or an initialization block. 9. StackOverflowError - Typically thrown when a method recurses too deeply. (Each invocation is added to the stack.) 10. NoClassDefFoundError - Thrown when the JVM can't find a class it needs, because of a command line error, a classpath issue, or a missing .class file.

What are the different ways to handle exceptions? There are two ways to handle exception, 1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. 2. List the desired exceptions in the throws clause of the method and let the caller of the method hadle those exceptions. 76. What are the steps in the JDBC connection? While making a JDBC connection we go through the following steps : Step 1 : Register the database driver by using : Class.forName(\" driver classs for that specific database\" ); Class.forName("com.mysql.jdbc.Driver"); Step 2 : Now create a database connection using : Connection con = DriverManager.getConnection(url,username,password); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/art",art,art); Step 3: Now Create a query using : Statement stmt = Connection.Statement(\"select * from EMP\"); Step 4 : Exceute the query : ResultSet rs = stmt.exceuteQuery(); while(rs.next()){ System.out.println(rs.getString(1)); }

You might also like