Java Questions & Answers – Coding best practices

This set of Java Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

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

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

Answer: c
Explanation: In case of exit, the program exits abruptly hence would never be able to debug the root cause of the issue.
advertisement
advertisement

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)

Note: Join free Sanfoundry classes at Telegram or Youtube
  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
View Answer

Answer: b
Explanation: Arithmetic and logical operations are much faster than division and multiplication.
advertisement

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
View Answer

Answer: d
Explanation: Finally block is called in successful as well exception scenarios. Hence, all the connections are closed properly which avoids memory leak.
advertisement

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()
View Answer

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

  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
View Answer

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

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

Answer: a
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++)
View Answer

Answer: b
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 language, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.