Java Interview Questions | Page 10


Question 91:

How do you iterate any Map object in java?

There are several ways iterating over Map in Java.

Method 1 :- Iterating over entries using For-each loop.

Map<String, String> map = new HashMap<String, String>();
for (Map.Entry<String, String> entry : map.entrySet()) {
      System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

Method 2 :- Iterating using Iterator.

Map<String, String> map = new HashMap<String, String>();
Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry<String, String> entry = entries.next();
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Question 92:

Here is a situation:-

Birthday happy;
happy = new AdultBirthday( "Joe", 39);

happy.greeting();

Which greeting() method is executed considering that greeting method is defined in Birthday class and AdultBirthday extends Birthday class?

In the above question Birthday class reference variable refers to AdultBirthday object.

Case I :- If greeting method() is defined in both classes, AdultBirthday class method will be invoked.

Case II :-  If greeting method() is defined in Birthday class only, Birthday class method will be invoked.

Case III:-  If greeting method() is defined in AdultBirthday class only, Compiler gives error.

Question 93:

Here is a situation:-

Birthday happy;
happy = new AdultBirthday( "Joe", 39);

happy.greeting();

Which greeting() method is executed considering greeting method is defined in AdultBirthday and AdultBirthday extends Birthday class?

In the above question Birthday class reference variable refers to AdultBirthday object.

Case I :- If greeting method() is defined in both classes, AdultBirthday class method will be invoked.

Case II :-  If greeting method() is defined in Birthday class only, Birthday class method will be invoked.

Case III:-  If greeting method() is defined in AdultBirthday class only, Compiler gives error.

Question 94:

What is Exception?

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. After a method throws an exception, the runtime system attempts to find something to handle it. The list of methods is known as the call stack.

Runtime_Stack

Question 95:

What is process?

In computing, a process is an instance of a computer program that is being executed.

A process generally has a complete private set of basic run-time resources; in particular; each process has its own memory space.


Question 96:

When throw keyword is used?

In Java if exception occurs, an exception object is created and it can be handled using try-catch block. Sometime we might want to generate exception explicitly in our code, for example :- If usome mandatory data does not exists for system startup. This is accomplised using throw keyword. You can throw any type of Throwable object using the keyword throw. It interrupts the method. Anything after the throw statement would not be executed, unless the thrown exception is handled. The exception object is not returned from the method, it is thrown from the method. That means that the exception object is not the return value of the method and the calling method can be interrupted too and the stack continues until some appropriate exception handler handles it.
The syntax of throw keyword is :-

throw new ExceptionType(); //ExceptionType should extend Throwable class

For example :-

class DemoTest{
    public int checkValidRollNumber(int rollNumber) throws MyException
    {
      if (rollNumber < 0)
      {
        throw MyException("Roll Number is not Valid");
      }
    }
}

In this above example if Roll Number is less than 0 code generates an exception of type MyException.

Question 97:

When throws keyword is used?

In Java, a code can throw exception. This exception can be handled in two ways. Either the code should be enclosed by try block & exception handling is done in same method, or thge method can specify all checked exceptions that can be thrown within the scope of method and can be handled by calling method.

To do so, "throws" keyword is used. It throws the exception to immediate calling method in the hierarchy.

The syntax of "throws" keyword is as :- 

type method_name(parameter_list)throws exception_list
{
    //code here
}

For example :- 

class DemoThrowsException {
public void fileInfo(File file) throws IOException { 
      if (file.length() > 12345){
            throw new IOException("File too big");
      }
}

In the above code the fileInfo method throws IOException to calling method if the length of file is greater than 12345.

This exception should be handled by either calling method or even a calling method can rethrow and declare its method signature stating the Exception.

Question 98:

How to iterate over any Enum?

Let us take an example:-

public enum Direction {
  NORTH,
  NORTHEAST,
  EAST,
  SOUTHEAST,
  SOUTH,
  SOUTHWEST,
  WEST,
  NORTHWEST
}

Now we want to iterate the above enum. The below code illusterates how to iterate over above enum:-

for (Direction dir : Direction.values()) {
// do what you want
}
Question 99:

Why getClass() method of Object class is final?

The getClass method is final. This keeps Java programmers from fooling reflective programs. If it were not final, a programmer could override getClass to return tthe wrong class.

Question 100:

What is the difference between Runnable & Callable Interface?

Callable interface declares call() method and you need to provide generics as type of Object call() should return-

public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}

Runnable on the other hand is interface that declares run() method that is called when you create a Thread with the runnable and call start() on it. You can also directly call run() but that just executes the run() method is same thread.

public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used 
     * to create a thread, starting the thread causes the object's 
     * <code>run</code> method to be called in that separately executing 
     * thread. 
     * <p>
     * The general contract of the method <code>run</code> is that it may 
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

To summarize few notable Difference are

  1. A Runnable object does not return a result whereas a Callable object returns a result.
  2. A Runnable object cannot throw a checked exception wheras a Callable object can throw an exception.
  3. The Runnable interface has been around since Java 1.0 whereas Callable was only introduced in Java 1.5.
Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media