Author Topic: Ano ano ba kadalasan na tinatanong sa Technical Job Interview sa JAVA ?  (Read 178 times)

0 Members and 1 Guest are viewing this topic.

Offline LadyProgrammer

  • Full Member
  • *
  • Posts: 118
  • Karma: +0/-0
    • View Profile
Ano ano ba kadalasan na tinatanong sa Technical Job Interview sa JAVA ? Pahingi naman ng tips. Madami kasing concepts ang Java e. saan ba ok mag focus?

Techronnati | where technology never sleeps


Mountain View

Offline MrSpecialist

  • Sr. Member
  • *
  • Posts: 345
  • Karma: +1/-0
  • I'm an expert!
    • View Profile
Bigyan kita ng tip ha:

---------------------------------------------------------------
Java Collections Interview Questions and Answers
--------------------------------------------------------------


1.How to filter a Java collection?

The best way to filter a Java collection is to use Java 8. Java streams and lambdas can be used to filter a collection as below,

Quote
List<Person> passedStudents = students.stream()
    .filter(p -> p.getMark() > 50).collect(Collectors.toList());

If for some reason you are not in a position to use Java 8 or the interviewer insists on pre Java 8 solution, following is the best way to filter a Java collection.

2.How to Sort a Java Collection?

Use a Comparator to sort a Java Collection.

 
Quote
List<Animal> animals = new ArrayList<Animal>();
Comparator<Animal> comparator = new Comparator<Animal>() {
    public int compare(Animal c1, Animal c2) {
        //sort logic here
      return c2.getHeight() - c1.getHeight();      
    }
};

Collections.sort(animals, comparator);

If Animal implements Comparable, then following is just enough.

Quote
Collections.sort(animals);

3.Best way to convert a List to a Set.

Instantiate Set using the HashSet.

Quote
Set<Animal> animalSet = new HashSet<Animal>(animalList);

4.When to use LinkedList over ArrayList?

Java’s LinkedList implementation is a doubly linked list. ArrayList is a dynamically resizing array implementation. So to compare between LinkedList and ArrayList is almost similar to comparing a doubly linked list and a dynamically resizing array.

LinkedList is convenient for back and forth traversal sequentially, but random access to an element is proportionally costlier to the size of the LinkedList. At the same time, ArrayList is best suited for random access using a position.

LinkedList is best for inserting and deleting an element at any place of the LinkedList. An ArrayList is not suited for inserting or deleting elements in the mid of the ArrayList. Since everytime a new element is inserted, all the elements should be shifted down and dynamic resizing should be done.

With respect to memory usage of LinkedList and ArrayList, LinkedList collection uses more memory as it needs to keep pointers to the adjacent elements. This overhead is not present for the ArrayList, just the memory required for the data is sufficient. Consider these factors and decide between LinkedList or ArrayList depending on the use case.

5.Difference between HashMap and Hashtable

HashMap is not synchronized but Hashtable is synchronized.
HashMap allows null as key and value. Since the key is unique, only one null is allowed as key. Hashtable does not allows null in key or value.
LinkedHashMap extends HashMap and so can be converted. It helps to have a fixed iteration order. It is not possible with Hashtable.
In essense, there is almost no reason to use a Java Hashtable.

6.Explain Java hashCode() and equals() method.

equals() method is used to determine the equality of two Java objects. When we have a custom class we need to override the equals() method and provide an implementation so that it can be used to find the equality between two instance of it. By Java specification there is a contract between equals() and hashCode(). It says,

"if two objects are equal, that is obj1.equals(obj2) is true then, obj1.hashCode() and obj2.hashCode() must return same integer"

Whenever we choose to override equals(), then we must override the hashCode() method. hashCode() is used to calculate the position bucket and keys.

7.What is Java Priority Queue?

Java PriorityQueue is a data structure that is part of Java collections framework. It is an implementation of a Queue wherein the order of elements will be decided based on priority of each elements. A comparator can be provided in the constructor when a PriorityQueue is instantiated. That comparator will decide the sort order of elements in the PriorityQueue collection instance.

8.Difference between ArrayList and Vector.

We have beaten this enough in a old article difference between Vector and ArrayList in Java.

Vector is synchronized and ArrayList is not.

Vector doubles its internal size when its increased. But, ArrayList increases by half of its size when its increased.
ArrayList gives better performance over Vector as its not synchronized.
ArrayList’s Iterators are fail-fast but Vector’s Enumeration is not fail-fast.
ArrayList was introduced in Java 1.2 and Vector even before that. But initially Vector was not part of Java collections framework and later made part of collections framework.
As of now, there is no need to use Vector and it can be considered legacy and to be deprecated. If you need synchronized collection an ArrayList can be synchronized and used.

9.What are Java Concurrent Collection Classes?

Concurrent Collections were introduced in Java 5 along with annotations and generics. These classes are in java.util.concurrent package and they help solve common concurrency problems. They are efficient and helps us to reduce common boilerplate concurrency code. Important concurrent collection classes are BlockingQueue, ConcurrentMap, ConcurrentNavigableMap and ExecutorService.

10. Explain about Comparable and Comparator

A class can implement the Comparable interface to define the natural ordering of the objects. If you take a list of Strings, generally it is ordered by alphabetical comparisons. So when a String class is created, it can be made to implement Comparable interface and override the compareTo method to provide the comparison definition. We can use them as,

str1.compareTo(str2);
Now, what will you do if you want to compare two strings based on it length. We go for the Comparator. We create a class and let it implement the Comparator interface and override compare method. We can use them as,

Collections.sort(listOfStrings, comparatorObj);
The natural ordering is up to the person designing the classes. Comparator can be used in that scenario also and it can be used when we need multiple sorting options. Imagine a situation where a class is already available and we cannot modify it. In that case also, Comparator is the choice.

Offline JusticeLeague

  • Sr. Member
  • *
  • Posts: 359
  • Karma: +1/-0
    • View Profile
how about yung concept ng object oriented programming? madalas kaya eto itanong?

Offline CyborgRepublic

  • Full Member
  • *
  • Posts: 203
  • Karma: +0/-0
    • View Profile
@JusticeLeague, oo kelangan review-hin mo din yan. May mga companies nagtatanong din ng concept ng Abstraction, Polymorphism, criteria of object oriented programming, concepts about classes tsaka yung Design pattern na ginagamit.

Yung iba nagtatanong difference ng Struts and Struts 2 Framework tsaka yung IoC sa Spring Framework.
« Last Edit: July 21, 2016, 06:19:16 AM by CyborgRepublic »

Offline nomadic

  • Full Member
  • *
  • Posts: 105
  • Karma: +0/-0
    • View Profile
Re: Ano ano ba kadalasan na tinatanong sa Technical Job Interview sa JAVA ?
« Reply #4 on: August 30, 2016, 01:53:18 AM »
Baka itanong din pano mag utilize ng java framework. mukhang kelangan aralin lahat ng framework

Offline Corps

  • Sr. Member
  • *
  • Posts: 276
  • Karma: +0/-0
    • View Profile
Re: Ano ano ba kadalasan na tinatanong sa Technical Job Interview sa JAVA ?
« Reply #5 on: October 19, 2016, 12:37:46 AM »
pano ba sagutin pag mga design patterns na ang tinanong?

Offline LadyProgrammer

  • Full Member
  • *
  • Posts: 118
  • Karma: +0/-0
    • View Profile
Re: Ano ano ba kadalasan na tinatanong sa Technical Job Interview sa JAVA ?
« Reply #6 on: November 16, 2016, 11:01:03 AM »
usong usong tanungin yung mga design patterns like what is singleton, concepts behind GoF Creational Patterns and GoF Behavorial Patterns mga ganun, factory, prototype, builder concepts.

Offline MoneyRepublic

  • Full Member
  • *
  • Posts: 166
  • Karma: +0/-0
    • View Profile
Re: Ano ano ba kadalasan na tinatanong sa Technical Job Interview sa JAVA ?
« Reply #7 on: November 18, 2016, 05:39:46 AM »
eto minsan ganito mga questions:


1.What is an exception?


Exceptions are the objects representing the logical errors that occur at run time and makes JVM enters into the state of  "ambiguity".
The objects which are automatically created by the JVM for representing these run time errors are known as Exceptions

 
2.What are the differences between exception and error.

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
Difference between exception and error

3.How the exceptions are handled in java

Exceptions can  be handled using try catch blocks.
The statements which are proven to generate exceptions need to keep in try block so that whenever is there any exception identified in try block that will be assigned to corresponding exception class object in catch block
More information about try catch finally

4.What is the super class for Exception and Error

Throwable.


public class Exception extends Throwable implements Serializable


public class Error extends Throwable implements Serializable

5.Exceptions are defined in which java package

java.lang.Exception

6.What is throw keyword in java?

Throw keyword is used to throw exception manually.
Whenever it is required to suspend the execution of the functionality based on the user defined logical error condition we will use this throw keyword to throw exception.
Throw keyword in java

7.Can we have try block without catch block

It is possible to have try block without catch block by using finally block
Java supports try with finally block
As we know finally block will always executes even there is an exception occurred in try block, Except System.exit() it will executes always.
Is it possible to write try block without catch block

8.Can we write multiple catch blocks under single try block?

Yes we can write multiple catch blocks under single try block.
Multiple catch blocks in java




9. What are unreachable blocks in java

The block of statements to which the control would never reach under any case can be called as unreachable blocks.
Unreachable blocks are not supported by java.
Unreachable blocks in java






10.How to write user defined exception or custom exception in java

By extending Exception class we can define custom exceptions.
We need to write a constructor for passing message .
User defined exception in java


class CustomException extends Exception { } // this will create Checked Exception
class CustomException extends IOExcpetion { } // this will create Checked exception
class CustomException extends NullPonterExcpetion { } // this will create UnChecked
exception

11.What are the different ways to print exception message on console.

In Java there are three ways to find the details of the exception .
They are
Using an object of java.lang.Exception
Using public void printStackTrace() method
Using public String getMessage() method.


3 different ways to print exception message in java


12.What are the differences between final finally and finalize in java

Finally vs final vs finalize

Final:
Any variable declare along with final modifier then those variables treated as final variable.
 if we declare final variables along with static will became constants.
public final String name = "foo"; //never change this value
If you declare method as final that method also known as final methods.Final methods are not overridden.means we can't overridden that method in anyway.

Code: [Select]
public final void add(){
 }
 public class A{
 void add(){
 //Can't override
 }

 }



If you declare class is final that class is also known as final classes.Final classes are not extended.means we can't extens that calss in anyway.

Code: [Select]
public final class indhu{
 }


Code: [Select]
public class classNotAllowed extends indhu {...} //not allowed

Finally:
Finally blocks are followed by try or catch.finally blocks are complasary executable blocks.But finally is useful for more than just exception handling.
 it allows the programmer to avoid having cleanup code accidentally bypassed by a return,
 continue, or break,Closing streams, network connection, database connection. Putting cleanup  code in a finally block is always a good practice even when no exceptions are anticipated
 where finally doesn't execute e.g. returning value from finally block, calling System.exit from try block etc
finally block always execute, except in case of JVM dies i.e. calling System.exit() .

Code: [Select]
lock.lock();
try {
  //do stuff
} catch (SomeException se) {
  //handle se
} finally {
  lock.unlock(); //always executed, even if Exception or Error or se
  //here close the database connection and any return statements like that we have to write
}

Finalize():
finalize() is a method which is present in Java.lang.Object class.
 Before an object is garbage collected, the garbage collector calls this finalize() of object.Any unreferncebefore destorying if that object having any connections with databse or anything..It will remove  the connections and it will call finalize() of object.It will destroy the object.
If you want to Explicitly call this garbage collector you can use System.gc() or Runtime.gc() objects are there from a long time the garbage collector will destroy that objects.
public void finalize() {
  //free resources (e.g. unallocate memory)
  super.finalize();
}







13.Can we write return statement in try and catch blocks

Yes we can write return statement of the method in try block and catch block
We need to follow some rules to use it.Please check below link
Return statement in try and catch blocks





14. Can we write return statement in finally block


Yes we can write return statement of the method in finally block
We need to follow some rules to use it.Please check below link
Return statement in finally block

finally%2Bwith%2Breturn%2Bstatement%2Bin%2Bjava


15. What are the differences between throw and throws


 throw keyword used to throw user defined exceptions.(we can throw predefined exception too)
Using throws keyword we can explicitly provide the information about unhand-led exceptions of the method to the end user, Java compiler, JVM.
Throw vs throws



16.Can we change an exception of a method with throws clause from unchecked to checked while overriding it?


 No. As mentioned above already
If super class method throws exceptions in sub class if you want to mention throws  then use  same class  or its  sub class exception.
So we can not change from unchecked to checked

 17.What are the rules we need to follow in overriding if super class method throws exception ?



 If sub class throws checked exception super class should throw same or super class exception of this.
If super class method  throws checked or unchecked exceptions its not mandatory to put throws in sub class overridden method.
If super class method throws exceptions in sub class if you want to mention throws  then use  same class  or its  sub class exception.

 


18.What happens if we not follow above rules if super class method throws some exception.

 Compile time error will come.

Code: [Select]
package MethodOverridingExamplePrograms;
public class Super{
 
public void add(){
 System.out.println("Super class add method");
}

}

package MethodOverridingInterviewPrograms;
public class Sub extends Super{
 
void add() throws Exception{ [color=red]//Compiler Error: Exception Exception is not compatible with
throws clause in Super.add()[/color]
System.out.println("Sub class add method");

}

}

package ExceptionHandlingInterviewPrograms;
public class Super{
 
public void add(){
 System.out.println("Super class add method");
}

}

package ExceptionHandlingInterviewPrograms;
public class Sub extends Super{
 
void add() throws NullPointerException{ // this method throws unchecked exception so no
isuues
System.out.println("Sub class add method");

}

}
19. Is it possible to write multiple exceptions in single catch block


It is not possible prior to java 7.
new feature added in java 7.


Code: [Select]
package exceptionsFreshersandExperienced;
public class ExceptionhandlingInterviewQuestions{
 
/**
 * @www.techronnati.com
 **/
 public static void main(String[] args) {

 
try {
 
// your code here
           
} catch (IOException | SQLException ex) {
            System.out.println(e);
}
 
}
 
}


20. What is try with resource block

One more interesting feature of Java 7 is try with resource
In Java, if you use a resource like input or output streams or any database connection logic you always need to close it after using.
It also can throw exceptions so it has to be in a try block and catch block. The closing logic  should be in finally block. This is a least the way until Java 7. This has several disadvantages:

    You'd have to check if your ressource is null before closing it
    The closing itself can throw exceptions so your finally had to contain another try -catch
    Programmers tend to forget to close their resources

The solution given by using try with resource statement.

Code: [Select]
try(resource-specification)
{
 
//use the resource
 
}catch(Exception e)
{
 
//code

}

Top 20 Java Exception handling interview questions and answers for freshers and experienced

Offline CyborgRepublic

  • Full Member
  • *
  • Posts: 203
  • Karma: +0/-0
    • View Profile
Re: Ano ano ba kadalasan na tinatanong sa Technical Job Interview sa JAVA ?
« Reply #8 on: November 21, 2016, 01:13:48 PM »
Thanks for sharing this post. 

sino nakatry na dito na during technical interview, talagang pinag program sila? naexperience nyo na ba yun?

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
154 Views
Last post October 08, 2008, 03:25:09 PM
by MoneyRepublic
2 Replies
50 Views
Last post July 24, 2014, 09:37:18 AM
by GBTalk
0 Replies
79 Views
Last post July 21, 2016, 02:10:29 AM
by LadyProgrammer
0 Replies
129 Views
Last post July 27, 2016, 09:16:00 AM
by MrSpecialist
0 Replies
158 Views
Last post October 19, 2016, 12:43:39 AM
by h2obubbler

Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.