This set of Java test on “Coding best practices”.
1. What should the return type of method where there is no return value? Answer: b 2. What data structure should be used when number of elements is fixed? Answer: a 3. What causes the program to exit abruptly and hence its usage should be minimalistic? Answer: c 4. Which of the following is good coding practice to determine oddity? ii) a) i Answer: b 5. Which one of the following causes memory leak? Answer: d 6. Which of the following is a best practice to measure time taken by a process for execution? Answer: b 7. What one of the following is best practice to handle Null Pointer exception? a) Option (i) Answer: b 8. Which of the below is true about java class structure? Answer: c 9. Which of the below is false about java coding? Answer: a 10. Which is better in terms of performance for iterating an array? Answer: b To practice all areas of Java for tests, here is complete set on Multiple Choice Questions and Answers on Java.
a) Null
b) Empty collection
c) Singleton collection
d) Empty String
Clarification: Returning Empty collection is a good practice. It eliminates chances of unhandled null pointer exceptions.
a) Array
b) Array list
c) Vector
d) Set
Clarification: Array list has variable size. Array is stored in contiguous memory. Hence, reading is faster. Also, array is memory efficient.
a) Try
b) Finally
c) Exit
d) Catch
Clarification: In case of exit, the program exits abruptly hence would never be able to debug the root cause of the issue.
i)
public boolen abc(int num)
{
return num % 2 == 1;
}
public boolean xyz(int num)
{
return (num & 1)!= 0;
}
b) ii
c) (i) causes compilation error
d) (ii) causes compilation error
Clarification: Arithmetic and logical operations are much faster than division and multiplication.
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
Clarification: Finally block is called in successful as well exception scenarios. Hence, all the connections are closed properly which avoids memory leak.
a) System.currentTimeMillis()
b) System.nanoTime()
c) System.getCurrentTime()
d) System.getProcessingTime()
Clarification: System.nanoTime takes around 1/100000 th of a second whereas System.currentTimeMillis takes around 1/1000th of a second.
i) int noOfStudents = line.listStudents().count;
ii) int noOfStudents = getCountOfStudents(line);
public int getCountOfStudents(List line)
{
if(line != null)
{
if(line.listOfStudents() != null)
{
return line.listOfStudents().size();
}
}
throw new NullPointerException("List is empty");
}
b) Option (ii)
c) Compilation Error
d) Option (ii) gives incorrect result
Clarification: Null check must be done while dealing with nested structures to avoid null pointer exceptions.
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
Clarification: 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.
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
Clarification: 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.
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++)
Clarification: reverse traversal of array take half number cycles as compared to forward traversal. The other for loops will go in infinite loop.