250+ TOP MCQs on Coding best practices and Answers

This set of Java test on “Coding best practices”.

1. What should the return type of method where there is no return value?
a) Null
b) Empty collection
c) Singleton collection
d) Empty String

Answer: b
Clarification: 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?
a) Array
b) Array list
c) Vector
d) Set

Answer: a
Clarification: 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?
a) Try
b) Finally
c) Exit
d) Catch

Answer: c
Clarification: 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?
i)

  1. public boolen abc(int num)
  2. {
  3. 	return num % 2 == 1;
  4. }

ii)

  1. public boolean xyz(int num)
  2. {
  3. 	return (num & 1)!= 0;
  4.  }

a) i
b) ii
c) (i) causes compilation error
d) (ii) causes compilation error

Answer: b
Clarification: 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

Answer: d
Clarification: 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?
a) System.currentTimeMillis()
b) System.nanoTime()
c) System.getCurrentTime()
d) System.getProcessingTime()

Answer: b
Clarification: 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);

  1.     public int getCountOfStudents(List line)
  2.     {
  3. 	if(line != null)
  4.         {
  5. 		if(line.listOfStudents() != null)
  6.                 {
  7. 			return line.listOfStudents().size();
  8. 		}
  9. 	}
  10. 	throw new NullPointerException("List is empty");
  11.     }

a) Option (i)
b) Option (ii)
c) Compilation Error
d) Option (ii) gives incorrect result

Answer: b
Clarification: 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

Answer: c
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.

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

Answer: a
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.

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++)

Answer: b
Clarification: reverse traversal of array take half number cycles as compared to forward traversal. The other for loops will go in infinite loop.

To practice all areas of Java for tests, here is complete set on Multiple Choice Questions and Answers on Java.

contest

Leave a Reply

Your email address will not be published. Required fields are marked *