This set of Java test focuses on “Coding best practices”.
1. What should the return type of method where there is no return value?
b) Empty collection
c) Singleton collection
d) Empty String
Explanation: Returning Empty collection is a good practice. It eliminates chances of unhandled null pointer exceptions.
2. What data structure should be used when number of elements is fixed?
b) Array list
Explanation: Array list has variable size. Array is stored in contiguous memory. Hence, reading is faster. Also, array is memory efficient.
3. What causes the program to exit abruptly and hence its usage should be minimalistic?
Explanation: In case of exit, the program exits abruptly hence would never be able to debug the root cause of the issue.
4. Which of the following is good coding practice to determine oddity?
public boolen abc(int num)
return num % 2 == 1;
public boolean xyz(int num)
return (num & 1)!= 0;
c) option (i) causes compilation error
d) option (ii) causes compilation error
Explanation: Arithmetic and logical operations are much faster than division and multiplication.
5. Which one of the following causes memory leak?
a) Release database connection when querying is complete
b) Use Finally block as much as possible
c) Release instances stored in static tables
d) Not using Finally block often
Explanation: Finally block is called in successful as well exception scenarios. Hence, all the connections are closed properly which avoids memory leak.
6. Which of the following is a best practice to measure time taken by a process for execution?
Explanation: System.nanoTime takes around 1/100000 th of a second whereas System.currentTimeMillis takes around 1/1000th of a second.
7. What one of the following is best practice to handle Null Pointer exception?
i) int noOfStudents = line.listStudents().count;
ii) int noOfStudents = getCountOfStudents(line);
public int getCountOfStudents(List line)
if(line != null)
if(line.listOfStudents() != null)
throw new NullPointerException("List is empty");
a) Option (i)
b) Option (ii)
c) Compilation Error
d) Option (ii) gives incorrect result
Explanation: Null check must be done while dealing with nested structures to avoid null pointer exceptions.
8. Which of the below is true about java class structure?
a) The class name should start with lowercase
b) The class should have thousands of lines of code
c) The class should only contain those attribute and functionality which it should; hence keeping it short
d) The class attributes and methods should be public
Explanation: Class name should always start with upper case and contain those attribute and functionality which it should (Single Responsibility Principle); hence keeping it short. The attributes should be usually private with get and set methods.
9. Which of the below is false about java coding?
a) variable names should be short
b) variable names should be such that they avoid ambiguity
c) test case method names should be created as english sentences without spaces
d) class constants should be used when we want to share data between class methods
Explanation: variable names like i, a, abc, etc should be avoided. They should be real world names which avoid ambiguity. Test case name should explain its significance.
10. Which is better in terms of performance for iterating an array?
a) for(int i=0; i<100; i++)
b) for(int i=99; i>=0; i–)
c) for(int i=100; i<0; i++)
d) for(int i=99; i>0; i++)
Explanation: reverse traversal of array take half number cycles as compared to forward traversal. The other for loops will go in infinite loop.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java for tests, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.