Interview Questions

Q:

What are pass by reference and pass by value ?

Answer

When an object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object, it doesn’t affect the original value. 


When an object is passed by reference, this means that the actual object is not passed, rather a reference of the object is passed. Thus, any changes made by the external method, are also reflected in all places.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 2031
Q:

Explain different ways of creating a thread. Which one would you prefer and why ?

Answer

There are three ways that can be used in order for a Thread to be created:


A class may extend the Thread class.


A class may implement the Runnable interface.


An application can use the Executor framework, in order to create a thread pool.


The Runnable interface is preferred, as it does not require an object to inherit the Thread class. In case your application design requires multiple inheritance, only interfaces can help you. Also, the thread pool is very efficient and can be implemented and used very easily.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 3599
Q:

What is the difference between processes and threads ?

Answer

- A process is an execution of a program, while a Thread is a single execution sequence within a process.


- A process can contain multiple threads. A Thread is sometimes called a lightweight process.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 2440
Q:

What do you know about the big-O notation and can you give some examples with respect to different data structures ?

Answer

The Big-O notation simply describes how well an algorithm scales or performs in the worst case scenario as the number of elements in a data structure increases. The Big-O notation can also be used to describe other behavior such as memory consumption. Since the collection classes are actually data structures, we usually use the Big-O notation to chose the best implementation to use, based on time, memory and performance. Big-O notation can give a good indication about performance for large amounts of data.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 3365
Q:

What’s the difference between Enumeration and Iterator interfaces ?

Answer

Enumeration is twice as fast as compared to an Iterator and uses very less memory. However, the Iterator is much safer compared to Enumeration, because other threads are not able to modify the collection object that is currently traversed by the iterator. Also, Iteratorsallow the caller to remove elements from the underlying collection, something which is not possible with Enumerations.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 5278
Q:

What is the output of this program ?

class main_arguments {
            public static void main(String [ ] args)
            {
                String [][] argument = new String[2][2];
                int x;
                argument[0] = args;
                x = argument[0].length;
                for (int y = 0; y < x; y++)
                    System.out.print(" " + argument[0][y]);           
            }
        }

A) 1 1 B) 1 0
C) 1 0 3 D) 1 2 3
 
Answer & Explanation Answer: D) 1 2 3

Explanation:

In argument[0] = args;, the reference variable arg[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements.
Output:
$ javac main_arguments.java
$ java main_arguments
1 2 3

Report Error

View Answer Report Error Discuss

Filed Under: Java
Exam Prep: GATE

0 17441
Q:

What is autoboxing and unboxing ?

Answer

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing. 

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 3091
Q:

Can you access non static variable in static context ?

Answer

A static variable in Java belongs to its class and its value remains the same for all its instances. A static variable is initialized when the class is loaded by the JVM. If your code tries to access a non-static variable, without any instance, the compiler will complain, because those variables are not created yet and they are not associated with any instance.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 2032