Java Interview Questions

Java Quick View

Java Software Engineering Questions

Question 1: What is the three tier model?
Answer: It is the presentation, logic, backend
Question 2: Why do we have index table in the database?
Answer: Because the index table contain the information of the other tables. It will
be faster if we access the index table to find out what the other contain.
Question 3: Give an example of using JDBC access the database.
Answer:
try
{
Class.forName(“register the driver”);
Connection con = DriverManager.getConnection(“url of db”, “username”,”password”);
Statement state = con.createStatement();
state.executeUpdate(“create table testing(firstname varchar(20), lastname varchar(20))”);
state.executeQuery(“insert into testing values(’phu’,’huynh’)”);
state.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
Question 4: What is the different of an Applet and a Java Application
Answer: The applet doesn’t have the main function
Question 5: How do we pass a reference parameter to a function in Java?
Answer: Even though Java doesn’t accept reference parameter, but we can
pass in the object for the parameter of the function.
For example in C++, we can do this:

void changeValue(int& a)
{
a++;
}
void main()
{
int b=2;
changeValue(b);
}

however in Java, we cannot do the same thing. So we can pass the
the int value into Integer object, and we pass this object into the
the function. And this function will change the object.

Good Questions

What are the advantages of OOPL?

Ans: Object oriented programming languages directly represent the real life objects. The features of OOPL as inhreitance, polymorphism, encapsulation makes it powerful.

Q2: What do mean by polymorphisum, inheritance, encapsulation?

Ans: Polymorhisum: is a feature of OOPl that at run time depending upon the type of object the appropriate method is called.
Inheritance: is a feature of OOPL that represents the “is a” relationship between different objects(classes). Say in real life a manager is a employee. So in OOPL manger class is inherited from the employee class.
Encapsulation: is a feature of OOPL that is used to hide the information.

Q3: What do you mean by static methods?

Ans: By using the static method there is no need creating an object of that class to use that method. We can directly call that method on that class. For example, say class A has static function f(), then we can call f() function as A.f(). There is no need of creating an object of class A.

Q4: What do you mean by virtual methods?

Ans: virtual methods are used to use the polymorhism feature in C++. Say class A is inherited from class B. If we declare say fuction f() as virtual in class B and override the same function in class A then at runtime appropriate method of the class will be called depending upon the type of the object.

Q5: Given two tables Student(SID, Name, Course) and Level(SID, level) write the SQL statement to get the name and SID of the student who are taking course = 3 and at freshman level.

Ans: SELECT Student.name, Student.SID
FROM Student, Level
WHERE Student.SID = Level.SID
AND Level.Level = “freshman”
AND Student.Course = 3;

Q6: What are the disadvantages of using threads?

Ans: DeadLock.

Q1: Write the Java code to declare any constant (say gravitational constant) and to get its value

Ans: Class ABC
{
static final float GRAVITATIONAL_CONSTANT = 9.8;
public void getConstant()
{
system.out.println(“Gravitational_Constant: ” + GRAVITATIONAL_CONSTANT);
}
}
Q2: What do you mean by multiple inheritance in C++ ?

Ans: Multiple inheritance is a feature in C++ by which one class can be of different types. Say class teachingAssistant is inherited from two classes say teacher and Student.

Q3: Can you write Java code for declaration of multiple inheritance in Java ?

Ans: Class C extends A implements B
{
}

QUESTION: What is a JavaBean? (asked by Lifescan inc)

ANSWER: JavaBeans are reusable software components written in the Java programming language, designed to be manipulated visually by a software develpoment environment, like JBuilder or VisualAge for Java. They are similar to Microsoft’s ActiveX components, but designed to be platform-neutral, running anywhere there is a Java Virtual Machine (JVM).

QUESTION: What are the seven layers(OSI model) of networking? (asked by Caspio.com)

ANSWER: 1.Physical, 2.Data Link, 3.Network, 4.Transport, 5.Session, 6.Presentation and 7.Application Layers.

QUESTION: What are some advantages and disadvantages of Java Sockets? (asked by Arashsoft.com)

ANSWER:
Advantages of Java Sockets:

Sockets are flexible and sufficient. Efficient socket based programming can be easily implemented for general communications.

Sockets cause low network traffic. Unlike HTML forms and CGI scripts that generate and transfer whole web pages for each new request, Java applets can send only necessary updated information.

Disadvantages of Java Sockets:

Security restrictions are sometimes overbearing because a Java applet running in a Web browser is only able to establish connections to the machine where it came from, and to nowhere else on the network

Despite all of the useful and helpful Java features, Socket based communications allows only to send packets of raw data between applications. Both the client-side and server-side have to provide mechanisms to make the data useful in any way.

Since the data formats and protocols remain application specific, the re-use of socket based implementations is limited.

QUESTION: What is the difference between a NULL pointer and a void pointer? (asked by Lifescan inc)

ANSWER: A NULL pointer is a pointer of any type whose value is zero. A void pointer is a pointer to an object of an unknown type, and is guaranteed to have enough bits to hold a pointer to any object. A void pointer is not guaranteed to have enough bits to point to a function (though in general practice it does).

QUESTION: What is encapsulation technique? (asked by Microsoft)

ANSWER: Hiding data within the class and making it available only through the methods. This technique is used to protect your class against accidental changes to fields, which might leave the class in an inconsistent state.

) What is the purpose of garbage collection in Java, and when is it used?

The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

2) Describe synchronization in respect to multithreading.

With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.

>

3) How is JavaBeans differ from Enterprise JavaBeans?

The JavaBeans architecture is meant to provide a format for general-purpose components. On the other hand, the Enterprise JavaBeans architecture provides a format for highly specialized business logic components.

4) In what ways do design patterns help build better software?

Design patterns helps software developers to reuse successful designs and architectures. It helps them to choose design alternatives that make a system reusuable and avoid alternatives that compromise reusability through proven techniques as design patterns.

5) Describe 3-Tier Architecture in enterprise application development.

In 3-tier architecture, an application is broken up into 3 separate logical layers, each with a well-defined set of interfaces. The presentation layer typically consists of a graphical user interfaces. The business layer consists of the application or business logic, and the data layer contains the data that is needed for the application.

Q:In Java, what is the difference between an Interface and an Abstract class?

A: An Abstract class declares have at least one instance method that is declared abstract which will be implemented by the subclasses. An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior.

Q: Can you have virtual functions in Java? Yes or No. If yes, then what are virtual functions?

A: Yes, Java class functions are virtual by default. Virtual functions are functions of subclasses that can be invoked from a reference to their superclass. In other words, the functions of the actual object are called when a function is invoked on the reference to that object.

Q:Write a function to reverse a linked list p in C++?

A:

Link* reverse_list(Link* p)
{
if (p == NULL)
return NULL;

Link* h = p;
p = p->next;
h->next = NULL;
while (p != null)
{
Link* t = p->next;
p->next = h;
h = p;
p = t;
}

return h;
}

Q:In C++, what is the usefulness of Virtual destructors?

A:Virtual destructors are neccessary to reclaim memory that were allocated for objects in the class hierarchy. If a pointer to a base class object is deleted, then the compiler guarantees the various subclass destructors are called in reverse order of the object construction chain.

Q:What are mutex and semaphore? What is the difference between them?

A:A mutex is a synchronization object that allows only one process or thread to access a critical code block. A semaphore on the other hand allows one or more processes or threads to access a critial code block. A semaphore is a multiple mutex.

What is a Servlet?

Answer: Servlets are modules of Java code that run in a server application (hence the name “Servlets”, similar to “Applets” on the client side) to answer client requests.

Question2:

What advantages does CMOS have over TTL(transitor transitor logic)?

Answer:

  • low power dissipation
  • pulls up to rail
  • easy to interface

How is Java unlike C++? (Asked by Sun)

Answer:

Some language features of C++ have been removed. String manipulations in Java do not allow for buffer overflows and other typical attacks. OS-specific calls are not advised, but you can still call native methods. Everything is a class in Java. Everything is compiled to Java bytecode, not executable (although that is possible with compiler tools).

Question4:

What is HTML (Hypertext Markup Language)?

Answer:

HTML (HyperText Markup Language) is the set of “markup” symbols or tags inserted in a file intended for display on a World Wide Web browser. The markup tells the Web browser how to display a Web page’s words and images for the user.

Question5:

Define class.

Answer: A class describes a set of properties (primitives and objects) and behaviors (methods).

Check your Java Skill

  What is the difference between an Abstract class and Interface ?

  What is user defined exception ?

  What do you know about the garbage collector ?

  What is the difference between C++ & Java ?

  Explain RMI Architecture?

  How do you communicate in between Applets & Servlets ?

  What is the use of Servlets ?

  What is JDBC? How do you connect to the Database ?

  In an HTML form I have a Button which makes us to open another page in 15 seconds. How will do you that ?

  What is the difference between Process and Threads ?

  What is the difference between RMI & Corba ?

  What are the services in RMI ?

  How will you initialize an Applet ?

  What is the order of method invocation in an Applet ?

  When is update method called ?

  How will you pass values from HTML page to the Servlet ?

  Have you ever used HashTable and Dictionary ?

  How will you communicate between two Applets ?

  What are statements in JAVA ?

  What is JAR file ?

  What is JNI ?

  What is the base class for all swing components ?

  What is JFC ?

  What is Difference between AWT and Swing ?

  Considering notepad/IE or any other thing as process, What will happen if you start notepad or IE 3 times? Where 3 processes are started or 3 threads are started ?

  How does thread synchronization occurs inside a monitor ?

  How will you call an Applet using a Java Script function ?

  Is there any tag in HTML to upload and download files ?

  Why do you Canvas ?

  How can you push data from an Applet to Servlet ?

  What are 4 drivers available in JDBC ?

  How you can know about drivers and database information ?

  If you are truncated using JDBC, How can you know ..that how much data is truncated ?

  And What situation , each of the 4 drivers used ?

  How will you perform transaction using JDBC ?

  In RMI, server object first loaded into the memory and then the stub reference is sent to the client ? or whether a stub reference is directly sent to the client ?

  Suppose server object is not loaded into the memory, and the client request for it , what will happen?

  What is serialization ?

  Can you load the server object dynamically? If so, what are the major 3 steps involved in it ?

  What is difference RMI registry and OSAgent ?

  To a server method, the client wants to send a value 20, with this value exceeds to 20,. a message should be sent to the client ? What will you do for achieving for this ?

  What are the benefits of Swing over AWT ?

  Where the CardLayout is used ?

  What is the Layout for ToolBar ?

  What is the difference between Grid and GridbagLayout ?

  How will you add panel to a Frame ?

  What is the corresponding Layout for Card in Swing ?

  What is light weight component ?

  Can you run the product development on all operating systems ?

  What is the webserver used for running the Servlets ?

  What is Servlet API used for connecting database ?

  What is bean ? Where it can be used ?

  What is difference in between Java Class and Bean ?

  Can we send object using Sockets ?

  What is the RMI and Socket ?

  How to communicate 2 threads each other ?

  What are the files generated after using IDL to Java Compilet ?

Junior Java Programming Questions

  1. 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.
  2. 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.
  3. 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.
  4. What is the purpose of the finally clause of a try-catch-finally statement? – The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught.
  5. What is the argument type of a program’s main() method? – A program’s main() method takes an argument of the String[] type.
  6. Which Java operator is right associative? – The = operator is right associative.
  7. Can a double value be cast to a byte? – Yes, a double value can be cast to a byte.
  8. What is the difference between a break statement and a continue statement? – A 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.
  9. What must a class do to implement an interface? – It must provide all of the methods in the interface and identify the interface in its implements clause.
  10. What is the advantage of the event-delegation model over the earlier event-inheritance model? – The event-delegation model has two advantages over the event-inheritance model. First, it enables event handling to be handled by objects other than the ones that generate the events (or their containers). This allows a clean separation between a component’s design and its use. The other advantage of the event-delegation model is that it performs much better in applications where many events are generated. This performance improvement is due to the fact that the event-delegation model does not have to repeatedly process unhandled events, as is the case of the event-inheritance model.
  11. How are commas used in the intialization and iteration parts of a for statement? – Commas are used to separate multiple statements within the initialization and iteration parts of a for statement.
  12. What is an abstract method? – An abstract method is a method whose implementation is deferred to a subclass.
  13. What value does read() return when it has reached the end of a file? – The read() method returns -1 when it has reached the end of a file.
  14. Can a Byte object be cast to a double value? – No, an object cannot be cast to a primitive value.
  15. What is the difference between a static and a non-static inner class? – A non-static inner class may have object instances that are associated with instances of the class’s outer class. A static inner class does not have any object instances.
  16. If a variable is declared as private, where may the variable be accessed? – A private variable may only be accessed within the class in which it is declared.
  17. What is an object’s lock and which object’s have locks? – An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object’s lock. All objects and classes have locks. A class’s lock is acquired on the class’s Class object.
  18. What is the % operator? – It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first operand by the second operand.
  19. When can an object reference be cast to an interface reference? – An object reference be cast to an interface reference when the object implements the referenced interface.
  20. Which class is extended by all other classes? – The Object class is extended by all other classes.
  21. Can an object be garbage collected while it is still reachable? – A reachable object cannot be garbage collected. Only unreachable objects may be garbage collected.
  22. Is the ternary operator written x : y ? z or x ? y : z ? – It is written x ? y : z.
  23. How is rounding performed under integer division? – The fractional part of the result is truncated. This is known as rounding toward zero.
  24. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy? – The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.
  25. What classes of exceptions may be caught by a catch clause? – A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types.
  26. If a class is declared without any access modifiers, where may the class be accessed? – A class that is declared without any access modifiers is said to have package access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.
  27. Does a class inherit the constructors of its superclass? – A class does not inherit constructors from any of its superclasses.
  28. What is the purpose of the System class? – The purpose of the System class is to provide access to system resources.
  29. Name the eight primitive Java types. – The eight primitive types are byte, char, short, int, long, float, double, and boolean.
  30. Which class should you use to obtain design information about an object? – The Class class is used to obtain information about an object’s design.
  31. What gives Java its “write once and run anywhere” nature? – Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platorm specific and hence can be fed to any platform. After being fed to the JVM, which is specific to a particular operating system, the code platform specific machine code is generated thus making java platform independent.
  32. What are the four corner stones of OOP? – Abstraction, Encapsulation, Polymorphism and Inheritance.
  33. Difference between a Class and an Object? – A class is a definition or prototype whereas an object is an instance or living representation of the prototype.
  34. 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.
  35. What is a “stateless” protocol? – Without getting into lengthy debates, it is generally accepted that protocols like HTTP are stateless i.e. there is no retention of state between a transaction which is a single request response combination.
  36. What is constructor chaining and how is it achieved in Java? – A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement.
  37. What is passed by ref and what by value? – All Java method arguments are passed by value. However, Java does manipulate objects by reference, and all object variables themselves are references
  38. Can RMI and Corba based applications interact? – Yes they can. RMI is available with IIOP as the transport protocol instead of JRMP.
  39. You can create a String object as String str = “abc”; Why cant a button object be created as Button bt = “abc”;? Explain – The main reason you cannot create a button by Button bt1= “abc”; is because “abc” is a literal string (something slightly different than a String object, by the way) and bt1 is a Button object. The only object in Java that can be assigned a literal String is java.lang.String. Important to note that you are NOT calling a java.lang.String constuctor when you type String s = “abc”;
  40. What does the “abstract” keyword mean in front of a method? A class? – Abstract keyword declares either a method or a class. If a method has a abstract keyword in front of it,it is called abstract method.Abstract method hs no body.It has only arguments and return type.Abstract methods act as placeholder methods that are implemented in the subclasses. Abstract classes can’t be instantiated.If a class is declared as abstract,no objects of that class can be created.If a class contains any abstract method it must be declared as abstract.
  41. How many methods do u implement if implement the Serializable Interface? – The Serializable interface is just a “marker” interface, with no methods of its own to implement. Other ‘marker’ interfaces are
42.                java.rmi.Remote
43.                java.util.EventListener
  1. What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. import java.net.* versus import java.net.Socket)? – It makes no difference in the generated class files since only the classes that are actually used are referenced by the generated class file. There is another practical benefit to importing single classes, and this arises when two (or more) packages have classes with the same name. Take java.util.Timer and javax.swing.Timer, for example. If I import java.util.* and javax.swing.* and then try to use “Timer”, I get an error while compiling (the class name is ambiguous between both packages). Let’s say what you really wanted was the javax.swing.Timer class, and the only classes you plan on using in java.util are Collection and HashMap. In this case, some people will prefer to import java.util.Collection and import java.util.HashMap instead of importing java.util.*. This will now allow them to use Timer, Collection, HashMap, and other javax.swing classes without using fully qualified class names in.
  2. What is the difference between logical data independence and physical data independence? – Logical Data Independence – meaning immunity of external schemas to changeds in conceptual schema. Physical Data Independence – meaning immunity of conceptual schema to changes in the internal schema.
  3. What is a user-defined exception? – Apart from the exceptions already defined in Java package libraries, user can define his own exception classes by extending Exception class.
  4. Describe the visitor design pattern? – Represents an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. The root of a class hierarchy defines an abstract method to accept a visitor. Subclasses implement this method with visitor.visit(this). The Visitor interface has visit methods for all subclasses of the baseclass in the hierarchy.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.